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
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.)
29 if (! defined('PHPMYADMIN')) {
34 * Include the string handling class as we use it heavily
36 require_once './libraries/string.inc.php';
39 * Include data for the SQL Parser
41 require_once './libraries/sqlparser.data.php';
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();
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
69 function PMA_SQP_arrayAdd(&$arr, $type, $data, &$arrsize, $pos = 0)
71 $arr[] = array('type' => $type, 'data' => $data, 'pos' => $pos);
73 } // end of the "PMA_SQP_arrayAdd()" function
76 * Reset the error variable for the SQL parser
82 function PMA_SQP_resetError()
84 global $SQP_errorString;
85 $SQP_errorString = '';
86 unset($SQP_errorString);
90 * Get the contents of the error variable for the SQL parser
92 * @return string Error string from SQL parser
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
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
124 * @scope SQL Parser internal
126 function PMA_SQP_throwError($message, $sql)
128 global $SQP_errorString;
129 $SQP_errorString = '<p>'
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.'
137 . 'ERROR: ' . $message . "\n"
138 . 'SQL: ' . htmlspecialchars($sql) . "\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
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)/",
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"
193 . '----' . __('END CUT') . '----' . '<br />' . "\n";
195 $SQP_errorString .= '----' . __('BEGIN RAW') . '----<br />' . "\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
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);
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;
260 $punct_queryend = ';';
261 $punct_qualifier = '.';
262 $punct_listsep = ',';
263 $bracket_list = '()[]{}';
264 $allpunct_list = '-,;:!?/.^~\*&%+<=>|';
265 $allpunct_list_pair = array(
278 $quote_list = '\'"`';
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);
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;
308 $this_was_space = true;
310 PMA_SQP_arrayAdd($sql_array, 'white_newline', '', $arraysize);
314 // Checks for white space
315 if ($GLOBALS['PMA_String']->isSpace($c)) {
316 $this_was_space = true;
321 // Checks for comment lines.
325 $next_c = $GLOBALS['PMA_String']->substr($sql, $count2 +
1, 1);
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) <= ' ')))
339 $pos = $GLOBALS['PMA_String']->strpos($sql, "\n", $count2);
343 $pos = $GLOBALS['PMA_String']->strpos($sql, '*/', $count2);
349 $count2 = ($pos < $count2) ?
$len : $pos;
350 $str = $GLOBALS['PMA_String']->substr($sql, $count1, $count2 - $count1);
351 PMA_SQP_arrayAdd($sql_array, 'comment_' . $type, $str, $arraysize);
355 // Checks for something inside quotation marks
356 if ($GLOBALS['PMA_String']->strpos($quote_list, $c) !== false) {
357 $startquotepos = $count2;
364 $pos = $GLOBALS['PMA_String']->strpos(' ' . $sql, $quotetype, $oldpos +
1) - 1;
369 * Behave same as MySQL and accept end of query as end
371 * I know this is sick, but MySQL behaves like this:
373 * SELECT * FROM `table
377 * SELECT * FROM `table`
379 $pos_quote_separator = $GLOBALS['PMA_String']->strpos(
380 ' ' . $sql, $GLOBALS['sql_delimiter'], $oldpos +
1
382 if ($pos_quote_separator < 0) {
385 $sql_array['raw'] .= '`';
389 $sql = $GLOBALS['PMA_String']->substr($sql, 0, $pos_quote_separator)
390 . '`' . $GLOBALS['PMA_String']->substr($sql, $pos_quote_separator);
391 $sql_array['raw'] = $sql;
392 $pos = $pos_quote_separator;
394 if (class_exists('PMA_Message')
395 && $GLOBALS['is_ajax_request'] != true
398 __('Automatically appended backtick to the end of query!')
402 $debugstr = __('Unclosed quote')
403 . ' @ ' . $startquotepos. "\n"
404 . 'STR: ' . htmlspecialchars($quotetype);
405 PMA_SQP_throwError($debugstr, $sql);
410 // If the quote is the first character, it can't be
411 // escaped, so don't do the rest of the code
416 // Checks for MySQL escaping using a \
417 // And checks for ANSI escaping using the $quotetype character
419 && $GLOBALS['PMA_String']->charIsEscaped($sql, $pos)
424 } elseif (($pos +
1 < $len)
425 && ($GLOBALS['PMA_String']->substr($sql, $pos, 1) == $quotetype)
426 && ($GLOBALS['PMA_String']->substr($sql, $pos +
1, 1) == $quotetype)
433 } while ($len > $pos); // end do
438 switch ($quotetype) {
441 $this_was_quote = true;
445 $this_was_quote = true;
449 $this_was_quote = true;
454 $data = $GLOBALS['PMA_String']->substr($sql, $count1, $count2 - $count1);
455 PMA_SQP_arrayAdd($sql_array, $type, $data, $arraysize);
459 // Checks for brackets
460 if ($GLOBALS['PMA_String']->strpos($bracket_list, $c) !== false) {
461 // All bracket tokens are only one item long
462 $this_was_bracket = true;
465 if ($GLOBALS['PMA_String']->strpos('([{', $c) !== false) {
468 $type_type = 'close';
472 if ($GLOBALS['PMA_String']->strpos('()', $c) !== false) {
473 $type_style = 'round';
474 } elseif ($GLOBALS['PMA_String']->strpos('[]', $c) !== false) {
475 $type_style = 'square';
477 $type_style = 'curly';
480 $type = 'punct_bracket_' . $type_type . '_' . $type_style;
481 PMA_SQP_arrayAdd($sql_array, $type, $c, $arraysize);
487 var_dump($GLOBALS['PMA_String']->isSqlIdentifier($c, false));
490 var_dump($GLOBALS['PMA_String']->isDigit($GLOBALS['PMA_String']->substr($sql, $count2 + 1, 1)));
491 var_dump($previous_was_space);
492 var_dump($previous_was_bracket);
493 var_dump($previous_was_listsep);
497 // Checks for identifier (alpha or numeric)
498 if ($GLOBALS['PMA_String']->isSqlIdentifier($c, false)
501 && $GLOBALS['PMA_String']->isDigit($GLOBALS['PMA_String']->substr($sql, $count2 +
1, 1))
502 && ($previous_was_space ||
$previous_was_bracket ||
$previous_was_listsep))
505 echo $GLOBALS['PMA_String']->substr($sql, $count2);
512 * @todo a @ can also be present in expressions like
513 * FROM 'user'@'%' or TO 'user'@'%'
514 * in this case, the @ is wrongly marked as alpha_variable
516 $is_identifier = $previous_was_punct;
517 $is_sql_variable = $c == '@' && ! $previous_was_quote;
518 $is_user = $c == '@' && $previous_was_quote;
522 && $GLOBALS['PMA_String']->isDigit($c)
528 && $GLOBALS['PMA_String']->substr($sql, $count2, 1) == 'x'
530 $is_float_digit = $c == '.';
531 $is_float_digit_exponent = false;
535 var_dump($is_identifier);
536 var_dump($is_sql_variable);
538 var_dump($is_float_digit);
542 // Fast skip is especially needed for huge BLOB data
545 $pos = strspn($sql, '0123456789abcdefABCDEF', $count2);
546 if ($pos > $count2) {
550 } elseif ($is_digit) {
551 $pos = strspn($sql, '0123456789', $count2);
552 if ($pos > $count2) {
558 while (($count2 < $len) && $GLOBALS['PMA_String']->isSqlIdentifier($GLOBALS['PMA_String']->substr($sql, $count2, 1), ($is_sql_variable ||
$is_digit))) {
559 $c2 = $GLOBALS['PMA_String']->substr($sql, $count2, 1);
560 if ($is_sql_variable && ($c2 == '.')) {
564 if ($is_digit && (!$is_hex_digit) && ($c2 == '.')) {
566 if (!$is_float_digit) {
567 $is_float_digit = true;
570 $debugstr = __('Invalid Identifer')
571 . ' @ ' . ($count1+
1) . "\n"
572 . 'STR: ' . htmlspecialchars(
573 $GLOBALS['PMA_String']->substr($sql, $count1, $count2 - $count1)
575 PMA_SQP_throwError($debugstr, $sql);
579 if ($is_digit && (!$is_hex_digit) && (($c2 == 'e') ||
($c2 == 'E'))) {
580 if (!$is_float_digit_exponent) {
581 $is_float_digit_exponent = true;
582 $is_float_digit = true;
587 $is_float_digit = false;
590 if (($is_hex_digit && $GLOBALS['PMA_String']->isHexDigit($c2)) ||
($is_digit && $GLOBALS['PMA_String']->isDigit($c2))) {
595 $is_hex_digit = false;
601 $l = $count2 - $count1;
602 $str = $GLOBALS['PMA_String']->substr($sql, $count1, $l);
605 if ($is_digit ||
$is_float_digit ||
$is_hex_digit) {
607 if ($is_float_digit) {
609 } elseif ($is_hex_digit) {
614 } elseif ($is_user) {
615 $type = 'punct_user';
616 } elseif ($is_sql_variable != false) {
617 $type = 'alpha_variable';
620 } // end if... else....
621 PMA_SQP_arrayAdd($sql_array, $type, $str, $arraysize, $count2);
627 if ($GLOBALS['PMA_String']->strpos($allpunct_list, $c) !== false) {
628 while (($count2 < $len) && $GLOBALS['PMA_String']->strpos($allpunct_list, $GLOBALS['PMA_String']->substr($sql, $count2, 1)) !== false) {
631 $l = $count2 - $count1;
635 $punct_data = $GLOBALS['PMA_String']->substr($sql, $count1, $l);
638 // Special case, sometimes, althought two characters are
639 // adjectent directly, they ACTUALLY need to be seperate
643 var_dump($punct_data);
649 switch ($punct_data) {
650 case $punct_queryend:
651 $t_suffix = '_queryend';
653 case $punct_qualifier:
654 $t_suffix = '_qualifier';
655 $this_was_punct = true;
658 $this_was_listsep = true;
659 $t_suffix = '_listsep';
664 PMA_SQP_arrayAdd($sql_array, 'punct' . $t_suffix, $punct_data, $arraysize);
665 } elseif ($punct_data == $GLOBALS['sql_delimiter'] ||
isset($allpunct_list_pair[$punct_data])) {
666 // Ok, we have one of the valid combined punct expressions
667 PMA_SQP_arrayAdd($sql_array, 'punct', $punct_data, $arraysize);
669 // Bad luck, lets split it up more
670 $first = $punct_data[0];
671 $last2 = $punct_data[$l - 2] . $punct_data[$l - 1];
672 $last = $punct_data[$l - 1];
673 if (($first == ',') ||
($first == ';') ||
($first == '.') ||
($first == '*')) {
674 $count2 = $count1 +
1;
675 $punct_data = $first;
676 } elseif (($last2 == '/*') ||
(($last2 == '--') && ($count2 == $len ||
$GLOBALS['PMA_String']->substr($sql, $count2, 1) <= ' '))) {
678 $punct_data = $GLOBALS['PMA_String']->substr($sql, $count1, $count2 - $count1);
679 } elseif (($last == '-') ||
($last == '+') ||
($last == '!')) {
681 $punct_data = $GLOBALS['PMA_String']->substr($sql, $count1, $count2 - $count1);
682 } elseif ($last != '~') {
684 * @todo for negation operator, split in 2 tokens ?
685 * "select x&~1 from t"
686 * becomes "select x & ~ 1 from t" ?
688 $debugstr = __('Unknown Punctuation String')
689 . ' @ ' . ($count1+
1) . "\n"
690 . 'STR: ' . htmlspecialchars($punct_data);
691 PMA_SQP_throwError($debugstr, $sql);
694 PMA_SQP_arrayAdd($sql_array, 'punct', $punct_data, $arraysize);
696 } // end if... elseif... else
703 $debugstr = 'C1 C2 LEN: ' . $count1 . ' ' . $count2 . ' ' . $len . "\n"
704 . 'STR: ' . $GLOBALS['PMA_String']->substr($sql, $count1, $count2 - $count1) . "\n";
705 PMA_SQP_bug($debugstr, $sql);
708 } // end while ($count2 < $len)
716 if ($arraysize > 0) {
717 $t_next = $sql_array[0]['type'];
721 $d_next = $sql_array[0]['data'];
725 $d_next_upper = $t_next == 'alpha' ?
strtoupper($d_next) : $d_next;
727 $d_bef_prev_upper = '';
731 for ($i = 0; $i < $arraysize; $i++
) {
732 $t_bef_prev = $t_prev;
735 $d_bef_prev = $d_prev;
738 $d_bef_prev_upper = $d_prev_upper;
739 $d_prev_upper = $d_cur_upper;
740 $d_cur_upper = $d_next_upper;
741 if (($i +
1) < $arraysize) {
742 $t_next = $sql_array[$i +
1]['type'];
743 $d_next = $sql_array[$i +
1]['data'];
744 $d_next_upper = $t_next == 'alpha' ?
strtoupper($d_next) : $d_next;
751 //DEBUG echo "[prev: <strong>".$d_prev."</strong> ".$t_prev."][cur: <strong>".$d_cur."</strong> ".$t_cur."][next: <strong>".$d_next."</strong> ".$t_next."]<br />";
753 if ($t_cur == 'alpha') {
754 $t_suffix = '_identifier';
755 // for example: `thebit` bit(8) NOT NULL DEFAULT b'0'
756 if ($t_prev == 'alpha' && $d_prev == 'DEFAULT' && $d_cur == 'b' && $t_next == 'quote_single') {
757 $t_suffix = '_bitfield_constant_introducer';
758 } elseif (($t_next == 'punct_qualifier') ||
($t_prev == 'punct_qualifier')) {
759 $t_suffix = '_identifier';
760 } elseif (($t_next == 'punct_bracket_open_round')
761 && isset($PMA_SQPdata_function_name[$d_cur_upper])
764 * @todo 2005-10-16: in the case of a CREATE TABLE containing
765 * a TIMESTAMP, since TIMESTAMP() is also a function, it's
766 * found here and the token is wrongly marked as alpha_functionName.
767 * But we compensate for this when analysing for timestamp_not_null
768 * later in this script.
770 * Same applies to CHAR vs. CHAR() function.
772 $t_suffix = '_functionName';
773 /* There are functions which might be as well column types */
774 } elseif (isset($PMA_SQPdata_column_type[$d_cur_upper])) {
775 $t_suffix = '_columnType';
778 * Temporary fix for bugs #621357 and #2027720
780 * @todo FIX PROPERLY NEEDS OVERHAUL OF SQL TOKENIZER
782 if (($d_cur_upper == 'SET' ||
$d_cur_upper == 'BINARY') && $t_next != 'punct_bracket_open_round') {
783 $t_suffix = '_reservedWord';
785 //END OF TEMPORARY FIX
787 // CHARACTER is a synonym for CHAR, but can also be meant as
788 // CHARACTER SET. In this case, we have a reserved word.
789 if ($d_cur_upper == 'CHARACTER' && $d_next_upper == 'SET') {
790 $t_suffix = '_reservedWord';
794 // current is a column type, so previous must not be
795 // a reserved word but an identifier
796 // CREATE TABLE SG_Persons (first varchar(64))
798 //if ($sql_array[$i-1]['type'] =='alpha_reservedWord') {
799 // $sql_array[$i-1]['type'] = 'alpha_identifier';
802 } elseif (isset($PMA_SQPdata_reserved_word[$d_cur_upper])) {
803 $t_suffix = '_reservedWord';
804 } elseif (isset($PMA_SQPdata_column_attrib[$d_cur_upper])) {
805 $t_suffix = '_columnAttrib';
806 // INNODB is a MySQL table type, but in "SHOW INNODB STATUS",
807 // it should be regarded as a reserved word.
808 if ($d_cur_upper == 'INNODB'
809 && $d_prev_upper == 'SHOW'
810 && $d_next_upper == 'STATUS'
812 $t_suffix = '_reservedWord';
815 if ($d_cur_upper == 'DEFAULT' && $d_next_upper == 'CHARACTER') {
816 $t_suffix = '_reservedWord';
818 // Binary as character set
819 if ($d_cur_upper == 'BINARY'
820 && (($d_bef_prev_upper == 'CHARACTER' && $d_prev_upper == 'SET')
821 ||
($d_bef_prev_upper == 'SET' && $d_prev_upper == '=')
822 ||
($d_bef_prev_upper == 'CHARSET' && $d_prev_upper == '=')
823 ||
$d_prev_upper == 'CHARSET')
824 && in_array($d_cur, $mysql_charsets)
826 $t_suffix = '_charset';
828 } elseif (in_array($d_cur, $mysql_charsets)
829 ||
in_array($d_cur, $mysql_collations_flat)
830 ||
($d_cur{0} == '_' && in_array(substr($d_cur, 1), $mysql_charsets))
832 $t_suffix = '_charset';
836 // check if present in the list of forbidden words
837 if ($t_suffix == '_reservedWord'
838 && isset($PMA_SQPdata_forbidden_word[$d_cur_upper])
840 $sql_array[$i]['forbidden'] = true;
842 $sql_array[$i]['forbidden'] = false;
844 $sql_array[$i]['type'] .= $t_suffix;
848 // Stores the size of the array inside the array, as count() is a slow
850 $sql_array['len'] = $arraysize;
852 // DEBUG echo 'After parsing<pre>'; print_r($sql_array); echo '</pre>';
853 // Sends the data back
855 } // end of the "PMA_SQP_parse()" function
858 * Checks for token types being what we want...
860 * @param string $toCheck String of type that we have
861 * @param string $whatWeWant String of type that we want
863 * @return boolean result of check
867 function PMA_SQP_typeCheck($toCheck, $whatWeWant)
869 $typeSeparator = '_';
870 if (strcmp($whatWeWant, $toCheck) == 0) {
873 if (strpos($whatWeWant, $typeSeparator) === false) {
875 $whatWeWant, $toCheck,
876 strpos($toCheck, $typeSeparator)
886 * Analyzes SQL queries
888 * @param array $arr The SQL queries
890 * @return array The analyzed SQL queries
894 function PMA_SQP_analyze($arr)
896 if ($arr == array() ||
! isset($arr['len'])) {
903 'select_expr_clause'=> '', // the whole stuff between SELECT and FROM , except DISTINCT
904 'position_of_first_select' => '', // the array index
906 'group_by_clause'=> '',
907 'order_by_clause'=> '',
908 'having_clause' => '',
909 'limit_clause' => '',
910 'where_clause' => '',
911 'where_clause_identifiers' => array(),
912 'unsorted_query' => '',
913 'queryflags' => array(),
914 'select_expr' => array(),
915 'table_ref' => array(),
916 'foreign_keys' => array(),
917 'create_table_fields' => array()
919 $subresult_empty = $subresult;
920 $seek_queryend = false;
921 $seen_end_of_table_ref = false;
922 $number_of_brackets_in_extract = 0;
923 $number_of_brackets_in_group_concat = 0;
925 $number_of_brackets = 0;
926 $in_subquery = false;
927 $seen_subquery = false;
930 // for SELECT EXTRACT(YEAR_MONTH FROM CURDATE())
931 // we must not use CURDATE as a table_ref
932 // so we track whether we are in the EXTRACT()
935 // for GROUP_CONCAT(...)
936 $in_group_concat = false;
938 /* Description of analyzer results
940 * db, table, column, alias
941 * ------------------------
943 * Inside the $subresult array, we create ['select_expr'] and ['table_ref']
946 * The SELECT syntax (simplified) is
949 * select_expression,...
950 * [FROM [table_references]
953 * ['select_expr'] is filled with each expression, the key represents the
954 * expression position in the list (0-based) (so we don't lose track of
955 * multiple occurences of the same column).
957 * ['table_ref'] is filled with each table ref, same thing for the key.
959 * I create all sub-values empty, even if they are
960 * not present (for example no select_expression alias).
962 * There is a debug section at the end of loop #1, if you want to
963 * see the exact contents of select_expr and table_ref
968 * In $subresult, array 'queryflags' is filled, according to what we
971 * Currently, those are generated:
973 * ['queryflags']['select_from'] = 1; if this is a real SELECT...FROM
974 * ['queryflags']['drop_database'] = 1;if this is a DROP DATABASE
975 * ['queryflags']['reload'] = 1; for the purpose of reloading the
977 * ['queryflags']['distinct'] = 1; for a DISTINCT
978 * ['queryflags']['union'] = 1; for a UNION
979 * ['queryflags']['join'] = 1; for a JOIN
980 * ['queryflags']['offset'] = 1; for the presence of OFFSET
981 * ['queryflags']['procedure'] = 1; for the presence of PROCEDURE
982 * ['queryflags']['is_explain'] = 1; for the presence of EXPLAIN
983 * ['queryflags']['is_delete'] = 1; for the presence of DELETE
984 * ['queryflags']['is_affected'] = 1; for the presence of UPDATE, DELETE
985 * or INSERT|LOAD DATA|REPLACE
986 * ['queryflags']['is_replace'] = 1; for the presence of REPLACE
987 * ['queryflags']['is_insert'] = 1; for the presence of INSERT
988 * ['queryflags']['is_maint'] = 1; for the presence of CHECK|ANALYZE
989 * |REPAIR|OPTIMIZE TABLE
990 * ['queryflags']['is_show'] = 1; for the presence of SHOW
991 * ['queryflags']['is_analyse'] = 1; for the presence of PROCEDURE ANALYSE
992 * ['queryflags']['is_export'] = 1; for the presence of INTO OUTFILE
993 * ['queryflags']['is_group'] = 1; for the presence of GROUP BY|HAVING|
995 * ['queryflags']['is_func'] = 1; for the presence of SUM|AVG|STD|STDDEV
996 * |MIN|MAX|BIT_OR|BIT_AND
997 * ['queryflags']['is_count'] = 1; for the presence of SELECT COUNT
998 * ['queryflags']['is_procedure'] = 1; for the presence of CALL
1003 * The select is splitted in those clauses:
1004 * ['select_expr_clause']
1006 * ['group_by_clause']
1007 * ['order_by_clause']
1012 * The identifiers of the WHERE clause are put into the array
1013 * ['where_clause_identifier']
1015 * For a SELECT, the whole query without the ORDER BY clause is put into
1016 * ['unsorted_query']
1020 * The CREATE TABLE may contain FOREIGN KEY clauses, so they get
1021 * analyzed and ['foreign_keys'] is an array filled with
1022 * the constraint name, the index list,
1023 * the REFERENCES table name and REFERENCES index list,
1024 * and ON UPDATE | ON DELETE clauses
1026 * position_of_first_select
1027 * ------------------------
1029 * The array index of the first SELECT we find. Will be used to
1030 * insert a SQL_CALC_FOUND_ROWS.
1032 * create_table_fields
1033 * -------------------
1035 * Used to detect the DEFAULT CURRENT_TIMESTAMP and
1036 * ON UPDATE CURRENT_TIMESTAMP clauses of the CREATE TABLE query.
1037 * Also used to store the default value of the field.
1038 * An array, each element is the identifier name.
1039 * Note that for now, the timestamp_not_null element is created
1040 * even for non-TIMESTAMP fields.
1042 * Sub-elements: ['type'] which contains the column type
1043 * optional (currently they are never false but can be absent):
1044 * ['default_current_timestamp'] boolean
1045 * ['on_update_current_timestamp'] boolean
1046 * ['timestamp_not_null'] boolean
1048 * section_before_limit, section_after_limit
1049 * -----------------------------------------
1051 * Marks the point of the query where we can insert a LIMIT clause;
1052 * so the section_before_limit will contain the left part before
1053 * a possible LIMIT clause
1056 * End of description of analyzer results
1060 // TODO: current logic checks for only one word, so I put only the
1061 // first word of the reserved expressions that end a table ref;
1062 // maybe this is not ok (the first word might mean something else)
1063 // $words_ending_table_ref = array(
1068 // 'LOCK IN SHARE MODE',
1074 $words_ending_table_ref = array(
1086 $words_ending_clauses = array(
1094 $supported_query_types = array(
1097 // Support for these additional query types will come later on.
1112 // loop #1 for each token: select_expr, table_ref for SELECT
1114 for ($i = 0; $i < $size; $i++
) {
1115 //DEBUG echo "Loop1 <strong>" . $arr[$i]['data']
1116 //. "</strong> (" . $arr[$i]['type'] . ")<br />";
1118 // High speed seek for locating the end of the current query
1119 if ($seek_queryend == true) {
1120 if ($arr[$i]['type'] == 'punct_queryend') {
1121 $seek_queryend = false;
1124 } // end if (type == punct_queryend)
1125 } // end if ($seek_queryend)
1128 * Note: do not split if this is a punct_queryend for the first and only
1130 * @todo when we find a UNION, should we split in another subresult?
1132 if ($arr[$i]['type'] == 'punct_queryend' && ($i +
1 != $size)) {
1133 $result[] = $subresult;
1134 $subresult = $subresult_empty;
1136 } // end if (type == punct_queryend)
1138 // ==============================================================
1139 if ($arr[$i]['type'] == 'punct_bracket_open_round') {
1140 $number_of_brackets++
;
1142 $number_of_brackets_in_extract++
;
1144 if ($in_group_concat) {
1145 $number_of_brackets_in_group_concat++
;
1148 // ==============================================================
1149 if ($arr[$i]['type'] == 'punct_bracket_close_round') {
1150 $number_of_brackets--;
1151 if ($number_of_brackets == 0) {
1152 $in_subquery = false;
1155 $number_of_brackets_in_extract--;
1156 if ($number_of_brackets_in_extract == 0) {
1157 $in_extract = false;
1160 if ($in_group_concat) {
1161 $number_of_brackets_in_group_concat--;
1162 if ($number_of_brackets_in_group_concat == 0) {
1163 $in_group_concat = false;
1170 * skip the subquery to avoid setting
1171 * select_expr or table_ref with the contents
1172 * of this subquery; this is to avoid a bug when
1173 * trying to edit the results of
1174 * select * from child where not exists (select id from
1175 * parent where child.parent_id = parent.id);
1179 // ==============================================================
1180 if ($arr[$i]['type'] == 'alpha_functionName') {
1181 $upper_data = strtoupper($arr[$i]['data']);
1182 if ($upper_data =='EXTRACT') {
1184 $number_of_brackets_in_extract = 0;
1186 if ($upper_data =='GROUP_CONCAT') {
1187 $in_group_concat = true;
1188 $number_of_brackets_in_group_concat = 0;
1192 // ==============================================================
1193 if ($arr[$i]['type'] == 'alpha_reservedWord') {
1194 // We don't know what type of query yet, so run this
1195 if ($subresult['querytype'] == '') {
1196 $subresult['querytype'] = strtoupper($arr[$i]['data']);
1197 } // end if (querytype was empty)
1199 // Check if we support this type of query
1200 if (!isset($supported_query_types[$subresult['querytype']])) {
1201 // Skip ahead to the next one if we don't
1202 $seek_queryend = true;
1204 } // end if (query not supported)
1207 $upper_data = strtoupper($arr[$i]['data']);
1209 * @todo reset for each query?
1212 if ($upper_data == 'SELECT') {
1213 if ($number_of_brackets > 0) {
1214 $in_subquery = true;
1215 $seen_subquery = true;
1216 // this is a subquery so do not analyze inside it
1220 $previous_was_identifier = false;
1221 $current_select_expr = -1;
1222 $seen_end_of_table_ref = false;
1223 } // end if (data == SELECT)
1225 if ($upper_data =='FROM' && !$in_extract) {
1226 $current_table_ref = -1;
1228 $previous_was_identifier = false;
1229 $save_table_ref = true;
1230 } // end if (data == FROM)
1232 // here, do not 'continue' the loop, as we have more work for
1233 // reserved words below
1234 } // end if (type == alpha_reservedWord)
1236 // ==============================
1237 if ($arr[$i]['type'] == 'quote_backtick'
1238 ||
$arr[$i]['type'] == 'quote_double'
1239 ||
$arr[$i]['type'] == 'quote_single'
1240 ||
$arr[$i]['type'] == 'alpha_identifier'
1241 ||
($arr[$i]['type'] == 'alpha_reservedWord'
1242 && $arr[$i]['forbidden'] == false)
1244 switch ($arr[$i]['type']) {
1245 case 'alpha_identifier':
1246 case 'alpha_reservedWord':
1248 * this is not a real reservedWord, because it's not
1249 * present in the list of forbidden words, for example
1250 * "storage" which can be used as an identifier
1253 $identifier = $arr[$i]['data'];
1256 case 'quote_backtick':
1257 case 'quote_double':
1258 case 'quote_single':
1259 $identifier = PMA_Util
::unQuote($arr[$i]['data']);
1263 if ($subresult['querytype'] == 'SELECT'
1264 && ! $in_group_concat
1265 && ! ($seen_subquery && $arr[$i - 1]['type'] == 'punct_bracket_close_round')
1268 if ($previous_was_identifier && isset($chain)) {
1269 // found alias for this select_expr, save it
1270 // but only if we got something in $chain
1271 // (for example, SELECT COUNT(*) AS cnt
1272 // puts nothing in $chain, so we avoid
1273 // setting the alias)
1274 $alias_for_select_expr = $identifier;
1276 $chain[] = $identifier;
1277 $previous_was_identifier = true;
1279 } // end if !$previous_was_identifier
1282 if ($save_table_ref && !$seen_end_of_table_ref) {
1283 if ($previous_was_identifier) {
1284 // found alias for table ref
1285 // save it for later
1286 $alias_for_table_ref = $identifier;
1288 $chain[] = $identifier;
1289 $previous_was_identifier = true;
1291 } // end if ($previous_was_identifier)
1292 } // end if ($save_table_ref &&!$seen_end_of_table_ref)
1293 } // end if (!$seen_from)
1294 } // end if (querytype SELECT)
1295 } // end if (quote_backtick or double quote or alpha_identifier)
1297 // ===================================
1298 if ($arr[$i]['type'] == 'punct_qualifier') {
1299 // to be able to detect an identifier following another
1300 $previous_was_identifier = false;
1302 } // end if (punct_qualifier)
1305 * @todo check if 3 identifiers following one another -> error
1308 // s a v e a s e l e c t e x p r
1309 // finding a list separator or FROM
1310 // means that we must save the current chain of identifiers
1311 // into a select expression
1313 // for now, we only save a select expression if it contains
1314 // at least one identifier, as we are interested in checking
1315 // the columns and table names, so in "select * from persons",
1316 // the "*" is not saved
1318 if (isset($chain) && !$seen_end_of_table_ref
1319 && ((!$seen_from && $arr[$i]['type'] == 'punct_listsep')
1320 ||
($arr[$i]['type'] == 'alpha_reservedWord' && $upper_data == 'FROM'))
1322 $size_chain = count($chain);
1323 $current_select_expr++
;
1324 $subresult['select_expr'][$current_select_expr] = array(
1329 'table_true_name' => '',
1333 if (isset($alias_for_select_expr) && strlen($alias_for_select_expr)) {
1334 // we had found an alias for this select expression
1335 $subresult['select_expr'][$current_select_expr]['alias'] = $alias_for_select_expr;
1336 unset($alias_for_select_expr);
1338 // there is at least a column
1339 $subresult['select_expr'][$current_select_expr]['column'] = $chain[$size_chain - 1];
1340 $subresult['select_expr'][$current_select_expr]['expr'] = $chain[$size_chain - 1];
1343 if ($size_chain > 1) {
1344 $subresult['select_expr'][$current_select_expr]['table_name'] = $chain[$size_chain - 2];
1345 // we assume for now that this is also the true name
1346 $subresult['select_expr'][$current_select_expr]['table_true_name'] = $chain[$size_chain - 2];
1347 $subresult['select_expr'][$current_select_expr]['expr']
1348 = $subresult['select_expr'][$current_select_expr]['table_name']
1349 . '.' . $subresult['select_expr'][$current_select_expr]['expr'];
1350 } // end if ($size_chain > 1)
1353 if ($size_chain > 2) {
1354 $subresult['select_expr'][$current_select_expr]['db'] = $chain[$size_chain - 3];
1355 $subresult['select_expr'][$current_select_expr]['expr']
1356 = $subresult['select_expr'][$current_select_expr]['db']
1357 . '.' . $subresult['select_expr'][$current_select_expr]['expr'];
1358 } // end if ($size_chain > 2)
1362 * @todo explain this:
1364 if (($arr[$i]['type'] == 'alpha_reservedWord')
1365 && ($upper_data != 'FROM')
1367 $previous_was_identifier = true;
1370 } // end if (save a select expr)
1373 //======================================
1374 // s a v e a t a b l e r e f
1375 //======================================
1377 // maybe we just saw the end of table refs
1378 // but the last table ref has to be saved
1379 // or we are at the last token
1380 // or we just got a reserved word
1382 * @todo there could be another query after this one
1385 if (isset($chain) && $seen_from && $save_table_ref
1386 && ($arr[$i]['type'] == 'punct_listsep'
1387 ||
($arr[$i]['type'] == 'alpha_reservedWord' && $upper_data != "AS")
1388 ||
$seen_end_of_table_ref
1392 $size_chain = count($chain);
1393 $current_table_ref++
;
1394 $subresult['table_ref'][$current_table_ref] = array(
1398 'table_alias' => '',
1399 'table_true_name' => ''
1401 if (isset($alias_for_table_ref) && strlen($alias_for_table_ref)) {
1402 $subresult['table_ref'][$current_table_ref]['table_alias'] = $alias_for_table_ref;
1403 unset($alias_for_table_ref);
1405 $subresult['table_ref'][$current_table_ref]['table_name'] = $chain[$size_chain - 1];
1406 // we assume for now that this is also the true name
1407 $subresult['table_ref'][$current_table_ref]['table_true_name'] = $chain[$size_chain - 1];
1408 $subresult['table_ref'][$current_table_ref]['expr']
1409 = $subresult['table_ref'][$current_table_ref]['table_name'];
1411 if ($size_chain > 1) {
1412 $subresult['table_ref'][$current_table_ref]['db'] = $chain[$size_chain - 2];
1413 $subresult['table_ref'][$current_table_ref]['expr']
1414 = $subresult['table_ref'][$current_table_ref]['db']
1415 . '.' . $subresult['table_ref'][$current_table_ref]['expr'];
1416 } // end if ($size_chain > 1)
1418 // add the table alias into the whole expression
1419 $subresult['table_ref'][$current_table_ref]['expr']
1420 .= ' ' . $subresult['table_ref'][$current_table_ref]['table_alias'];
1423 $previous_was_identifier = true;
1426 } // end if (save a table ref)
1429 // when we have found all table refs,
1430 // for each table_ref alias, put the true name of the table
1431 // in the corresponding select expressions
1433 if (isset($current_table_ref)
1434 && ($seen_end_of_table_ref ||
$i == $size-1)
1435 && $subresult != $subresult_empty
1437 for ($tr=0; $tr <= $current_table_ref; $tr++
) {
1438 $alias = $subresult['table_ref'][$tr]['table_alias'];
1439 $truename = $subresult['table_ref'][$tr]['table_true_name'];
1440 for ($se=0; $se <= $current_select_expr; $se++
) {
1443 && $subresult['select_expr'][$se]['table_true_name'] == $alias
1445 $subresult['select_expr'][$se]['table_true_name'] = $truename;
1446 } // end if (found the alias)
1447 } // end for (select expressions)
1449 } // end for (table refs)
1450 } // end if (set the true names)
1453 // e n d i n g l o o p #1
1454 // set the $previous_was_identifier to false if the current
1455 // token is not an identifier
1456 if (($arr[$i]['type'] != 'alpha_identifier')
1457 && ($arr[$i]['type'] != 'quote_double')
1458 && ($arr[$i]['type'] != 'quote_single')
1459 && ($arr[$i]['type'] != 'quote_backtick')
1461 $previous_was_identifier = false;
1464 // however, if we are on AS, we must keep the $previous_was_identifier
1465 if (($arr[$i]['type'] == 'alpha_reservedWord')
1466 && ($upper_data == 'AS')
1468 $previous_was_identifier = true;
1471 if (($arr[$i]['type'] == 'alpha_reservedWord')
1472 && ($upper_data =='ON' ||
$upper_data =='USING')
1474 $save_table_ref = false;
1475 } // end if (data == ON)
1477 if (($arr[$i]['type'] == 'alpha_reservedWord')
1478 && ($upper_data =='JOIN' ||
$upper_data =='FROM')
1480 $save_table_ref = true;
1481 } // end if (data == JOIN)
1484 * no need to check the end of table ref if we already did
1486 * @todo maybe add "&& $seen_from"
1488 if (!$seen_end_of_table_ref) {
1489 // if this is the last token, it implies that we have
1490 // seen the end of table references
1491 // Check for the end of table references
1493 // Note: if we are analyzing a GROUP_CONCAT clause,
1494 // we might find a word that seems to indicate that
1495 // we have found the end of table refs (like ORDER)
1496 // but it's a modifier of the GROUP_CONCAT so
1497 // it's not the real end of table refs
1499 ||
($arr[$i]['type'] == 'alpha_reservedWord'
1500 && !$in_group_concat
1501 && isset($words_ending_table_ref[$upper_data]))
1503 $seen_end_of_table_ref = true;
1504 // to be able to save the last table ref, but do not
1505 // set it true if we found a word like "ON" that has
1506 // already set it to false
1507 if (isset($save_table_ref) && $save_table_ref != false) {
1508 $save_table_ref = true;
1511 } // end if (check for end of table ref)
1512 } //end if (!$seen_end_of_table_ref)
1514 if ($seen_end_of_table_ref) {
1515 $save_table_ref = false;
1518 } // end for $i (loop #1)
1522 if (isset($current_select_expr)) {
1523 for ($trace=0; $trace<=$current_select_expr; $trace++) {
1525 reset ($subresult['select_expr'][$trace]);
1526 while (list ($key, $val) = each ($subresult['select_expr'][$trace]))
1527 echo "sel expr $trace $key => $val<br />\n";
1531 if (isset($current_table_ref)) {
1532 echo "current_table_ref = " . $current_table_ref . "<br>";
1533 for ($trace=0; $trace<=$current_table_ref; $trace++) {
1536 reset ($subresult['table_ref'][$trace]);
1537 while (list ($key, $val) = each ($subresult['table_ref'][$trace]))
1538 echo "table ref $trace $key => $val<br />\n";
1542 // -------------------------------------------------------
1545 // loop #2: - queryflags
1546 // - querytype (for queries != 'SELECT')
1547 // - section_before_limit, section_after_limit
1549 // we will also need this queryflag in loop 2
1551 if (isset($current_table_ref) && $current_table_ref > -1) {
1552 $subresult['queryflags']['select_from'] = 1;
1555 $section_before_limit = '';
1556 $section_after_limit = ''; // truly the section after the limit clause
1557 $seen_reserved_word = false;
1558 $seen_group = false;
1559 $seen_order = false;
1560 $seen_order_by = false;
1561 $in_group_by = false; // true when we are inside the GROUP BY clause
1562 $in_order_by = false; // true when we are inside the ORDER BY clause
1563 $in_having = false; // true when we are inside the HAVING clause
1564 $in_select_expr = false; // true when we are inside the select expr clause
1565 $in_where = false; // true when we are inside the WHERE clause
1566 $seen_limit = false; // true if we have seen a LIMIT clause
1567 $in_limit = false; // true when we are inside the LIMIT clause
1568 $after_limit = false; // true when we are after the LIMIT clause
1569 $in_from = false; // true when we are in the FROM clause
1570 $in_group_concat = false;
1571 $first_reserved_word = '';
1572 $current_identifier = '';
1573 $unsorted_query = $arr['raw']; // in case there is no ORDER BY
1574 $number_of_brackets = 0;
1575 $in_subquery = false;
1577 for ($i = 0; $i < $size; $i++
) {
1578 //DEBUG echo "Loop2 <strong>" . $arr[$i]['data']
1579 //. "</strong> (" . $arr[$i]['type'] . ")<br />";
1581 if ($arr[$i]['type'] == 'punct_bracket_open_round') {
1582 $number_of_brackets++
;
1585 if ($arr[$i]['type'] == 'punct_bracket_close_round') {
1586 $number_of_brackets--;
1587 if ($number_of_brackets == 0) {
1588 $in_subquery = false;
1592 if ($arr[$i]['type'] == 'alpha_reservedWord') {
1593 $upper_data = strtoupper($arr[$i]['data']);
1595 if ($upper_data == 'SELECT' && $number_of_brackets > 0) {
1596 $in_subquery = true;
1599 if (!$seen_reserved_word) {
1600 $first_reserved_word = $upper_data;
1601 $subresult['querytype'] = $upper_data;
1602 $seen_reserved_word = true;
1604 if ($first_reserved_word == 'SELECT') {
1605 $position_of_first_select = $i;
1608 if ($first_reserved_word == 'EXPLAIN') {
1609 $subresult['queryflags']['is_explain'] = 1;
1612 if ($first_reserved_word == 'DELETE') {
1613 $subresult['queryflags']['is_delete'] = 1;
1614 $subresult['queryflags']['is_affected'] = 1;
1617 if ($first_reserved_word == 'UPDATE') {
1618 $subresult['queryflags']['is_affected'] = 1;
1621 if ($first_reserved_word == 'REPLACE') {
1622 $subresult['queryflags']['is_replace'] = 1;
1625 if ($first_reserved_word == 'SHOW') {
1626 $subresult['queryflags']['is_show'] = 1;
1630 // for the presence of DROP DATABASE
1631 if ($first_reserved_word == 'DROP' && $upper_data == 'DATABASE') {
1632 $subresult['queryflags']['drop_database'] = 1;
1634 // A table has to be created, renamed, dropped -> navi panel
1635 // should be reloaded
1636 $keywords1 = array('CREATE', 'ALTER', 'DROP');
1637 $keywords2 = array('VIEW', 'TABLE', 'DATABASE', 'SCHEMA');
1638 if (in_array($first_reserved_word, $keywords1)
1639 && in_array($upper_data, $keywords2)
1641 $subresult['queryflags']['reload'] = 1;
1644 // for the presence of INSERT|LOAD DATA
1645 if (in_array($first_reserved_word, array('INSERT', 'LOAD'))
1646 && $upper_data == 'REPLACE'
1648 $subresult['queryflags']['is_insert'] = 1;
1649 $subresult['queryflags']['is_affected'] = 1;
1652 // for the presence of CHECK|ANALYZE|REPAIR|OPTIMIZE TABLE
1654 'CHECK', 'ANALYZE', 'REPAIR', 'OPTIMIZE'
1656 if (in_array($first_reserved_word, $keywords)
1657 && $upper_data == 'TABLE'
1659 $subresult['queryflags']['is_maint'] = 1;
1663 if ($upper_data == 'LIMIT' && ! $in_subquery) {
1664 $section_before_limit = substr($arr['raw'], 0, $arr[$i]['pos'] - 5);
1668 $in_order_by = false; // @todo maybe others to set false
1671 if ($upper_data == 'PROCEDURE') {
1672 $subresult['queryflags']['procedure'] = 1;
1674 $after_limit = true;
1676 // for the presence of PROCEDURE ANALYSE
1677 if (isset($subresult['queryflags']['select_from'])
1678 && $subresult['queryflags']['select_from'] == 1
1680 && $arr[$i +
1]['type'] == 'alpha_reservedWord'
1681 && strtoupper($arr[$i +
1]['data']) == 'ANALYSE'
1683 $subresult['queryflags']['is_analyse'] = 1;
1687 // for the presence of INTO OUTFILE
1688 if ($upper_data == 'INTO'
1689 && isset($subresult['queryflags']['select_from'])
1690 && $subresult['queryflags']['select_from'] == 1
1692 && $arr[$i +
1]['type'] == 'alpha_reservedWord'
1693 && strtoupper($arr[$i +
1]['data']) == 'OUTFILE'
1695 $subresult['queryflags']['is_export'] = 1;
1698 * @todo set also to false if we find FOR UPDATE or LOCK IN SHARE MODE
1700 if ($upper_data == 'SELECT') {
1701 $in_select_expr = true;
1702 $select_expr_clause = '';
1704 // for the presence of SELECT COUNT
1705 if (isset($subresult['queryflags']['select_from'])
1706 && $subresult['queryflags']['select_from'] == 1
1707 && !isset($subresult['queryflags']['is_group'])
1709 && $arr[$i +
1]['type'] == 'alpha_functionName'
1710 && strtoupper($arr[$i +
1]['data']) == 'COUNT'
1712 $subresult['queryflags']['is_count'] = 1;
1716 if ($upper_data == 'DISTINCT' && !$in_group_concat) {
1717 $subresult['queryflags']['distinct'] = 1;
1720 if ($upper_data == 'UNION') {
1721 $subresult['queryflags']['union'] = 1;
1724 if ($upper_data == 'JOIN') {
1725 $subresult['queryflags']['join'] = 1;
1728 if ($upper_data == 'OFFSET') {
1729 $subresult['queryflags']['offset'] = 1;
1732 // for the presence of CALL
1733 if ($upper_data == 'CALL') {
1734 $subresult['queryflags']['is_procedure'] = 1;
1737 // if this is a real SELECT...FROM
1738 if ($upper_data == 'FROM'
1739 && isset($subresult['queryflags']['select_from'])
1740 && $subresult['queryflags']['select_from'] == 1
1744 $in_select_expr = false;
1748 // (we could have less resetting of variables to false
1749 // if we trust that the query respects the standard
1750 // MySQL order for clauses)
1752 // we use $seen_group and $seen_order because we are looking
1754 if ($upper_data == 'GROUP') {
1756 $seen_order = false;
1758 $in_order_by = false;
1760 $in_select_expr = false;
1763 // for the presence of GROUP BY|HAVING|SELECT DISTINCT
1764 if (isset($subresult['queryflags']['select_from'])
1765 && $subresult['queryflags']['select_from'] == 1
1767 && $arr[$i +
1]['type'] == 'alpha_reservedWord'
1768 && in_array(strtoupper($arr[$i +
1]['data']), array("BY", "HAVING", "SELECT"))
1770 && $arr[$i +
2]['type'] == 'alpha_reservedWord'
1771 && strtoupper($arr[$i +
2]['data']) == 'DISTINCT'
1773 $subresult['queryflags']['is_group'] = 1;
1776 if ($upper_data == 'ORDER' && !$in_group_concat) {
1778 $seen_group = false;
1780 $in_group_by = false;
1782 $in_select_expr = false;
1785 if ($upper_data == 'HAVING') {
1787 $having_clause = '';
1788 $seen_group = false;
1789 $seen_order = false;
1790 $in_group_by = false;
1791 $in_order_by = false;
1793 $in_select_expr = false;
1797 if ($upper_data == 'WHERE') {
1800 $where_clause_identifiers = array();
1801 $seen_group = false;
1802 $seen_order = false;
1803 $in_group_by = false;
1804 $in_order_by = false;
1806 $in_select_expr = false;
1810 if ($upper_data == 'BY') {
1812 $in_group_by = true;
1813 $group_by_clause = '';
1816 $seen_order_by = true;
1817 // Here we assume that the ORDER BY keywords took
1818 // exactly 8 characters.
1819 // We use $GLOBALS['PMA_String']->substr() to be charset-safe; otherwise
1820 // if the table name contains accents, the unsorted
1821 // query would be missing some characters.
1822 $unsorted_query = $GLOBALS['PMA_String']->substr(
1823 $arr['raw'], 0, $arr[$i]['pos'] - 8
1825 $in_order_by = true;
1826 $order_by_clause = '';
1830 // if we find one of the words that could end the clause
1831 if (isset($words_ending_clauses[$upper_data])) {
1833 $in_group_by = false;
1834 $in_order_by = false;
1837 $in_select_expr = false;
1841 } // endif (reservedWord)
1843 // do not add a space after a function name
1845 * @todo can we combine loop 2 and loop 1? some code is repeated here...
1849 if ($arr[$i]['type'] == 'alpha_functionName') {
1851 $upper_data = strtoupper($arr[$i]['data']);
1852 if ($upper_data =='GROUP_CONCAT') {
1853 $in_group_concat = true;
1854 $number_of_brackets_in_group_concat = 0;
1858 if ($arr[$i]['type'] == 'punct_bracket_open_round') {
1859 if ($in_group_concat) {
1860 $number_of_brackets_in_group_concat++
;
1863 if ($arr[$i]['type'] == 'punct_bracket_close_round') {
1864 if ($in_group_concat) {
1865 $number_of_brackets_in_group_concat--;
1866 if ($number_of_brackets_in_group_concat == 0) {
1867 $in_group_concat = false;
1873 // do not add a space after an identifier if followed by a dot
1874 if ($arr[$i]['type'] == 'alpha_identifier'
1875 && $i < $size - 1 && $arr[$i +
1]['data'] == '.'
1880 // do not add a space after a dot if followed by an identifier
1881 if ($arr[$i]['data'] == '.' && $i < $size - 1
1882 && $arr[$i +
1]['type'] == 'alpha_identifier'
1887 // for the presence of INSERT|LOAD DATA
1888 if ($arr[$i]['type'] == 'alpha_identifier'
1889 && strtoupper($arr[$i]['data']) == 'DATA'
1891 && $arr[$i - 1]['type'] == 'alpha_reservedWord'
1892 && in_array(strtoupper($arr[$i - 1]['data']), array("INSERT", "LOAD"))
1894 $subresult['queryflags']['is_insert'] = 1;
1895 $subresult['queryflags']['is_affected'] = 1;
1898 // for the presence of SUM|AVG|STD|STDDEV|MIN|MAX|BIT_OR|BIT_AND
1899 if ($arr[$i]['type'] == 'alpha_functionName'
1900 && in_array(strtoupper($arr[$i]['data']), array("SUM","AVG","STD","STDDEV","MIN","MAX","BIT_OR","BIT_AND"))
1901 && isset($subresult['queryflags']['select_from'])
1902 && $subresult['queryflags']['select_from'] == 1
1903 && !isset($subresult['queryflags']['is_group'])
1905 $subresult['queryflags']['is_func'] = 1;
1908 if ($in_select_expr && $upper_data != 'SELECT'
1909 && $upper_data != 'DISTINCT'
1911 $select_expr_clause .= $arr[$i]['data'] . $sep;
1913 if ($in_from && $upper_data != 'FROM') {
1914 $from_clause .= $arr[$i]['data'] . $sep;
1916 if ($in_group_by && $upper_data != 'GROUP' && $upper_data != 'BY') {
1917 $group_by_clause .= $arr[$i]['data'] . $sep;
1919 if ($in_order_by && $upper_data != 'ORDER' && $upper_data != 'BY') {
1920 // add a space only before ASC or DESC
1921 // not around the dot between dbname and tablename
1922 if ($arr[$i]['type'] == 'alpha_reservedWord') {
1923 $order_by_clause .= $sep;
1925 $order_by_clause .= $arr[$i]['data'];
1927 if ($in_having && $upper_data != 'HAVING') {
1928 $having_clause .= $arr[$i]['data'] . $sep;
1930 if ($in_where && $upper_data != 'WHERE') {
1931 $where_clause .= $arr[$i]['data'] . $sep;
1933 if (($arr[$i]['type'] == 'quote_backtick')
1934 ||
($arr[$i]['type'] == 'alpha_identifier')
1936 $where_clause_identifiers[] = $arr[$i]['data'];
1940 // to grab the rest of the query after the ORDER BY clause
1941 if (isset($subresult['queryflags']['select_from'])
1942 && $subresult['queryflags']['select_from'] == 1
1945 && $upper_data != 'BY'
1947 $unsorted_query .= $arr[$i]['data'];
1948 if ($arr[$i]['type'] != 'punct_bracket_open_round'
1949 && $arr[$i]['type'] != 'punct_bracket_close_round'
1950 && $arr[$i]['type'] != 'punct'
1952 $unsorted_query .= $sep;
1957 if ($upper_data == 'OFFSET') {
1958 $limit_clause .= $sep;
1960 $limit_clause .= $arr[$i]['data'];
1961 if ($upper_data == 'LIMIT' ||
$upper_data == 'OFFSET') {
1962 $limit_clause .= $sep;
1965 if ($after_limit && $seen_limit) {
1966 $section_after_limit .= $arr[$i]['data'] . $sep;
1969 // clear $upper_data for next iteration
1971 } // end for $i (loop #2)
1972 if (empty($section_before_limit)) {
1973 $section_before_limit = $arr['raw'];
1976 // -----------------------------------------------------
1977 // loop #3: foreign keys and MySQL 4.1.2+ TIMESTAMP options
1978 // (for now, check only the first query)
1979 // (for now, identifiers are assumed to be backquoted)
1981 // If we find that we are dealing with a CREATE TABLE query,
1982 // we look for the next punct_bracket_open_round, which
1983 // introduces the fields list. Then, when we find a
1984 // quote_backtick, it must be a field, so we put it into
1985 // the create_table_fields array. Even if this field is
1986 // not a timestamp, it will be useful when logic has been
1987 // added for complete field attributes analysis.
1989 $seen_foreign = false;
1990 $seen_references = false;
1991 $seen_constraint = false;
1992 $foreign_key_number = -1;
1993 $seen_create_table = false;
1994 $seen_create = false;
1995 $seen_alter = false;
1996 $in_create_table_fields = false;
1997 $brackets_level = 0;
1998 $in_timestamp_options = false;
1999 $seen_default = false;
2001 for ($i = 0; $i < $size; $i++
) {
2002 if ($arr[$i]['type'] == 'alpha_reservedWord') {
2003 $upper_data = strtoupper($arr[$i]['data']);
2005 if ($upper_data == 'NOT' && $in_timestamp_options) {
2006 $create_table_fields[$current_identifier]['timestamp_not_null'] = true;
2010 if ($upper_data == 'CREATE') {
2011 $seen_create = true;
2014 if ($upper_data == 'ALTER') {
2018 if ($upper_data == 'TABLE' && $seen_create) {
2019 $seen_create_table = true;
2020 $create_table_fields = array();
2023 if ($upper_data == 'CURRENT_TIMESTAMP') {
2024 if ($in_timestamp_options) {
2025 if ($seen_default) {
2026 $create_table_fields[$current_identifier]['default_current_timestamp'] = true;
2031 if ($upper_data == 'CONSTRAINT') {
2032 $foreign_key_number++
;
2033 $seen_foreign = false;
2034 $seen_references = false;
2035 $seen_constraint = true;
2037 if ($upper_data == 'FOREIGN') {
2038 $seen_foreign = true;
2039 $seen_references = false;
2040 $seen_constraint = false;
2042 if ($upper_data == 'REFERENCES') {
2043 $seen_foreign = false;
2044 $seen_references = true;
2045 $seen_constraint = false;
2051 // [ON DELETE {CASCADE | SET NULL | NO ACTION | RESTRICT}]
2052 // [ON UPDATE {CASCADE | SET NULL | NO ACTION | RESTRICT}]
2054 // but we set ['on_delete'] or ['on_cascade'] to
2055 // CASCADE | SET_NULL | NO_ACTION | RESTRICT
2057 // ON UPDATE CURRENT_TIMESTAMP
2059 if ($upper_data == 'ON') {
2060 if (isset($arr[$i+
1]) && $arr[$i+
1]['type'] == 'alpha_reservedWord') {
2061 $second_upper_data = strtoupper($arr[$i+
1]['data']);
2062 if ($second_upper_data == 'DELETE') {
2063 $clause = 'on_delete';
2065 if ($second_upper_data == 'UPDATE') {
2066 $clause = 'on_update';
2068 // ugly workaround because currently, NO is not
2069 // in the list of reserved words in sqlparser.data
2070 // (we got a bug report about not being able to use
2071 // 'no' as an identifier)
2073 && ($arr[$i+
2]['type'] == 'alpha_reservedWord'
2074 ||
($arr[$i+
2]['type'] == 'alpha_identifier'
2075 && strtoupper($arr[$i+
2]['data'])=='NO'))
2077 $third_upper_data = strtoupper($arr[$i+
2]['data']);
2078 if ($third_upper_data == 'CASCADE'
2079 ||
$third_upper_data == 'RESTRICT'
2081 $value = $third_upper_data;
2082 } elseif ($third_upper_data == 'SET'
2083 ||
$third_upper_data == 'NO'
2085 if ($arr[$i+
3]['type'] == 'alpha_reservedWord') {
2086 $value = $third_upper_data . '_'
2087 . strtoupper($arr[$i+
3]['data']);
2089 } elseif ($third_upper_data == 'CURRENT_TIMESTAMP') {
2090 if ($clause == 'on_update'
2091 && $in_timestamp_options
2093 $create_table_fields[$current_identifier]['on_update_current_timestamp'] = true;
2094 $seen_default = false;
2100 if (!empty($value)) {
2101 $foreign[$foreign_key_number][$clause] = $value;
2104 } // endif (isset($clause))
2108 } // end of reserved words analysis
2111 if ($arr[$i]['type'] == 'punct_bracket_open_round') {
2113 if ($seen_create_table && $brackets_level == 1) {
2114 $in_create_table_fields = true;
2119 if ($arr[$i]['type'] == 'punct_bracket_close_round') {
2121 if ($seen_references) {
2122 $seen_references = false;
2124 if ($seen_create_table && $brackets_level == 0) {
2125 $in_create_table_fields = false;
2129 if (($arr[$i]['type'] == 'alpha_columnAttrib')) {
2130 $upper_data = strtoupper($arr[$i]['data']);
2131 if ($seen_create_table && $in_create_table_fields) {
2132 if ($upper_data == 'DEFAULT') {
2133 $seen_default = true;
2134 $create_table_fields[$current_identifier]['default_value'] = $arr[$i +
1]['data'];
2140 * @see @todo 2005-10-16 note: the "or" part here is a workaround for a bug
2142 if (($arr[$i]['type'] == 'alpha_columnType')
2143 ||
($arr[$i]['type'] == 'alpha_functionName' && $seen_create_table)
2145 $upper_data = strtoupper($arr[$i]['data']);
2146 if ($seen_create_table && $in_create_table_fields
2147 && isset($current_identifier)
2149 $create_table_fields[$current_identifier]['type'] = $upper_data;
2150 if ($upper_data == 'TIMESTAMP') {
2151 $arr[$i]['type'] = 'alpha_columnType';
2152 $in_timestamp_options = true;
2154 $in_timestamp_options = false;
2155 if ($upper_data == 'CHAR') {
2156 $arr[$i]['type'] = 'alpha_columnType';
2163 if ($arr[$i]['type'] == 'quote_backtick'
2164 ||
$arr[$i]['type'] == 'alpha_identifier'
2167 if ($arr[$i]['type'] == 'quote_backtick') {
2168 // remove backquotes
2169 $identifier = PMA_Util
::unQuote($arr[$i]['data']);
2171 $identifier = $arr[$i]['data'];
2174 if ($seen_create_table && $in_create_table_fields) {
2175 $current_identifier = $identifier;
2176 // we set this one even for non TIMESTAMP type
2177 $create_table_fields[$current_identifier]['timestamp_not_null'] = false;
2180 if ($seen_constraint) {
2181 $foreign[$foreign_key_number]['constraint'] = $identifier;
2184 if ($seen_foreign && $brackets_level > 0) {
2185 $foreign[$foreign_key_number]['index_list'][] = $identifier;
2188 if ($seen_references) {
2189 if ($seen_alter && $brackets_level > 0) {
2190 $foreign[$foreign_key_number]['ref_index_list'][] = $identifier;
2191 // here, the first bracket level corresponds to the
2192 // bracket of CREATE TABLE
2193 // so if we are on level 2, it must be the index list
2194 // of the foreign key REFERENCES
2195 } elseif ($brackets_level > 1) {
2196 $foreign[$foreign_key_number]['ref_index_list'][] = $identifier;
2197 } elseif ($arr[$i+
1]['type'] == 'punct_qualifier') {
2198 // identifier is `db`.`table`
2199 // the first pass will pick the db name
2200 // the next pass will pick the table name
2201 $foreign[$foreign_key_number]['ref_db_name'] = $identifier;
2203 // identifier is `table`
2204 $foreign[$foreign_key_number]['ref_table_name'] = $identifier;
2208 } // end for $i (loop #3)
2211 // Fill the $subresult array
2213 if (isset($create_table_fields)) {
2214 $subresult['create_table_fields'] = $create_table_fields;
2217 if (isset($foreign)) {
2218 $subresult['foreign_keys'] = $foreign;
2221 if (isset($select_expr_clause)) {
2222 $subresult['select_expr_clause'] = $select_expr_clause;
2224 if (isset($from_clause)) {
2225 $subresult['from_clause'] = $from_clause;
2227 if (isset($group_by_clause)) {
2228 $subresult['group_by_clause'] = $group_by_clause;
2230 if (isset($order_by_clause)) {
2231 $subresult['order_by_clause'] = $order_by_clause;
2233 if (isset($having_clause)) {
2234 $subresult['having_clause'] = $having_clause;
2236 if (isset($limit_clause)) {
2237 $subresult['limit_clause'] = $limit_clause;
2239 if (isset($where_clause)) {
2240 $subresult['where_clause'] = $where_clause;
2242 if (isset($unsorted_query) && !empty($unsorted_query)) {
2243 $subresult['unsorted_query'] = $unsorted_query;
2245 if (isset($where_clause_identifiers)) {
2246 $subresult['where_clause_identifiers'] = $where_clause_identifiers;
2249 if (isset($position_of_first_select)) {
2250 $subresult['position_of_first_select'] = $position_of_first_select;
2251 $subresult['section_before_limit'] = $section_before_limit;
2252 $subresult['section_after_limit'] = $section_after_limit;
2255 // They are naughty and didn't have a trailing semi-colon,
2256 // then still handle it properly
2257 if ($subresult['querytype'] != '') {
2258 $result[] = $subresult;
2261 } // end of the "PMA_SQP_analyze()" function
2265 * Formats SQL queries
2267 * @param array $arr The SQL queries
2268 * @param string $mode formatting mode
2269 * @param integer $start_token starting token
2270 * @param integer $number_of_tokens number of tokens to format, -1 = all
2272 * @return string The formatted SQL queries
2276 function PMA_SQP_format(
2277 $arr, $mode='text', $start_token=0,
2278 $number_of_tokens=-1
2280 //DEBUG echo 'in Format<pre>'; print_r($arr); echo '</pre>';
2281 // then check for an array
2282 if (! is_array($arr)) {
2283 return htmlspecialchars($arr);
2285 // first check for the SQL parser having hit an error
2286 if (PMA_SQP_isError()) {
2287 return htmlspecialchars($arr['raw']);
2289 // else do it properly
2293 $html_line_break = "\n";
2297 $html_line_break = '<br />';
2303 $infunction = false;
2304 $space_punct_listsep = ' ';
2305 $space_punct_listsep_function_name = ' ';
2306 // $space_alpha_reserved_word = '<br />'."\n";
2307 $space_alpha_reserved_word = ' ';
2309 $keywords_with_brackets_1before = array(
2316 $keywords_with_brackets_2before = array(
2328 // These reserved words do NOT get a newline placed near them.
2329 $keywords_no_newline = array(
2345 // These reserved words introduce a privilege list
2346 $keywords_priv_list = array(
2351 if ($number_of_tokens == -1) {
2352 $number_of_tokens = $arr['len'];
2355 if ($number_of_tokens >= 0) {
2359 $typearr[3] = $arr[$start_token]['type'];
2362 $in_priv_list = false;
2363 for ($i = $start_token; $i < $number_of_tokens; $i++
) {
2364 // DEBUG echo "Loop format <strong>" . $arr[$i]['data']
2365 // . "</strong> " . $arr[$i]['type'] . "<br />";
2368 // array_shift($typearr);
2375 if (($i +
1) < $number_of_tokens) {
2376 $typearr[4] = $arr[$i +
1]['type'];
2381 for ($j=0; $j<4; $j++
) {
2382 $typearr[$j] = $typearr[$j +
1];
2385 switch ($typearr[2]) {
2386 case 'alpha_bitfield_constant_introducer':
2390 case 'white_newline':
2393 case 'punct_bracket_open_round':
2395 $infunction = false;
2396 $keyword_brackets_2before = isset(
2397 $keywords_with_brackets_2before[strtoupper($arr[$i - 2]['data'])]
2399 $keyword_brackets_1before = isset(
2400 $keywords_with_brackets_1before[strtoupper($arr[$i - 1]['data'])]
2402 // Make sure this array is sorted!
2403 if (($typearr[1] == 'alpha_functionName')
2404 ||
($typearr[1] == 'alpha_columnType') ||
($typearr[1] == 'punct')
2405 ||
($typearr[3] == 'digit_integer') ||
($typearr[3] == 'digit_hex')
2406 ||
($typearr[3] == 'digit_float')
2407 ||
($typearr[0] == 'alpha_reservedWord' && $keyword_brackets_2before)
2408 ||
($typearr[1] == 'alpha_reservedWord' && $keyword_brackets_1before)
2415 if ($mode != 'query_only') {
2416 $after .= '<div class="syntax_indent' . $indent . '">';
2422 case 'alpha_identifier':
2423 if (($typearr[1] == 'punct_qualifier')
2424 ||
($typearr[3] == 'punct_qualifier')
2429 // for example SELECT 1 somealias
2430 if ($typearr[1] == 'digit_integer') {
2433 if (($typearr[3] == 'alpha_columnType')
2434 ||
($typearr[3] == 'alpha_identifier')
2440 case 'punct_qualifier':
2444 case 'punct_listsep':
2445 if ($infunction == true) {
2446 $after .= $space_punct_listsep_function_name;
2448 $after .= $space_punct_listsep;
2451 case 'punct_queryend':
2452 if (($typearr[3] != 'comment_mysql')
2453 && ($typearr[3] != 'comment_ansi')
2454 && $typearr[3] != 'comment_c'
2456 $after .= $html_line_break;
2457 $after .= $html_line_break;
2459 $space_punct_listsep = ' ';
2460 $space_punct_listsep_function_name = ' ';
2461 $space_alpha_reserved_word = ' ';
2462 $in_priv_list = false;
2464 case 'comment_mysql':
2465 case 'comment_ansi':
2466 $after .= $html_line_break;
2472 // select * from mytable limit 0,-1
2473 // (a side effect of this workaround is that
2478 if ($typearr[3] != 'digit_integer') {
2482 case 'punct_bracket_close_round':
2483 // only close bracket level when it was opened before
2484 if ($bracketlevel > 0) {
2486 if ($infunction == true) {
2492 $before .= ($mode != 'query_only' ?
'</div>' : ' ');
2494 $infunction = ($functionlevel > 0) ?
true : false;
2497 case 'alpha_columnType':
2498 if ($typearr[3] == 'alpha_columnAttrib') {
2501 if ($typearr[1] == 'alpha_columnType') {
2505 case 'alpha_columnAttrib':
2507 // ALTER TABLE tbl_name AUTO_INCREMENT = 1
2508 // COLLATE LATIN1_GENERAL_CI DEFAULT
2509 if ($typearr[1] == 'alpha_identifier'
2510 ||
$typearr[1] == 'alpha_charset'
2514 if (($typearr[3] == 'alpha_columnAttrib')
2515 ||
($typearr[3] == 'quote_single')
2516 ||
($typearr[3] == 'digit_integer')
2521 // AUTO_INCREMENT = 31DEFAULT_CHARSET = utf-8
2523 if ($typearr[2] == 'alpha_columnAttrib'
2524 && $typearr[3] == 'alpha_reservedWord'
2529 // select * from mysql.user where binary user="root"
2530 // binary is marked as alpha_columnAttrib
2531 // but should be marked as a reserved word
2532 if (strtoupper($arr[$i]['data']) == 'BINARY'
2533 && $typearr[3] == 'alpha_identifier'
2538 case 'alpha_functionName':
2540 case 'alpha_reservedWord':
2541 // do not uppercase the reserved word if we are calling
2542 // this function in query_only mode, because we need
2543 // the original query (otherwise we get problems with
2544 // semi-reserved words like "storage" which is legal
2545 // as an identifier name)
2547 if ($mode != 'query_only') {
2548 $arr[$i]['data'] = strtoupper($arr[$i]['data']);
2551 if ((($typearr[1] != 'alpha_reservedWord')
2552 ||
(($typearr[1] == 'alpha_reservedWord')
2553 && isset($keywords_no_newline[strtoupper($arr[$i - 1]['data'])])))
2554 && ($typearr[1] != 'punct_level_plus')
2555 && (!isset($keywords_no_newline[$arr[$i]['data']]))
2557 // do not put a space before the first token, because
2558 // we use a lot of pattern matching checking for the
2559 // first reserved word at beginning of query
2560 // so do not put a newline before
2562 // also we must not be inside a privilege list
2564 // the alpha_identifier exception is there to
2566 // GRANT SELECT ON mydb.mytable TO myuser@localhost
2567 // (else, we get mydb.mytableTO)
2569 // the quote_single exception is there to
2571 // GRANT ... TO 'marc'@'domain.com' IDENTIFIED...
2573 * @todo fix all cases and find why this happens
2577 ||
$typearr[1] == 'alpha_identifier'
2578 ||
$typearr[1] == 'quote_single'
2579 ||
$typearr[1] == 'white_newline'
2581 $before .= $space_alpha_reserved_word;
2584 // on first keyword, check if it introduces a
2586 if (isset($keywords_priv_list[$arr[$i]['data']])) {
2587 $in_priv_list = true;
2594 switch ($arr[$i]['data']) {
2603 if (!$in_priv_list) {
2604 $space_punct_listsep = $html_line_break;
2605 $space_alpha_reserved_word = ' ';
2621 if (!$in_priv_list) {
2622 $space_punct_listsep = $html_line_break;
2623 $space_alpha_reserved_word = ' ';
2631 if (!$in_priv_list) {
2632 $space_punct_listsep = $html_line_break;
2633 $space_alpha_reserved_word = ' ';
2638 if (!$in_priv_list) {
2639 $space_punct_listsep = $html_line_break;
2640 $space_alpha_reserved_word = $html_line_break;
2644 $space_punct_listsep = ' ';
2645 $space_alpha_reserved_word = $html_line_break;
2648 $space_punct_listsep = ' ';
2649 $space_alpha_reserved_word = $html_line_break;
2657 } // end switch ($arr[$i]['data'])
2661 case 'digit_integer':
2665 * @todo could there be other types preceding a digit?
2667 if ($typearr[1] == 'alpha_reservedWord') {
2670 if ($infunction && $typearr[3] == 'punct_bracket_close_round') {
2673 if ($typearr[1] == 'alpha_columnAttrib') {
2677 case 'alpha_variable':
2680 case 'quote_double':
2681 case 'quote_single':
2682 // workaround: for the query
2683 // REVOKE SELECT ON `base2\_db`.* FROM 'user'@'%'
2684 // the @ is incorrectly marked as alpha_variable
2685 // in the parser, and here, the '%' gets a blank before,
2686 // which is a syntax error
2687 if ($typearr[1] != 'punct_user'
2688 && $typearr[1] != 'alpha_bitfield_constant_introducer'
2692 if ($infunction && $typearr[3] == 'punct_bracket_close_round') {
2696 case 'quote_backtick':
2697 // here we check for punct_user to handle correctly
2698 // DEFINER = `username`@`%`
2699 // where @ is the punct_user and `%` is the quote_backtick
2700 if ($typearr[3] != 'punct_qualifier'
2701 && $typearr[3] != 'alpha_variable'
2702 && $typearr[3] != 'punct_user'
2706 if ($typearr[1] != 'punct_qualifier'
2707 && $typearr[1] != 'alpha_variable'
2708 && $typearr[1] != 'punct_user'
2715 } // end switch ($typearr[2])
2718 if ($typearr[3] != 'punct_qualifier') {
2724 if ($mode == 'text') {
2725 $str .= htmlspecialchars($arr[$i]['data']);
2727 $str .= $arr[$i]['data'];
2731 // close unclosed indent levels
2732 while ($indent > 0) {
2734 $str .= ($mode != 'query_only' ?
'</div>' : ' ');
2738 } // end of the "PMA_SQP_format()" function
2741 * Gets SQL queries with no format
2743 * @param array $arr The SQL queries list
2745 * @return string The SQL queries with no format
2749 function PMA_SQP_formatNone($arr)
2751 $formatted_sql = htmlspecialchars($arr['raw']);
2752 $formatted_sql = preg_replace(
2753 "@((\015\012)|(\015)|(\012)){3,}@",
2758 return $formatted_sql;
2759 } // end of the "PMA_SQP_formatNone()" function
2762 * Checks whether a given name is MySQL reserved word
2764 * @param string $column The word to be checked
2766 * @return boolean whether true or false
2768 function PMA_SQP_isKeyWord($column)
2770 global $PMA_SQPdata_forbidden_word;
2771 return in_array(strtoupper($column), $PMA_SQPdata_forbidden_word);
2776 * Get Parser Data Map from sqlparser.data.php
2778 * @return Array Parser Data Map from sqlparser.data.php
2780 function PMA_SQP_getParserDataMap()
2782 include 'libraries/sqlparser.data.php';
2784 'PMA_SQPdata_function_name' => $PMA_SQPdata_function_name,
2785 'PMA_SQPdata_column_attrib' => $PMA_SQPdata_column_attrib,
2786 'PMA_SQPdata_reserved_word' => $PMA_SQPdata_reserved_word,
2787 'PMA_SQPdata_forbidden_word' => $PMA_SQPdata_forbidden_word,
2788 'PMA_SQPdata_column_type' => $PMA_SQPdata_column_type,
2792 * Get Parser analyze Map from parse_analyze_inc.php
2794 * @param array $sql_query The SQL string
2795 * @param array $db Current DB
2797 * @return Array analyze Map from parse_analyze_inc.php
2799 function PMA_SQP_getParserAnalyzeMap($sql_query, $db)
2801 include 'libraries/parse_analyze.inc.php';
2802 return $analyzed_sql_results;