added japanese language
[openemr.git] / phpmyadmin / libraries / sqlparser.lib.php
blob7c836129415423ced9a709e245ff1b2ab7b9d6bc
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /** SQL Parser Functions for phpMyAdmin
5 * These functions define an SQL parser system, capable of understanding and
6 * extracting data from a MySQL type SQL query.
8 * The basic procedure for using the new SQL parser:
9 * On any page that needs to extract data from a query or to pretty-print a
10 * query, you need code like this up at the top:
12 * ($sql contains the query)
13 * $parsed_sql = PMA_SQP_parse($sql);
15 * If you want to extract data from it then, you just need to run
16 * $sql_info = PMA_SQP_analyze($parsed_sql);
18 * See comments in PMA_SQP_analyze for the returned info
19 * from the analyzer.
21 * If you want a pretty-printed version of the query, do:
22 * $string = PMA_SQP_format($parsed_sql);
23 * (note that that you need to have syntax.css.php included somehow in your
24 * page for it to work, I recommend '<link rel="stylesheet" type="text/css"
25 * href="syntax.css.php" />' at the moment.)
27 * @package PhpMyAdmin
29 if (! defined('PHPMYADMIN')) {
30 exit;
33 /**
34 * Include the string handling class as we use it heavily
36 require_once './libraries/string.inc.php';
38 /**
39 * Include data for the SQL Parser
41 require_once './libraries/sqlparser.data.php';
43 /**
44 * Charset information
46 if (!defined('TESTSUITE') && ! PMA_DRIZZLE) {
47 include_once './libraries/mysql_charsets.inc.php';
49 if (! isset($mysql_charsets)) {
50 $mysql_charsets = array();
51 $mysql_collations_flat = array();
54 /**
55 * Stores parsed elemented of query to array.
57 * Currently we don't need the $pos (token position in query)
58 * for other purposes than LIMIT clause verification,
59 * so many calls to this function do not include the 4th parameter
61 * @param array &$arr Array to store element
62 * @param string $type Type of element
63 * @param string $data Data (text) of element
64 * @param int &$arrsize Size of array
65 * @param int $pos Position of an element
67 * @return void
69 function PMA_SQP_arrayAdd(&$arr, $type, $data, &$arrsize, $pos = 0)
71 $arr[] = array('type' => $type, 'data' => $data, 'pos' => $pos);
72 $arrsize++;
73 } // end of the "PMA_SQP_arrayAdd()" function
75 /**
76 * Reset the error variable for the SQL parser
78 * @access public
80 * @return void
82 function PMA_SQP_resetError()
84 global $SQP_errorString;
85 $SQP_errorString = '';
86 unset($SQP_errorString);
89 /**
90 * Get the contents of the error variable for the SQL parser
92 * @return string Error string from SQL parser
94 * @access public
96 function PMA_SQP_getErrorString()
98 global $SQP_errorString;
99 return isset($SQP_errorString) ? $SQP_errorString : '';
103 * Check if the SQL parser hit an error
105 * @return boolean error state
107 * @access public
109 function PMA_SQP_isError()
111 global $SQP_errorString;
112 return isset($SQP_errorString) && !empty($SQP_errorString);
116 * Set an error message for the system
118 * @param string $message The error message
119 * @param string $sql The failing SQL query
121 * @return void
123 * @access private
124 * @scope SQL Parser internal
126 function PMA_SQP_throwError($message, $sql)
128 global $SQP_errorString;
129 $SQP_errorString = '<p>'
130 . __(
131 'There seems to be an error in your SQL query. The MySQL server '
132 . 'error output below, if there is any, may also help you in '
133 . 'diagnosing the problem.'
135 . '</p>' . "\n"
136 . '<pre>' . "\n"
137 . 'ERROR: ' . $message . "\n"
138 . 'SQL: ' . htmlspecialchars($sql) . "\n"
139 . '</pre>' . "\n";
141 } // end of the "PMA_SQP_throwError()" function
145 * Do display the bug report
147 * @param string $message The error message
148 * @param string $sql The failing SQL query
150 * @return void
152 * @access public
154 function PMA_SQP_bug($message, $sql)
156 global $SQP_errorString;
157 $debugstr = 'ERROR: ' . $message . "\n";
158 $debugstr .= 'MySQL: ' . PMA_MYSQL_STR_VERSION . "\n";
159 $debugstr .= 'USR OS, AGENT, VER: ' . PMA_USR_OS . ' ';
160 $debugstr .= PMA_USR_BROWSER_AGENT . ' ' . PMA_USR_BROWSER_VER . "\n";
161 $debugstr .= 'PMA: ' . PMA_VERSION . "\n";
162 $debugstr .= 'PHP VER,OS: ' . PMA_PHP_STR_VERSION . ' ' . PHP_OS . "\n";
163 $debugstr .= 'LANG: ' . $GLOBALS['lang'] . "\n";
164 $debugstr .= 'SQL: ' . htmlspecialchars($sql);
166 $encodedstr = $debugstr;
167 if (@function_exists('gzcompress')) {
168 $encodedstr = gzcompress($debugstr, 9);
170 $encodedstr = preg_replace(
171 "/(\015\012)|(\015)|(\012)/",
172 '<br />' . "\n",
173 chunk_split(base64_encode($encodedstr))
177 $SQP_errorString .= __(
178 'There is a chance that you may have found a bug in the SQL parser. '
179 . 'Please examine your query closely, and check that the quotes are '
180 . 'correct and not mis-matched. Other possible failure causes may be '
181 . 'that you are uploading a file with binary outside of a quoted text '
182 . 'area. You can also try your query on the MySQL command line '
183 . 'interface. The MySQL server error output below, if there is any, '
184 . 'may also help you in diagnosing the problem. If you still have '
185 . 'problems or if the parser fails where the command line interface '
186 . 'succeeds, please reduce your SQL query input to the single query '
187 . 'that causes problems, and submit a bug report with the data chunk '
188 . 'in the CUT section below:'
190 $SQP_errorString .= '<br />' . "\n"
191 . '----' . __('BEGIN CUT') . '----' . '<br />' . "\n"
192 . $encodedstr . "\n"
193 . '----' . __('END CUT') . '----' . '<br />' . "\n";
195 $SQP_errorString .= '----' . __('BEGIN RAW') . '----<br />' . "\n"
196 . '<pre>' . "\n"
197 . $debugstr
198 . '</pre>' . "\n"
199 . '----' . __('END RAW') . '----<br />' . "\n";
201 } // end of the "PMA_SQP_bug()" function
205 * Parses the SQL queries
207 * @param string $sql The SQL query list
209 * @return mixed Most of times, nothing...
211 * @global array The current PMA configuration
212 * @global array MySQL column attributes
213 * @global array MySQL reserved words
214 * @global array MySQL column types
215 * @global array MySQL function names
216 * @global array List of available character sets
217 * @global array List of available collations
219 * @access public
221 function PMA_SQP_parse($sql)
223 static $PMA_SQPdata_column_attrib, $PMA_SQPdata_reserved_word;
224 static $PMA_SQPdata_column_type;
225 static $PMA_SQPdata_function_name, $PMA_SQPdata_forbidden_word;
226 global $mysql_charsets, $mysql_collations_flat;
228 // Convert all line feeds to Unix style
229 $sql = str_replace("\r\n", "\n", $sql);
230 $sql = str_replace("\r", "\n", $sql);
232 $len = $GLOBALS['PMA_String']->strlen($sql);
233 if ($len == 0) {
234 return array();
237 // Create local hashtables
238 if (!isset($PMA_SQPdata_column_attrib)) {
239 $PMA_SQPdata_column_attrib = array_flip(
240 $GLOBALS['PMA_SQPdata_column_attrib']
242 $PMA_SQPdata_function_name = array_flip(
243 $GLOBALS['PMA_SQPdata_function_name']
245 $PMA_SQPdata_reserved_word = array_flip(
246 $GLOBALS['PMA_SQPdata_reserved_word']
248 $PMA_SQPdata_forbidden_word = array_flip(
249 $GLOBALS['PMA_SQPdata_forbidden_word']
251 $PMA_SQPdata_column_type = array_flip(
252 $GLOBALS['PMA_SQPdata_column_type']
256 $sql_array = array();
257 $sql_array['raw'] = $sql;
258 $count1 = 0;
259 $count2 = 0;
260 $punct_queryend = ';';
261 $punct_qualifier = '.';
262 $punct_listsep = ',';
263 $bracket_list = '()[]{}';
264 $allpunct_list = '-,;:!?/.^~\*&%+<=>|';
265 $allpunct_list_pair = array(
266 '!=' => 1,
267 '&&' => 1,
268 ':=' => 1,
269 '<<' => 1,
270 '<=' => 1,
271 '<=>' => 1,
272 '<>' => 1,
273 '>=' => 1,
274 '>>' => 1,
275 '||' => 1,
276 '==' => 1
278 $quote_list = '\'"`';
279 $arraysize = 0;
281 $previous_was_space = false;
282 $this_was_space = false;
283 $previous_was_bracket = false;
284 $this_was_bracket = false;
285 $previous_was_punct = false;
286 $this_was_punct = false;
287 $previous_was_listsep = false;
288 $this_was_listsep = false;
289 $previous_was_quote = false;
290 $this_was_quote = false;
292 while ($count2 < $len) {
293 $c = $GLOBALS['PMA_String']->substr($sql, $count2, 1);
294 $count1 = $count2;
296 $previous_was_space = $this_was_space;
297 $this_was_space = false;
298 $previous_was_bracket = $this_was_bracket;
299 $this_was_bracket = false;
300 $previous_was_punct = $this_was_punct;
301 $this_was_punct = false;
302 $previous_was_listsep = $this_was_listsep;
303 $this_was_listsep = false;
304 $previous_was_quote = $this_was_quote;
305 $this_was_quote = false;
307 if (($c == "\n")) {
308 $this_was_space = true;
309 $count2++;
310 PMA_SQP_arrayAdd($sql_array, 'white_newline', '', $arraysize);
311 continue;
314 // Checks for white space
315 if ($GLOBALS['PMA_String']->isSpace($c)) {
316 $this_was_space = true;
317 $count2++;
318 continue;
321 // Checks for comment lines.
322 // MySQL style #
323 // C style /* */
324 // ANSI style --
325 $next_c = $GLOBALS['PMA_String']->substr($sql, $count2 + 1, 1);
326 if (($c == '#')
327 || (($count2 + 1 < $len) && ($c == '/') && ($next_c == '*'))
328 || (($count2 + 2 == $len) && ($c == '-') && ($next_c == '-'))
329 || (($count2 + 2 < $len) && ($c == '-') && ($next_c == '-') && (($GLOBALS['PMA_String']->substr($sql, $count2 + 2, 1) <= ' ')))
331 $count2++;
332 $pos = 0;
333 $type = 'bad';
334 switch ($c) {
335 case '#':
336 $type = 'mysql';
337 break;
338 case '-':
339 $type = 'ansi';
340 $pos = $GLOBALS['PMA_String']->strpos($sql, "\n", $count2);
341 break;
342 case '/':
343 $type = 'c';
344 $pos = $GLOBALS['PMA_String']->strpos($sql, '*/', $count2);
345 $pos += 2;
346 break;
347 default:
348 break;
349 } // end switch
350 $count2 = ($pos < $count2) ? $len : $pos;
351 $str = $GLOBALS['PMA_String']->substr(
352 $sql, $count1, $count2 - $count1
354 PMA_SQP_arrayAdd($sql_array, 'comment_' . $type, $str, $arraysize);
355 continue;
356 } // end if
358 // Checks for something inside quotation marks
359 if ($GLOBALS['PMA_String']->strpos($quote_list, $c) !== false) {
360 $startquotepos = $count2;
361 $quotetype = $c;
362 $count2++;
363 $pos = $count2;
364 $oldpos = 0;
365 do {
366 $oldpos = $pos;
367 $pos = $GLOBALS['PMA_String']->strpos(
368 ' ' . $sql, $quotetype, $oldpos + 1
369 ) - 1;
370 // ($pos === false)
371 if ($pos < 0) {
372 if ($c == '`') {
374 * Behave same as MySQL and accept end of query as end
375 * of backtick.
376 * I know this is sick, but MySQL behaves like this:
378 * SELECT * FROM `table
380 * is treated like
382 * SELECT * FROM `table`
384 $pos_quote_separator = $GLOBALS['PMA_String']->strpos(
385 ' ' . $sql, $GLOBALS['sql_delimiter'], $oldpos + 1
386 ) - 1;
387 if ($pos_quote_separator < 0) {
388 $len += 1;
389 $sql .= '`';
390 $sql_array['raw'] .= '`';
391 $pos = $len;
392 } else {
393 $len += 1;
394 $sql = $GLOBALS['PMA_String']->substr(
395 $sql, 0, $pos_quote_separator
396 ) . '`' . $GLOBALS['PMA_String']->substr(
397 $sql, $pos_quote_separator
399 $sql_array['raw'] = $sql;
400 $pos = $pos_quote_separator;
402 if (class_exists('PMA_Message')
403 && $GLOBALS['is_ajax_request'] != true
405 PMA_Message::notice(
406 __('Automatically appended backtick to the end of query!')
407 )->display();
409 } else {
410 $debugstr = __('Unclosed quote')
411 . ' @ ' . $startquotepos . "\n"
412 . 'STR: ' . htmlspecialchars($quotetype);
413 PMA_SQP_throwError($debugstr, $sql);
414 return $sql_array;
418 // If the quote is the first character, it can't be
419 // escaped, so don't do the rest of the code
420 if ($pos == 0) {
421 break;
424 // Checks for MySQL escaping using a \
425 // And checks for ANSI escaping using the $quotetype character
426 if (($pos < $len)
427 && $GLOBALS['PMA_String']->charIsEscaped($sql, $pos)
428 && $c != '`'
430 $pos ++;
431 continue;
432 } elseif (($pos + 1 < $len)
433 && ($GLOBALS['PMA_String']->substr($sql, $pos, 1) == $quotetype)
434 && ($GLOBALS['PMA_String']->substr($sql, $pos + 1, 1) == $quotetype)
436 $pos = $pos + 2;
437 continue;
438 } else {
439 break;
441 } while ($len > $pos); // end do
443 $count2 = $pos;
444 $count2++;
445 $type = 'quote_';
446 switch ($quotetype) {
447 case '\'':
448 $type .= 'single';
449 $this_was_quote = true;
450 break;
451 case '"':
452 $type .= 'double';
453 $this_was_quote = true;
454 break;
455 case '`':
456 $type .= 'backtick';
457 $this_was_quote = true;
458 break;
459 default:
460 break;
461 } // end switch
462 $data = $GLOBALS['PMA_String']->substr($sql, $count1, $count2 - $count1);
463 PMA_SQP_arrayAdd($sql_array, $type, $data, $arraysize);
464 continue;
467 // Checks for brackets
468 if ($GLOBALS['PMA_String']->strpos($bracket_list, $c) !== false) {
469 // All bracket tokens are only one item long
470 $this_was_bracket = true;
471 $count2++;
472 $type_type = '';
473 if ($GLOBALS['PMA_String']->strpos('([{', $c) !== false) {
474 $type_type = 'open';
475 } else {
476 $type_type = 'close';
479 $type_style = '';
480 if ($GLOBALS['PMA_String']->strpos('()', $c) !== false) {
481 $type_style = 'round';
482 } elseif ($GLOBALS['PMA_String']->strpos('[]', $c) !== false) {
483 $type_style = 'square';
484 } else {
485 $type_style = 'curly';
488 $type = 'punct_bracket_' . $type_type . '_' . $type_style;
489 PMA_SQP_arrayAdd($sql_array, $type, $c, $arraysize);
490 continue;
493 /* DEBUG
494 echo '<pre>1';
495 var_dump($GLOBALS['PMA_String']->isSqlIdentifier($c, false));
496 var_dump($c == '@');
497 var_dump($c == '.');
498 var_dump(
499 $GLOBALS['PMA_String']->isDigit(
500 $GLOBALS['PMA_String']->substr($sql, $count2 + 1, 1)
503 var_dump($previous_was_space);
504 var_dump($previous_was_bracket);
505 var_dump($previous_was_listsep);
506 echo '</pre>';
509 // Checks for identifier (alpha or numeric)
510 if ($GLOBALS['PMA_String']->isSqlIdentifier($c, false)
511 || $c == '@'
512 || ($c == '.'
513 && $GLOBALS['PMA_String']->isDigit($GLOBALS['PMA_String']->substr($sql, $count2 + 1, 1))
514 && ($previous_was_space || $previous_was_bracket || $previous_was_listsep))
516 /* DEBUG
517 echo $GLOBALS['PMA_String']->substr($sql, $count2);
518 echo '<hr />';
521 $count2++;
524 * @todo a @ can also be present in expressions like
525 * FROM 'user'@'%' or TO 'user'@'%'
526 * in this case, the @ is wrongly marked as alpha_variable
528 $is_identifier = $previous_was_punct;
529 $is_sql_variable = $c == '@' && ! $previous_was_quote;
530 $is_user = $c == '@' && $previous_was_quote;
531 $is_digit = (
532 !$is_identifier
533 && !$is_sql_variable
534 && $GLOBALS['PMA_String']->isDigit($c)
536 $is_hex_digit = (
537 $is_digit
538 && $c == '0'
539 && $count2 < $len
540 && $GLOBALS['PMA_String']->substr($sql, $count2, 1) == 'x'
542 $is_float_digit = $c == '.';
543 $is_float_digit_exponent = false;
545 /* DEBUG
546 echo '<pre>2';
547 var_dump($is_identifier);
548 var_dump($is_sql_variable);
549 var_dump($is_digit);
550 var_dump($is_float_digit);
551 echo '</pre>';
554 // Fast skip is especially needed for huge BLOB data
555 if ($is_hex_digit) {
556 $count2++;
557 $pos = strspn($sql, '0123456789abcdefABCDEF', $count2);
558 if ($pos > $count2) {
559 $count2 = $pos;
561 unset($pos);
562 } elseif ($is_digit) {
563 $pos = strspn($sql, '0123456789', $count2);
564 if ($pos > $count2) {
565 $count2 = $pos;
567 unset($pos);
570 while (($count2 < $len) && $GLOBALS['PMA_String']->isSqlIdentifier($GLOBALS['PMA_String']->substr($sql, $count2, 1), ($is_sql_variable || $is_digit))) {
571 $c2 = $GLOBALS['PMA_String']->substr($sql, $count2, 1);
572 if ($is_sql_variable && ($c2 == '.')) {
573 $count2++;
574 continue;
576 if ($is_digit && (!$is_hex_digit) && ($c2 == '.')) {
577 $count2++;
578 if (!$is_float_digit) {
579 $is_float_digit = true;
580 continue;
581 } else {
582 $debugstr = __('Invalid Identifer')
583 . ' @ ' . ($count1+1) . "\n"
584 . 'STR: ' . htmlspecialchars(
585 $GLOBALS['PMA_String']->substr(
586 $sql, $count1, $count2 - $count1
589 PMA_SQP_throwError($debugstr, $sql);
590 return $sql_array;
593 if ($is_digit
594 && (!$is_hex_digit)
595 && (($c2 == 'e') || ($c2 == 'E'))
597 if (!$is_float_digit_exponent) {
598 $is_float_digit_exponent = true;
599 $is_float_digit = true;
600 $count2++;
601 continue;
602 } else {
603 $is_digit = false;
604 $is_float_digit = false;
607 if (($is_hex_digit && $GLOBALS['PMA_String']->isHexDigit($c2))
608 || ($is_digit && $GLOBALS['PMA_String']->isDigit($c2))
610 $count2++;
611 continue;
612 } else {
613 $is_digit = false;
614 $is_hex_digit = false;
617 $count2++;
618 } // end while
620 $l = $count2 - $count1;
621 $str = $GLOBALS['PMA_String']->substr($sql, $count1, $l);
623 $type = '';
624 if ($is_digit || $is_float_digit || $is_hex_digit) {
625 $type = 'digit';
626 if ($is_float_digit) {
627 $type .= '_float';
628 } elseif ($is_hex_digit) {
629 $type .= '_hex';
630 } else {
631 $type .= '_integer';
633 } elseif ($is_user) {
634 $type = 'punct_user';
635 } elseif ($is_sql_variable != false) {
636 $type = 'alpha_variable';
637 } else {
638 $type = 'alpha';
639 } // end if... else....
640 PMA_SQP_arrayAdd($sql_array, $type, $str, $arraysize, $count2);
642 continue;
645 // Checks for punct
646 if ($GLOBALS['PMA_String']->strpos($allpunct_list, $c) !== false) {
647 while (($count2 < $len) && $GLOBALS['PMA_String']->strpos($allpunct_list, $GLOBALS['PMA_String']->substr($sql, $count2, 1)) !== false) {
648 $count2++;
650 $l = $count2 - $count1;
651 if ($l == 1) {
652 $punct_data = $c;
653 } else {
654 $punct_data = $GLOBALS['PMA_String']->substr($sql, $count1, $l);
657 // Special case, sometimes, althought two characters are
658 // adjectent directly, they ACTUALLY need to be seperate
659 /* DEBUG
660 echo '<pre>';
661 var_dump($l);
662 var_dump($punct_data);
663 echo '</pre>';
666 if ($l == 1) {
667 $t_suffix = '';
668 switch ($punct_data) {
669 case $punct_queryend:
670 $t_suffix = '_queryend';
671 break;
672 case $punct_qualifier:
673 $t_suffix = '_qualifier';
674 $this_was_punct = true;
675 break;
676 case $punct_listsep:
677 $this_was_listsep = true;
678 $t_suffix = '_listsep';
679 break;
680 default:
681 break;
683 PMA_SQP_arrayAdd(
684 $sql_array, 'punct' . $t_suffix, $punct_data, $arraysize
686 } elseif ($punct_data == $GLOBALS['sql_delimiter']
687 || isset($allpunct_list_pair[$punct_data])
689 // Ok, we have one of the valid combined punct expressions
690 PMA_SQP_arrayAdd($sql_array, 'punct', $punct_data, $arraysize);
691 } else {
692 // Bad luck, lets split it up more
693 $first = $punct_data[0];
694 $last2 = $punct_data[$l - 2] . $punct_data[$l - 1];
695 $last = $punct_data[$l - 1];
696 if (($first == ',') || ($first == ';') || ($first == '.')
697 || ($first == '*')
699 $count2 = $count1 + 1;
700 $punct_data = $first;
701 } elseif (($last2 == '/*') || (($last2 == '--') && ($count2 == $len || $GLOBALS['PMA_String']->substr($sql, $count2, 1) <= ' '))) {
702 $count2 -= 2;
703 $punct_data = $GLOBALS['PMA_String']->substr(
704 $sql, $count1, $count2 - $count1
706 } elseif (($last == '-') || ($last == '+') || ($last == '!')) {
707 $count2--;
708 $punct_data = $GLOBALS['PMA_String']->substr(
709 $sql, $count1, $count2 - $count1
711 } elseif ($last != '~') {
713 * @todo for negation operator, split in 2 tokens ?
714 * "select x&~1 from t"
715 * becomes "select x & ~ 1 from t" ?
717 $debugstr = __('Unknown Punctuation String')
718 . ' @ ' . ($count1+1) . "\n"
719 . 'STR: ' . htmlspecialchars($punct_data);
720 PMA_SQP_throwError($debugstr, $sql);
721 return $sql_array;
723 PMA_SQP_arrayAdd($sql_array, 'punct', $punct_data, $arraysize);
724 continue;
725 } // end if... elseif... else
726 continue;
729 // DEBUG
730 $count2++;
732 $debugstr = 'C1 C2 LEN: ' . $count1 . ' ' . $count2 . ' ' . $len . "\n"
733 . 'STR: ' . $GLOBALS['PMA_String']->substr(
734 $sql, $count1, $count2 - $count1
735 ) . "\n";
736 PMA_SQP_bug($debugstr, $sql);
737 return $sql_array;
739 } // end while ($count2 < $len)
742 echo '<pre>';
743 print_r($sql_array);
744 echo '</pre>';
747 if ($arraysize > 0) {
748 $t_next = $sql_array[0]['type'];
749 $t_prev = '';
750 $t_bef_prev = '';
751 $t_cur = '';
752 $d_next = $sql_array[0]['data'];
753 $d_prev = '';
754 $d_bef_prev = '';
755 $d_cur = '';
756 $d_next_upper = $t_next == 'alpha' ? strtoupper($d_next) : $d_next;
757 $d_prev_upper = '';
758 $d_bef_prev_upper = '';
759 $d_cur_upper = '';
762 for ($i = 0; $i < $arraysize; $i++) {
763 $t_bef_prev = $t_prev;
764 $t_prev = $t_cur;
765 $t_cur = $t_next;
766 $d_bef_prev = $d_prev;
767 $d_prev = $d_cur;
768 $d_cur = $d_next;
769 $d_bef_prev_upper = $d_prev_upper;
770 $d_prev_upper = $d_cur_upper;
771 $d_cur_upper = $d_next_upper;
772 if (($i + 1) < $arraysize) {
773 $t_next = $sql_array[$i + 1]['type'];
774 $d_next = $sql_array[$i + 1]['data'];
775 $d_next_upper = $t_next == 'alpha' ? strtoupper($d_next) : $d_next;
776 } else {
777 $t_next = '';
778 $d_next = '';
779 $d_next_upper = '';
782 /* DEBUG
783 echo "[prev: <strong>".$d_prev."</strong> ".$t_prev."][cur: <strong>"
784 . $d_cur."</strong> ".$t_cur."][next: <strong>".$d_next."</strong> "
785 . $t_next."]<br />";
788 if ($t_cur == 'alpha') {
789 $t_suffix = '_identifier';
790 // for example: `thebit` bit(8) NOT NULL DEFAULT b'0'
791 if ($t_prev == 'alpha' && $d_prev == 'DEFAULT' && $d_cur == 'b'
792 && $t_next == 'quote_single'
794 $t_suffix = '_bitfield_constant_introducer';
795 } elseif (($t_next == 'punct_qualifier')
796 || ($t_prev == 'punct_qualifier')
798 $t_suffix = '_identifier';
799 } elseif (($t_next == 'punct_bracket_open_round')
800 && isset($PMA_SQPdata_function_name[$d_cur_upper])
803 * @todo 2005-10-16: in the case of a CREATE TABLE containing
804 * a TIMESTAMP, since TIMESTAMP() is also a function, it's
805 * found here and the token is wrongly marked as alpha_functionName.
806 * But we compensate for this when analysing for timestamp_not_null
807 * later in this script.
809 * Same applies to CHAR vs. CHAR() function.
811 $t_suffix = '_functionName';
812 /* There are functions which might be as well column types */
813 } elseif (isset($PMA_SQPdata_column_type[$d_cur_upper])) {
814 $t_suffix = '_columnType';
817 * Temporary fix for bugs #621357 and #2027720
819 * @todo FIX PROPERLY NEEDS OVERHAUL OF SQL TOKENIZER
821 if (($d_cur_upper == 'SET' || $d_cur_upper == 'BINARY')
822 && $t_next != 'punct_bracket_open_round'
824 $t_suffix = '_reservedWord';
826 //END OF TEMPORARY FIX
828 // CHARACTER is a synonym for CHAR, but can also be meant as
829 // CHARACTER SET. In this case, we have a reserved word.
830 if ($d_cur_upper == 'CHARACTER' && $d_next_upper == 'SET') {
831 $t_suffix = '_reservedWord';
834 // experimental
835 // current is a column type, so previous must not be
836 // a reserved word but an identifier
837 // CREATE TABLE SG_Persons (first varchar(64))
839 //if ($sql_array[$i-1]['type'] =='alpha_reservedWord') {
840 // $sql_array[$i-1]['type'] = 'alpha_identifier';
843 } elseif (isset($PMA_SQPdata_reserved_word[$d_cur_upper])) {
844 $t_suffix = '_reservedWord';
845 } elseif (isset($PMA_SQPdata_column_attrib[$d_cur_upper])) {
846 $t_suffix = '_columnAttrib';
847 // INNODB is a MySQL table type, but in "SHOW INNODB STATUS",
848 // it should be regarded as a reserved word.
849 if ($d_cur_upper == 'INNODB'
850 && $d_prev_upper == 'SHOW'
851 && $d_next_upper == 'STATUS'
853 $t_suffix = '_reservedWord';
856 if ($d_cur_upper == 'DEFAULT' && $d_next_upper == 'CHARACTER') {
857 $t_suffix = '_reservedWord';
859 // Binary as character set
860 if ($d_cur_upper == 'BINARY'
861 && (($d_bef_prev_upper == 'CHARACTER' && $d_prev_upper == 'SET')
862 || ($d_bef_prev_upper == 'SET' && $d_prev_upper == '=')
863 || ($d_bef_prev_upper == 'CHARSET' && $d_prev_upper == '=')
864 || $d_prev_upper == 'CHARSET')
865 && in_array($d_cur, $mysql_charsets)
867 $t_suffix = '_charset';
869 } elseif (in_array($d_cur, $mysql_charsets)
870 || in_array($d_cur, $mysql_collations_flat)
871 || ($d_cur{0} == '_' && in_array(substr($d_cur, 1), $mysql_charsets))
873 $t_suffix = '_charset';
874 } else {
875 // Do nothing
877 // check if present in the list of forbidden words
878 if ($t_suffix == '_reservedWord'
879 && isset($PMA_SQPdata_forbidden_word[$d_cur_upper])
881 $sql_array[$i]['forbidden'] = true;
882 } else {
883 $sql_array[$i]['forbidden'] = false;
885 $sql_array[$i]['type'] .= $t_suffix;
887 } // end for
889 // Stores the size of the array inside the array, as count() is a slow
890 // operation.
891 $sql_array['len'] = $arraysize;
893 // DEBUG echo 'After parsing<pre>'; print_r($sql_array); echo '</pre>';
894 // Sends the data back
895 return $sql_array;
896 } // end of the "PMA_SQP_parse()" function
899 * Checks for token types being what we want...
901 * @param string $toCheck String of type that we have
902 * @param string $whatWeWant String of type that we want
904 * @return boolean result of check
906 * @access private
908 function PMA_SQP_typeCheck($toCheck, $whatWeWant)
910 $typeSeparator = '_';
911 if (strcmp($whatWeWant, $toCheck) == 0) {
912 return true;
913 } else {
914 if (strpos($whatWeWant, $typeSeparator) === false) {
915 return strncmp(
916 $whatWeWant, $toCheck,
917 strpos($toCheck, $typeSeparator)
918 ) == 0;
919 } else {
920 return false;
927 * Analyzes SQL queries
929 * @param array $arr The SQL queries
931 * @return array The analyzed SQL queries
933 * @access public
935 function PMA_SQP_analyze($arr)
937 if ($arr == array() || ! isset($arr['len'])) {
938 return array();
940 $result = array();
941 $size = $arr['len'];
942 $subresult = array(
943 'querytype' => '',
944 // the whole stuff between SELECT and FROM , except DISTINCT
945 'select_expr_clause'=> '',
946 'position_of_first_select' => '', // the array index
947 'from_clause'=> '',
948 'group_by_clause'=> '',
949 'order_by_clause'=> '',
950 'having_clause' => '',
951 'limit_clause' => '',
952 'where_clause' => '',
953 'where_clause_identifiers' => array(),
954 'unsorted_query' => '',
955 'queryflags' => array(),
956 'select_expr' => array(),
957 'table_ref' => array(),
958 'foreign_keys' => array(),
959 'create_table_fields' => array()
961 $subresult_empty = $subresult;
962 $seek_queryend = false;
963 $seen_end_of_table_ref = false;
964 $number_of_brackets_in_extract = 0;
965 $number_of_brackets_in_group_concat = 0;
967 $number_of_brackets = 0;
968 $in_subquery = false;
969 $seen_subquery = false;
970 $seen_from = false;
972 // for SELECT EXTRACT(YEAR_MONTH FROM CURDATE())
973 // we must not use CURDATE as a table_ref
974 // so we track whether we are in the EXTRACT()
975 $in_extract = false;
977 // for GROUP_CONCAT(...)
978 $in_group_concat = false;
980 /* Description of analyzer results
982 * db, table, column, alias
983 * ------------------------
985 * Inside the $subresult array, we create ['select_expr'] and ['table_ref']
986 * arrays.
988 * The SELECT syntax (simplified) is
990 * SELECT
991 * select_expression,...
992 * [FROM [table_references]
995 * ['select_expr'] is filled with each expression, the key represents the
996 * expression position in the list (0-based) (so we don't lose track of
997 * multiple occurences of the same column).
999 * ['table_ref'] is filled with each table ref, same thing for the key.
1001 * I create all sub-values empty, even if they are
1002 * not present (for example no select_expression alias).
1004 * There is a debug section at the end of loop #1, if you want to
1005 * see the exact contents of select_expr and table_ref
1007 * queryflags
1008 * ----------
1010 * In $subresult, array 'queryflags' is filled, according to what we
1011 * find in the query.
1013 * Currently, those are generated:
1015 * ['queryflags']['select_from'] = 1; if this is a real SELECT...FROM
1016 * ['queryflags']['drop_database'] = 1;if this is a DROP DATABASE
1017 * ['queryflags']['reload'] = 1; for the purpose of reloading the
1018 * navigation bar
1019 * ['queryflags']['distinct'] = 1; for a DISTINCT
1020 * ['queryflags']['union'] = 1; for a UNION
1021 * ['queryflags']['join'] = 1; for a JOIN
1022 * ['queryflags']['offset'] = 1; for the presence of OFFSET
1023 * ['queryflags']['procedure'] = 1; for the presence of PROCEDURE
1024 * ['queryflags']['is_explain'] = 1; for the presence of EXPLAIN
1025 * ['queryflags']['is_delete'] = 1; for the presence of DELETE
1026 * ['queryflags']['is_affected'] = 1; for the presence of UPDATE, DELETE
1027 * or INSERT|LOAD DATA|REPLACE
1028 * ['queryflags']['is_replace'] = 1; for the presence of REPLACE
1029 * ['queryflags']['is_insert'] = 1; for the presence of INSERT
1030 * ['queryflags']['is_maint'] = 1; for the presence of CHECK|ANALYZE
1031 * |REPAIR|OPTIMIZE TABLE
1032 * ['queryflags']['is_show'] = 1; for the presence of SHOW
1033 * ['queryflags']['is_analyse'] = 1; for the presence of PROCEDURE ANALYSE
1034 * ['queryflags']['is_export'] = 1; for the presence of INTO OUTFILE
1035 * ['queryflags']['is_group'] = 1; for the presence of GROUP BY|HAVING|
1036 * SELECT DISTINCT
1037 * ['queryflags']['is_func'] = 1; for the presence of SUM|AVG|STD|STDDEV
1038 * |MIN|MAX|BIT_OR|BIT_AND
1039 * ['queryflags']['is_count'] = 1; for the presence of SELECT COUNT
1040 * ['queryflags']['is_procedure'] = 1; for the presence of CALL
1041 * ['queryflags']['is_subquery'] = 1; contains a subquery
1043 * query clauses
1044 * -------------
1046 * The select is splitted in those clauses:
1047 * ['select_expr_clause']
1048 * ['from_clause']
1049 * ['group_by_clause']
1050 * ['order_by_clause']
1051 * ['having_clause']
1052 * ['limit_clause']
1053 * ['where_clause']
1055 * The identifiers of the WHERE clause are put into the array
1056 * ['where_clause_identifier']
1058 * For a SELECT, the whole query without the ORDER BY clause is put into
1059 * ['unsorted_query']
1061 * foreign keys
1062 * ------------
1063 * The CREATE TABLE may contain FOREIGN KEY clauses, so they get
1064 * analyzed and ['foreign_keys'] is an array filled with
1065 * the constraint name, the index list,
1066 * the REFERENCES table name and REFERENCES index list,
1067 * and ON UPDATE | ON DELETE clauses
1069 * position_of_first_select
1070 * ------------------------
1072 * The array index of the first SELECT we find. Will be used to
1073 * insert a SQL_CALC_FOUND_ROWS.
1075 * create_table_fields
1076 * -------------------
1078 * Used to detect the DEFAULT CURRENT_TIMESTAMP and
1079 * ON UPDATE CURRENT_TIMESTAMP clauses of the CREATE TABLE query.
1080 * Also used to store the default value of the field.
1081 * An array, each element is the identifier name.
1082 * Note that for now, the timestamp_not_null element is created
1083 * even for non-TIMESTAMP fields.
1085 * Sub-elements: ['type'] which contains the column type
1086 * optional (currently they are never false but can be absent):
1087 * ['default_current_timestamp'] boolean
1088 * ['on_update_current_timestamp'] boolean
1089 * ['timestamp_not_null'] boolean
1091 * section_before_limit, section_after_limit
1092 * -----------------------------------------
1094 * Marks the point of the query where we can insert a LIMIT clause;
1095 * so the section_before_limit will contain the left part before
1096 * a possible LIMIT clause
1099 * End of description of analyzer results
1102 // must be sorted
1103 // TODO: current logic checks for only one word, so I put only the
1104 // first word of the reserved expressions that end a table ref;
1105 // maybe this is not ok (the first word might mean something else)
1106 // $words_ending_table_ref = array(
1107 // 'FOR UPDATE',
1108 // 'GROUP BY',
1109 // 'HAVING',
1110 // 'LIMIT',
1111 // 'LOCK IN SHARE MODE',
1112 // 'ORDER BY',
1113 // 'PROCEDURE',
1114 // 'UNION',
1115 // 'WHERE'
1116 // );
1117 $words_ending_table_ref = array(
1118 'FOR' => 1,
1119 'GROUP' => 1,
1120 'HAVING' => 1,
1121 'LIMIT' => 1,
1122 'LOCK' => 1,
1123 'ORDER' => 1,
1124 'PROCEDURE' => 1,
1125 'UNION' => 1,
1126 'WHERE' => 1
1129 $words_ending_clauses = array(
1130 'FOR' => 1,
1131 'LIMIT' => 1,
1132 'LOCK' => 1,
1133 'PROCEDURE' => 1,
1134 'UNION' => 1
1137 $supported_query_types = array(
1138 'SELECT' => 1,
1140 // Support for these additional query types will come later on.
1141 'DELETE' => 1,
1142 'INSERT' => 1,
1143 'REPLACE' => 1,
1144 'TRUNCATE' => 1,
1145 'UPDATE' => 1,
1146 'EXPLAIN' => 1,
1147 'DESCRIBE' => 1,
1148 'SHOW' => 1,
1149 'CREATE' => 1,
1150 'SET' => 1,
1151 'ALTER' => 1
1155 // loop #1 for each token: select_expr, table_ref for SELECT
1157 for ($i = 0; $i < $size; $i++) {
1158 //DEBUG echo "Loop1 <strong>" . $arr[$i]['data']
1159 //. "</strong> (" . $arr[$i]['type'] . ")<br />";
1161 // High speed seek for locating the end of the current query
1162 if ($seek_queryend == true) {
1163 if ($arr[$i]['type'] == 'punct_queryend') {
1164 $seek_queryend = false;
1165 } else {
1166 continue;
1167 } // end if (type == punct_queryend)
1168 } // end if ($seek_queryend)
1171 * Note: do not split if this is a punct_queryend for the first and only
1172 * query
1173 * @todo when we find a UNION, should we split in another subresult?
1175 if ($arr[$i]['type'] == 'punct_queryend' && ($i + 1 != $size)) {
1176 $result[] = $subresult;
1177 $subresult = $subresult_empty;
1178 continue;
1179 } // end if (type == punct_queryend)
1181 // ==============================================================
1182 if ($arr[$i]['type'] == 'punct_bracket_open_round') {
1183 $number_of_brackets++;
1184 if ($in_extract) {
1185 $number_of_brackets_in_extract++;
1187 if ($in_group_concat) {
1188 $number_of_brackets_in_group_concat++;
1191 // ==============================================================
1192 if ($arr[$i]['type'] == 'punct_bracket_close_round') {
1193 $number_of_brackets--;
1194 if ($number_of_brackets == 0) {
1195 $in_subquery = false;
1197 if ($in_extract) {
1198 $number_of_brackets_in_extract--;
1199 if ($number_of_brackets_in_extract == 0) {
1200 $in_extract = false;
1203 if ($in_group_concat) {
1204 $number_of_brackets_in_group_concat--;
1205 if ($number_of_brackets_in_group_concat == 0) {
1206 $in_group_concat = false;
1211 if ($in_subquery) {
1213 * skip the subquery to avoid setting
1214 * select_expr or table_ref with the contents
1215 * of this subquery; this is to avoid a bug when
1216 * trying to edit the results of
1217 * select * from child where not exists (select id from
1218 * parent where child.parent_id = parent.id);
1220 continue;
1222 // ==============================================================
1223 if ($arr[$i]['type'] == 'alpha_functionName') {
1224 $upper_data = strtoupper($arr[$i]['data']);
1225 if ($upper_data =='EXTRACT') {
1226 $in_extract = true;
1227 $number_of_brackets_in_extract = 0;
1229 if ($upper_data =='GROUP_CONCAT') {
1230 $in_group_concat = true;
1231 $number_of_brackets_in_group_concat = 0;
1235 // ==============================================================
1236 if ($arr[$i]['type'] == 'alpha_reservedWord') {
1237 // We don't know what type of query yet, so run this
1238 if ($subresult['querytype'] == '') {
1239 $subresult['querytype'] = strtoupper($arr[$i]['data']);
1240 } // end if (querytype was empty)
1242 // Check if we support this type of query
1243 if (!isset($supported_query_types[$subresult['querytype']])) {
1244 // Skip ahead to the next one if we don't
1245 $seek_queryend = true;
1246 continue;
1247 } // end if (query not supported)
1249 // upper once
1250 $upper_data = strtoupper($arr[$i]['data']);
1252 * @todo reset for each query?
1255 if ($upper_data == 'SELECT') {
1256 if ($number_of_brackets > 0) {
1257 $in_subquery = true;
1258 $seen_subquery = true;
1259 $subresult['queryflags']['is_subquery'] = 1;
1260 // this is a subquery so do not analyze inside it
1261 continue;
1263 $seen_from = false;
1264 $previous_was_identifier = false;
1265 $current_select_expr = -1;
1266 $seen_end_of_table_ref = false;
1267 } // end if (data == SELECT)
1269 if ($upper_data =='FROM' && !$in_extract) {
1270 $current_table_ref = -1;
1271 $seen_from = true;
1272 $previous_was_identifier = false;
1273 $save_table_ref = true;
1274 } // end if (data == FROM)
1276 // here, do not 'continue' the loop, as we have more work for
1277 // reserved words below
1278 } // end if (type == alpha_reservedWord)
1280 // ==============================
1281 if ($arr[$i]['type'] == 'quote_backtick'
1282 || $arr[$i]['type'] == 'quote_double'
1283 || $arr[$i]['type'] == 'quote_single'
1284 || $arr[$i]['type'] == 'alpha_identifier'
1285 || ($arr[$i]['type'] == 'alpha_reservedWord'
1286 && $arr[$i]['forbidden'] == false)
1288 switch ($arr[$i]['type']) {
1289 case 'alpha_identifier':
1290 case 'alpha_reservedWord':
1292 * this is not a real reservedWord, because it's not
1293 * present in the list of forbidden words, for example
1294 * "storage" which can be used as an identifier
1297 $identifier = $arr[$i]['data'];
1298 break;
1300 case 'quote_backtick':
1301 case 'quote_double':
1302 case 'quote_single':
1303 $identifier = PMA_Util::unQuote($arr[$i]['data']);
1304 break;
1305 } // end switch
1307 if ($subresult['querytype'] == 'SELECT'
1308 && ! $in_group_concat
1309 && ! ($seen_subquery && $arr[$i - 1]['type'] == 'punct_bracket_close_round')
1311 if (!$seen_from) {
1312 if ($previous_was_identifier && isset($chain)) {
1313 // found alias for this select_expr, save it
1314 // but only if we got something in $chain
1315 // (for example, SELECT COUNT(*) AS cnt
1316 // puts nothing in $chain, so we avoid
1317 // setting the alias)
1318 $alias_for_select_expr = $identifier;
1319 } else {
1320 if (! isset($chain)) {
1321 $chain = array();
1323 $chain[] = $identifier;
1324 $previous_was_identifier = true;
1326 } // end if !$previous_was_identifier
1327 } else {
1328 // ($seen_from)
1329 if ($save_table_ref && !$seen_end_of_table_ref) {
1330 if ($previous_was_identifier) {
1331 // found alias for table ref
1332 // save it for later
1333 $alias_for_table_ref = $identifier;
1334 } else {
1335 if (! isset($chain)) {
1336 $chain = array();
1338 $chain[] = $identifier;
1339 $previous_was_identifier = true;
1341 } // end if ($previous_was_identifier)
1342 } // end if ($save_table_ref &&!$seen_end_of_table_ref)
1343 } // end if (!$seen_from)
1344 } // end if (querytype SELECT)
1345 } // end if (quote_backtick or double quote or alpha_identifier)
1347 // ===================================
1348 if ($arr[$i]['type'] == 'punct_qualifier') {
1349 // to be able to detect an identifier following another
1350 $previous_was_identifier = false;
1351 continue;
1352 } // end if (punct_qualifier)
1355 * @todo check if 3 identifiers following one another -> error
1358 // s a v e a s e l e c t e x p r
1359 // finding a list separator or FROM
1360 // means that we must save the current chain of identifiers
1361 // into a select expression
1363 // for now, we only save a select expression if it contains
1364 // at least one identifier, as we are interested in checking
1365 // the columns and table names, so in "select * from persons",
1366 // the "*" is not saved
1368 if (isset($chain) && !$seen_end_of_table_ref
1369 && ((!$seen_from && $arr[$i]['type'] == 'punct_listsep')
1370 || ($arr[$i]['type'] == 'alpha_reservedWord' && $upper_data == 'FROM'))
1372 $size_chain = count($chain);
1373 $current_select_expr++;
1374 $subresult['select_expr'][$current_select_expr] = array(
1375 'expr' => '',
1376 'alias' => '',
1377 'db' => '',
1378 'table_name' => '',
1379 'table_true_name' => '',
1380 'column' => ''
1383 if (isset($alias_for_select_expr) && strlen($alias_for_select_expr)) {
1384 // we had found an alias for this select expression
1385 $subresult['select_expr'][$current_select_expr]['alias']
1386 = $alias_for_select_expr;
1387 unset($alias_for_select_expr);
1389 // there is at least a column
1390 $subresult['select_expr'][$current_select_expr]['column']
1391 = $chain[$size_chain - 1];
1392 $subresult['select_expr'][$current_select_expr]['expr']
1393 = $chain[$size_chain - 1];
1395 // maybe a table
1396 if ($size_chain > 1) {
1397 $subresult['select_expr'][$current_select_expr]['table_name']
1398 = $chain[$size_chain - 2];
1399 // we assume for now that this is also the true name
1400 $subresult['select_expr'][$current_select_expr]['table_true_name']
1401 = $chain[$size_chain - 2];
1402 $subresult['select_expr'][$current_select_expr]['expr']
1403 = $subresult['select_expr'][$current_select_expr]['table_name']
1404 . '.' . $subresult['select_expr'][$current_select_expr]['expr'];
1405 } // end if ($size_chain > 1)
1407 // maybe a db
1408 if ($size_chain > 2) {
1409 $subresult['select_expr'][$current_select_expr]['db']
1410 = $chain[$size_chain - 3];
1411 $subresult['select_expr'][$current_select_expr]['expr']
1412 = $subresult['select_expr'][$current_select_expr]['db']
1413 . '.' . $subresult['select_expr'][$current_select_expr]['expr'];
1414 } // end if ($size_chain > 2)
1415 unset($chain);
1418 * @todo explain this:
1420 if (($arr[$i]['type'] == 'alpha_reservedWord')
1421 && ($upper_data != 'FROM')
1423 $previous_was_identifier = true;
1426 } // end if (save a select expr)
1429 //======================================
1430 // s a v e a t a b l e r e f
1431 //======================================
1433 // maybe we just saw the end of table refs
1434 // but the last table ref has to be saved
1435 // or we are at the last token
1436 // or we just got a reserved word
1438 * @todo there could be another query after this one
1441 if (isset($chain) && $seen_from && $save_table_ref
1442 && ($arr[$i]['type'] == 'punct_listsep'
1443 || ($arr[$i]['type'] == 'alpha_reservedWord' && $upper_data != "AS")
1444 || $seen_end_of_table_ref
1445 || $i == $size - 1)
1448 $size_chain = count($chain);
1449 $current_table_ref++;
1450 $subresult['table_ref'][$current_table_ref] = array(
1451 'expr' => '',
1452 'db' => '',
1453 'table_name' => '',
1454 'table_alias' => '',
1455 'table_true_name' => ''
1457 if (isset($alias_for_table_ref) && strlen($alias_for_table_ref)) {
1458 $subresult['table_ref'][$current_table_ref]['table_alias']
1459 = $alias_for_table_ref;
1460 unset($alias_for_table_ref);
1462 $subresult['table_ref'][$current_table_ref]['table_name']
1463 = $chain[$size_chain - 1];
1464 // we assume for now that this is also the true name
1465 $subresult['table_ref'][$current_table_ref]['table_true_name']
1466 = $chain[$size_chain - 1];
1467 $subresult['table_ref'][$current_table_ref]['expr']
1468 = $subresult['table_ref'][$current_table_ref]['table_name'];
1469 // maybe a db
1470 if ($size_chain > 1) {
1471 $subresult['table_ref'][$current_table_ref]['db']
1472 = $chain[$size_chain - 2];
1473 $subresult['table_ref'][$current_table_ref]['expr']
1474 = $subresult['table_ref'][$current_table_ref]['db']
1475 . '.' . $subresult['table_ref'][$current_table_ref]['expr'];
1476 } // end if ($size_chain > 1)
1478 // add the table alias into the whole expression
1479 $subresult['table_ref'][$current_table_ref]['expr']
1480 .= ' ' . $subresult['table_ref'][$current_table_ref]['table_alias'];
1482 unset($chain);
1483 $previous_was_identifier = true;
1484 //continue;
1486 } // end if (save a table ref)
1489 // when we have found all table refs,
1490 // for each table_ref alias, put the true name of the table
1491 // in the corresponding select expressions
1493 if (isset($current_table_ref)
1494 && ($seen_end_of_table_ref || $i == $size-1)
1495 && $subresult != $subresult_empty
1497 for ($tr=0; $tr <= $current_table_ref; $tr++) {
1498 $alias = $subresult['table_ref'][$tr]['table_alias'];
1499 $truename = $subresult['table_ref'][$tr]['table_true_name'];
1500 for ($se=0; $se <= $current_select_expr; $se++) {
1501 if (isset($alias)
1502 && strlen($alias)
1503 && $subresult['select_expr'][$se]['table_true_name'] == $alias
1505 $subresult['select_expr'][$se]['table_true_name']
1506 = $truename;
1507 } // end if (found the alias)
1508 } // end for (select expressions)
1510 } // end for (table refs)
1511 } // end if (set the true names)
1514 // e n d i n g l o o p #1
1515 // set the $previous_was_identifier to false if the current
1516 // token is not an identifier
1517 if (($arr[$i]['type'] != 'alpha_identifier')
1518 && ($arr[$i]['type'] != 'quote_double')
1519 && ($arr[$i]['type'] != 'quote_single')
1520 && ($arr[$i]['type'] != 'quote_backtick')
1522 $previous_was_identifier = false;
1523 } // end if
1525 // however, if we are on AS, we must keep the $previous_was_identifier
1526 if (($arr[$i]['type'] == 'alpha_reservedWord')
1527 && ($upper_data == 'AS')
1529 $previous_was_identifier = true;
1532 if (($arr[$i]['type'] == 'alpha_reservedWord')
1533 && ($upper_data =='ON' || $upper_data =='USING')
1535 $save_table_ref = false;
1536 } // end if (data == ON)
1538 if (($arr[$i]['type'] == 'alpha_reservedWord')
1539 && ($upper_data =='JOIN' || $upper_data =='FROM')
1541 $save_table_ref = true;
1542 } // end if (data == JOIN)
1545 * no need to check the end of table ref if we already did
1547 * @todo maybe add "&& $seen_from"
1549 if (!$seen_end_of_table_ref) {
1550 // if this is the last token, it implies that we have
1551 // seen the end of table references
1552 // Check for the end of table references
1554 // Note: if we are analyzing a GROUP_CONCAT clause,
1555 // we might find a word that seems to indicate that
1556 // we have found the end of table refs (like ORDER)
1557 // but it's a modifier of the GROUP_CONCAT so
1558 // it's not the real end of table refs
1559 if (($i == $size-1)
1560 || ($arr[$i]['type'] == 'alpha_reservedWord'
1561 && !$in_group_concat
1562 && isset($words_ending_table_ref[$upper_data]))
1564 $seen_end_of_table_ref = true;
1565 // to be able to save the last table ref, but do not
1566 // set it true if we found a word like "ON" that has
1567 // already set it to false
1568 if (isset($save_table_ref) && $save_table_ref != false) {
1569 $save_table_ref = true;
1570 } //end if
1572 } // end if (check for end of table ref)
1573 } //end if (!$seen_end_of_table_ref)
1575 if ($seen_end_of_table_ref) {
1576 $save_table_ref = false;
1577 } // end if
1579 } // end for $i (loop #1)
1581 //DEBUG
1583 if (isset($current_select_expr)) {
1584 for ($trace=0; $trace<=$current_select_expr; $trace++) {
1585 echo "<br />";
1586 reset ($subresult['select_expr'][$trace]);
1587 while (list ($key, $val) = each ($subresult['select_expr'][$trace]))
1588 echo "sel expr $trace $key => $val<br />\n";
1592 if (isset($current_table_ref)) {
1593 echo "current_table_ref = " . $current_table_ref . "<br>";
1594 for ($trace=0; $trace<=$current_table_ref; $trace++) {
1596 echo "<br />";
1597 reset ($subresult['table_ref'][$trace]);
1598 while (list ($key, $val) = each ($subresult['table_ref'][$trace]))
1599 echo "table ref $trace $key => $val<br />\n";
1603 // -------------------------------------------------------
1606 // loop #2: - queryflags
1607 // - querytype (for queries != 'SELECT')
1608 // - section_before_limit, section_after_limit
1610 // we will also need this queryflag in loop 2
1611 // so set it here
1612 if (isset($current_table_ref) && $current_table_ref > -1) {
1613 $subresult['queryflags']['select_from'] = 1;
1616 $section_before_limit = '';
1617 $section_after_limit = ''; // truly the section after the limit clause
1618 $seen_reserved_word = false;
1619 $seen_group = false;
1620 $seen_order = false;
1621 $seen_order_by = false;
1622 $in_group_by = false; // true when we are inside the GROUP BY clause
1623 $in_order_by = false; // true when we are inside the ORDER BY clause
1624 $in_having = false; // true when we are inside the HAVING clause
1625 $in_select_expr = false; // true when we are inside the select expr clause
1626 $in_where = false; // true when we are inside the WHERE clause
1627 $seen_limit = false; // true if we have seen a LIMIT clause
1628 $in_limit = false; // true when we are inside the LIMIT clause
1629 $after_limit = false; // true when we are after the LIMIT clause
1630 $in_from = false; // true when we are in the FROM clause
1631 $in_group_concat = false;
1632 $first_reserved_word = '';
1633 $current_identifier = '';
1634 $unsorted_query = $arr['raw']; // in case there is no ORDER BY
1635 $number_of_brackets = 0;
1636 $in_subquery = false;
1638 $arrayFunctions = array(
1639 "SUM","AVG","STD","STDDEV","MIN","MAX","BIT_OR","BIT_AND"
1641 $arrayKeyWords = array("BY", "HAVING", "SELECT");
1643 for ($i = 0; $i < $size; $i++) {
1644 //DEBUG echo "Loop2 <strong>" . $arr[$i]['data']
1645 //. "</strong> (" . $arr[$i]['type'] . ")<br />";
1647 if ($arr[$i]['type'] == 'punct_bracket_open_round') {
1648 $number_of_brackets++;
1651 if ($arr[$i]['type'] == 'punct_bracket_close_round') {
1652 $number_of_brackets--;
1653 if ($number_of_brackets == 0) {
1654 $in_subquery = false;
1658 if ($arr[$i]['type'] == 'alpha_reservedWord') {
1659 $upper_data = strtoupper($arr[$i]['data']);
1661 if ($upper_data == 'SELECT' && $number_of_brackets > 0) {
1662 $in_subquery = true;
1665 if (!$seen_reserved_word) {
1666 $first_reserved_word = $upper_data;
1667 $subresult['querytype'] = $upper_data;
1668 $seen_reserved_word = true;
1670 if ($first_reserved_word === 'SELECT') {
1671 $position_of_first_select = $i;
1672 } elseif ($first_reserved_word === 'EXPLAIN') {
1673 $subresult['queryflags']['is_explain'] = 1;
1674 } elseif ($first_reserved_word === 'DELETE') {
1675 $subresult['queryflags']['is_delete'] = 1;
1676 $subresult['queryflags']['is_affected'] = 1;
1677 } elseif ($first_reserved_word === 'UPDATE') {
1678 $subresult['queryflags']['is_affected'] = 1;
1679 } elseif ($first_reserved_word === 'REPLACE') {
1680 $subresult['queryflags']['is_replace'] = 1;
1681 $subresult['queryflags']['is_affected'] = 1;
1682 } elseif ($first_reserved_word === 'INSERT') {
1683 $subresult['queryflags']['is_insert'] = 1;
1684 $subresult['queryflags']['is_affected'] = 1;
1685 } elseif ($first_reserved_word === 'SHOW') {
1686 $subresult['queryflags']['is_show'] = 1;
1689 } else {
1690 // for the presence of DROP DATABASE
1691 if ($first_reserved_word == 'DROP' && $upper_data == 'DATABASE') {
1692 $subresult['queryflags']['drop_database'] = 1;
1694 // A table has to be created, renamed, dropped -> navi panel
1695 // should be reloaded
1696 $keywords1 = array('CREATE', 'ALTER', 'DROP');
1697 $keywords2 = array('VIEW', 'TABLE', 'DATABASE', 'SCHEMA');
1698 if (in_array($first_reserved_word, $keywords1)
1699 && in_array($upper_data, $keywords2)
1701 $subresult['queryflags']['reload'] = 1;
1703 // for the presence of CHECK|ANALYZE|REPAIR|OPTIMIZE TABLE
1704 $keywords = array(
1705 'CHECK', 'ANALYZE', 'REPAIR', 'OPTIMIZE'
1707 if (in_array($first_reserved_word, $keywords)
1708 && $upper_data == 'TABLE'
1710 $subresult['queryflags']['is_maint'] = 1;
1714 if ($upper_data == 'LIMIT' && ! $in_subquery) {
1715 $section_before_limit = substr($arr['raw'], 0, $arr[$i]['pos'] - 5);
1716 $in_limit = true;
1717 $seen_limit = true;
1718 $limit_clause = '';
1719 $in_order_by = false; // @todo maybe others to set false
1722 if ($upper_data == 'PROCEDURE') {
1723 $subresult['queryflags']['procedure'] = 1;
1724 $in_limit = false;
1725 $after_limit = true;
1727 // for the presence of PROCEDURE ANALYSE
1728 if (isset($subresult['queryflags']['select_from'])
1729 && $subresult['queryflags']['select_from'] == 1
1730 && ($i + 1) < $size
1731 && $arr[$i + 1]['type'] == 'alpha_reservedWord'
1732 && strtoupper($arr[$i + 1]['data']) == 'ANALYSE'
1734 $subresult['queryflags']['is_analyse'] = 1;
1738 // for the presence of INTO OUTFILE
1739 if ($upper_data == 'INTO'
1740 && isset($subresult['queryflags']['select_from'])
1741 && $subresult['queryflags']['select_from'] == 1
1742 && ($i + 1) < $size
1743 && $arr[$i + 1]['type'] == 'alpha_reservedWord'
1744 && strtoupper($arr[$i + 1]['data']) == 'OUTFILE'
1746 $subresult['queryflags']['is_export'] = 1;
1749 * @todo set also to false if we find FOR UPDATE or LOCK IN SHARE MODE
1751 if ($upper_data == 'SELECT') {
1752 $in_select_expr = true;
1753 $select_expr_clause = '';
1755 // for the presence of SELECT COUNT
1756 if (isset($subresult['queryflags']['select_from'])
1757 && $subresult['queryflags']['select_from'] == 1
1758 && !isset($subresult['queryflags']['is_group'])
1759 && ($i + 1) < $size
1760 && $arr[$i + 1]['type'] == 'alpha_functionName'
1761 && strtoupper($arr[$i + 1]['data']) == 'COUNT'
1763 $subresult['queryflags']['is_count'] = 1;
1767 if ($upper_data == 'DISTINCT' && !$in_group_concat) {
1768 $subresult['queryflags']['distinct'] = 1;
1771 if ($upper_data == 'UNION') {
1772 $subresult['queryflags']['union'] = 1;
1775 if ($upper_data == 'JOIN') {
1776 $subresult['queryflags']['join'] = 1;
1779 if ($upper_data == 'OFFSET') {
1780 $subresult['queryflags']['offset'] = 1;
1783 // for the presence of CALL
1784 if ($upper_data == 'CALL') {
1785 $subresult['queryflags']['is_procedure'] = 1;
1788 // if this is a real SELECT...FROM
1789 if ($upper_data == 'FROM'
1790 && isset($subresult['queryflags']['select_from'])
1791 && $subresult['queryflags']['select_from'] == 1
1793 $in_from = true;
1794 $from_clause = '';
1795 $in_select_expr = false;
1799 // (we could have less resetting of variables to false
1800 // if we trust that the query respects the standard
1801 // MySQL order for clauses)
1803 // we use $seen_group and $seen_order because we are looking
1804 // for the BY
1805 if ($upper_data == 'GROUP') {
1806 $seen_group = true;
1807 $seen_order = false;
1808 $in_having = false;
1809 $in_order_by = false;
1810 $in_where = false;
1811 $in_select_expr = false;
1812 $in_from = false;
1814 // for the presence of GROUP BY|HAVING|SELECT DISTINCT
1815 if (isset($subresult['queryflags']['select_from'])
1816 && $subresult['queryflags']['select_from'] == 1
1817 && ($i + 1) < $size
1818 && $arr[$i + 1]['type'] == 'alpha_reservedWord'
1819 && in_array(strtoupper($arr[$i + 1]['data']), $arrayKeyWords)
1820 && ($i + 2) < $size
1821 && $arr[$i + 2]['type'] == 'alpha_reservedWord'
1822 && strtoupper($arr[$i + 2]['data']) == 'DISTINCT'
1824 $subresult['queryflags']['is_group'] = 1;
1827 if ($upper_data == 'ORDER' && !$in_group_concat) {
1828 $seen_order = true;
1829 $seen_group = false;
1830 $in_having = false;
1831 $in_group_by = false;
1832 $in_where = false;
1833 $in_select_expr = false;
1834 $in_from = false;
1836 if ($upper_data == 'HAVING') {
1837 $in_having = true;
1838 $having_clause = '';
1839 $seen_group = false;
1840 $seen_order = false;
1841 $in_group_by = false;
1842 $in_order_by = false;
1843 $in_where = false;
1844 $in_select_expr = false;
1845 $in_from = false;
1848 if ($upper_data == 'WHERE') {
1849 $in_where = true;
1850 $where_clause = '';
1851 $where_clause_identifiers = array();
1852 $seen_group = false;
1853 $seen_order = false;
1854 $in_group_by = false;
1855 $in_order_by = false;
1856 $in_having = false;
1857 $in_select_expr = false;
1858 $in_from = false;
1861 if ($upper_data == 'BY') {
1862 if ($seen_group) {
1863 $in_group_by = true;
1864 $group_by_clause = '';
1866 if ($seen_order) {
1867 $seen_order_by = true;
1868 // Here we assume that the ORDER BY keywords took
1869 // exactly 8 characters.
1870 // We use $GLOBALS['PMA_String']->substr() to be charset-safe;
1871 // otherwise if the table name contains accents, the unsorted
1872 // query would be missing some characters.
1873 $unsorted_query = $GLOBALS['PMA_String']->substr(
1874 $arr['raw'], 0, $arr[$i]['pos'] - 8
1876 $in_order_by = true;
1877 $order_by_clause = '';
1881 // if we find one of the words that could end the clause
1882 if (isset($words_ending_clauses[$upper_data])) {
1884 $in_group_by = false;
1885 $in_order_by = false;
1886 $in_having = false;
1887 $in_where = false;
1888 $in_select_expr = false;
1889 $in_from = false;
1892 } // endif (reservedWord)
1894 // do not add a space after a function name
1896 * @todo can we combine loop 2 and loop 1? some code is repeated here...
1899 $sep = ' ';
1900 if ($arr[$i]['type'] == 'alpha_functionName') {
1901 $sep='';
1902 $upper_data = strtoupper($arr[$i]['data']);
1903 if ($upper_data =='GROUP_CONCAT') {
1904 $in_group_concat = true;
1905 $number_of_brackets_in_group_concat = 0;
1909 if ($arr[$i]['type'] == 'punct_bracket_open_round') {
1910 if ($in_group_concat) {
1911 $number_of_brackets_in_group_concat++;
1914 if ($arr[$i]['type'] == 'punct_bracket_close_round') {
1915 if ($in_group_concat) {
1916 $number_of_brackets_in_group_concat--;
1917 if ($number_of_brackets_in_group_concat == 0) {
1918 $in_group_concat = false;
1924 // do not add a space after an identifier if followed by a dot
1925 if ($arr[$i]['type'] == 'alpha_identifier'
1926 && $i < $size - 1 && $arr[$i + 1]['data'] == '.'
1928 $sep = '';
1931 // do not add a space after a dot if followed by an identifier
1932 if ($arr[$i]['data'] == '.' && $i < $size - 1
1933 && $arr[$i + 1]['type'] == 'alpha_identifier'
1935 $sep = '';
1938 // for the presence of INSERT|LOAD DATA
1939 if ($arr[$i]['type'] == 'alpha_identifier'
1940 && strtoupper($arr[$i]['data']) == 'DATA'
1941 && ($i - 1) >= 0
1942 && $arr[$i - 1]['type'] == 'alpha_reservedWord'
1943 && in_array(strtoupper($arr[$i - 1]['data']), array("INSERT", "LOAD"))
1945 $subresult['queryflags']['is_insert'] = 1;
1946 $subresult['queryflags']['is_affected'] = 1;
1949 // for the presence of SUM|AVG|STD|STDDEV|MIN|MAX|BIT_OR|BIT_AND
1950 if ($arr[$i]['type'] == 'alpha_functionName'
1951 && in_array(strtoupper($arr[$i]['data']), $arrayFunctions)
1952 && isset($subresult['queryflags']['select_from'])
1953 && $subresult['queryflags']['select_from'] == 1
1954 && !isset($subresult['queryflags']['is_group'])
1956 $subresult['queryflags']['is_func'] = 1;
1959 if ($in_select_expr && $upper_data != 'SELECT'
1960 && $upper_data != 'DISTINCT'
1962 $select_expr_clause .= $arr[$i]['data'] . $sep;
1964 if ($in_from && $upper_data != 'FROM') {
1965 $from_clause .= $arr[$i]['data'] . $sep;
1967 if ($in_group_by && $upper_data != 'GROUP' && $upper_data != 'BY') {
1968 $group_by_clause .= $arr[$i]['data'] . $sep;
1970 if ($in_order_by && $upper_data != 'ORDER' && $upper_data != 'BY') {
1971 // add a space only before ASC or DESC
1972 // not around the dot between dbname and tablename
1973 if ($arr[$i]['type'] == 'alpha_reservedWord') {
1974 $order_by_clause .= $sep;
1976 $order_by_clause .= $arr[$i]['data'];
1978 if ($in_having && $upper_data != 'HAVING') {
1979 $having_clause .= $arr[$i]['data'] . $sep;
1981 if ($in_where && $upper_data != 'WHERE') {
1982 $where_clause .= $arr[$i]['data'] . $sep;
1984 if (($arr[$i]['type'] == 'quote_backtick')
1985 || ($arr[$i]['type'] == 'alpha_identifier')
1987 $where_clause_identifiers[] = $arr[$i]['data'];
1991 // to grab the rest of the query after the ORDER BY clause
1992 if (isset($subresult['queryflags']['select_from'])
1993 && $subresult['queryflags']['select_from'] == 1
1994 && ! $in_order_by
1995 && $seen_order_by
1996 && $upper_data != 'BY'
1998 $unsorted_query .= $arr[$i]['data'];
1999 if ($arr[$i]['type'] != 'punct_bracket_open_round'
2000 && $arr[$i]['type'] != 'punct_bracket_close_round'
2001 && $arr[$i]['type'] != 'punct'
2003 $unsorted_query .= $sep;
2007 if ($in_limit) {
2008 if ($upper_data == 'OFFSET') {
2009 $limit_clause .= $sep;
2011 $limit_clause .= $arr[$i]['data'];
2012 if ($upper_data == 'LIMIT' || $upper_data == 'OFFSET') {
2013 $limit_clause .= $sep;
2016 if ($after_limit && $seen_limit) {
2017 $section_after_limit .= $arr[$i]['data'] . $sep;
2020 // clear $upper_data for next iteration
2021 $upper_data='';
2022 } // end for $i (loop #2)
2023 if (empty($section_before_limit)) {
2024 $section_before_limit = $arr['raw'];
2027 // -----------------------------------------------------
2028 // loop #3: foreign keys and MySQL 4.1.2+ TIMESTAMP options
2029 // (for now, check only the first query)
2030 // (for now, identifiers are assumed to be backquoted)
2032 // If we find that we are dealing with a CREATE TABLE query,
2033 // we look for the next punct_bracket_open_round, which
2034 // introduces the fields list. Then, when we find a
2035 // quote_backtick, it must be a field, so we put it into
2036 // the create_table_fields array. Even if this field is
2037 // not a timestamp, it will be useful when logic has been
2038 // added for complete field attributes analysis.
2040 $seen_foreign = false;
2041 $seen_references = false;
2042 $seen_constraint = false;
2043 $foreign_key_number = -1;
2044 $seen_create_table = false;
2045 $seen_create = false;
2046 $seen_alter = false;
2047 $in_create_table_fields = false;
2048 $brackets_level = 0;
2049 $in_timestamp_options = false;
2050 $seen_default = false;
2052 for ($i = 0; $i < $size; $i++) {
2053 if ($arr[$i]['type'] == 'alpha_reservedWord') {
2054 $upper_data = strtoupper($arr[$i]['data']);
2056 if ($upper_data == 'NOT' && $in_timestamp_options) {
2057 if (! isset($create_table_fields)) {
2058 $create_table_fields = array();
2060 $create_table_fields[$current_identifier]['timestamp_not_null']
2061 = true;
2065 if ($upper_data == 'CREATE') {
2066 $seen_create = true;
2069 if ($upper_data == 'ALTER') {
2070 $seen_alter = true;
2073 if ($upper_data == 'TABLE' && $seen_create) {
2074 $seen_create_table = true;
2075 $create_table_fields = array();
2078 if ($upper_data == 'CURRENT_TIMESTAMP') {
2079 if ($in_timestamp_options) {
2080 if ($seen_default) {
2081 $create_table_fields[$current_identifier]['default_current_timestamp'] = true;
2086 if ($upper_data == 'CONSTRAINT') {
2087 $foreign_key_number++;
2088 $seen_foreign = false;
2089 $seen_references = false;
2090 $seen_constraint = true;
2092 if ($upper_data == 'FOREIGN') {
2093 $seen_foreign = true;
2094 $seen_references = false;
2095 $seen_constraint = false;
2097 if ($upper_data == 'REFERENCES') {
2098 $seen_foreign = false;
2099 $seen_references = true;
2100 $seen_constraint = false;
2104 // Cases covered:
2106 // [ON DELETE {CASCADE | SET NULL | NO ACTION | RESTRICT}]
2107 // [ON UPDATE {CASCADE | SET NULL | NO ACTION | RESTRICT}]
2109 // but we set ['on_delete'] or ['on_cascade'] to
2110 // CASCADE | SET_NULL | NO_ACTION | RESTRICT
2112 // ON UPDATE CURRENT_TIMESTAMP
2114 if ($upper_data == 'ON') {
2115 if (isset($arr[$i+1])
2116 && $arr[$i+1]['type'] == 'alpha_reservedWord'
2118 $second_upper_data = strtoupper($arr[$i+1]['data']);
2119 if ($second_upper_data == 'DELETE') {
2120 $clause = 'on_delete';
2122 if ($second_upper_data == 'UPDATE') {
2123 $clause = 'on_update';
2125 // ugly workaround because currently, NO is not
2126 // in the list of reserved words in sqlparser.data
2127 // (we got a bug report about not being able to use
2128 // 'no' as an identifier)
2129 if (isset($clause)
2130 && ($arr[$i+2]['type'] == 'alpha_reservedWord'
2131 || ($arr[$i+2]['type'] == 'alpha_identifier'
2132 && strtoupper($arr[$i+2]['data'])=='NO'))
2134 $third_upper_data = strtoupper($arr[$i+2]['data']);
2135 if ($third_upper_data == 'CASCADE'
2136 || $third_upper_data == 'RESTRICT'
2138 $value = $third_upper_data;
2139 } elseif ($third_upper_data == 'SET'
2140 || $third_upper_data == 'NO'
2142 if ($arr[$i+3]['type'] == 'alpha_reservedWord') {
2143 $value = $third_upper_data . '_'
2144 . strtoupper($arr[$i+3]['data']);
2146 } elseif ($third_upper_data == 'CURRENT_TIMESTAMP') {
2147 if ($clause == 'on_update'
2148 && $in_timestamp_options
2150 $create_table_fields[$current_identifier]['on_update_current_timestamp'] = true;
2151 $seen_default = false;
2154 } else {
2155 $value = '';
2157 if (!empty($value)) {
2158 if (! isset($foreign)) {
2159 $foreign = array();
2161 $foreign[$foreign_key_number][$clause] = $value;
2163 unset($clause);
2164 } // endif (isset($clause))
2168 } // end of reserved words analysis
2171 if ($arr[$i]['type'] == 'punct_bracket_open_round') {
2172 $brackets_level++;
2173 if ($seen_create_table && $brackets_level == 1) {
2174 $in_create_table_fields = true;
2179 if ($arr[$i]['type'] == 'punct_bracket_close_round') {
2180 $brackets_level--;
2181 if ($seen_references) {
2182 $seen_references = false;
2184 if ($seen_create_table && $brackets_level == 0) {
2185 $in_create_table_fields = false;
2189 if (($arr[$i]['type'] == 'alpha_columnAttrib')) {
2190 $upper_data = strtoupper($arr[$i]['data']);
2191 if ($seen_create_table && $in_create_table_fields) {
2192 if ($upper_data == 'DEFAULT') {
2193 $seen_default = true;
2194 $create_table_fields[$current_identifier]['default_value']
2195 = $arr[$i + 1]['data'];
2201 * @see @todo 2005-10-16 note: the "or" part here is a workaround for a bug
2203 if (($arr[$i]['type'] == 'alpha_columnType')
2204 || ($arr[$i]['type'] == 'alpha_functionName' && $seen_create_table)
2206 $upper_data = strtoupper($arr[$i]['data']);
2207 if ($seen_create_table && $in_create_table_fields
2208 && isset($current_identifier)
2210 $create_table_fields[$current_identifier]['type'] = $upper_data;
2211 if ($upper_data == 'TIMESTAMP') {
2212 $arr[$i]['type'] = 'alpha_columnType';
2213 $in_timestamp_options = true;
2214 } else {
2215 $in_timestamp_options = false;
2216 if ($upper_data == 'CHAR') {
2217 $arr[$i]['type'] = 'alpha_columnType';
2224 if ($arr[$i]['type'] == 'quote_backtick'
2225 || $arr[$i]['type'] == 'alpha_identifier'
2228 if ($arr[$i]['type'] == 'quote_backtick') {
2229 // remove backquotes
2230 $identifier = PMA_Util::unQuote($arr[$i]['data']);
2231 } else {
2232 $identifier = $arr[$i]['data'];
2235 if ($seen_create_table && $in_create_table_fields) {
2236 $current_identifier = $identifier;
2237 // we set this one even for non TIMESTAMP type
2238 $create_table_fields[$current_identifier]['timestamp_not_null']
2239 = false;
2242 if ($seen_constraint) {
2243 $foreign[$foreign_key_number]['constraint'] = $identifier;
2246 if ($seen_foreign && $brackets_level > 0) {
2247 $foreign[$foreign_key_number]['index_list'][] = $identifier;
2250 if ($seen_references) {
2251 if ($seen_alter && $brackets_level > 0) {
2252 $foreign[$foreign_key_number]['ref_index_list'][] = $identifier;
2253 // here, the first bracket level corresponds to the
2254 // bracket of CREATE TABLE
2255 // so if we are on level 2, it must be the index list
2256 // of the foreign key REFERENCES
2257 } elseif ($brackets_level > 1) {
2258 $foreign[$foreign_key_number]['ref_index_list'][] = $identifier;
2259 } elseif ($arr[$i+1]['type'] == 'punct_qualifier') {
2260 // identifier is `db`.`table`
2261 // the first pass will pick the db name
2262 // the next pass will pick the table name
2263 $foreign[$foreign_key_number]['ref_db_name'] = $identifier;
2264 } else {
2265 // identifier is `table`
2266 $foreign[$foreign_key_number]['ref_table_name'] = $identifier;
2270 } // end for $i (loop #3)
2273 // Fill the $subresult array
2275 if (isset($create_table_fields)) {
2276 $subresult['create_table_fields'] = $create_table_fields;
2279 if (isset($foreign)) {
2280 $subresult['foreign_keys'] = $foreign;
2283 if (isset($select_expr_clause)) {
2284 $subresult['select_expr_clause'] = $select_expr_clause;
2286 if (isset($from_clause)) {
2287 $subresult['from_clause'] = $from_clause;
2289 if (isset($group_by_clause)) {
2290 $subresult['group_by_clause'] = $group_by_clause;
2292 if (isset($order_by_clause)) {
2293 $subresult['order_by_clause'] = $order_by_clause;
2295 if (isset($having_clause)) {
2296 $subresult['having_clause'] = $having_clause;
2298 if (isset($limit_clause)) {
2299 $subresult['limit_clause'] = $limit_clause;
2301 if (isset($where_clause)) {
2302 $subresult['where_clause'] = $where_clause;
2304 if (isset($unsorted_query) && !empty($unsorted_query)) {
2305 $subresult['unsorted_query'] = $unsorted_query;
2307 if (isset($where_clause_identifiers)) {
2308 $subresult['where_clause_identifiers'] = $where_clause_identifiers;
2311 if (isset($position_of_first_select)) {
2312 $subresult['position_of_first_select'] = $position_of_first_select;
2313 $subresult['section_before_limit'] = $section_before_limit;
2314 $subresult['section_after_limit'] = $section_after_limit;
2317 // They are naughty and didn't have a trailing semi-colon,
2318 // then still handle it properly
2319 if ($subresult['querytype'] != '') {
2320 $result[] = $subresult;
2322 return $result;
2323 } // end of the "PMA_SQP_analyze()" function
2327 * Formats SQL queries
2329 * @param array $arr The SQL queries
2330 * @param string $mode formatting mode
2331 * @param integer $start_token starting token
2332 * @param integer $number_of_tokens number of tokens to format, -1 = all
2334 * @return string The formatted SQL queries
2336 * @access public
2338 function PMA_SQP_format(
2339 $arr, $mode='text', $start_token=0,
2340 $number_of_tokens=-1
2342 //DEBUG echo 'in Format<pre>'; print_r($arr); echo '</pre>';
2343 // then check for an array
2344 if (! is_array($arr)) {
2345 return htmlspecialchars($arr);
2347 // first check for the SQL parser having hit an error
2348 if (PMA_SQP_isError()) {
2349 return htmlspecialchars($arr['raw']);
2351 // else do it properly
2352 switch ($mode) {
2353 case 'query_only':
2354 $str = '';
2355 $html_line_break = "\n";
2356 break;
2357 case 'text':
2358 $str = '';
2359 $html_line_break = '<br />';
2360 break;
2361 } // end switch
2362 $indent = 0;
2363 $bracketlevel = 0;
2364 $functionlevel = 0;
2365 $infunction = false;
2366 $space_punct_listsep = ' ';
2367 $space_punct_listsep_function_name = ' ';
2368 // $space_alpha_reserved_word = '<br />'."\n";
2369 $space_alpha_reserved_word = ' ';
2371 $keywords_with_brackets_1before = array(
2372 'INDEX' => 1,
2373 'KEY' => 1,
2374 'ON' => 1,
2375 'USING' => 1
2378 $keywords_with_brackets_2before = array(
2379 'IGNORE' => 1,
2380 'INDEX' => 1,
2381 'INTO' => 1,
2382 'KEY' => 1,
2383 'PRIMARY' => 1,
2384 'PROCEDURE' => 1,
2385 'REFERENCES' => 1,
2386 'UNIQUE' => 1,
2387 'USE' => 1
2390 // These reserved words do NOT get a newline placed near them.
2391 $keywords_no_newline = array(
2392 'AS' => 1,
2393 'ASC' => 1,
2394 'DESC' => 1,
2395 'DISTINCT' => 1,
2396 'DUPLICATE' => 1,
2397 'HOUR' => 1,
2398 'INTERVAL' => 1,
2399 'IS' => 1,
2400 'LIKE' => 1,
2401 'NOT' => 1,
2402 'NULL' => 1,
2403 'ON' => 1,
2404 'REGEXP' => 1
2407 // These reserved words introduce a privilege list
2408 $keywords_priv_list = array(
2409 'GRANT' => 1,
2410 'REVOKE' => 1
2413 if ($number_of_tokens == -1) {
2414 $number_of_tokens = $arr['len'];
2416 $typearr = array();
2417 if ($number_of_tokens >= 0) {
2418 $typearr[0] = '';
2419 $typearr[1] = '';
2420 $typearr[2] = '';
2421 $typearr[3] = $arr[$start_token]['type'];
2424 $in_priv_list = false;
2425 for ($i = $start_token; $i < $number_of_tokens; $i++) {
2426 // DEBUG echo "Loop format <strong>" . $arr[$i]['data']
2427 // . "</strong> " . $arr[$i]['type'] . "<br />";
2428 $before = '';
2429 $after = '';
2430 // array_shift($typearr);
2432 0 prev2
2433 1 prev
2434 2 current
2435 3 next
2437 if (($i + 1) < $number_of_tokens) {
2438 $typearr[4] = $arr[$i + 1]['type'];
2439 } else {
2440 $typearr[4] = '';
2443 for ($j=0; $j<4; $j++) {
2444 $typearr[$j] = $typearr[$j + 1];
2447 switch ($typearr[2]) {
2448 case 'alpha_bitfield_constant_introducer':
2449 $before = ' ';
2450 $after = '';
2451 break;
2452 case 'white_newline':
2453 $before = '';
2454 break;
2455 case 'punct_bracket_open_round':
2456 $bracketlevel++;
2457 $infunction = false;
2458 $keyword_brackets_2before = isset(
2459 $keywords_with_brackets_2before[strtoupper($arr[$i - 2]['data'])]
2461 $keyword_brackets_1before = isset(
2462 $keywords_with_brackets_1before[strtoupper($arr[$i - 1]['data'])]
2464 // Make sure this array is sorted!
2465 if (($typearr[1] == 'alpha_functionName')
2466 || ($typearr[1] == 'alpha_columnType') || ($typearr[1] == 'punct')
2467 || ($typearr[3] == 'digit_integer') || ($typearr[3] == 'digit_hex')
2468 || ($typearr[3] == 'digit_float')
2469 || ($typearr[0] == 'alpha_reservedWord' && $keyword_brackets_2before)
2470 || ($typearr[1] == 'alpha_reservedWord' && $keyword_brackets_1before)
2472 $functionlevel++;
2473 $infunction = true;
2474 $after .= ' ';
2475 } else {
2476 $indent++;
2477 if ($mode != 'query_only') {
2478 $after .= '<div class="syntax_indent' . $indent . '">';
2479 } else {
2480 $after .= ' ';
2483 break;
2484 case 'alpha_identifier':
2485 if (($typearr[1] == 'punct_qualifier')
2486 || ($typearr[3] == 'punct_qualifier')
2488 $after = '';
2489 $before = '';
2491 // for example SELECT 1 somealias
2492 if ($typearr[1] == 'digit_integer') {
2493 $before = ' ';
2495 if (($typearr[3] == 'alpha_columnType')
2496 || ($typearr[3] == 'alpha_identifier')
2498 $after .= ' ';
2500 break;
2501 case 'punct_user':
2502 case 'punct_qualifier':
2503 $before = '';
2504 $after = '';
2505 break;
2506 case 'punct_listsep':
2507 if ($infunction == true) {
2508 $after .= $space_punct_listsep_function_name;
2509 } else {
2510 $after .= $space_punct_listsep;
2512 break;
2513 case 'punct_queryend':
2514 if (($typearr[3] != 'comment_mysql')
2515 && ($typearr[3] != 'comment_ansi')
2516 && $typearr[3] != 'comment_c'
2518 $after .= $html_line_break;
2519 $after .= $html_line_break;
2521 $space_punct_listsep = ' ';
2522 $space_punct_listsep_function_name = ' ';
2523 $space_alpha_reserved_word = ' ';
2524 $in_priv_list = false;
2525 break;
2526 case 'comment_mysql':
2527 case 'comment_ansi':
2528 $after .= $html_line_break;
2529 break;
2530 case 'punct':
2531 $before .= ' ';
2533 // workaround for
2534 // select * from mytable limit 0,-1
2535 // (a side effect of this workaround is that
2536 // select 20 - 9
2537 // becomes
2538 // select 20 -9
2539 // )
2540 if ($typearr[3] != 'digit_integer') {
2541 $after .= ' ';
2543 break;
2544 case 'punct_bracket_close_round':
2545 // only close bracket level when it was opened before
2546 if ($bracketlevel > 0) {
2547 $bracketlevel--;
2548 if ($infunction == true) {
2549 $functionlevel--;
2550 $after .= ' ';
2551 $before .= ' ';
2552 } else {
2553 $indent--;
2554 $before .= ($mode != 'query_only' ? '</div>' : ' ');
2556 $infunction = ($functionlevel > 0) ? true : false;
2558 break;
2559 case 'alpha_columnType':
2560 if ($typearr[3] == 'alpha_columnAttrib') {
2561 $after .= ' ';
2563 if ($typearr[1] == 'alpha_columnType') {
2564 $before .= ' ';
2566 break;
2567 case 'alpha_columnAttrib':
2569 // ALTER TABLE tbl_name AUTO_INCREMENT = 1
2570 // COLLATE LATIN1_GENERAL_CI DEFAULT
2571 if ($typearr[1] == 'alpha_identifier'
2572 || $typearr[1] == 'alpha_charset'
2574 $before .= ' ';
2576 if (($typearr[3] == 'alpha_columnAttrib')
2577 || ($typearr[3] == 'quote_single')
2578 || ($typearr[3] == 'digit_integer')
2580 $after .= ' ';
2582 // workaround for
2583 // AUTO_INCREMENT = 31DEFAULT_CHARSET = utf-8
2585 if ($typearr[2] == 'alpha_columnAttrib'
2586 && $typearr[3] == 'alpha_reservedWord'
2588 $before .= ' ';
2590 // workaround for
2591 // select * from mysql.user where binary user="root"
2592 // binary is marked as alpha_columnAttrib
2593 // but should be marked as a reserved word
2594 if (strtoupper($arr[$i]['data']) == 'BINARY'
2595 && $typearr[3] == 'alpha_identifier'
2597 $after .= ' ';
2599 break;
2600 case 'alpha_functionName':
2601 break;
2602 case 'alpha_reservedWord':
2603 // do not uppercase the reserved word if we are calling
2604 // this function in query_only mode, because we need
2605 // the original query (otherwise we get problems with
2606 // semi-reserved words like "storage" which is legal
2607 // as an identifier name)
2609 if ($mode != 'query_only') {
2610 $arr[$i]['data'] = strtoupper($arr[$i]['data']);
2613 if ((($typearr[1] != 'alpha_reservedWord')
2614 || (($typearr[1] == 'alpha_reservedWord')
2615 && isset($keywords_no_newline[strtoupper($arr[$i - 1]['data'])])))
2616 && ($typearr[1] != 'punct_level_plus')
2617 && (!isset($keywords_no_newline[$arr[$i]['data']]))
2619 // do not put a space before the first token, because
2620 // we use a lot of pattern matching checking for the
2621 // first reserved word at beginning of query
2622 // so do not put a newline before
2624 // also we must not be inside a privilege list
2625 if ($i > 0) {
2626 // the alpha_identifier exception is there to
2627 // catch cases like
2628 // GRANT SELECT ON mydb.mytable TO myuser@localhost
2629 // (else, we get mydb.mytableTO)
2631 // the quote_single exception is there to
2632 // catch cases like
2633 // GRANT ... TO 'marc'@'domain.com' IDENTIFIED...
2635 * @todo fix all cases and find why this happens
2638 if (!$in_priv_list
2639 || $typearr[1] == 'alpha_identifier'
2640 || $typearr[1] == 'quote_single'
2641 || $typearr[1] == 'white_newline'
2643 $before .= $space_alpha_reserved_word;
2645 } else {
2646 // on first keyword, check if it introduces a
2647 // privilege list
2648 if (isset($keywords_priv_list[$arr[$i]['data']])) {
2649 $in_priv_list = true;
2652 } else {
2653 $before .= ' ';
2656 switch ($arr[$i]['data']) {
2657 case 'CREATE':
2658 case 'ALTER':
2659 case 'DROP':
2660 case 'RENAME';
2661 case 'TRUNCATE':
2662 case 'ANALYZE':
2663 case 'ANALYSE':
2664 case 'OPTIMIZE':
2665 if (!$in_priv_list) {
2666 $space_punct_listsep = $html_line_break;
2667 $space_alpha_reserved_word = ' ';
2669 break;
2670 case 'EVENT':
2671 case 'TABLESPACE':
2672 case 'TABLE':
2673 case 'FUNCTION':
2674 case 'INDEX':
2675 case 'PROCEDURE':
2676 case 'SERVER':
2677 case 'TRIGGER':
2678 case 'DATABASE':
2679 case 'VIEW':
2680 case 'GROUP':
2681 break;
2682 case 'SET':
2683 if (!$in_priv_list) {
2684 $space_punct_listsep = $html_line_break;
2685 $space_alpha_reserved_word = ' ';
2687 break;
2688 case 'EXPLAIN':
2689 case 'DESCRIBE':
2690 case 'DELETE':
2691 case 'SHOW':
2692 case 'UPDATE':
2693 if (!$in_priv_list) {
2694 $space_punct_listsep = $html_line_break;
2695 $space_alpha_reserved_word = ' ';
2697 break;
2698 case 'INSERT':
2699 case 'REPLACE':
2700 if (!$in_priv_list) {
2701 $space_punct_listsep = $html_line_break;
2702 $space_alpha_reserved_word = $html_line_break;
2704 break;
2705 case 'VALUES':
2706 $space_punct_listsep = ' ';
2707 $space_alpha_reserved_word = $html_line_break;
2708 break;
2709 case 'SELECT':
2710 $space_punct_listsep = ' ';
2711 $space_alpha_reserved_word = $html_line_break;
2712 break;
2713 case 'CALL':
2714 case 'DO':
2715 case 'HANDLER':
2716 break;
2717 default:
2718 break;
2719 } // end switch ($arr[$i]['data'])
2721 $after .= ' ';
2722 break;
2723 case 'digit_integer':
2724 case 'digit_float':
2725 case 'digit_hex':
2727 * @todo could there be other types preceding a digit?
2729 if ($typearr[1] == 'alpha_reservedWord') {
2730 $after .= ' ';
2732 if ($infunction && $typearr[3] == 'punct_bracket_close_round') {
2733 $after .= ' ';
2735 if ($typearr[1] == 'alpha_columnAttrib') {
2736 $before .= ' ';
2738 break;
2739 case 'alpha_variable':
2740 $after = ' ';
2741 break;
2742 case 'quote_double':
2743 case 'quote_single':
2744 // workaround: for the query
2745 // REVOKE SELECT ON `base2\_db`.* FROM 'user'@'%'
2746 // the @ is incorrectly marked as alpha_variable
2747 // in the parser, and here, the '%' gets a blank before,
2748 // which is a syntax error
2749 if ($typearr[1] != 'punct_user'
2750 && $typearr[1] != 'alpha_bitfield_constant_introducer'
2752 $before .= ' ';
2754 if ($infunction && $typearr[3] == 'punct_bracket_close_round') {
2755 $after .= ' ';
2757 break;
2758 case 'quote_backtick':
2759 // here we check for punct_user to handle correctly
2760 // DEFINER = `username`@`%`
2761 // where @ is the punct_user and `%` is the quote_backtick
2762 if ($typearr[3] != 'punct_qualifier'
2763 && $typearr[3] != 'alpha_variable'
2764 && $typearr[3] != 'punct_user'
2766 $after .= ' ';
2768 if ($typearr[1] != 'punct_qualifier'
2769 && $typearr[1] != 'alpha_variable'
2770 && $typearr[1] != 'punct_user'
2772 $before .= ' ';
2774 break;
2775 default:
2776 break;
2777 } // end switch ($typearr[2])
2780 if ($typearr[3] != 'punct_qualifier') {
2781 $after .= ' ';
2783 $after .= "\n";
2785 $str .= $before;
2786 if ($mode == 'text') {
2787 $str .= htmlspecialchars($arr[$i]['data']);
2788 } else {
2789 $str .= $arr[$i]['data'];
2791 $str .= $after;
2792 } // end for
2793 // close unclosed indent levels
2794 while ($indent > 0) {
2795 $indent--;
2796 $str .= ($mode != 'query_only' ? '</div>' : ' ');
2799 return $str;
2800 } // end of the "PMA_SQP_format()" function
2803 * Gets SQL queries with no format
2805 * @param array $arr The SQL queries list
2807 * @return string The SQL queries with no format
2809 * @access public
2811 function PMA_SQP_formatNone($arr)
2813 $formatted_sql = htmlspecialchars($arr['raw']);
2814 $formatted_sql = preg_replace(
2815 "@((\015\012)|(\015)|(\012)){3,}@",
2816 "\n\n",
2817 $formatted_sql
2820 return $formatted_sql;
2821 } // end of the "PMA_SQP_formatNone()" function
2824 * Checks whether a given name is MySQL reserved word
2826 * @param string $column The word to be checked
2828 * @return boolean whether true or false
2830 function PMA_SQP_isKeyWord($column)
2832 global $PMA_SQPdata_forbidden_word;
2833 return in_array(strtoupper($column), $PMA_SQPdata_forbidden_word);
2838 * Get Parser Data Map from sqlparser.data.php
2840 * @return Array Parser Data Map from sqlparser.data.php
2842 function PMA_SQP_getParserDataMap()
2844 include 'libraries/sqlparser.data.php';
2845 return array(
2846 'PMA_SQPdata_function_name' => $PMA_SQPdata_function_name,
2847 'PMA_SQPdata_column_attrib' => $PMA_SQPdata_column_attrib,
2848 'PMA_SQPdata_reserved_word' => $PMA_SQPdata_reserved_word,
2849 'PMA_SQPdata_forbidden_word' => $PMA_SQPdata_forbidden_word,
2850 'PMA_SQPdata_column_type' => $PMA_SQPdata_column_type,
2854 * Get Parser analyze Map from parse_analyze_inc.php
2856 * @param array $sql_query The SQL string
2857 * @param array $db Current DB
2859 * @return Array analyze Map from parse_analyze_inc.php
2861 function PMA_SQP_getParserAnalyzeMap($sql_query, $db)
2863 include 'libraries/parse_analyze.inc.php';
2864 return $analyzed_sql_results;