From db0e689d4c507fc36171c9862fa928a607a42233 Mon Sep 17 00:00:00 2001
From: Mike Beck
Date: Wed, 12 Jun 2002 11:59:35 +0000
Subject: [PATCH] enabled Syntaxcoloring for SQL Statements. This can be turned
off in config.inc.php3
---
.cvsignore | 1 +
config.inc.php3 | 63 ++++++++++++++++++++++++-
libraries/common.lib.php3 | 98 +++++++++++++++++++++++++++++++++++++--
read_dump.php3 | 2 +-
sql.php3 | 2 +-
5 files changed, 158 insertions(+), 8 deletions(-)
diff --git a/.cvsignore b/.cvsignore
index c221041d0..583b5f841 100644
--- a/.cvsignore
+++ b/.cvsignore
@@ -1,2 +1,3 @@
ChangeLog_till_2.2.6.tar.gz
config.inc.developer.php3
+phpmyadmin.wpj
\ No newline at end of file
diff --git a/config.inc.php3 b/config.inc.php3
index 64dd01f2a..41727dd60 100755
--- a/config.inc.php3
+++ b/config.inc.php3
@@ -226,6 +226,14 @@ $cfg['ModifyDeleteAtRight'] = FALSE; // show edit/delete links on right s
$cfg['DefaultDisplay'] = 'horizontal'; // default display direction (horizontal|vertical)
$cfg['RepeatCells'] = 100; // repeat header names every X cells? (0 = deactivate)
+$cfg['UseSyntaxColoring'] = TRUE; // use syntaxcoloring on output of SQL, might be a little slower
+// Colors used for Syntaxcoloring of SQL Statements
+$cfg['colorFunctions'] = 'red';
+$cfg['colorKeywords'] = 'blue';
+$cfg['colorStrings'] = 'green';
+$cfg['colorColType'] = '#FF9900';
+$cfg['colorAdd'] = '#9999CC';
+
/**
* MySQL settings
@@ -294,11 +302,62 @@ if ($cfg['ShowFunctionFields']) {
'TO_DAYS',
'UNIX_TIMESTAMP',
'USER',
- 'WEEKDAY'
+ 'WEEKDAY',
+ 'CONCAT'
);
} // end if
-
+if($cfg['UseSyntaxColoring']) {
+ $cfg['keywords']=array(
+ 'SELECT',
+ 'INSERT',
+ 'LEFT',
+ 'UPDATE',
+ 'REPLACE',
+ 'EXPLAIN',
+ 'FROM',
+ 'WHERE',
+ 'LIMIT',
+ 'INTO',
+ 'ALTER',
+ 'ADD',
+ 'DROP',
+ 'GROUP',
+ 'ORDER',
+ 'CHANGE',
+ 'CREATE',
+ 'DELETE'
+ );
+} // end if
+if($cfg['UseSyntaxColoring']) {
+ $cfg['additional']=array(
+ 'TABLE',
+ 'DEFAULT',
+ 'NULL',
+ 'NOT',
+ 'INDEX',
+ 'PRIMARY',
+ 'KEY',
+ 'UNIQUE',
+ 'BINARY',
+ 'UNSIGNED',
+ 'ZEROFILL',
+ 'AUTO_INCREMENT',
+ 'AND',
+ 'OR',
+ 'DISTINCT',
+ 'DISTINCTROW',
+ 'BY',
+ 'ON',
+ 'JOIN',
+ 'BETWEEN',
+ 'BETWEEN',
+ 'IN',
+ 'IF',
+ 'ELSE',
+ 'SET'
+ );
+}
/**
* Unset magic_quotes_runtime - do not change!
*/
diff --git a/libraries/common.lib.php3 b/libraries/common.lib.php3
index 687612718..c24215b1b 100644
--- a/libraries/common.lib.php3
+++ b/libraries/common.lib.php3
@@ -34,6 +34,8 @@ if (!defined('PMA_COMMON_LIB_INCLUDED')){
* the PMA_mysqlDie() function must be before the connection to db but after
* mysql extension has been loaded
*
+ * the PMA_mysqlDie() function needs the PMA_format_sql() Function
+ *
* ... so the required order is:
*
* - parsing of the configuration file
@@ -41,6 +43,7 @@ if (!defined('PMA_COMMON_LIB_INCLUDED')){
* MySQL release number)
* - load of mysql extension (if necessary)
* - definition of PMA_sqlAddslashes()
+ * - definition of PMA_format_sql()
* - definition of PMA_mysqlDie()
* - definition of PMA_isInto()
* - definition of PMA_setFontSizes()
@@ -189,6 +192,84 @@ h1 {font-family: sans-serif; font-size: large; font-weight: bold}
return $a_string;
} // end of the 'PMA_sqlAddslashes()' function
+ /**
+ * format sql strings
+ *
+ * @param string sql
+ *
+ * @return string the formatted sql
+ *
+ * @access public
+ *
+ * @author Mike Beck
+ */
+ function PMA_format_sql ($sql) {
+ global $cfg;
+
+ $_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 , ()
+ $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
+ $sql = '';
+ 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;}
+ // 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) == '"') {
+ // start of a string
+ $_temp = $_word;
+ } else {
+ if(isset($_temp) && strlen($_temp)>0){
+ // we are continuing a string
+ $_temp .= $_word;
+ }
+ }
+ if(substr($_word, strlen($_word)-1, 1) == '\''
+ || substr($_word, strlen($_word)-1, 1) == '"') {
+ // End of a String
+ $_word = '' . $_temp . '';
+ $_temp = '';
+ } else {
+ // no String
+ if(eregi($_sfuncs, $_word)) {
+ $_word = '' . $_word . '';
+ } else {
+ if(eregi($_skeyw, $_word)) {
+ $_word = "\n".'' . $_word . '';
+ } else {
+ if(eregi($_scoltype, $_word)) {
+ $_word = '' . $_word . '';
+ } else {
+ if(eregi($_add, $_word)) {
+ $_word = '' . $_word . '';
+ }
+ }
+ }
+ }
+ }
+ if(!isset($_temp) || strlen($_temp) == 0) {
+ if($_num != 0 && $_word != '(') {
+ $sql .= ' ';
+ }
+ $sql .= $_word;
+ }
+ } // End while
+ return $sql;
+ } // End of PMA_format_sql function
/**
* Displays a MySQL error message in the right frame.
@@ -203,7 +284,7 @@ h1 {font-family: sans-serif; font-size: large; font-weight: bold}
function PMA_mysqlDie($error_message = '', $the_query = '',
$is_modify_link = TRUE, $back_url = '')
{
-
+ global $cfg;
if (empty($GLOBALS['is_header_sent'])) {
// rabus: If we include header.inc.php3 here, we get a huge set of
// "Undefined variable" errors (see bug #549570)!
@@ -231,7 +312,11 @@ h1 {font-family: sans-serif; font-size: large; font-weight: bold}
. '' . $GLOBALS['strEdit'] . ''
. ']' . "\n";
} // end if
- echo '' . "\n" . $query_base . "\n" . '
' . "\n";
+ if($cfg['UseSyntaxColoring']){
+ echo '' . "\n" . PMA_format_sql($query_base) . "\n" . '
' . "\n";
+ } else {
+ echo '' . "\n" . $query_base . "\n" . '
' . "\n";
+ }
echo '
' . "\n";
} // end if
if (!empty($error_message)) {
@@ -902,6 +987,7 @@ h1 {font-family: sans-serif; font-size: large; font-weight: bold}
*/
function PMA_showMessage($message)
{
+ global $cfg;
// Reloads the navigation frame via JavaScript if required
if (isset($GLOBALS['reload']) && $GLOBALS['reload']) {
echo "\n";
@@ -975,13 +1061,17 @@ if (typeof(document.getElementById) != 'undefined'
$sqlnr = 1;
if (!empty($GLOBALS['show_as_php'])) {
$new_line = '";
' . "\n" . ' $sql .= "';
- } else {
- $new_line = '
' . "\n" . ' ';
+ }else{
+ $new_line = "\n";
}
$query_base = htmlspecialchars($GLOBALS['sql_query']);
$query_base = ereg_replace("((\015\012)|(\015)|(\012))+", $new_line, $query_base);
if (!empty($GLOBALS['show_as_php'])) {
$query_base = '$sql = "' . $query_base;
+ } else {
+ if($cfg['UseSyntaxColoring']) {
+ $query_base = PMA_format_sql($query_base);
+ }
}
// Prepares links that may be displayed to edit/explain the query
diff --git a/read_dump.php3 b/read_dump.php3
index b40dab813..7e1604e75 100644
--- a/read_dump.php3
+++ b/read_dump.php3
@@ -353,7 +353,7 @@ if ($sql_query != '') {
$a_sql_query = $pieces[$i];
$result = mysql_query($a_sql_query);
if ($result == FALSE) { // readdump failed
- $my_die = $a_sql_query;
+ $my_die = PMA_format_sql($a_sql_query);
break;
}
if (!isset($reload) && eregi('^(DROP|CREATE)[[:space:]]+(IF EXISTS[[:space:]]+)?(TABLE|DATABASE)[[:space:]]+(.+)', $a_sql_query)) {
diff --git a/sql.php3 b/sql.php3
index 9f4576bcd..d8916326a 100755
--- a/sql.php3
+++ b/sql.php3
@@ -508,7 +508,7 @@ else {
// Do print the page if required
if (isset($printview) && $printview == '1') {
- echo "\n";
+ echo "\n";
?>