コード例 #1
0
ファイル: BaseDriver.php プロジェクト: artfocus/migrations
 /**
  * Loads and executes SQL queries from given file. Taken from Adminer (Apache License), modified.
  *
  * @author   Jakub Vrána
  * @author   Jan Tvrdík
  * @author   Michael Moravec
  * @author   Jan Skrasek
  * @license  Apache License
  *
  * @param  string $path
  * @return int number of executed queries
  */
 public function loadFile($path)
 {
     $query = @file_get_contents($path);
     if ($query === FALSE) {
         throw new IOException("Cannot open file '{$path}'.");
     }
     $delimiter = ';';
     $offset = $queries = 0;
     $space = "(?:\\s|/\\*.*\\*/|(?:#|-- )[^\\n]*\\n|--\\n)";
     if ($this instanceof PgSqlDriver) {
         $parse = '[\'"]|/\\*|-- |$|\\$[^$]*\\$';
     } else {
         $parse = '[\'"`#]|/\\*|-- |$';
     }
     while ($query != '') {
         if (!$offset && preg_match("~^{$space}*DELIMITER\\s+(\\S+)~i", $query, $match)) {
             $delimiter = $match[1];
             $query = substr($query, strlen($match[0]));
         } else {
             preg_match('(' . preg_quote($delimiter) . "\\s*|{$parse})", $query, $match, PREG_OFFSET_CAPTURE, $offset);
             // should always match
             $found = $match[0][0];
             $offset = $match[0][1] + strlen($found);
             if (!$found && rtrim($query) === '') {
                 break;
             }
             if (!$found || rtrim($found) == $delimiter) {
                 // end of a query
                 $q = substr($query, 0, $match[0][1]);
                 $queries++;
                 $this->dbal->exec($q);
                 $query = substr($query, $offset);
                 $offset = 0;
             } else {
                 // find matching quote or comment end
                 while (preg_match('(' . ($found == '/*' ? '\\*/' : ($found == '[' ? ']' : (preg_match('~^-- |^#~', $found) ? "\n" : preg_quote($found) . "|\\\\."))) . '|$)s', $query, $match, PREG_OFFSET_CAPTURE, $offset)) {
                     //! respect sql_mode NO_BACKSLASH_ESCAPES
                     $s = $match[0][0];
                     $offset = $match[0][1] + strlen($s);
                     if ($s[0] !== '\\') {
                         break;
                     }
                 }
             }
         }
     }
     return $queries;
 }
コード例 #2
0
ファイル: BaseDriver.php プロジェクト: nextras/migrations
 /**
  * Loads and executes SQL queries from given file. Taken from Adminer (Apache License), modified.
  *
  * @author   Jakub Vrána
  * @author   Jan Tvrdík
  * @author   Michael Moravec
  * @author   Jan Skrasek
  * @license  Apache License
  *
  * @param  string $path
  * @return int number of executed queries
  */
 public function loadFile($path)
 {
     $content = @file_get_contents($path);
     if ($content === FALSE) {
         throw new IOException("Cannot open file '{$path}'.");
     }
     $queryOffset = 0;
     $parseOffset = 0;
     $queries = 0;
     $space = "(?:\\s|/\\*.*\\*/|(?:#|-- )[^\\n]*\\n|--\\n)";
     $delimiter = ';';
     $delimiterRe = "~^{$space}*DELIMITER\\s+(\\S+)~i";
     $openRe = $this instanceof PgSqlDriver ? '[\'"]|/\\*|-- |\\z|\\$[^$]*\\$' : '[\'"`#]|/\\*|-- |\\z';
     $parseRe = "(;|{$openRe})";
     $endReTable = ['\'' => '(\'|\\\\.|\\z)s', '"' => '("|\\\\.|\\z)s', '/*' => '(\\*/|\\z)', '[' => '(]|\\z)'];
     while (TRUE) {
         if (preg_match($delimiterRe, $content, $match, 0, $queryOffset)) {
             $delimiter = $match[1];
             $queryOffset += strlen($match[0]);
             $parseOffset += strlen($match[0]);
             $parseRe = '(' . preg_quote($delimiter) . "|{$openRe})";
         }
         while (TRUE) {
             preg_match($parseRe, $content, $match, PREG_OFFSET_CAPTURE, $parseOffset);
             // should always match
             $found = $match[0][0];
             $parseOffset = $match[0][1] + strlen($found);
             if ($found === $delimiter) {
                 // delimited query
                 $queryLength = $match[0][1] - $queryOffset;
                 break;
             } elseif ($found) {
                 // find matching quote or comment end
                 $endRe = isset($endReTable[$found]) ? $endReTable[$found] : '(' . (preg_match('~^-- |^#~', $found) ? "\n" : preg_quote($found) . "|\\\\.") . '|\\z)s';
                 while (preg_match($endRe, $content, $match, PREG_OFFSET_CAPTURE, $parseOffset)) {
                     //! respect sql_mode NO_BACKSLASH_ESCAPES
                     $s = $match[0][0];
                     $parseOffset = $match[0][1] + strlen($s);
                     if ($s[0] !== '\\') {
                         continue 2;
                     }
                 }
             } else {
                 // last query or EOF
                 if (rtrim(substr($content, $queryOffset)) === '') {
                     break 2;
                 } else {
                     $queryLength = $match[0][1] - $queryOffset;
                     break;
                 }
             }
         }
         $q = substr($content, $queryOffset, $queryLength);
         $queries++;
         $this->dbal->exec($q);
         $queryOffset = $parseOffset;
     }
     return $queries;
 }
コード例 #3
0
ファイル: DibiAdapter.php プロジェクト: nextras/migrations
 public function escapeIdentifier($value)
 {
     return $this->innerAdapter->escapeIdentifier($value);
 }