2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * SQL import plugin for phpMyAdmin
6 * @package PhpMyAdmin-Import
9 if (! defined('PHPMYADMIN')) {
16 if (isset($plugin_list)) {
17 $plugin_list['sql'] = array(
20 'options_text' => __('Options'),
22 $compats = PMA_DBI_getCompatibilities();
23 if (count($compats) > 0) {
25 foreach ($compats as $val) {
28 $plugin_list['sql']['options'] = array(
29 array('type' => 'begin_group', 'name' => 'general_opts'),
32 'name' => 'compatibility',
33 'text' => __('SQL compatibility mode:'),
36 'manual_MySQL_Database_Administration',
42 'name' => 'no_auto_value_on_zero',
43 'text' => __('Do not use <code>AUTO_INCREMENT</code> for zero values'),
45 'manual_MySQL_Database_Administration',
47 'sqlmode_no_auto_value_on_zero'
51 array('type' => 'end_group'),
55 /* We do not define function when plugin is just queried for information above */
60 // Defaults for parser
65 $big_value = 2147483647;
66 $delimiter_keyword = 'DELIMITER '; // include the space because it's mandatory
67 $length_of_delimiter_keyword = strlen($delimiter_keyword);
69 if (isset($_POST['sql_delimiter'])) {
70 $sql_delimiter = $_POST['sql_delimiter'];
75 // Handle compatibility options
77 if (isset($_REQUEST['sql_compatibility']) && 'NONE' != $_REQUEST['sql_compatibility']) {
78 $sql_modes[] = $_REQUEST['sql_compatibility'];
80 if (isset($_REQUEST['sql_no_auto_value_on_zero'])) {
81 $sql_modes[] = 'NO_AUTO_VALUE_ON_ZERO';
83 if (count($sql_modes) > 0) {
84 PMA_DBI_try_query('SET SQL_MODE="' . implode(',', $sql_modes) . '"');
89 * will be set in PMA_importGetNextChunk()
91 * @global boolean $GLOBALS['finished']
93 $GLOBALS['finished'] = false;
95 while (!($GLOBALS['finished'] && $i >= $len) && !$error && !$timeout_passed) {
96 $data = PMA_importGetNextChunk();
97 if ($data === false) {
98 // subtract data we didn't handle yet and stop processing
99 $offset -= strlen($buffer);
101 } elseif ($data === true) {
102 // Handle rest of buffer
104 // Append new data to buffer
108 // Do not parse string when we're not at the end and don't have ; inside
109 if ((strpos($buffer, $sql_delimiter, $i) === false) && !$GLOBALS['finished']) {
113 // Current length of our buffer
114 $len = strlen($buffer);
116 // Grab some SQL queries out of it
118 $found_delimiter = false;
119 // Find first interesting character
121 // this is about 7 times faster that looking for each sequence i
122 // one by one with strpos()
123 if (preg_match('/(\'|"|#|-- |\/\*|`|(?i)(?<![A-Z0-9_])' . $delimiter_keyword . ')/', $buffer, $matches, PREG_OFFSET_CAPTURE
, $i)) {
124 // in $matches, index 0 contains the match for the complete
125 // expression but we don't use it
126 $first_position = $matches[1][1];
128 $first_position = $big_value;
131 * @todo we should not look for a delimiter that might be
132 * inside quotes (or even double-quotes)
134 // the cost of doing this one with preg_match() would be too high
135 $first_sql_delimiter = strpos($buffer, $sql_delimiter, $i);
136 if ($first_sql_delimiter === false) {
137 $first_sql_delimiter = $big_value;
139 $found_delimiter = true;
142 // set $i to the position of the first quote, comment.start or delimiter found
143 $i = min($first_position, $first_sql_delimiter);
145 if ($i == $big_value) {
146 // none of the above was found in the string
149 if (!$GLOBALS['finished']) {
152 // at the end there might be some whitespace...
153 if (trim($buffer) == '') {
158 // We hit end of query, go there!
159 $i = strlen($buffer) - 1;
162 // Grab current character
166 if (strpos('\'"`', $ch) !== false) {
171 $pos = strpos($buffer, $quote, $i +
1);
173 * Behave same as MySQL and accept end of query as end of backtick.
174 * I know this is sick, but MySQL behaves like this:
176 * SELECT * FROM `table
180 * SELECT * FROM `table`
182 if ($pos === false && $quote == '`' && $found_delimiter) {
183 $pos = $first_sql_delimiter - 1;
184 // No quote? Too short string
185 } elseif ($pos === false) {
186 // We hit end of string => unclosed quote, but we handle it as end of query
187 if ($GLOBALS['finished']) {
191 $found_delimiter = false;
194 // Was not the quote escaped?
196 while ($buffer[$j] == '\\') $j--;
197 // Even count means it was not escaped
198 $endq = (((($pos - 1) - $j) %
2) == 0);
202 if ($first_sql_delimiter < $pos) {
203 $found_delimiter = false;
210 // Aren't we at the end?
211 if ($GLOBALS['finished'] && $i == $len) {
218 // Not enough data to decide
219 if ((($i == ($len - 1) && ($ch == '-' ||
$ch == '/'))
220 ||
($i == ($len - 2) && (($ch == '-' && $buffer[$i +
1] == '-')
221 ||
($ch == '/' && $buffer[$i +
1] == '*')))) && !$GLOBALS['finished']) {
227 ||
($i < ($len - 1) && $ch == '-' && $buffer[$i +
1] == '-'
228 && (($i < ($len - 2) && $buffer[$i +
2] <= ' ')
229 ||
($i == ($len - 1) && $GLOBALS['finished'])))
230 ||
($i < ($len - 1) && $ch == '/' && $buffer[$i +
1] == '*')
232 // Copy current string to SQL
233 if ($start_pos != $i) {
234 $sql .= substr($buffer, $start_pos, $i - $start_pos);
237 $start_of_comment = $i;
238 // do not use PHP_EOL here instead of "\n", because the export
239 // file might have been produced on a different system
240 $i = strpos($buffer, $ch == '/' ?
'*/' : "\n", $i);
241 // didn't we hit end of string?
243 if ($GLOBALS['finished']) {
255 // We need to send the comment part in case we are defining
256 // a procedure or function and comments in it are valuable
257 $sql .= substr($buffer, $start_of_comment, $i - $start_of_comment);
258 // Next query part will start here
260 // Aren't we at the end?
267 // Change delimiter, if redefined, and skip it (don't send to server!)
268 if (strtoupper(substr($buffer, $i, $length_of_delimiter_keyword)) == $delimiter_keyword
269 && ($i +
$length_of_delimiter_keyword < $len)) {
270 // look for EOL on the character immediately after 'DELIMITER '
271 // (see previous comment about PHP_EOL)
272 $new_line_pos = strpos($buffer, "\n", $i +
$length_of_delimiter_keyword);
273 // it might happen that there is no EOL
274 if (false === $new_line_pos) {
275 $new_line_pos = $len;
277 $sql_delimiter = substr($buffer, $i +
$length_of_delimiter_keyword, $new_line_pos - $i - $length_of_delimiter_keyword);
278 $i = $new_line_pos +
1;
279 // Next query part will start here
285 if ($found_delimiter ||
($GLOBALS['finished'] && ($i == $len - 1))) {
287 if ($start_pos < $len) {
288 $length_to_grab = $i - $start_pos;
290 if (! $found_delimiter) {
293 $tmp_sql .= substr($buffer, $start_pos, $length_to_grab);
294 unset($length_to_grab);
296 // Do not try to execute empty SQL
297 if (! preg_match('/^([\s]*;)*$/', trim($tmp_sql))) {
299 PMA_importRunQuery($sql, substr($buffer, 0, $i +
strlen($sql_delimiter)));
300 $buffer = substr($buffer, $i +
strlen($sql_delimiter));
302 $len = strlen($buffer);
306 // Any chance we will get a complete query?
307 //if ((strpos($buffer, ';') === false) && !$GLOBALS['finished']) {
308 if ((strpos($buffer, $sql_delimiter) === false) && !$GLOBALS['finished']) {
316 } // End of parser loop
317 } // End of import loop
318 // Commit any possible data in buffers
319 PMA_importRunQuery('', substr($buffer, 0, $len));
320 PMA_importRunQuery();