cvs: pear /SQL_Parser Compiler.php Dialect_ANSI.php Dialect_MySQL.php Lexer.php Parser.php ctype.php package.xml /SQL_Parser/Parser Compiler.php
Lexer.php ctype.php /SQL_Parser/Parser/Dialect ANSI.php MySQL.php
| From: | Sebastian Mendel | Date: | Fri, 06 Jul 2007 13:44:33 +0000 |
| Subject: | cvs: pear /SQL_Parser Compiler.php Dialect_ANSI.php Dialect_MySQL.php Lexer.php Parser.php ctype.php package.xml /SQL_Parser/Parser Compiler.php Lexer.php ctype.php /SQL_Parser/Parser/Dialect ANSI.php MySQL.php |
||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-47886@lists.php.net to get a copy of this message | ||
cybot Fri Jul 6 13:44:33 2007 UTC
Added files:
/pear/SQL_Parser/Parser Compiler.php Lexer.php ctype.php
/pear/SQL_Parser/Parser/Dialect ANSI.php MySQL.php
Removed files:
/pear/SQL_Parser Compiler.php Dialect_ANSI.php Dialect_MySQL.php
Lexer.php ctype.php
Modified files:
/pear/SQL_Parser Parser.php package.xml
Log:
fixed directory structure
http://cvs.php.net/viewvc.cgi/pear/SQL_Parser/Parser.php?r1=1.41&r2=1.42&diff_format=u Index: pear/SQL_Parser/Parser.php diff -u pear/SQL_Parser/Parser.php:1.41 pear/SQL_Parser/Parser.php:1.42 --- pear/SQL_Parser/Parser.php:1.41 Fri Jul 6 10:44:42 2007 +++ pear/SQL_Parser/Parser.php Fri Jul 6 13:44:33 2007 @@ -25,7 +25,7 @@ // | John Griffin <jgriffin316@netscape.net> | // +----------------------------------------------------------------------+ // -// $Id: Parser.php,v 1.41 2007/07/06 10:44:42 cybot Exp $ +// $Id: Parser.php,v 1.42 2007/07/06 13:44:33 cybot Exp $ // // The plan for moving forward: // - Refactor sentinel conditions to show flow @@ -35,8 +35,8 @@ // outputting the same parse tree require_once 'PEAR.php'; -require_once 'SQL/Lexer.php'; -require_once 'Expressions/include.php'; +require_once 'SQL/Parser/Lexer.php'; +require_once 'SQL/Expressions/include.php'; /** * A sql parser @@ -70,7 +70,7 @@ if (is_string($string)) { // Initialize the Lexer with a 3-level look-back buffer - $this->lexer = new Lexer($string, 3, $this->lexeropts); + $this->lexer = new SQL_Parser_Lexer($string, 3, $this->lexeropts); $this->lexer->symbols =& $this->symbols; } } @@ -83,7 +83,7 @@ return $this->raiseError('Unknown SQL dialect:'.$dialect); } - include 'SQL/Dialect_'.$dialect.'.php'; + include 'SQL/Parser/Dialect/' . $dialect . '.php'; $this->types = array_flip($dialect['types']); $this->functions = array_flip($dialect['functions']); $this->operators = array_flip($dialect['operators']); @@ -1269,7 +1269,7 @@ { if (is_string($string)) { // Initialize the Lexer with a 3-level look-back buffer - $this->lexer = new Lexer($string, 3, $this->lexeropts); + $this->lexer = new SQL_Parser_Lexer($string, 3, $this->lexeropts); $this->lexer->symbols =& $this->symbols; } else { if (!is_object($this->lexer)) { http://cvs.php.net/viewvc.cgi/pear/SQL_Parser/package.xml?r1=1.15&r2=1.16&diff_format=u Index: pear/SQL_Parser/package.xml diff -u pear/SQL_Parser/package.xml:1.15 pear/SQL_Parser/package.xml:1.16 --- pear/SQL_Parser/package.xml:1.15 Fri Jul 6 08:58:17 2007 +++ pear/SQL_Parser/package.xml Fri Jul 6 13:44:33 2007 @@ -60,11 +60,15 @@ <file baseinstalldir="/SQL" name="test_parser.php" role="test" /> <file baseinstalldir="/SQL" name="update.php" role="test" /> </dir> <!-- //tests --> - <file baseinstalldir="/SQL" name="Compiler.php" role="php" /> - <file baseinstalldir="/SQL" name="ctype.php" role="php" /> - <file baseinstalldir="/SQL" name="Dialect_ANSI.php" role="php" /> - <file baseinstalldir="/SQL" name="Dialect_MySQL.php" role="php" /> - <file baseinstalldir="/SQL" name="Lexer.php" role="php" /> + <dir name="Parser"> + <dir name="Dialect"> + <file baseinstalldir="/SQL" name="ANSI.php" role="php" /> + <file baseinstalldir="/SQL" name="MySQL.php" role="php" /> + </dir> + <file baseinstalldir="/SQL" name="Compiler.php" role="php" /> + <file baseinstalldir="/SQL" name="ctype.php" role="php" /> + <file baseinstalldir="/SQL" name="Lexer.php" role="php" /> + </dir> <file baseinstalldir="/SQL" name="Parser.php" role="php" /> </dir> <!-- / --> </contents> http://cvs.php.net/viewvc.cgi/pear/SQL_Parser/Parser/Compiler.php?view=markup&rev=1.1 Index: pear/SQL_Parser/Parser/Compiler.php +++ pear/SQL_Parser/Parser/Compiler.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | Copyright (c) 2003-2004 John Griffin | // +----------------------------------------------------------------------+ // | This library is free software; you can redistribute it and/or | // | modify it under the terms of the GNU Lesser General Public | // | License as published by the Free Software Foundation; either | // | version 2.1 of the License, or (at your option) any later version. | // | | // | This library is distributed in the hope that it will be useful, | // | but WITHOUT ANY WARRANTY; without even the implied warranty of | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | // | Lesser General Public License for more details. | // | | // | You should have received a copy of the GNU Lesser General Public | // | License along with this library; if not, write to the Free Software | // | Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA| // +----------------------------------------------------------------------+ // | Authors: John Griffin <jgriffin316@netscape.net> | // +----------------------------------------------------------------------+ // // $Id: Compiler.php,v 1.1 2007/07/06 13:44:33 cybot Exp $ // require_once 'PEAR.php'; /** * A SQL parse tree compiler. * * @author John Griffin <jgriffin316@netscape.net> * @version 0.1 * @access public * @package SQL_Parser */ class SQL_Parser_Compiler { var $tree; // {{{ function SQL_Parser_Compiler($array = null) function SQL_Parser_Compiler($array = null) { $this->tree = $array; } // }}} // {{{ function getWhereValue ($arg) function getWhereValue ($arg) { switch ($arg['type']) { case 'ident': case 'real_val': case 'int_val': case 'null': $value = $arg['value']; break; case 'text_val': $value = '\''.$arg['value'].'\''; break; case 'subclause': $value = '('.$this->compileSearchClause($arg['value']).')'; break; default: return PEAR::raiseError('Unknown type: '.$arg['type']); } return $value; } // }}} // {{{ function getParams($arg) function getParams($arg) { $types = count($arg['type']); for ($i = 0; $i < $types; $i++) { switch ($arg['type'][$i]) { case 'ident': case 'real_val': case 'int_val': case 'null': $value[] = $arg['value'][$i]; break; case 'text_val': $value[] = '\''.$arg['value'][$i].'\''; break; default: return PEAR::raiseError('Unknown type: '.$arg['type'][$i]); } } $value ='('.implode(', ', $value).')'; return $value; } // }}} // {{{ function compileFunctionOpts($arg) function compileFunctionOpts($arg) { $types = count($arg['type']); for ($i = 0; $i < $types; $i++) { switch ($arg['type'][$i]) { case 'ident': case 'real_val': case 'int_val': case 'null': $value[] = $arg['arg'][$i]; break; case 'text_val': $value[] = '\''.$arg['arg'][$i].'\''; break; default: return PEAR::raiseError('Unknown type: '.$arg['type'][$i]); } } $value = implode(', ', $value); return $value; } // }}} // {{{ function compileSearchClause function compileSearchClause($where_clause) { $value = ''; if (isset ($where_clause['arg_1']['value'])) { $value = $this->getWhereValue ($where_clause['arg_1']); if (PEAR::isError($value)) { return $value; } $sql = $value; } else { $value = $this->compileSearchClause($where_clause['arg_1']); if (PEAR::isError($value)) { return $value; } $sql = $value; } if (isset ($where_clause['op'])) { if ($where_clause['op'] == 'in') { $value = $this->getParams($where_clause['arg_2']); if (PEAR::isError($value)) { return $value; } if (isset($where_clause['neg'])) { $sql .= ' not'; } $sql .= ' '.$where_clause['op'].' '.$value; } elseif ($where_clause['op'] == 'is') { $value = isset ($where_clause['neg']) ? 'not null' : 'null'; $sql .= ' is '.$value; } else { $sql .= ' '.$where_clause['op'].' '; if (isset ($where_clause['arg_2']['value'])) { $value = $this->getWhereValue ($where_clause['arg_2']); if (PEAR::isError($value)) { return $value; } $sql .= $value; } else { $value = $this->compileSearchClause($where_clause['arg_2']); if (PEAR::isError($value)) { return $value; } $sql .= $value; } } } return $sql; } // }}} // {{{ function compileSelect() function compileSelect() { // save the command and set quantifiers $sql = 'select '; if (isset($this->tree['set_quantifier'])) { $sql .= $this->tree['set_quantifier'].' '; } // save the column names and set functions $cols = count($this->tree['column_names']); for ($i = 0; $i < $cols; $i++) { $column = $this->tree['column_names'][$i]; if ($this->tree['column_aliases'][$i] != '') { $column .= ' as '.$this->tree['column_aliases'][$i]; } $column_names[] = $column; } $funcs = count($this->tree['set_function']); for ($i = 0; $i < $funcs; $i++) { $column = $this->tree['set_function'][$i]['name'].'('; if (isset ($this->tree['set_function'][$i]['distinct'])) { $column .= 'distinct '; } if (isset ($this->tree['set_function'][$i]['arg'])) { $column .= $this->compileFunctionOpts($this->tree['set_function'][$i]); } $column .= ')'; if ($this->tree['set_function'][$i]['alias'] != '') { $column .= ' as '.$this->tree['set_function'][$i]['alias']; } $column_names[] = $column; } if (isset($column_names)) { $sql .= implode (", ", $column_names); } // save the tables $sql .= ' from '; $c_tables = count($this->tree['table_names']); for ($i = 0; $i < $c_tables; $i++) { $sql .= $this->tree['table_names'][$i]; if ($this->tree['table_aliases'][$i] != '') { $sql .= ' as '.$this->tree['table_aliases'][$i]; } if ($this->tree['table_join_clause'][$i] != '') { $search_string = $this->compileSearchClause ($this->tree['table_join_clause'][$i]); if (PEAR::isError($search_string)) { return $search_string; } $sql .= ' on '.$search_string; } if (isset($this->tree['table_join'][$i])) { $sql .= ' '.$this->tree['table_join'][$i].' '; } } // save the where clause if (isset($this->tree['where_clause'])) { $search_string = $this->compileSearchClause ($this->tree['where_clause']); if (PEAR::isError($search_string)) { return $search_string; } $sql .= ' where '.$search_string; } // save the group by clause if (isset ($this->tree['group_by'])) { $sql .= ' group by '.implode(', ', $this->tree['group_by']); } // save the order by clause if (isset ($this->tree['sort_order'])) { foreach ($this->tree['sort_order'] as $key => $value) { $sort_order[] = $key.' '.$value; } $sql .= ' order by '.implode(', ', $sort_order); } // save the limit clause if (isset ($this->tree['limit_clause'])) { $sql .= ' limit '.$this->tree['limit_clause']['start'].','.$this->tree['limit_clause']['length']; } return $sql; } // }}} // {{{ function compileUpdate() function compileUpdate() { $sql = 'update '.implode(', ', $this->tree['table_names']); // save the set clause $cols = count($this->tree['column_names']); for ($i = 0; $i < $cols; $i++) { $set_columns[] = $this->tree['column_names'][$i].' = '.$this->getWhereValue($this->tree['values'][$i]); } $sql .= ' set '.implode (', ', $set_columns); // save the where clause if (isset($this->tree['where_clause'])) { $search_string = $this->compileSearchClause ($this->tree['where_clause']); if (PEAR::isError($search_string)) { return $search_string; } $sql .= ' where '.$search_string; } return $sql; } // }}} // {{{ function compileDelete() function compileDelete() { $sql = 'delete from '.implode(', ', $this->tree['table_names']); // save the where clause if (isset($this->tree['where_clause'])) { $search_string = $this->compileSearchClause ($this->tree['where_clause']); if (PEAR::isError($search_string)) { return $search_string; } $sql .= ' where '.$search_string; } return $sql; } // }}} // {{{ function compileInsert() function compileInsert() { $sql = 'insert into '.$this->tree['table_names'][0].' ('. implode(', ', $this->tree['column_names']).') values ('; $c_vals = count($this->tree['values']); for ($i = 0; $i < $c_vals; $i++) { $value = $this->getWhereValue ($this->tree['values'][$i]); if (PEAR::isError($value)) { return $value; } $value_array[] = $value; } $sql .= implode(', ', $value_array).')'; return $sql; } // }}} // {{{ function compile($array = null) function compile($array = null) { $this->tree = $array; switch ($this->tree['command']) { case 'select': return $this->compileSelect(); break; case 'update': return $this->compileUpdate(); break; case 'delete': return $this->compileDelete(); break; case 'insert': return $this->compileInsert(); break; case 'create': case 'drop': case 'alter': default: return PEAR::raiseError('Unknown action: '.$this->tree['command']); } // switch ($this->_tree["command"]) } // }}} } http://cvs.php.net/viewvc.cgi/pear/SQL_Parser/Parser/Lexer.php?view=markup&rev=1.1 Index: pear/SQL_Parser/Parser/Lexer.php +++ pear/SQL_Parser/Parser/Lexer.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | Copyright (c) 2002-2004 Brent Cook | // +----------------------------------------------------------------------+ // | This library is free software; you can redistribute it and/or | // | modify it under the terms of the GNU Lesser General Public | // | License as published by the Free Software Foundation; either | // | version 2.1 of the License, or (at your option) any later version. | // | | // | This library is distributed in the hope that it will be useful, | // | but WITHOUT ANY WARRANTY; without even the implied warranty of | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | // | Lesser General Public License for more details. | // | | // | You should have received a copy of the GNU Lesser General Public | // | License along with this library; if not, write to the Free Software | // | Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA| // +----------------------------------------------------------------------+ // | Authors: Brent Cook <busterbcook@yahoo.com> | // | Jason Pell <jasonpell@hotmail.com> | // +----------------------------------------------------------------------+ // // $Id: Lexer.php,v 1.1 2007/07/06 13:44:33 cybot Exp $ // include 'SQL/Parser/ctype.php'; // {{{ token definitions // variables: 'ident', 'sys_var' // values: 'real_val', 'text_val', 'int_val', null // }}} /** * A lexigraphical analyser inspired by the msql lexer * * @author Brent Cook <busterbcook@yahoo.com> * @version 0.5 * @access public * @package SQL_Parser */ class SQL_Parser_Lexer { // array of valid tokens for the lexer to recognize // format is 'token literal'=>TOKEN_VALUE var $symbols = array(); // {{{ instance variables var $tokPtr = 0; var $tokStart = 0; var $tokLen = 0; var $tokText = ''; var $lineNo = 0; var $lineBegin = 0; var $string = ''; var $stringLen = 0; // Will not be altered by skip() var $tokAbsStart = 0; var $skipText = ''; // Provide lookahead capability. var $lookahead = 0; // Specify how many tokens to save in tokenStack, so the // token stream can be pushed back. var $tokenStack = array(); var $stackPtr = 0; // }}} // {{{ incidental functions function SQL_Parser_Lexer($string = '', $lookahead = 0, $lexeropts) { $this->string = $string; $this->stringLen = strlen($string); $this->lookahead = $lookahead; $this->allowIdentFirstDigit = $lexeropts['allowIdentFirstDigit']; } function get() { ++$this->tokPtr; ++$this->tokLen; return ($this->tokPtr <= $this->stringLen) ? $this->string{$this->tokPtr - 1} : null; } function unget() { --$this->tokPtr; --$this->tokLen; } function skip() { ++$this->tokStart; return ($this->tokPtr != $this->stringLen) ? $this->string{$this->tokPtr++} : ''; } function revert() { $this->tokPtr = $this->tokStart; $this->tokLen = 0; } function isCompop($c) { return (($c == '<') || ($c == '>') || ($c == '=') || ($c == '!')); } // }}} // {{{ pushBack() /* * Push back a token, so the very next call to lex() will return that token. * Calls to this function will be ignored if there is no lookahead specified * to the constructor, or the pushBack() function has already been called the * maximum number of token's that can be looked ahead. */ function pushBack() { if ($this->lookahead > 0 && count($this->tokenStack) > 0 && $this->stackPtr > 0) { $this->stackPtr--; } } // }}} // {{{ lex() function lex() { if ($this->lookahead > 0) { // The stackPtr, should always be the same as the count of // elements in the tokenStack. The stackPtr, can be thought // of as pointing to the next token to be added. If however // a pushBack() call is made, the stackPtr, will be less than the // count, to indicate that we should take that token from the // stack, instead of calling nextToken for a new token. if ($this->stackPtr < count($this->tokenStack)) { $this->tokText = $this->tokenStack[$this->stackPtr]['tokText']; $this->skipText = $this->tokenStack[$this->stackPtr]['skipText']; $token = $this->tokenStack[$this->stackPtr]['token']; // We have read the token, so now iterate again. $this->stackPtr++; return $token; } else { // If $tokenStack is full (equal to lookahead), pop the oldest // element off, to make room for the new one. if ($this->stackPtr == $this->lookahead) { // For some reason array_shift and // array_pop screw up the indexing, so we do it manually. for ($i = 0; $i < (count($this->tokenStack) - 1); $i++) { $this->tokenStack[$i] = $this->tokenStack[$i + 1]; } // Indicate that we should put the element in // at the stackPtr position. $this->stackPtr--; } $token = $this->nextToken(); $this->tokenStack[$this->stackPtr] = array('token'=>$token, 'tokText'=>$this->tokText, 'skipText'=>$this->skipText); $this->stackPtr++; return $token; } } else { return $this->nextToken(); } } // }}} // {{{ nextToken() function nextToken() { if ($this->string == '') { return; } $state = 0; $this->tokAbsStart = $this->tokStart; while (true){ //echo "State: $state, Char: $c\n"; switch ($state) { // {{{ State 0 : Start of token case 0: $this->tokPtr = $this->tokStart; $this->tokText = ''; $this->tokLen = 0; $c = $this->get(); if (is_null($c)) { // End Of Input $state = 1000; break; } while (($c == ' ') || ($c == "\t") || ($c == "\n") || ($c == "\r") ) { if ($c == "\n" || $c == "\r") { // Handle MAC/Unix/Windows line endings. if ($c == "\r") { $c = $this->skip(); // If not DOS newline if ($c != "\n") { $this->unget(); } } ++$this->lineNo; $this->lineBegin = $this->tokPtr; } $c = $this->skip(); $this->tokLen = 1; } // Escape quotes and backslashes if ($c == '\\') { $t = $this->get(); if ($t == '\'' || $t == '\\' || $t == '"') { $this->tokText = $t; $this->tokStart = $this->tokPtr; return $this->tokText; } else { $this->unget(); // Unknown token. Revert to single char $state = 999; break; } } if (($c == '\'') || ($c == '"')) { // text string $quote = $c; $state = 12; break; } if ($c == '_') { // system variable $state = 18; break; } if (ctype_alpha(ord($c))) { // keyword or ident $state = 1; break; } if (ctype_digit(ord($c))) { // real or int number $state = 5; break; } if ($c == '.') { $t = $this->get(); if ($t == '.') { // ellipsis if ($this->get() == '.') { $this->tokText = '...'; $this->tokStart = $this->tokPtr; return $this->tokText; } else { $state = 999; break; } } else if (ctype_digit(ord($t))) { // real number $this->unget(); $state = 7; break; } else { // period $this->unget(); } } if ($c == '#') { // Comments $state = 14; break; } if ($c == '-') { $t = $this->get(); if ($t == '-') { $state = 14; break; } else { // negative number $this->unget(); $state = 5; break; } } if ($this->isCompop($c)) { // comparison operator $state = 10; break; } // Unknown token. Revert to single char $state = 999; break; // }}} // {{{ State 1 : Incomplete keyword or ident case 1: $c = $this->get(); if (ctype_alnum(ord($c)) || ($c == '_')) { $state = 1; break; } $state = 2; break; // }}} /* {{{ State 2 : Complete keyword or ident */ case 2: $this->unget(); $this->tokText = substr($this->string, $this->tokStart, $this->tokLen); $testToken = strtolower($this->tokText); if (isset($this->symbols[$testToken])) { $this->skipText = substr($this->string, $this->tokAbsStart, $this->tokStart-$this->tokAbsStart); $this->tokStart = $this->tokPtr; return $testToken; } else { $this->skipText = substr($this->string, $this->tokAbsStart, $this->tokStart-$this->tokAbsStart); $this->tokStart = $this->tokPtr; return 'ident'; } break; // }}} // {{{ State 5: Incomplete real or int number case 5: $c = $this->get(); if (ctype_digit(ord($c))) { $state = 5; break; } else if ($c == '.') { $t = $this->get(); if($t == '.') { // ellipsis $this->unget(); } else { // real number $state = 7; break; } } else if(ctype_alpha(ord($c))) { // Do we allow idents to begin with a digit? if ($this->allowIdentFirstDigit) { $state = 1; } else { // a number must end with non-alpha character $state = 999; } break; } else { // complete number $state = 6; break; } // }}} // {{{ State 6: Complete integer number case 6: $this->unget(); $this->tokText = intval(substr($this->string, $this->tokStart, $this->tokLen)); $this->skipText = substr($this->string, $this->tokAbsStart, $this->tokStart-$this->tokAbsStart); $this->tokStart = $this->tokPtr; return 'int_val'; break; // }}} // {{{ State 7: Incomplete real number case 7: $c = $this->get(); if ($c == 'e' || $c == 'E') { $state = 15; break; } if (ctype_digit(ord($c))) { $state = 7; break; } $state = 8; break; // }}} // {{{ State 8: Complete real number case 8: $this->unget(); $this->tokText = floatval(substr($this->string, $this->tokStart, $this->tokLen)); $this->skipText = substr($this->string, $this->tokAbsStart, $this->tokStart-$this->tokAbsStart); $this->tokStart = $this->tokPtr; return 'real_val'; // }}} // {{{ State 10: Incomplete comparison operator case 10: $c = $this->get(); if ($this->isCompop($c)) { $state = 10; break; } $state = 11; break; // }}} // {{{ State 11: Complete comparison operator case 11: $this->unget(); $this->tokText = substr($this->string, $this->tokStart, $this->tokLen); if ($this->tokText) { $this->skipText = substr($this->string, $this->tokAbsStart, $this->tokStart-$this->tokAbsStart); $this->tokStart = $this->tokPtr; return $this->tokText; } $state = 999; break; // }}} // {{{ State 12: Incomplete text string case 12: $bail = false; while (!$bail) { switch ($this->get()) { case '': $this->tokText = null; $bail = true; break; case "\\": if (!$this->get()) { $this->tokText = null; $bail = true; } //$bail = true; break; case $quote: $this->tokText = stripslashes(substr($this->string, ($this->tokStart+1), ($this->tokLen-2))); $bail = true; break; } } if (!is_null($this->tokText)) { $state = 13; break; } $state = 999; break; // }}} // {{{ State 13: Complete text string case 13: $this->skipText = substr($this->string, $this->tokAbsStart, $this->tokStart-$this->tokAbsStart); $this->tokStart = $this->tokPtr; return 'text_val'; break; // }}} // {{{ State 14: Comment case 14: $c = $this->skip(); if ($c == "\n" || $c == "\r" || $c == "") { // Handle MAC/Unix/Windows line endings. if ($c == "\r") { $c = $this->skip(); // If not DOS newline if ($c != "\n") { $this->unget(); } } if ($c != "") { ++$this->lineNo; $this->lineBegin = $this->tokPtr; } // We need to skip all the text. $this->tokStart = $this->tokPtr; $state = 0; } else { $state = 14; } break; // }}} // {{{ State 15: Exponent Sign in Scientific Notation case 15: $c = $this->get(); if($c == '-' || $c == '+') { $state = 16; break; } $state = 999; break; // }}} // {{{ state 16: Exponent Value-first digit in Scientific Notation case 16: $c = $this->get(); if (ctype_digit(ord($c))) { $state = 17; break; } $state = 999; // if no digit, then token is unknown break; // }}} // {{{ State 17: Exponent Value in Scientific Notation case 17: $c = $this->get(); if (ctype_digit(ord($c))) { $state = 17; break; } $state = 8; // At least 1 exponent digit was required break; // }}} // {{{ State 18 : Incomplete System Variable case 18: $c = $this->get(); if (ctype_alnum(ord($c)) || $c == '_') { $state = 18; break; } $state = 19; break; // }}} // {{{ State 19: Complete Sys Var case 19: $this->unget(); $this->tokText = substr($this->string, $this->tokStart, $this->tokLen); $this->skipText = substr($this->string, $this->tokAbsStart, $this->tokStart-$this->tokAbsStart); $this->tokStart = $this->tokPtr; return 'sys_var'; // }}} // {{{ State 999 : Unknown token. Revert to single char case 999: $this->revert(); $this->tokText = $this->get(); $this->skipText = substr($this->string, $this->tokAbsStart, $this->tokStart-$this->tokAbsStart); $this->tokStart = $this->tokPtr; return $this->tokText; // }}} // {{{ State 1000 : End Of Input case 1000: $this->tokText = '*end of input*'; $this->skipText = substr($this->string, $this->tokAbsStart, $this->tokStart-$this->tokAbsStart); $this->tokStart = $this->tokPtr; return null; // }}} } } } // }}} } http://cvs.php.net/viewvc.cgi/pear/SQL_Parser/Parser/ctype.php?view=markup&rev=1.1 Index: pear/SQL_Parser/Parser/ctype.php +++ pear/SQL_Parser/Parser/ctype.php <?php /** * PHP ctype compatibility functions. See the PHP ctype module for more * information on usage. * Note: These functions expect an integer argument, like the C versions * To use with a PHP character, use ord($c). These functions do not support * string arguments like their PHP extension counterparts * * @author John Millaway * @author Brent Cook <busterbcook@yahoo.com * @access public * @package SQL_Parser */ if (!extension_loaded('ctype')) { function ctype_alnum($c) { global $ctype__; return ($ctype__[$c] & 7); // (1 | 2 | 4) } function ctype_alpha($c) { global $ctype__; return ($ctype__[$c] & 3); // (1 | 2) } function ctype_cntrl($c) { global $ctype__; return ($ctype__[$c] & 40); } function ctype_digit($c) { global $ctype__; return ($ctype__[$c] & 4); } function ctype_graph($c) { global $ctype__; return ($ctype__[$c] & 27); // (20 | 1 | 2 | 4) } function ctype_lower($c) { global $ctype__; return ($ctype__[$c] & 2); } function ctype_print($c) { global $ctype__; return ($ctype__[$c] & 227); // (20 | 1 | 2 | 4 | 200) } function ctype_punct($c) { global $ctype__; return ($ctype__[$c] & 20); } function ctype_space($c) { global $ctype__; return ($ctype__[$c] & 10); } function ctype_upper($c) { global $ctype__; return ($ctype__[$c] & 1); } function ctype_xdigit($c) { global $ctype__; return ($ctype__[$c] & 104); // (100 | 4)); } $ctype__ = array(32,32,32,32,32,32,32,32,32,40,40,40,40,40,32,32,32,32,32,32,32,32,32, 32,32,32,32,32,32,32,32,32,-120,16,16,16,16,16,16,16,16,16,16,16,16, 16,16,16,4,4,4,4,4,4,4,4,4,4,16,16,16,16,16,16,16,65,65,65,65,65,65, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,16,16,16,16,16,16,66,66,66, 66,66,66,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,16,16,16,16,32,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0); } http://cvs.php.net/viewvc.cgi/pear/SQL_Parser/Parser/Dialect/ANSI.php?view=markup&rev=1.1 Index: pear/SQL_Parser/Parser/Dialect/ANSI.php +++ pear/SQL_Parser/Parser/Dialect/ANSI.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | Copyright (c) 2002-2004 Brent Cook | // +----------------------------------------------------------------------+ // | This library is free software; you can redistribute it and/or | // | modify it under the terms of the GNU Lesser General Public | // | License as published by the Free Software Foundation; either | // | version 2.1 of the License, or (at your option) any later version. | // | | // | This library is distributed in the hope that it will be useful, | // | but WITHOUT ANY WARRANTY; without even the implied warranty of | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | // | Lesser General Public License for more details. | // | | // | You should have received a copy of the GNU Lesser General Public | // | License along with this library; if not, write to the Free Software | // | Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA| // +----------------------------------------------------------------------+ // | Authors: Brent Cook <busterbcook@yahoo.com> | // +----------------------------------------------------------------------+ // // $Id: ANSI.php,v 1.1 2007/07/06 13:44:33 cybot Exp $ // // define tokens accepted by the SQL dialect. $dialect = array( 'commands' => array('alter','create','drop','select','delete','insert','update'), 'operators' => array('=','<>','<','<=','>','>=','like','clike','slike','not','is','in','between','and','or'), 'types' => array( 'character','char','varchar','nchar','bit','numeric','decimal', 'dec','integer','int','smallint','float','real','double','date', 'time','timestamp','interval','bool','boolean','set','enum','text' ), 'conjunctions' => array('by','as','on','into','from','where','with'), 'functions' => array('avg','count','max','min','sum','nextval','currval'), 'reserved' => array( 'absolute','action','add','all','allocate','and','any','are','asc', 'ascending','assertion','at','authorization','begin','bit_length', 'both','cascade','cascaded','case','cast','catalog','char_length', 'character_length','check','close','coalesce','collate','collation', 'column','commit','connect','connection','constraint','constraints', 'continue','convert','corresponding','cross','current','current_date', 'current_time','current_timestamp','current_user','cursor','day', 'deallocate','declare','default','deferrable','deferred','desc', 'descending','describe','descriptor','diagnostics','disconnect', 'distinct','domain','else','end','end-exec','escape','except','exception', 'exec','execute','exists','external','extract','false','fetch','first', 'for','foreign','found','full','get','global','go','goto','grant','group', 'having','hour','identity','immediate','indicator','initially','inner', 'input','insensitive','intersect','isolation','join','key','language', 'last','leading','left','level','limit','local','lower','match','minute', 'module','month','names','national','natural','next','no','null','nullif', 'octet_length','of','only','open','option','or','order','outer','output', 'overlaps','pad','partial','position','precision','prepare','preserve', 'primary','prior','privileges','procedure','public','read','references', 'relative','restrict','revoke','right','rollback','rows','schema','scroll', 'second','section','session','session_user','size','some','space','sql', 'sqlcode','sqlerror','sqlstate','substring','system_user','table','temporary', 'then','timezone_hour','timezone_minute','to','trailing','transaction', 'translate','translation','trim','true','union','unique','unknown','upper', 'usage','user','using','value','values','varying','view','when','whenever', 'work','write','year','zone','eoc' ), 'synonyms' => array( 'decimal'=>'numeric','dec'=>'numeric','numeric'=>'numeric', 'float'=>'float','real'=>'real','double'=>'real','int'=>'int', 'integer'=>'int','interval'=>'interval','smallint'=>'smallint', 'timestamp'=>'timestamp','bool'=>'bool','boolean'=>'bool', 'set'=>'set','enum'=>'enum','text'=>'text','char'=>'char', 'character'=>'char','varchar'=>'varchar','ascending'=>'asc', 'asc'=>'asc','descending'=>'desc','desc'=>'desc','date'=>'date', 'time'=>'time' ), 'lexeropts' => array('allowIdentFirstDigit' => false), 'parseropts' => array() ); http://cvs.php.net/viewvc.cgi/pear/SQL_Parser/Parser/Dialect/MySQL.php?view=markup&rev=1.1 Index: pear/SQL_Parser/Parser/Dialect/MySQL.php +++ pear/SQL_Parser/Parser/Dialect/MySQL.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | Copyright (c) 2004 John Griffin | // +----------------------------------------------------------------------+ // | This library is free software; you can redistribute it and/or | // | modify it under the terms of the GNU Lesser General Public | // | License as published by the Free Software Foundation; either | // | version 2.1 of the License, or (at your option) any later version. | // | | // | This library is distributed in the hope that it will be useful, | // | but WITHOUT ANY WARRANTY; without even the implied warranty of | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | // | Lesser General Public License for more details. | // | | // | You should have received a copy of the GNU Lesser General Public | // | License along with this library; if not, write to the Free Software | // | Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA| // +----------------------------------------------------------------------+ // | Authors: John Griffin <jgriffin316@netscape.net> | // +----------------------------------------------------------------------+ // // $Id: MySQL.php,v 1.1 2007/07/06 13:44:33 cybot Exp $ // // define tokens accepted by the SQL dialect. $dialect = array( 'commands' => array('alter', 'create', 'drop', 'select', 'delete', 'insert', 'update'), 'operators' => array('=', '<>', '<', '<=', '>', '>=', 'like', 'clike', 'slike', 'not', 'is', 'in', 'between'), 'types' => array( 'character', 'char', 'varchar', 'nchar', 'bit', 'numeric', 'decimal', 'dec', 'integer', 'int', 'tinyint', 'smallint', 'float', 'real', 'double', 'date', 'datetime', 'time', 'timestamp', 'interval', 'bool', 'boolean', 'set', 'enum', 'text' ), 'conjunctions' => array('by', 'as', 'on', 'into', 'from', 'where', 'with'), 'functions' => array( 'abs', 'acos', 'adddate', 'addtime', 'aes_encrypt', 'aes_decrypt', 'against', 'ascii', 'asin', 'atan', 'avg', 'benchmark', 'bin', 'bit_and', 'bit_or', 'bitcount', 'bitlength', 'cast', 'ceiling', 'char', 'char_length', 'character_length', 'charset', 'coalesce', 'coercibility', 'collation', 'compress', 'concat', 'concat_ws', 'conection_id', 'conv', 'convert', 'convert_tz', 'cos', 'cot', 'count', 'crc32', 'curdate', 'current_user', 'currval', 'curtime', 'database', 'date_add', 'date_diff', 'date_format', 'date_sub', 'day', 'dayname', 'dayofmonth', 'dayofweek', 'dayofyear', 'decode', 'default', 'degrees', 'des_decrypt', 'des_encrypt', 'elt', 'encode', 'encrypt', 'exp', 'export_set', 'extract', 'field', 'find_in_set', 'floor', 'format', 'found_rows', 'from_days', 'from_unixtime', 'get_format', 'get_lock', 'group_concat', 'greatest', 'hex', 'hour', 'if', 'ifnull', 'in', 'inet_aton', 'inet_ntoa', 'insert', 'instr', 'interval', 'is_free_lock', 'is_used_lock', 'last_day', 'last_insert_id', 'lcase', 'least', 'left', 'length', 'ln', 'load_file', 'localtime', 'localtimestamp', 'locate', 'log', 'log2', 'log10', 'lower', 'lpad', 'ltrim', 'make_set', 'makedate', 'maketime', 'master_pos_wait', 'match', 'max', 'md5', 'microsecond', 'mid', 'min', 'minute', 'mod', 'month', 'monthname', 'nextval', 'now', 'nullif', 'oct', 'octet_length', 'old_password', 'ord', 'password', 'period_add', 'period_diff', 'pi', 'position', 'pow', 'power', 'quarter', 'quote', 'radians', 'rand', 'release_lock', 'repeat', 'replace', 'reverse', 'right', 'round', 'row_count', 'rpad', 'rtrim', 'sec_to_time', 'second', 'session_user', 'sha', 'sha1', 'sign', 'soundex', 'space', 'sqrt', 'std', 'stddev', 'stddev_pop', 'stddev_samp', 'strcmp', 'str_to_date', 'subdate', 'substring', 'substring_index', 'subtime', 'sum', 'sysdate', 'system_user', 'tan', 'time', 'timediff', 'timestamp', 'timestampadd', 'timestampdiff', 'time_format', 'time_to_sec', 'to_days', 'trim', 'truncate', 'ucase', 'uncompress', 'uncompressed_length', 'unhex', 'unix_timestamp', 'upper', 'user', 'utc_date', 'utc_time', 'utc_timestamp', 'uuid', 'var_pop', 'var_samp', 'variance', 'version', 'week', 'weekday', 'weekofyear', 'year', 'yearweek' ), 'reserved' => array( 'add', 'all', 'alter', 'analyze', 'and', 'as', 'asc', 'asensitive', 'auto_increment', 'bdb', 'before', 'berkeleydb', 'between', 'bigint', 'binary', 'blob', 'both', 'by', 'call', 'cascade', 'case', 'change', 'char', 'character', 'check', 'collate', 'column', 'columns', 'condition', 'connection', 'constraint', 'continue', 'create', 'cross', 'current_date', 'current_time', 'current_timestamp', 'cursor', 'database', 'databases', 'day_hour', 'day_microsecond', 'day_minute', 'day_second', 'dec', 'decimal', 'declare', 'default', 'delayed', 'delete', 'desc', 'describe', 'deterministic', 'distinct', 'distinctrow', 'div', 'double', 'drop', 'else', 'elseif', 'enclosed', 'escaped', 'exists', 'exit', 'explain', 'false', 'fetch', 'fields', 'float', 'for', 'force', 'foreign', 'found', 'frac_second', 'from', 'fulltext', 'grant', 'group', 'having', 'high_priority', 'hour_microsecond', 'hour_minute', 'hour_second', 'if', 'ignore', 'in', 'index', 'infile', 'inner', 'innodb', 'inout', 'insensitive', 'insert', 'int', 'integer', 'interval', 'into', 'io_thread', 'is', 'iterate', 'join', 'key', 'keys', 'kill', 'leading', 'leave', 'left', 'like', 'limit', 'lines', 'load', 'localtime', 'localtimestamp', 'lock', 'long', 'longblob', 'longtext', 'loop', 'low_priority', 'master_server_id', 'match', 'mediumblob', 'mediumint', 'mediumtext', 'middleint', 'minute_microsecond', 'minute_second', 'mod', 'natural', 'not', 'no_write_to_binlog', 'null', 'numeric', 'on', 'optimize', 'option', 'optionally', 'or', 'order', 'out', 'outer', 'outfile', 'precision', 'primary', 'privileges', 'procedure', 'purge', 'read', 'real', 'references', 'regexp', 'rename', 'repeat', 'replace', 'require', 'restrict', 'return', 'revoke', 'right', 'rlike', 'second_microsecond', 'select', 'sensitive', 'separator', 'set', 'show', 'smallint', 'some', 'soname', 'spatial', 'specific', 'sql', 'sqlexception', 'sqlstate', 'sqlwarning', 'sql_big_result', 'sql_calc_found_rows', 'sql_small_result', 'sql_tsi_day', 'sql_tsi_frac_second', 'sql_tsi_hour', 'sql_tsi_minute', 'sql_tsi_month', 'sql_tsi_quarter', 'sql_tsi_second', 'sql_tsi_week', 'sql_tsi_year', 'ssl', 'starting', 'straight_join', 'striped', 'table', 'tables', 'terminated', 'then', 'timestampadd', 'timestampdiff', 'tinyblob', 'tinyint', 'tinytext', 'to', 'trailing', 'true', 'undo', 'union', 'unique', 'unlock', 'unsigned', 'update', 'usage', 'use', 'user_resources', 'using', 'utc_date', 'utc_time', 'utc_timestamp', 'values', 'varbinary', 'varchar', 'varcharacter', 'varying', 'when', 'where', 'while', 'with', 'write', 'xor', 'year_month', 'zerofill' ), 'synonyms' => array( 'decimal'=>'numeric', 'dec'=>'numeric', 'numeric'=>'numeric', 'float'=>'float', 'real'=>'real', 'double'=>'real', 'int'=>'int', 'integer'=>'int', 'interval'=>'interval', 'smallint'=>'smallint', 'tinyint'=>'tinyint', 'timestamp'=>'timestamp', 'bool'=>'bool', 'boolean'=>'bool', 'set'=>'set', 'enum'=>'enum', 'text'=>'text', 'char'=>'char', 'character'=>'char', 'varchar'=>'varchar', 'ascending'=>'asc', 'asc'=>'asc', 'descending'=>'desc', 'desc'=>'desc', 'date'=>'date', 'time'=>'time' ), 'lexeropts' => array('allowIdentFirstDigit'=>true), 'parseropts' => array() );
http://cvs.php.net/viewvc.cgi/pear/SQL_Parser/Parser.php?r1=1.41&r2=1.42&diff_format=u Index: pear/SQL_Parser/Parser.php diff -u pear/SQL_Parser/Parser.php:1.41 pear/SQL_Parser/Parser.php:1.42 --- pear/SQL_Parser/Parser.php:1.41 Fri Jul 6 10:44:42 2007 +++ pear/SQL_Parser/Parser.php Fri Jul 6 13:44:33 2007 @@ -25,7 +25,7 @@ // | John Griffin <jgriffin316@netscape.net> | // +----------------------------------------------------------------------+ // -// $Id: Parser.php,v 1.41 2007/07/06 10:44:42 cybot Exp $ +// $Id: Parser.php,v 1.42 2007/07/06 13:44:33 cybot Exp $ // // The plan for moving forward: // - Refactor sentinel conditions to show flow @@ -35,8 +35,8 @@ // outputting the same parse tree require_once 'PEAR.php'; -require_once 'SQL/Lexer.php'; -require_once 'Expressions/include.php'; +require_once 'SQL/Parser/Lexer.php'; +require_once 'SQL/Expressions/include.php'; /** * A sql parser @@ -70,7 +70,7 @@ if (is_string($string)) { // Initialize the Lexer with a 3-level look-back buffer - $this->lexer = new Lexer($string, 3, $this->lexeropts); + $this->lexer = new SQL_Parser_Lexer($string, 3, $this->lexeropts); $this->lexer->symbols =& $this->symbols; } } @@ -83,7 +83,7 @@ return $this->raiseError('Unknown SQL dialect:'.$dialect); } - include 'SQL/Dialect_'.$dialect.'.php'; + include 'SQL/Parser/Dialect/' . $dialect . '.php'; $this->types = array_flip($dialect['types']); $this->functions = array_flip($dialect['functions']); $this->operators = array_flip($dialect['operators']); @@ -1269,7 +1269,7 @@ { if (is_string($string)) { // Initialize the Lexer with a 3-level look-back buffer - $this->lexer = new Lexer($string, 3, $this->lexeropts); + $this->lexer = new SQL_Parser_Lexer($string, 3, $this->lexeropts); $this->lexer->symbols =& $this->symbols; } else { if (!is_object($this->lexer)) { http://cvs.php.net/viewvc.cgi/pear/SQL_Parser/package.xml?r1=1.15&r2=1.16&diff_format=u Index: pear/SQL_Parser/package.xml diff -u pear/SQL_Parser/package.xml:1.15 pear/SQL_Parser/package.xml:1.16 --- pear/SQL_Parser/package.xml:1.15 Fri Jul 6 08:58:17 2007 +++ pear/SQL_Parser/package.xml Fri Jul 6 13:44:33 2007 @@ -60,11 +60,15 @@ <file baseinstalldir="/SQL" name="test_parser.php" role="test" /> <file baseinstalldir="/SQL" name="update.php" role="test" /> </dir> <!-- //tests --> - <file baseinstalldir="/SQL" name="Compiler.php" role="php" /> - <file baseinstalldir="/SQL" name="ctype.php" role="php" /> - <file baseinstalldir="/SQL" name="Dialect_ANSI.php" role="php" /> - <file baseinstalldir="/SQL" name="Dialect_MySQL.php" role="php" /> - <file baseinstalldir="/SQL" name="Lexer.php" role="php" /> + <dir name="Parser"> + <dir name="Dialect"> + <file baseinstalldir="/SQL" name="ANSI.php" role="php" /> + <file baseinstalldir="/SQL" name="MySQL.php" role="php" /> + </dir> + <file baseinstalldir="/SQL" name="Compiler.php" role="php" /> + <file baseinstalldir="/SQL" name="ctype.php" role="php" /> + <file baseinstalldir="/SQL" name="Lexer.php" role="php" /> + </dir> <file baseinstalldir="/SQL" name="Parser.php" role="php" /> </dir> <!-- / --> </contents> http://cvs.php.net/viewvc.cgi/pear/SQL_Parser/Parser/Compiler.php?view=markup&rev=1.1 Index: pear/SQL_Parser/Parser/Compiler.php +++ pear/SQL_Parser/Parser/Compiler.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | Copyright (c) 2003-2004 John Griffin | // +----------------------------------------------------------------------+ // | This library is free software; you can redistribute it and/or | // | modify it under the terms of the GNU Lesser General Public | // | License as published by the Free Software Foundation; either | // | version 2.1 of the License, or (at your option) any later version. | // | | // | This library is distributed in the hope that it will be useful, | // | but WITHOUT ANY WARRANTY; without even the implied warranty of | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | // | Lesser General Public License for more details. | // | | // | You should have received a copy of the GNU Lesser General Public | // | License along with this library; if not, write to the Free Software | // | Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA| // +----------------------------------------------------------------------+ // | Authors: John Griffin <jgriffin316@netscape.net> | // +----------------------------------------------------------------------+ // // $Id: Compiler.php,v 1.1 2007/07/06 13:44:33 cybot Exp $ // require_once 'PEAR.php'; /** * A SQL parse tree compiler. * * @author John Griffin <jgriffin316@netscape.net> * @version 0.1 * @access public * @package SQL_Parser */ class SQL_Parser_Compiler { var $tree; // {{{ function SQL_Parser_Compiler($array = null) function SQL_Parser_Compiler($array = null) { $this->tree = $array; } // }}} // {{{ function getWhereValue ($arg) function getWhereValue ($arg) { switch ($arg['type']) { case 'ident': case 'real_val': case 'int_val': case 'null': $value = $arg['value']; break; case 'text_val': $value = '\''.$arg['value'].'\''; break; case 'subclause': $value = '('.$this->compileSearchClause($arg['value']).')'; break; default: return PEAR::raiseError('Unknown type: '.$arg['type']); } return $value; } // }}} // {{{ function getParams($arg) function getParams($arg) { $types = count($arg['type']); for ($i = 0; $i < $types; $i++) { switch ($arg['type'][$i]) { case 'ident': case 'real_val': case 'int_val': case 'null': $value[] = $arg['value'][$i]; break; case 'text_val': $value[] = '\''.$arg['value'][$i].'\''; break; default: return PEAR::raiseError('Unknown type: '.$arg['type'][$i]); } } $value ='('.implode(', ', $value).')'; return $value; } // }}} // {{{ function compileFunctionOpts($arg) function compileFunctionOpts($arg) { $types = count($arg['type']); for ($i = 0; $i < $types; $i++) { switch ($arg['type'][$i]) { case 'ident': case 'real_val': case 'int_val': case 'null': $value[] = $arg['arg'][$i]; break; case 'text_val': $value[] = '\''.$arg['arg'][$i].'\''; break; default: return PEAR::raiseError('Unknown type: '.$arg['type'][$i]); } } $value = implode(', ', $value); return $value; } // }}} // {{{ function compileSearchClause function compileSearchClause($where_clause) { $value = ''; if (isset ($where_clause['arg_1']['value'])) { $value = $this->getWhereValue ($where_clause['arg_1']); if (PEAR::isError($value)) { return $value; } $sql = $value; } else { $value = $this->compileSearchClause($where_clause['arg_1']); if (PEAR::isError($value)) { return $value; } $sql = $value; } if (isset ($where_clause['op'])) { if ($where_clause['op'] == 'in') { $value = $this->getParams($where_clause['arg_2']); if (PEAR::isError($value)) { return $value; } if (isset($where_clause['neg'])) { $sql .= ' not'; } $sql .= ' '.$where_clause['op'].' '.$value; } elseif ($where_clause['op'] == 'is') { $value = isset ($where_clause['neg']) ? 'not null' : 'null'; $sql .= ' is '.$value; } else { $sql .= ' '.$where_clause['op'].' '; if (isset ($where_clause['arg_2']['value'])) { $value = $this->getWhereValue ($where_clause['arg_2']); if (PEAR::isError($value)) { return $value; } $sql .= $value; } else { $value = $this->compileSearchClause($where_clause['arg_2']); if (PEAR::isError($value)) { return $value; } $sql .= $value; } } } return $sql; } // }}} // {{{ function compileSelect() function compileSelect() { // save the command and set quantifiers $sql = 'select '; if (isset($this->tree['set_quantifier'])) { $sql .= $this->tree['set_quantifier'].' '; } // save the column names and set functions $cols = count($this->tree['column_names']); for ($i = 0; $i < $cols; $i++) { $column = $this->tree['column_names'][$i]; if ($this->tree['column_aliases'][$i] != '') { $column .= ' as '.$this->tree['column_aliases'][$i]; } $column_names[] = $column; } $funcs = count($this->tree['set_function']); for ($i = 0; $i < $funcs; $i++) { $column = $this->tree['set_function'][$i]['name'].'('; if (isset ($this->tree['set_function'][$i]['distinct'])) { $column .= 'distinct '; } if (isset ($this->tree['set_function'][$i]['arg'])) { $column .= $this->compileFunctionOpts($this->tree['set_function'][$i]); } $column .= ')'; if ($this->tree['set_function'][$i]['alias'] != '') { $column .= ' as '.$this->tree['set_function'][$i]['alias']; } $column_names[] = $column; } if (isset($column_names)) { $sql .= implode (", ", $column_names); } // save the tables $sql .= ' from '; $c_tables = count($this->tree['table_names']); for ($i = 0; $i < $c_tables; $i++) { $sql .= $this->tree['table_names'][$i]; if ($this->tree['table_aliases'][$i] != '') { $sql .= ' as '.$this->tree['table_aliases'][$i]; } if ($this->tree['table_join_clause'][$i] != '') { $search_string = $this->compileSearchClause ($this->tree['table_join_clause'][$i]); if (PEAR::isError($search_string)) { return $search_string; } $sql .= ' on '.$search_string; } if (isset($this->tree['table_join'][$i])) { $sql .= ' '.$this->tree['table_join'][$i].' '; } } // save the where clause if (isset($this->tree['where_clause'])) { $search_string = $this->compileSearchClause ($this->tree['where_clause']); if (PEAR::isError($search_string)) { return $search_string; } $sql .= ' where '.$search_string; } // save the group by clause if (isset ($this->tree['group_by'])) { $sql .= ' group by '.implode(', ', $this->tree['group_by']); } // save the order by clause if (isset ($this->tree['sort_order'])) { foreach ($this->tree['sort_order'] as $key => $value) { $sort_order[] = $key.' '.$value; } $sql .= ' order by '.implode(', ', $sort_order); } // save the limit clause if (isset ($this->tree['limit_clause'])) { $sql .= ' limit '.$this->tree['limit_clause']['start'].','.$this->tree['limit_clause']['length']; } return $sql; } // }}} // {{{ function compileUpdate() function compileUpdate() { $sql = 'update '.implode(', ', $this->tree['table_names']); // save the set clause $cols = count($this->tree['column_names']); for ($i = 0; $i < $cols; $i++) { $set_columns[] = $this->tree['column_names'][$i].' = '.$this->getWhereValue($this->tree['values'][$i]); } $sql .= ' set '.implode (', ', $set_columns); // save the where clause if (isset($this->tree['where_clause'])) { $search_string = $this->compileSearchClause ($this->tree['where_clause']); if (PEAR::isError($search_string)) { return $search_string; } $sql .= ' where '.$search_string; } return $sql; } // }}} // {{{ function compileDelete() function compileDelete() { $sql = 'delete from '.implode(', ', $this->tree['table_names']); // save the where clause if (isset($this->tree['where_clause'])) { $search_string = $this->compileSearchClause ($this->tree['where_clause']); if (PEAR::isError($search_string)) { return $search_string; } $sql .= ' where '.$search_string; } return $sql; } // }}} // {{{ function compileInsert() function compileInsert() { $sql = 'insert into '.$this->tree['table_names'][0].' ('. implode(', ', $this->tree['column_names']).') values ('; $c_vals = count($this->tree['values']); for ($i = 0; $i < $c_vals; $i++) { $value = $this->getWhereValue ($this->tree['values'][$i]); if (PEAR::isError($value)) { return $value; } $value_array[] = $value; } $sql .= implode(', ', $value_array).')'; return $sql; } // }}} // {{{ function compile($array = null) function compile($array = null) { $this->tree = $array; switch ($this->tree['command']) { case 'select': return $this->compileSelect(); break; case 'update': return $this->compileUpdate(); break; case 'delete': return $this->compileDelete(); break; case 'insert': return $this->compileInsert(); break; case 'create': case 'drop': case 'alter': default: return PEAR::raiseError('Unknown action: '.$this->tree['command']); } // switch ($this->_tree["command"]) } // }}} } http://cvs.php.net/viewvc.cgi/pear/SQL_Parser/Parser/Lexer.php?view=markup&rev=1.1 Index: pear/SQL_Parser/Parser/Lexer.php +++ pear/SQL_Parser/Parser/Lexer.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | Copyright (c) 2002-2004 Brent Cook | // +----------------------------------------------------------------------+ // | This library is free software; you can redistribute it and/or | // | modify it under the terms of the GNU Lesser General Public | // | License as published by the Free Software Foundation; either | // | version 2.1 of the License, or (at your option) any later version. | // | | // | This library is distributed in the hope that it will be useful, | // | but WITHOUT ANY WARRANTY; without even the implied warranty of | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | // | Lesser General Public License for more details. | // | | // | You should have received a copy of the GNU Lesser General Public | // | License along with this library; if not, write to the Free Software | // | Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA| // +----------------------------------------------------------------------+ // | Authors: Brent Cook <busterbcook@yahoo.com> | // | Jason Pell <jasonpell@hotmail.com> | // +----------------------------------------------------------------------+ // // $Id: Lexer.php,v 1.1 2007/07/06 13:44:33 cybot Exp $ // include 'SQL/Parser/ctype.php'; // {{{ token definitions // variables: 'ident', 'sys_var' // values: 'real_val', 'text_val', 'int_val', null // }}} /** * A lexigraphical analyser inspired by the msql lexer * * @author Brent Cook <busterbcook@yahoo.com> * @version 0.5 * @access public * @package SQL_Parser */ class SQL_Parser_Lexer { // array of valid tokens for the lexer to recognize // format is 'token literal'=>TOKEN_VALUE var $symbols = array(); // {{{ instance variables var $tokPtr = 0; var $tokStart = 0; var $tokLen = 0; var $tokText = ''; var $lineNo = 0; var $lineBegin = 0; var $string = ''; var $stringLen = 0; // Will not be altered by skip() var $tokAbsStart = 0; var $skipText = ''; // Provide lookahead capability. var $lookahead = 0; // Specify how many tokens to save in tokenStack, so the // token stream can be pushed back. var $tokenStack = array(); var $stackPtr = 0; // }}} // {{{ incidental functions function SQL_Parser_Lexer($string = '', $lookahead = 0, $lexeropts) { $this->string = $string; $this->stringLen = strlen($string); $this->lookahead = $lookahead; $this->allowIdentFirstDigit = $lexeropts['allowIdentFirstDigit']; } function get() { ++$this->tokPtr; ++$this->tokLen; return ($this->tokPtr <= $this->stringLen) ? $this->string{$this->tokPtr - 1} : null; } function unget() { --$this->tokPtr; --$this->tokLen; } function skip() { ++$this->tokStart; return ($this->tokPtr != $this->stringLen) ? $this->string{$this->tokPtr++} : ''; } function revert() { $this->tokPtr = $this->tokStart; $this->tokLen = 0; } function isCompop($c) { return (($c == '<') || ($c == '>') || ($c == '=') || ($c == '!')); } // }}} // {{{ pushBack() /* * Push back a token, so the very next call to lex() will return that token. * Calls to this function will be ignored if there is no lookahead specified * to the constructor, or the pushBack() function has already been called the * maximum number of token's that can be looked ahead. */ function pushBack() { if ($this->lookahead > 0 && count($this->tokenStack) > 0 && $this->stackPtr > 0) { $this->stackPtr--; } } // }}} // {{{ lex() function lex() { if ($this->lookahead > 0) { // The stackPtr, should always be the same as the count of // elements in the tokenStack. The stackPtr, can be thought // of as pointing to the next token to be added. If however // a pushBack() call is made, the stackPtr, will be less than the // count, to indicate that we should take that token from the // stack, instead of calling nextToken for a new token. if ($this->stackPtr < count($this->tokenStack)) { $this->tokText = $this->tokenStack[$this->stackPtr]['tokText']; $this->skipText = $this->tokenStack[$this->stackPtr]['skipText']; $token = $this->tokenStack[$this->stackPtr]['token']; // We have read the token, so now iterate again. $this->stackPtr++; return $token; } else { // If $tokenStack is full (equal to lookahead), pop the oldest // element off, to make room for the new one. if ($this->stackPtr == $this->lookahead) { // For some reason array_shift and // array_pop screw up the indexing, so we do it manually. for ($i = 0; $i < (count($this->tokenStack) - 1); $i++) { $this->tokenStack[$i] = $this->tokenStack[$i + 1]; } // Indicate that we should put the element in // at the stackPtr position. $this->stackPtr--; } $token = $this->nextToken(); $this->tokenStack[$this->stackPtr] = array('token'=>$token, 'tokText'=>$this->tokText, 'skipText'=>$this->skipText); $this->stackPtr++; return $token; } } else { return $this->nextToken(); } } // }}} // {{{ nextToken() function nextToken() { if ($this->string == '') { return; } $state = 0; $this->tokAbsStart = $this->tokStart; while (true){ //echo "State: $state, Char: $c\n"; switch ($state) { // {{{ State 0 : Start of token case 0: $this->tokPtr = $this->tokStart; $this->tokText = ''; $this->tokLen = 0; $c = $this->get(); if (is_null($c)) { // End Of Input $state = 1000; break; } while (($c == ' ') || ($c == "\t") || ($c == "\n") || ($c == "\r") ) { if ($c == "\n" || $c == "\r") { // Handle MAC/Unix/Windows line endings. if ($c == "\r") { $c = $this->skip(); // If not DOS newline if ($c != "\n") { $this->unget(); } } ++$this->lineNo; $this->lineBegin = $this->tokPtr; } $c = $this->skip(); $this->tokLen = 1; } // Escape quotes and backslashes if ($c == '\\') { $t = $this->get(); if ($t == '\'' || $t == '\\' || $t == '"') { $this->tokText = $t; $this->tokStart = $this->tokPtr; return $this->tokText; } else { $this->unget(); // Unknown token. Revert to single char $state = 999; break; } } if (($c == '\'') || ($c == '"')) { // text string $quote = $c; $state = 12; break; } if ($c == '_') { // system variable $state = 18; break; } if (ctype_alpha(ord($c))) { // keyword or ident $state = 1; break; } if (ctype_digit(ord($c))) { // real or int number $state = 5; break; } if ($c == '.') { $t = $this->get(); if ($t == '.') { // ellipsis if ($this->get() == '.') { $this->tokText = '...'; $this->tokStart = $this->tokPtr; return $this->tokText; } else { $state = 999; break; } } else if (ctype_digit(ord($t))) { // real number $this->unget(); $state = 7; break; } else { // period $this->unget(); } } if ($c == '#') { // Comments $state = 14; break; } if ($c == '-') { $t = $this->get(); if ($t == '-') { $state = 14; break; } else { // negative number $this->unget(); $state = 5; break; } } if ($this->isCompop($c)) { // comparison operator $state = 10; break; } // Unknown token. Revert to single char $state = 999; break; // }}} // {{{ State 1 : Incomplete keyword or ident case 1: $c = $this->get(); if (ctype_alnum(ord($c)) || ($c == '_')) { $state = 1; break; } $state = 2; break; // }}} /* {{{ State 2 : Complete keyword or ident */ case 2: $this->unget(); $this->tokText = substr($this->string, $this->tokStart, $this->tokLen); $testToken = strtolower($this->tokText); if (isset($this->symbols[$testToken])) { $this->skipText = substr($this->string, $this->tokAbsStart, $this->tokStart-$this->tokAbsStart); $this->tokStart = $this->tokPtr; return $testToken; } else { $this->skipText = substr($this->string, $this->tokAbsStart, $this->tokStart-$this->tokAbsStart); $this->tokStart = $this->tokPtr; return 'ident'; } break; // }}} // {{{ State 5: Incomplete real or int number case 5: $c = $this->get(); if (ctype_digit(ord($c))) { $state = 5; break; } else if ($c == '.') { $t = $this->get(); if($t == '.') { // ellipsis $this->unget(); } else { // real number $state = 7; break; } } else if(ctype_alpha(ord($c))) { // Do we allow idents to begin with a digit? if ($this->allowIdentFirstDigit) { $state = 1; } else { // a number must end with non-alpha character $state = 999; } break; } else { // complete number $state = 6; break; } // }}} // {{{ State 6: Complete integer number case 6: $this->unget(); $this->tokText = intval(substr($this->string, $this->tokStart, $this->tokLen)); $this->skipText = substr($this->string, $this->tokAbsStart, $this->tokStart-$this->tokAbsStart); $this->tokStart = $this->tokPtr; return 'int_val'; break; // }}} // {{{ State 7: Incomplete real number case 7: $c = $this->get(); if ($c == 'e' || $c == 'E') { $state = 15; break; } if (ctype_digit(ord($c))) { $state = 7; break; } $state = 8; break; // }}} // {{{ State 8: Complete real number case 8: $this->unget(); $this->tokText = floatval(substr($this->string, $this->tokStart, $this->tokLen)); $this->skipText = substr($this->string, $this->tokAbsStart, $this->tokStart-$this->tokAbsStart); $this->tokStart = $this->tokPtr; return 'real_val'; // }}} // {{{ State 10: Incomplete comparison operator case 10: $c = $this->get(); if ($this->isCompop($c)) { $state = 10; break; } $state = 11; break; // }}} // {{{ State 11: Complete comparison operator case 11: $this->unget(); $this->tokText = substr($this->string, $this->tokStart, $this->tokLen); if ($this->tokText) { $this->skipText = substr($this->string, $this->tokAbsStart, $this->tokStart-$this->tokAbsStart); $this->tokStart = $this->tokPtr; return $this->tokText; } $state = 999; break; // }}} // {{{ State 12: Incomplete text string case 12: $bail = false; while (!$bail) { switch ($this->get()) { case '': $this->tokText = null; $bail = true; break; case "\\": if (!$this->get()) { $this->tokText = null; $bail = true; } //$bail = true; break; case $quote: $this->tokText = stripslashes(substr($this->string, ($this->tokStart+1), ($this->tokLen-2))); $bail = true; break; } } if (!is_null($this->tokText)) { $state = 13; break; } $state = 999; break; // }}} // {{{ State 13: Complete text string case 13: $this->skipText = substr($this->string, $this->tokAbsStart, $this->tokStart-$this->tokAbsStart); $this->tokStart = $this->tokPtr; return 'text_val'; break; // }}} // {{{ State 14: Comment case 14: $c = $this->skip(); if ($c == "\n" || $c == "\r" || $c == "") { // Handle MAC/Unix/Windows line endings. if ($c == "\r") { $c = $this->skip(); // If not DOS newline if ($c != "\n") { $this->unget(); } } if ($c != "") { ++$this->lineNo; $this->lineBegin = $this->tokPtr; } // We need to skip all the text. $this->tokStart = $this->tokPtr; $state = 0; } else { $state = 14; } break; // }}} // {{{ State 15: Exponent Sign in Scientific Notation case 15: $c = $this->get(); if($c == '-' || $c == '+') { $state = 16; break; } $state = 999; break; // }}} // {{{ state 16: Exponent Value-first digit in Scientific Notation case 16: $c = $this->get(); if (ctype_digit(ord($c))) { $state = 17; break; } $state = 999; // if no digit, then token is unknown break; // }}} // {{{ State 17: Exponent Value in Scientific Notation case 17: $c = $this->get(); if (ctype_digit(ord($c))) { $state = 17; break; } $state = 8; // At least 1 exponent digit was required break; // }}} // {{{ State 18 : Incomplete System Variable case 18: $c = $this->get(); if (ctype_alnum(ord($c)) || $c == '_') { $state = 18; break; } $state = 19; break; // }}} // {{{ State 19: Complete Sys Var case 19: $this->unget(); $this->tokText = substr($this->string, $this->tokStart, $this->tokLen); $this->skipText = substr($this->string, $this->tokAbsStart, $this->tokStart-$this->tokAbsStart); $this->tokStart = $this->tokPtr; return 'sys_var'; // }}} // {{{ State 999 : Unknown token. Revert to single char case 999: $this->revert(); $this->tokText = $this->get(); $this->skipText = substr($this->string, $this->tokAbsStart, $this->tokStart-$this->tokAbsStart); $this->tokStart = $this->tokPtr; return $this->tokText; // }}} // {{{ State 1000 : End Of Input case 1000: $this->tokText = '*end of input*'; $this->skipText = substr($this->string, $this->tokAbsStart, $this->tokStart-$this->tokAbsStart); $this->tokStart = $this->tokPtr; return null; // }}} } } } // }}} } http://cvs.php.net/viewvc.cgi/pear/SQL_Parser/Parser/ctype.php?view=markup&rev=1.1 Index: pear/SQL_Parser/Parser/ctype.php +++ pear/SQL_Parser/Parser/ctype.php <?php /** * PHP ctype compatibility functions. See the PHP ctype module for more * information on usage. * Note: These functions expect an integer argument, like the C versions * To use with a PHP character, use ord($c). These functions do not support * string arguments like their PHP extension counterparts * * @author John Millaway * @author Brent Cook <busterbcook@yahoo.com * @access public * @package SQL_Parser */ if (!extension_loaded('ctype')) { function ctype_alnum($c) { global $ctype__; return ($ctype__[$c] & 7); // (1 | 2 | 4) } function ctype_alpha($c) { global $ctype__; return ($ctype__[$c] & 3); // (1 | 2) } function ctype_cntrl($c) { global $ctype__; return ($ctype__[$c] & 40); } function ctype_digit($c) { global $ctype__; return ($ctype__[$c] & 4); } function ctype_graph($c) { global $ctype__; return ($ctype__[$c] & 27); // (20 | 1 | 2 | 4) } function ctype_lower($c) { global $ctype__; return ($ctype__[$c] & 2); } function ctype_print($c) { global $ctype__; return ($ctype__[$c] & 227); // (20 | 1 | 2 | 4 | 200) } function ctype_punct($c) { global $ctype__; return ($ctype__[$c] & 20); } function ctype_space($c) { global $ctype__; return ($ctype__[$c] & 10); } function ctype_upper($c) { global $ctype__; return ($ctype__[$c] & 1); } function ctype_xdigit($c) { global $ctype__; return ($ctype__[$c] & 104); // (100 | 4)); } $ctype__ = array(32,32,32,32,32,32,32,32,32,40,40,40,40,40,32,32,32,32,32,32,32,32,32, 32,32,32,32,32,32,32,32,32,-120,16,16,16,16,16,16,16,16,16,16,16,16, 16,16,16,4,4,4,4,4,4,4,4,4,4,16,16,16,16,16,16,16,65,65,65,65,65,65, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,16,16,16,16,16,16,66,66,66, 66,66,66,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,16,16,16,16,32,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0); } http://cvs.php.net/viewvc.cgi/pear/SQL_Parser/Parser/Dialect/ANSI.php?view=markup&rev=1.1 Index: pear/SQL_Parser/Parser/Dialect/ANSI.php +++ pear/SQL_Parser/Parser/Dialect/ANSI.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | Copyright (c) 2002-2004 Brent Cook | // +----------------------------------------------------------------------+ // | This library is free software; you can redistribute it and/or | // | modify it under the terms of the GNU Lesser General Public | // | License as published by the Free Software Foundation; either | // | version 2.1 of the License, or (at your option) any later version. | // | | // | This library is distributed in the hope that it will be useful, | // | but WITHOUT ANY WARRANTY; without even the implied warranty of | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | // | Lesser General Public License for more details. | // | | // | You should have received a copy of the GNU Lesser General Public | // | License along with this library; if not, write to the Free Software | // | Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA| // +----------------------------------------------------------------------+ // | Authors: Brent Cook <busterbcook@yahoo.com> | // +----------------------------------------------------------------------+ // // $Id: ANSI.php,v 1.1 2007/07/06 13:44:33 cybot Exp $ // // define tokens accepted by the SQL dialect. $dialect = array( 'commands' => array('alter','create','drop','select','delete','insert','update'), 'operators' => array('=','<>','<','<=','>','>=','like','clike','slike','not','is','in','between','and','or'), 'types' => array( 'character','char','varchar','nchar','bit','numeric','decimal', 'dec','integer','int','smallint','float','real','double','date', 'time','timestamp','interval','bool','boolean','set','enum','text' ), 'conjunctions' => array('by','as','on','into','from','where','with'), 'functions' => array('avg','count','max','min','sum','nextval','currval'), 'reserved' => array( 'absolute','action','add','all','allocate','and','any','are','asc', 'ascending','assertion','at','authorization','begin','bit_length', 'both','cascade','cascaded','case','cast','catalog','char_length', 'character_length','check','close','coalesce','collate','collation', 'column','commit','connect','connection','constraint','constraints', 'continue','convert','corresponding','cross','current','current_date', 'current_time','current_timestamp','current_user','cursor','day', 'deallocate','declare','default','deferrable','deferred','desc', 'descending','describe','descriptor','diagnostics','disconnect', 'distinct','domain','else','end','end-exec','escape','except','exception', 'exec','execute','exists','external','extract','false','fetch','first', 'for','foreign','found','full','get','global','go','goto','grant','group', 'having','hour','identity','immediate','indicator','initially','inner', 'input','insensitive','intersect','isolation','join','key','language', 'last','leading','left','level','limit','local','lower','match','minute', 'module','month','names','national','natural','next','no','null','nullif', 'octet_length','of','only','open','option','or','order','outer','output', 'overlaps','pad','partial','position','precision','prepare','preserve', 'primary','prior','privileges','procedure','public','read','references', 'relative','restrict','revoke','right','rollback','rows','schema','scroll', 'second','section','session','session_user','size','some','space','sql', 'sqlcode','sqlerror','sqlstate','substring','system_user','table','temporary', 'then','timezone_hour','timezone_minute','to','trailing','transaction', 'translate','translation','trim','true','union','unique','unknown','upper', 'usage','user','using','value','values','varying','view','when','whenever', 'work','write','year','zone','eoc' ), 'synonyms' => array( 'decimal'=>'numeric','dec'=>'numeric','numeric'=>'numeric', 'float'=>'float','real'=>'real','double'=>'real','int'=>'int', 'integer'=>'int','interval'=>'interval','smallint'=>'smallint', 'timestamp'=>'timestamp','bool'=>'bool','boolean'=>'bool', 'set'=>'set','enum'=>'enum','text'=>'text','char'=>'char', 'character'=>'char','varchar'=>'varchar','ascending'=>'asc', 'asc'=>'asc','descending'=>'desc','desc'=>'desc','date'=>'date', 'time'=>'time' ), 'lexeropts' => array('allowIdentFirstDigit' => false), 'parseropts' => array() ); http://cvs.php.net/viewvc.cgi/pear/SQL_Parser/Parser/Dialect/MySQL.php?view=markup&rev=1.1 Index: pear/SQL_Parser/Parser/Dialect/MySQL.php +++ pear/SQL_Parser/Parser/Dialect/MySQL.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | Copyright (c) 2004 John Griffin | // +----------------------------------------------------------------------+ // | This library is free software; you can redistribute it and/or | // | modify it under the terms of the GNU Lesser General Public | // | License as published by the Free Software Foundation; either | // | version 2.1 of the License, or (at your option) any later version. | // | | // | This library is distributed in the hope that it will be useful, | // | but WITHOUT ANY WARRANTY; without even the implied warranty of | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | // | Lesser General Public License for more details. | // | | // | You should have received a copy of the GNU Lesser General Public | // | License along with this library; if not, write to the Free Software | // | Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA| // +----------------------------------------------------------------------+ // | Authors: John Griffin <jgriffin316@netscape.net> | // +----------------------------------------------------------------------+ // // $Id: MySQL.php,v 1.1 2007/07/06 13:44:33 cybot Exp $ // // define tokens accepted by the SQL dialect. $dialect = array( 'commands' => array('alter', 'create', 'drop', 'select', 'delete', 'insert', 'update'), 'operators' => array('=', '<>', '<', '<=', '>', '>=', 'like', 'clike', 'slike', 'not', 'is', 'in', 'between'), 'types' => array( 'character', 'char', 'varchar', 'nchar', 'bit', 'numeric', 'decimal', 'dec', 'integer', 'int', 'tinyint', 'smallint', 'float', 'real', 'double', 'date', 'datetime', 'time', 'timestamp', 'interval', 'bool', 'boolean', 'set', 'enum', 'text' ), 'conjunctions' => array('by', 'as', 'on', 'into', 'from', 'where', 'with'), 'functions' => array( 'abs', 'acos', 'adddate', 'addtime', 'aes_encrypt', 'aes_decrypt', 'against', 'ascii', 'asin', 'atan', 'avg', 'benchmark', 'bin', 'bit_and', 'bit_or', 'bitcount', 'bitlength', 'cast', 'ceiling', 'char', 'char_length', 'character_length', 'charset', 'coalesce', 'coercibility', 'collation', 'compress', 'concat', 'concat_ws', 'conection_id', 'conv', 'convert', 'convert_tz', 'cos', 'cot', 'count', 'crc32', 'curdate', 'current_user', 'currval', 'curtime', 'database', 'date_add', 'date_diff', 'date_format', 'date_sub', 'day', 'dayname', 'dayofmonth', 'dayofweek', 'dayofyear', 'decode', 'default', 'degrees', 'des_decrypt', 'des_encrypt', 'elt', 'encode', 'encrypt', 'exp', 'export_set', 'extract', 'field', 'find_in_set', 'floor', 'format', 'found_rows', 'from_days', 'from_unixtime', 'get_format', 'get_lock', 'group_concat', 'greatest', 'hex', 'hour', 'if', 'ifnull', 'in', 'inet_aton', 'inet_ntoa', 'insert', 'instr', 'interval', 'is_free_lock', 'is_used_lock', 'last_day', 'last_insert_id', 'lcase', 'least', 'left', 'length', 'ln', 'load_file', 'localtime', 'localtimestamp', 'locate', 'log', 'log2', 'log10', 'lower', 'lpad', 'ltrim', 'make_set', 'makedate', 'maketime', 'master_pos_wait', 'match', 'max', 'md5', 'microsecond', 'mid', 'min', 'minute', 'mod', 'month', 'monthname', 'nextval', 'now', 'nullif', 'oct', 'octet_length', 'old_password', 'ord', 'password', 'period_add', 'period_diff', 'pi', 'position', 'pow', 'power', 'quarter', 'quote', 'radians', 'rand', 'release_lock', 'repeat', 'replace', 'reverse', 'right', 'round', 'row_count', 'rpad', 'rtrim', 'sec_to_time', 'second', 'session_user', 'sha', 'sha1', 'sign', 'soundex', 'space', 'sqrt', 'std', 'stddev', 'stddev_pop', 'stddev_samp', 'strcmp', 'str_to_date', 'subdate', 'substring', 'substring_index', 'subtime', 'sum', 'sysdate', 'system_user', 'tan', 'time', 'timediff', 'timestamp', 'timestampadd', 'timestampdiff', 'time_format', 'time_to_sec', 'to_days', 'trim', 'truncate', 'ucase', 'uncompress', 'uncompressed_length', 'unhex', 'unix_timestamp', 'upper', 'user', 'utc_date', 'utc_time', 'utc_timestamp', 'uuid', 'var_pop', 'var_samp', 'variance', 'version', 'week', 'weekday', 'weekofyear', 'year', 'yearweek' ), 'reserved' => array( 'add', 'all', 'alter', 'analyze', 'and', 'as', 'asc', 'asensitive', 'auto_increment', 'bdb', 'before', 'berkeleydb', 'between', 'bigint', 'binary', 'blob', 'both', 'by', 'call', 'cascade', 'case', 'change', 'char', 'character', 'check', 'collate', 'column', 'columns', 'condition', 'connection', 'constraint', 'continue', 'create', 'cross', 'current_date', 'current_time', 'current_timestamp', 'cursor', 'database', 'databases', 'day_hour', 'day_microsecond', 'day_minute', 'day_second', 'dec', 'decimal', 'declare', 'default', 'delayed', 'delete', 'desc', 'describe', 'deterministic', 'distinct', 'distinctrow', 'div', 'double', 'drop', 'else', 'elseif', 'enclosed', 'escaped', 'exists', 'exit', 'explain', 'false', 'fetch', 'fields', 'float', 'for', 'force', 'foreign', 'found', 'frac_second', 'from', 'fulltext', 'grant', 'group', 'having', 'high_priority', 'hour_microsecond', 'hour_minute', 'hour_second', 'if', 'ignore', 'in', 'index', 'infile', 'inner', 'innodb', 'inout', 'insensitive', 'insert', 'int', 'integer', 'interval', 'into', 'io_thread', 'is', 'iterate', 'join', 'key', 'keys', 'kill', 'leading', 'leave', 'left', 'like', 'limit', 'lines', 'load', 'localtime', 'localtimestamp', 'lock', 'long', 'longblob', 'longtext', 'loop', 'low_priority', 'master_server_id', 'match', 'mediumblob', 'mediumint', 'mediumtext', 'middleint', 'minute_microsecond', 'minute_second', 'mod', 'natural', 'not', 'no_write_to_binlog', 'null', 'numeric', 'on', 'optimize', 'option', 'optionally', 'or', 'order', 'out', 'outer', 'outfile', 'precision', 'primary', 'privileges', 'procedure', 'purge', 'read', 'real', 'references', 'regexp', 'rename', 'repeat', 'replace', 'require', 'restrict', 'return', 'revoke', 'right', 'rlike', 'second_microsecond', 'select', 'sensitive', 'separator', 'set', 'show', 'smallint', 'some', 'soname', 'spatial', 'specific', 'sql', 'sqlexception', 'sqlstate', 'sqlwarning', 'sql_big_result', 'sql_calc_found_rows', 'sql_small_result', 'sql_tsi_day', 'sql_tsi_frac_second', 'sql_tsi_hour', 'sql_tsi_minute', 'sql_tsi_month', 'sql_tsi_quarter', 'sql_tsi_second', 'sql_tsi_week', 'sql_tsi_year', 'ssl', 'starting', 'straight_join', 'striped', 'table', 'tables', 'terminated', 'then', 'timestampadd', 'timestampdiff', 'tinyblob', 'tinyint', 'tinytext', 'to', 'trailing', 'true', 'undo', 'union', 'unique', 'unlock', 'unsigned', 'update', 'usage', 'use', 'user_resources', 'using', 'utc_date', 'utc_time', 'utc_timestamp', 'values', 'varbinary', 'varchar', 'varcharacter', 'varying', 'when', 'where', 'while', 'with', 'write', 'xor', 'year_month', 'zerofill' ), 'synonyms' => array( 'decimal'=>'numeric', 'dec'=>'numeric', 'numeric'=>'numeric', 'float'=>'float', 'real'=>'real', 'double'=>'real', 'int'=>'int', 'integer'=>'int', 'interval'=>'interval', 'smallint'=>'smallint', 'tinyint'=>'tinyint', 'timestamp'=>'timestamp', 'bool'=>'bool', 'boolean'=>'bool', 'set'=>'set', 'enum'=>'enum', 'text'=>'text', 'char'=>'char', 'character'=>'char', 'varchar'=>'varchar', 'ascending'=>'asc', 'asc'=>'asc', 'descending'=>'desc', 'desc'=>'desc', 'date'=>'date', 'time'=>'time' ), 'lexeropts' => array('allowIdentFirstDigit'=>true), 'parseropts' => array() );