From f932ee45ed1d6c2d057071db7afbc2f6d7201608 Mon Sep 17 00:00:00 2001 From: Robin Johnson Date: Thu, 25 Jul 2002 05:02:54 +0000 Subject: [PATCH] libraries/sqlparser.lib.php3: - Cleaned up CSS creation functions - Formatting fixes libraries/sqlparser.data.php3: - Added 'ANALYSE' reserved word (note spelling) header.inc.php3: - Added CSS for SQL Parser libraries/common.lib.php3: - Change of PMA_format_sql to use new SQL Parser --- ChangeLog | 16 ++- header.inc.php3 | 20 ++++ libraries/common.lib.php3 | 178 +++++++++------------------------- libraries/sqlparser.data.php3 | 1 + libraries/sqlparser.lib.php3 | 74 +++++++++----- 5 files changed, 133 insertions(+), 156 deletions(-) diff --git a/ChangeLog b/ChangeLog index 67dc44ee7..05bafe86d 100755 --- a/ChangeLog +++ b/ChangeLog @@ -9,16 +9,26 @@ $Source$ * new lang: slovenian, thanks to Kositer Uros (urosh) 2002-07-24 Robin Johnson - * libraries/string.lib.php3: optimizations (thanks Loïc) - * libraries/sqlparser.lib.php3: SQL Parser merging (in progress) + * libraries/string.lib.php3: + - optimizations/PHP3 fixes (thanks Loïc) + * libraries/sqlparser.lib.php3: + - Merged - Spacing around if statements - Added require statements for parser - Moved to global constants for size of keyword arrays - Added CSS creation rules - * libraries/sqlparser.data.php3: Merged + - Cleaned up CSS creation functions + - Formatting fixes + * libraries/sqlparser.data.php3: + - Merged + - Added 'ANALYSE' reserved word (note spelling) * config.inc.php3: - Added SQL Parser variables - Remove old SQL Parser variables + * header.inc.php3: + - Added CSS for SQL Parser + * libraries/common.lib.php3: + - Change of PMA_format_sql to use new SQL Parser 2002-07-24 Loïc Chapeaux * libraries/string.lib.php3: Function documentation diff --git a/header.inc.php3 b/header.inc.php3 index 319b23574..8800cefc2 100755 --- a/header.inc.php3 +++ b/header.inc.php3 @@ -112,6 +112,26 @@ table.tabs { border-left: none; border-bottom: 1px solid #666; } + +.syntax { font-family: sans-serif; font-size: small; } +.syntax_comment { } +.syntax_digit { } +.syntax_digit_hex { } +.syntax_digit_integer { } +.syntax_digit_float { } +.syntax_punct { } +.syntax_alpha { text-transform: lowercase; } +.syntax_alpha_columnType { text-transform: uppercase; } +.syntax_alpha_columnAttrib { text-transform: uppercase; } +.syntax_alpha_reservedWord { text-transform: uppercase; font-weight: bold; } +.syntax_alpha_functionName { text-transform: uppercase; } +.syntax_alpha_identifier { } +.syntax_alpha_variable { } +.syntax_quote { } +.syntax_quote_backtick { } + //--> diff --git a/libraries/common.lib.php3 b/libraries/common.lib.php3 index 094c99bf4..4a58ed327 100644 --- a/libraries/common.lib.php3 +++ b/libraries/common.lib.php3 @@ -165,7 +165,17 @@ h1 {font-family: sans-serif; font-size: large; font-weight: bold} */ include('./libraries/defines.lib.php3'); + /** + * String handling + */ + include('./libraries/string.lib.php3'); + /** + * SQL Parser data and code + */ + include('./libraries/sqlparser.data.php3'); + include('./libraries/sqlparser.lib.php3'); + // If zlib output compression is set in the php configuration file, no // output buffering should be run if (PMA_PHP_INT_VERSION < 40000 @@ -230,7 +240,7 @@ h1 {font-family: sans-serif; font-size: large; font-weight: bold} /** * format sql strings * - * @param string sql + * @param struct pre-parsed SQL structure * * @return string the formatted sql * @@ -239,128 +249,38 @@ h1 {font-family: sans-serif; font-size: large; font-weight: bold} * * @access public * - * @author Mike Beck + * @author Robin Johnson */ - function PMA_format_sql ($sql) { - global $cfg, $mult; - -// lem9: bypass this function for now - return $sql; - - $sfuncs = '^' . implode('$|^', $cfg['Functions']) . '$'; - $skeyw = '^' . implode('$|^', $cfg['keywords']) . '$'; - $scoltype = '^' . implode('$|^', $cfg['ColumnTypes']) . '$'; - $add = '^' . implode('$|^', $cfg['additional']) . '$'; - - // First of all lets remove all newlines - we'll add our own later - $sql = str_replace("\n", ' ', $sql); - // There should always be blanks around = and after , () - // fixme - i would like to replace ';' with '; ' but then i need - // to know how to do that without getting ; within strings as well - $sql = str_replace('=', ' = ', $sql); - $sql = str_replace(',', ', ', $sql); - $sql = str_replace(')', ' ) ', $sql); - $sql = str_replace('(', ' ( ', $sql); - - // Now split everything by the blanks - $sql_parts = explode(' ', $sql); - // Start a loop over the parts check each word and put them back into - // $sql - unset($sql); - $s_nr = 0; - - while (list($num, $word) = each($sql_parts)) { - // We might have added to many blanks when checking for = and, - // which might lead to empty members in the array - if (strlen($word) == 0) { - continue; - } - $is_string = FALSE; - - // Anything inside quots might be more than one word - // so as we splitted by the blanks we have to try to get those - // parts back together - if ((substr($word, 0, 1) == '\'' || substr($word, 0, 1) == '"') - && !isset($temp)) { - // start of a string - $temp = $word; - $is_string = TRUE; - } else { - if (isset($temp)) { - // We are continuing a string - $temp .= $word; - $is_string = TRUE; - } - } // end if... else... - - if (substr($word, strlen($word) - 1, 1) == '\'' - || substr($word, strlen($word) - 1, 1) == '"') { - // End of a String - $word = '' . htmlspecialchars($temp) . ''; - unset($temp); - // Debug echo "fertig " . $word . '
'; - $is_string = FALSE; - } // end if - - if (!isset($is_string) || $is_string == FALSE) { - // No String - if (eregi($sfuncs, $word)) { - $word = '' . htmlspecialchars($word) . ''; - } else if (eregi($skeyw, $word)) { - $word = '' . htmlspecialchars($word) . ''; - if (!isset($mult) || $mult != TRUE) { - $word = "\n" . $word; - } - } else if (eregi($scoltype, $word)) { - $word = '' . htmlspecialchars($word) . ''; - } else if (eregi($add, $word)) { - $word = '' . htmlspecialchars($word) . ''; - } else if ($word == '(') { - if (isset($brack_o)) { - $skey = count($brack_o); - } else { - $skey = 0; - } - $brack_o[$skey] = $s_nr; - } else if ($word == ')') { - if (isset($brack_o)) { - unset($brack_o[count($brack_o) - 1]); - if (count($brack_o) == 0) { - unset($brack_o); - } - } else { - $brack_c[] = $s_nr; - } - } else if ($word == ';') { - $word = ';' . "\n"; - } + function PMA_format_sql ($ParsedSQL) + { + global $cfg; + + // Check that we actually have a valid set of parsed data + // well, not quite + if(!is_array($ParsedSQL)) { + // We don,t so just return the input directly + // This is intended to be used for when the SQL Parser is turned off + return $ParsedSQL; } - if (!isset($temp) || strlen($temp) == 0) { - $sql_p[$s_nr] = $word; - $s_nr++; - } - } // end while + $formattedSQL = ''; - if (isset($brack_o)) { - while (list($num, $elem) = each($brack_o)) { - $sql_p[$elem] = '' . $sql_p[$elem] . ''; - echo '
' . $GLOBALS['strMissingBracket'] . '
'; - } + switch($cfg['SQP']['fmtType']) { + case 'none': + $formattedSQL = PMA_SQP_FormatNone($ParsedSQL); + break; + case 'html': + $formattedSQL = PMA_SQP_FormatHTML($ParsedSQL); + break; + case 'text': + $formattedSQL = PMA_SQP_FormatText($ParsedSQL); + break; + default: + break; } - - if (isset($brack_c)) { - while (list($num, $elem) = each($brack_c)) { - $sql_p[$elem] = '' . $sql_p[$elem] . ''; - echo '
' . $GLOBALS['strMissingBracket'] . '
'; - } - } - - $sql = implode(' ', $_sql_p); - $sql = ereg_replace("((\015\012)|(\015)|(\012))+", '
', $sql); - $sql = ereg_replace('
[ ]*
', '
', $sql); - - return $sql; + + return $formattedSQL; + } // end of the "PMA_format_sql()" function @@ -394,13 +314,13 @@ h1 {font-family: sans-serif; font-size: large; font-weight: bold} $the_query = $GLOBALS['sql_query']; } + $ParsedSQL = PMA_SQP_Parse($the_query); + echo '

'. $GLOBALS['strError'] . '

' . "\n"; // if the config password is wrong, or the MySQL server does not // respond, do not show the query that would reveal the // username/password if (!empty($the_query) && !strstr($the_query, 'connect')) { - $query_base = htmlspecialchars($the_query); - $query_base = ereg_replace("((\015\012)|(\015)|(\012)){3,}", "\n\n", $query_base); echo '

' . "\n"; echo ' ' . $GLOBALS['strSQLQuery'] . ' : ' . "\n"; if ($is_modify_link) { @@ -410,7 +330,7 @@ h1 {font-family: sans-serif; font-size: large; font-weight: bold} } // end if echo '

' . "\n" . '

' . "\n" - . ' ' . ($cfg['UseSyntaxColoring'] ? PMA_format_sql($query_base) : $query_base) . "\n" + . ' ' . PMA_format_sql($ParsedSQL) . "\n" . '

' . "\n"; } // end if if (!empty($error_message)) { @@ -1192,9 +1112,9 @@ if (typeof(document.getElementById) != 'undefined' $sqlnr = 1; if (!empty($GLOBALS['show_as_php'])) { $new_line = '";
' . "\n" . ' $sql .= "'; - } else if ($cfg['UseSyntaxColoring'] == FALSE) { + } /* else if ($cfg['UseSyntaxColoring'] == FALSE) { $new_line = '
' . "\n"; - } + } */ if (isset($new_line)) { $query_base = htmlspecialchars($GLOBALS['sql_query']); $query_base = ereg_replace("((\015\012)|(\015)|(\012))+", $new_line, $query_base); @@ -1203,8 +1123,10 @@ if (typeof(document.getElementById) != 'undefined' } if (!empty($GLOBALS['show_as_php'])) { $query_base = '$sql = "' . $query_base; - } else if ($cfg['UseSyntaxColoring']) { - $query_base = PMA_format_sql($query_base); +// } else d$if ($cfg['UseSyntaxColoring']) { + } else { + $ParsedSQL = PMA_SQP_Parse($query_base); + $query_base = PMA_format_sql($ParsedSQL); } // Prepares links that may be displayed to edit/explain the query @@ -1258,11 +1180,7 @@ if (typeof(document.getElementById) != 'undefined' // If a 'LIMIT' clause has been programatically added to the query // displays it if (!empty($GLOBALS['sql_limit_to_append'])) { - if($cfg['UseSyntaxColoring']) { - echo PMA_format_sql($GLOBALS['sql_limit_to_append']); - } else { - echo $GLOBALS['sql_limit_to_append']; - } + echo PMA_format_sql(PMA_SQP_Parse($GLOBALS['sql_limit_to_append'])); } if (!empty($GLOBALS['show_as_php'])) { echo '";'; diff --git a/libraries/sqlparser.data.php3 b/libraries/sqlparser.data.php3 index 89441b468..f3287503b 100644 --- a/libraries/sqlparser.data.php3 +++ b/libraries/sqlparser.data.php3 @@ -183,6 +183,7 @@ if (!defined('PMA_SQP_DATA_INCLUDED')) { 'AGGREGATE', 'ALL', 'ALTER', + 'ANALYSE', 'ANALYZE', 'AND', 'AS', diff --git a/libraries/sqlparser.lib.php3 b/libraries/sqlparser.lib.php3 index e188d3941..4d086664a 100644 --- a/libraries/sqlparser.lib.php3 +++ b/libraries/sqlparser.lib.php3 @@ -32,8 +32,16 @@ if (!defined('PMA_SQP_LIB_INCLUDED')) { define('PMA_SQP_LIB_INCLUDED', 1); - require('./libraries/string.lib.php3'); - require('./libraries/sqlparser.data.php3'); + + /** + * Include the string libarry as we use it heavily + */ + include('./libraries/string.lib.php3'); + + /** + * Include data for the SQL Parser + */ + include('./libraries/sqlparser.data.php3'); if (!defined('DEBUGTIMING')) { function PMA_SQP_ArrayAdd(&$arr,$type,$data, &$arrsize) @@ -53,11 +61,21 @@ if (!defined('PMA_SQP_LIB_INCLUDED')) { } function PMA_SQP_Parse($sql) - { + { + global $cfg; + + // if the SQL parser is disabled + // just return the original query string + if($cfg['SQP']['enable'] == FALSE) { + echo 'FALSE'; + return $sql; + } + $len = strlen($sql); if ($len == 0) { return array(); } + $sql_array = array(); $sql_array['raw'] = $sql; $count1 = 0; @@ -369,6 +387,7 @@ if (!defined('PMA_SQP_LIB_INCLUDED')) { if ($arraysize > 0) { $t_next = $sql_array[0]['type']; $t_prev = NULL; + $t_cur = NULL; } for($i = 0; $i < $arraysize; $i++) { @@ -520,6 +539,7 @@ if (!defined('PMA_SQP_LIB_INCLUDED')) { 'INTO', 'KEY', 'PRIMARY', + 'PROCEDURE', 'REFERENCES', 'UNIQUE' ); @@ -658,6 +678,8 @@ if (!defined('PMA_SQP_LIB_INCLUDED')) { case 'DROP': case 'UPDATE': case 'TRUNCATE': + case 'ANALYZE': + case 'ANALYSE': $space_punct_listsep = '
'; $space_alpha_reservedWord = ' '; break; @@ -694,6 +716,14 @@ if (!defined('PMA_SQP_LIB_INCLUDED')) { $after .= ' '; } break; + case 'quote_backtick': + if ($typearr[3] != 'punct_qualifier') { + $after .= ' '; + } + if ($typearr[1] != 'punct_qualifier') { + $before .= ' '; + } + break; default: break; } @@ -716,32 +746,30 @@ if (!defined('PMA_SQP_LIB_INCLUDED')) { function PMA_SQP_BuildCssData() { global $cfg; - $css_string = << $col) { + $css_string = ''; + foreach($cfg['SQP']['fmtColor'] as $key => $col) { $css_string .= PMA_SQP_BuildCssRule('syntax_'.$key, 'color', $col); } for($i = 0; $i < 8; $i++) { - $css_string .= PMA_SQP_BuildCssRule('syntax_indent'.$i, 'margin-left', ($i * $cfg['SQP']['fmt']['indent']).$cfg['SQP']['fmt']['indentunits']); + $css_string .= PMA_SQP_BuildCssRule('syntax_indent'.$i, 'margin-left', ($i * $cfg['SQP']['fmtInd']).$cfg['SQP']['fmtIndUnit']); } return $css_string; } + + function PMA_SQP_FormatNone($arr) + { + $formattedSQL = htmlspecialchars($arr['raw']); + $formattedSQL = ereg_replace("((\015\012)|(\015)|(\012)){3,}", "\n\n", $formattedSQL); + return $formattedSQL; + } + + function PMA_SQP_FormatText($arr) + { + /** + * TODO WRITE THIS! + */ + return PMA_SQP_FormatNone($arr); + } } // $__PMA_SQP_LIB__