📄 sqlparser.lib.php
字号:
<?php/* $Id: sqlparser.lib.php,v 2.47 2006/01/17 17:02:30 cybot_tm Exp $ */// vim: expandtab sw=4 ts=4 sts=4:/** SQL Parser Functions for phpMyAdmin * * Copyright 2002 Robin Johnson <robbat2@users.sourceforge.net> * http://www.orbis-terrarum.net/?l=people.robbat2 * * These functions define an SQL parser system, capable of understanding and * extracting data from a MySQL type SQL query. * * The basic procedure for using the new SQL parser: * On any page that needs to extract data from a query or to pretty-print a * query, you need code like this up at the top: * * ($sql contains the query) * $parsed_sql = PMA_SQP_parse($sql); * * If you want to extract data from it then, you just need to run * $sql_info = PMA_SQP_analyze($parsed_sql); * * lem9: See comments in PMA_SQP_analyze for the returned info * from the analyzer. * * If you want a pretty-printed version of the query, do: * $string = PMA_SQP_formatHtml($parsed_sql); * (note that that you need to have syntax.css.php included somehow in your * page for it to work, I recommend '<link rel="stylesheet" type="text/css" * href="syntax.css.php" />' at the moment.) *//** * Minimum inclusion? (i.e. for the stylesheet builder) */if ( ! defined( 'PMA_MINIMUM_COMMON' ) ) { /** * Include the string library as we use it heavily */ require_once('./libraries/string.lib.php'); /** * Include data for the SQL Parser */ require_once('./libraries/sqlparser.data.php'); require_once('./libraries/mysql_charsets.lib.php'); if (!isset($mysql_charsets)) { $mysql_charsets = array(); $mysql_charsets_count = 0; $mysql_collations_flat = array(); $mysql_collations_count = 0; } if (!defined('DEBUG_TIMING')) { function PMA_SQP_arrayAdd(&$arr, $type, $data, &$arrsize) { $arr[] = array('type' => $type, 'data' => $data); $arrsize++; } // end of the "PMA_SQP_arrayAdd()" function } else { function PMA_SQP_arrayAdd(&$arr, $type, $data, &$arrsize) { global $timer; $t = $timer; $arr[] = array('type' => $type, 'data' => $data, 'time' => $t); $timer = microtime(); $arrsize++; } // end of the "PMA_SQP_arrayAdd()" function } // end if... else... /** * Reset the error variable for the SQL parser * * @access public */ // Added, Robbat2 - 13 Janurary 2003, 2:59PM function PMA_SQP_resetError() { global $SQP_errorString; $SQP_errorString = ''; unset($SQP_errorString); } /** * Get the contents of the error variable for the SQL parser * * @return string Error string from SQL parser * * @access public */ // Added, Robbat2 - 13 Janurary 2003, 2:59PM function PMA_SQP_getErrorString() { global $SQP_errorString; return isset($SQP_errorString) ? $SQP_errorString : ''; } /** * Check if the SQL parser hit an error * * @return boolean error state * * @access public */ // Added, Robbat2 - 13 Janurary 2003, 2:59PM function PMA_SQP_isError() { global $SQP_errorString; return isset($SQP_errorString) && !empty($SQP_errorString); } /** * Set an error message for the system * * @param string The error message * @param string The failing SQL query * * @access private * @scope SQL Parser internal */ // Revised, Robbat2 - 13 Janurary 2003, 2:59PM function PMA_SQP_throwError($message, $sql) { global $SQP_errorString; $SQP_errorString = '<p>'.$GLOBALS['strSQLParserUserError'] . '</p>' . "\n" . '<pre>' . "\n" . 'ERROR: ' . $message . "\n" . 'SQL: ' . htmlspecialchars($sql) . "\n" . '</pre>' . "\n"; } // end of the "PMA_SQP_throwError()" function /** * Do display the bug report * * @param string The error message * @param string The failing SQL query * * @access public */ function PMA_SQP_bug($message, $sql) { global $SQP_errorString; $debugstr = 'ERROR: ' . $message . "\n"; $debugstr .= 'CVS: $Id: sqlparser.lib.php,v 2.47 2006/01/17 17:02:30 cybot_tm Exp $' . "\n"; $debugstr .= 'MySQL: '.PMA_MYSQL_STR_VERSION . "\n"; $debugstr .= 'USR OS, AGENT, VER: ' . PMA_USR_OS . ' ' . PMA_USR_BROWSER_AGENT . ' ' . PMA_USR_BROWSER_VER . "\n"; $debugstr .= 'PMA: ' . PMA_VERSION . "\n"; $debugstr .= 'PHP VER,OS: ' . PMA_PHP_STR_VERSION . ' ' . PHP_OS . "\n"; $debugstr .= 'LANG: ' . $GLOBALS['lang'] . "\n"; $debugstr .= 'SQL: ' . htmlspecialchars($sql); $encodedstr = $debugstr; if (@function_exists('gzcompress')) { $encodedstr = gzcompress($debugstr, 9); } $encodedstr = preg_replace("/(\015\012)|(\015)|(\012)/", '<br />' . "\n", chunk_split(base64_encode($encodedstr))); $SQP_errorString .= $GLOBALS['strSQLParserBugMessage'] . '<br />' . "\n" . '----' . $GLOBALS['strBeginCut'] . '----' . '<br />' . "\n" . $encodedstr . "\n" . '----' . $GLOBALS['strEndCut'] . '----' . '<br />' . "\n"; $SQP_errorString .= '----' . $GLOBALS['strBeginRaw'] . '----<br />' . "\n" . '<pre>' . "\n" . $debugstr . '</pre>' . "\n" . '----' . $GLOBALS['strEndRaw'] . '----<br />' . "\n"; } // end of the "PMA_SQP_bug()" function /** * Parses the SQL queries * * @param string The SQL query list * * @return mixed Most of times, nothing... * * @global array The current PMA configuration * @global array MySQL column attributes * @global array MySQL reserved words * @global array MySQL column types * @global array MySQL function names * @global integer MySQL column attributes count * @global integer MySQL reserved words count * @global integer MySQL column types count * @global integer MySQL function names count * @global array List of available character sets * @global array List of available collations * @global integer Character sets count * @global integer Collations count * * @access public */ function PMA_SQP_parse($sql) { global $cfg; global $PMA_SQPdata_column_attrib, $PMA_SQPdata_reserved_word, $PMA_SQPdata_column_type, $PMA_SQPdata_function_name, $PMA_SQPdata_column_attrib_cnt, $PMA_SQPdata_reserved_word_cnt, $PMA_SQPdata_column_type_cnt, $PMA_SQPdata_function_name_cnt; global $mysql_charsets, $mysql_collations_flat, $mysql_charsets_count, $mysql_collations_count; global $PMA_SQPdata_forbidden_word, $PMA_SQPdata_forbidden_word_cnt; // rabus: Convert all line feeds to Unix style $sql = str_replace("\r\n", "\n", $sql); $sql = str_replace("\r", "\n", $sql); $len = PMA_strlen($sql); if ($len == 0) { return array(); } $sql_array = array(); $sql_array['raw'] = $sql; $count1 = 0; $count2 = 0; $punct_queryend = ';'; $punct_qualifier = '.'; $punct_listsep = ','; $punct_level_plus = '('; $punct_level_minus = ')'; $digit_floatdecimal = '.'; $digit_hexset = 'x'; $bracket_list = '()[]{}'; $allpunct_list = '-,;:!?/.^~\*&%+<=>|'; $allpunct_list_pair = array ( 0 => '!=', 1 => '&&', 2 => ':=', 3 => '<<', 4 => '<=', 5 => '<=>', 6 => '<>', 7 => '>=', 8 => '>>', 9 => '||' ); $allpunct_list_pair_size = 10; //count($allpunct_list_pair); $quote_list = '\'"`'; $arraysize = 0; while ($count2 < $len) { $c = PMA_substr($sql, $count2, 1); $count1 = $count2; if (($c == "\n")) { $count2++; PMA_SQP_arrayAdd($sql_array, 'white_newline', '', $arraysize); continue; } // Checks for white space if (PMA_STR_isSpace($c)) { $count2++; continue; } // Checks for comment lines. // MySQL style # // C style /* */ // ANSI style -- if (($c == '#') || (($count2 + 1 < $len) && ($c == '/') && (PMA_substr($sql, $count2 + 1, 1) == '*')) || (($count2 + 2 == $len) && ($c == '-') && (PMA_substr($sql, $count2 + 1, 1) == '-')) || (($count2 + 2 < $len) && ($c == '-') && (PMA_substr($sql, $count2 + 1, 1) == '-') && ((PMA_substr($sql, $count2 + 2, 1) <= ' ')))) { $count2++; $pos = 0; $type = 'bad'; switch ($c) { case '#': $type = 'mysql'; case '-': $type = 'ansi'; $pos = $GLOBALS['PMA_strpos']($sql, "\n", $count2); break; case '/': $type = 'c'; $pos = $GLOBALS['PMA_strpos']($sql, '*/', $count2); $pos += 2; break; default: break; } // end switch $count2 = ($pos < $count2) ? $len : $pos; $str = PMA_substr($sql, $count1, $count2 - $count1); PMA_SQP_arrayAdd($sql_array, 'comment_' . $type, $str, $arraysize); continue; } // end if // Checks for something inside quotation marks if (PMA_STR_strInStr($c, $quote_list)) { $startquotepos = $count2; $quotetype = $c; $count2++; $escaped = FALSE; $escaped_escaped = FALSE; $pos = $count2; $oldpos = 0; do { $oldpos = $pos; $pos = $GLOBALS['PMA_strpos'](' ' . $sql, $quotetype, $oldpos + 1) - 1; // ($pos === FALSE) if ($pos < 0) { $debugstr = $GLOBALS['strSQPBugUnclosedQuote'] . ' @ ' . $startquotepos. "\n" . 'STR: ' . htmlspecialchars($quotetype); PMA_SQP_throwError($debugstr, $sql); return $sql; } // If the quote is the first character, it can't be // escaped, so don't do the rest of the code if ($pos == 0) { break; } // Checks for MySQL escaping using a \ // And checks for ANSI escaping using the $quotetype character if (($pos < $len) && PMA_STR_charIsEscaped($sql, $pos)) { $pos ++; continue;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -