Beispiel #1
0
 /**
  * Import taken from Adminer, slightly modified
  * This implementation is aware of delimiters used for trigger definitions
  *
  * @author   Jakub Vrána, Jan Tvrdík, Michael Moravec, Filip Procházka
  * @license  Apache License
  */
 public static function executeBatch(Connection $connection, $query, $callback = NULL)
 {
     $db = $connection->getWrappedConnection();
     $delimiter = ';';
     $offset = 0;
     while ($query != '') {
         if (!$offset && preg_match('~^\\s*DELIMITER\\s+(.+)~i', $query, $match)) {
             $delimiter = $match[1];
             $query = substr($query, strlen($match[0]));
         } else {
             preg_match('(' . preg_quote($delimiter) . '|[\'`"]|/\\*|-- |#|$)', $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 || $found == $delimiter) {
                 // end of a query
                 $q = substr($query, 0, $match[0][1]);
                 try {
                     if ($callback) {
                         call_user_func($callback, $q, $db);
                     }
                     $db->query($q);
                 } catch (\Exception $e) {
                     throw new BatchImportException($e->getMessage() . "\n\n" . Nette\Utils\Strings::truncate(trim($q), 1200), 0, $e);
                 }
                 $query = substr($query, $offset);
                 $offset = 0;
             } else {
                 // find matching quote or comment end
                 while (preg_match('~' . ($found == '/*' ? '\\*/' : (preg_match('~-- |#~', $found) ? "\n" : "{$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;
                     }
                 }
             }
         }
     }
 }