update parser_sql

This commit is contained in:
shreesh arora
2021-05-03 01:06:30 +05:30
parent b7ef2391cc
commit d91def3d84
172 changed files with 8743 additions and 2571 deletions

View File

@@ -4,49 +4,76 @@
*
* This file implements an abstract processor, which implements some helper functions.
*
* Copyright (c) 2010-2012, Justin Swanhart
* with contributions by André Rothe <arothe@phosco.info, phosco@gmx.de>
* PHP version 5
*
* LICENSE:
* Copyright (c) 2010-2014 Justin Swanhart and André Rothe
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author André Rothe <andre.rothe@phosco.info>
* @copyright 2010-2014 Justin Swanhart and André Rothe
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version SVN: $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
require_once dirname(__FILE__) . '/../utils/ExpressionType.php';
require_once dirname(__FILE__) . '/../lexer/PHPSQLLexer.php';
namespace PHPSQLParser\processors;
use PHPSQLParser\lexer\PHPSQLLexer;
use PHPSQLParser\Options;
use PHPSQLParser\utils\ExpressionType;
/**
*
* This class processes contains some general functions for a processor.
*
* @author arothe
*
* This class contains some general functions for a processor.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
abstract class AbstractProcessor {
/**
* @var Options
*/
protected $options;
/**
* AbstractProcessor constructor.
*
* @param Options $options
*/
public function __construct(Options $options = null)
{
$this->options = $options;
}
/**
* This function implements the main functionality of a processor class.
* Always use default valuses for additional parameters within overridden functions.
*/
abstract public function process($tokens);
public abstract function process($tokens);
/**
* this function splits up a SQL statement into easy to "parse"
@@ -59,26 +86,83 @@ abstract class AbstractProcessor {
/**
* Revokes the quoting characters from an expression
* Possibibilies:
* `a`
* 'a'
* "a"
* `a`.`b`
* `a.b`
* a.`b`
* `a`.b
* It is also possible to have escaped quoting characters
* within an expression part:
* `a``b` => a`b
* And you can use whitespace between the parts:
* a . `b` => [a,b]
*/
protected function revokeQuotation($sql) {
$result = trim($sql);
$tmp = trim($sql);
$result = array();
if (($result[0] === '`') && ($result[strlen($result) - 1] === '`')) {
$result = substr($result, 1, -1);
return trim(str_replace('``', '`', $result));
$quote = false;
$start = 0;
$i = 0;
$len = strlen($tmp);
while ($i < $len) {
$char = $tmp[$i];
switch ($char) {
case '`':
case '\'':
case '"':
if ($quote === false) {
// start
$quote = $char;
$start = $i + 1;
break;
}
if ($quote !== $char) {
break;
}
if (isset($tmp[$i + 1]) && ($quote === $tmp[$i + 1])) {
// escaped
$i++;
break;
}
// end
$char = substr($tmp, $start, $i - $start);
$result[] = str_replace($quote . $quote, $quote, $char);
$start = $i + 1;
$quote = false;
break;
case '.':
if ($quote === false) {
// we have found a separator
$char = trim(substr($tmp, $start, $i - $start));
if ($char !== '') {
$result[] = $char;
}
$start = $i + 1;
}
break;
default:
// ignore
break;
}
$i++;
}
if (($result[0] === "'") && ($result[strlen($result) - 1] === "'")) {
$result = substr($result, 1, -1);
return trim(str_replace("''", "'", $result));
if ($quote === false && ($start < $len)) {
$char = trim(substr($tmp, $start, $i - $start));
if ($char !== '') {
$result[] = $char;
}
}
if (($result[0] === "\"") && ($result[strlen($result) - 1] === "\"")) {
$result = substr($result, 1, -1);
return trim(str_replace("\"\"", "\"", $result));
}
return $sql;
return array('delim' => (count($result) === 1 ? false : '.'), 'parts' => $result);
}
/**
@@ -89,33 +173,47 @@ abstract class AbstractProcessor {
$parenthesisRemoved = 0;
$trim = trim($token);
if ($trim !== "" && $trim[0] === "(") { // remove only one parenthesis pair now!
if ($trim !== '' && $trim[0] === '(') { // remove only one parenthesis pair now!
$parenthesisRemoved++;
$trim[0] = " ";
$trim[0] = ' ';
$trim = trim($trim);
}
$parenthesis = $parenthesisRemoved;
$i = 0;
$string = 0;
// Whether a string was opened or not, and with which character it was open (' or ")
$stringOpened = '';
while ($i < strlen($trim)) {
if ($trim[$i] === "\\") {
$i += 2; # an escape character, the next character is irrelevant
$i += 2; // an escape character, the next character is irrelevant
continue;
}
if ($trim[$i] === "'" || $trim[$i] === '"') {
$string++;
if ($trim[$i] === "'") {
if ($stringOpened === '') {
$stringOpened = "'";
} elseif ($stringOpened === "'") {
$stringOpened = '';
}
}
if (($string % 2 === 0) && ($trim[$i] === "(")) {
if ($trim[$i] === '"') {
if ($stringOpened === '') {
$stringOpened = '"';
} elseif ($stringOpened === '"') {
$stringOpened = '';
}
}
if (($stringOpened === '') && ($trim[$i] === '(')) {
$parenthesis++;
}
if (($string % 2 === 0) && ($trim[$i] === ")")) {
if (($stringOpened === '') && ($trim[$i] === ')')) {
if ($parenthesis == $parenthesisRemoved) {
$trim[$i] = " ";
$trim[$i] = ' ';
$parenthesisRemoved--;
}
$parenthesis--;
@@ -127,11 +225,11 @@ abstract class AbstractProcessor {
protected function getVariableType($expression) {
// $expression must contain only upper-case characters
if ($expression[1] !== "@") {
if ($expression[1] !== '@') {
return ExpressionType::USER_VARIABLE;
}
$type = substr($expression, 2, strpos($expression, ".", 2));
$type = substr($expression, 2, strpos($expression, '.', 2));
switch ($type) {
case 'GLOBAL':
@@ -149,16 +247,16 @@ abstract class AbstractProcessor {
}
protected function isCommaToken($token) {
return (trim($token) === ",");
return (trim($token) === ',');
}
protected function isWhitespaceToken($token) {
return (trim($token) === "");
return (trim($token) === '');
}
protected function isCommentToken($token) {
return isset($token[0]) && isset($token[1])
&& (($token[0] === '-' && $token[1] === '-') || ($token[0] === '/' && $token[1] === '*'));
&& (($token[0] === '-' && $token[1] === '-') || ($token[0] === '/' && $token[1] === '*'));
}
protected function isColumnReference($out) {
@@ -177,6 +275,10 @@ abstract class AbstractProcessor {
return (isset($out['expr_type']) && $out['expr_type'] === ExpressionType::AGGREGATE_FUNCTION);
}
protected function isCustomFunction($out) {
return (isset($out['expr_type']) && $out['expr_type'] === ExpressionType::CUSTOM_FUNCTION);
}
protected function isFunction($out) {
return (isset($out['expr_type']) && $out['expr_type'] === ExpressionType::SIMPLE_FUNCTION);
}
@@ -193,21 +295,36 @@ abstract class AbstractProcessor {
return (isset($out['expr_type']) && $out['expr_type'] === ExpressionType::SUBQUERY);
}
protected function isComment($out) {
return (isset($out['expr_type']) && $out['expr_type'] === ExpressionType::COMMENT);
}
public function processComment($expression) {
$result = array();
$result['expr_type'] = ExpressionType::COMMENT;
$result['value'] = $expression;
return $result;
}
/**
* translates an array of objects into an associative array
*/
public function toArray($tokenList) {
$expr = array();
foreach ($tokenList as $token) {
$expr[] = $token->toArray();
if ($token instanceof \PHPSQLParser\utils\ExpressionToken) {
$expr[] = $token->toArray();
} else {
$expr[] = $token;
}
}
return (empty($expr) ? false : $expr);
return $expr;
}
protected function array_insert_after($array, $key, $entry) {
$idx = array_search($key, array_keys($array));
$array = array_slice($array, 0, $idx + 1, true) + $entry
+ array_slice($array, $idx + 1, count($array) - 1, true);
+ array_slice($array, $idx + 1, count($array) - 1, true);
return $array;
}
}

View File

@@ -0,0 +1,100 @@
<?php
/**
* BracketProcessor.php
*
* This file implements the processor for the parentheses around the statements.
*
* PHP version 5
*
* LICENSE:
* Copyright (c) 2010-2014 Justin Swanhart and André Rothe
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author André Rothe <andre.rothe@phosco.info>
* @copyright 2010-2014 Justin Swanhart and André Rothe
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version SVN: $Id$
*
*/
namespace PHPSQLParser\processors;
use PHPSQLParser\utils\ExpressionType;
/**
* This class processes the parentheses around the statement.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class BracketProcessor extends AbstractProcessor {
protected function processTopLevel($sql) {
$processor = new DefaultProcessor($this->options);
return $processor->process($sql);
}
public function process($tokens) {
$token = $this->removeParenthesisFromStart($tokens[0]);
$subtree = $this->processTopLevel($token);
$remainingExpressions = $this->getRemainingNotBracketExpression($subtree);
if (isset($subtree['BRACKET'])) {
$subtree = $subtree['BRACKET'];
}
if (isset($subtree['SELECT'])) {
$subtree = array(
array('expr_type' => ExpressionType::QUERY, 'base_expr' => $token, 'sub_tree' => $subtree));
}
return array(
array('expr_type' => ExpressionType::BRACKET_EXPRESSION, 'base_expr' => trim($tokens[0]),
'sub_tree' => $subtree, 'remaining_expressions' => $remainingExpressions));
}
private function getRemainingNotBracketExpression($subtree)
{
// https://github.com/greenlion/PHP-SQL-Parser/issues/279
// https://github.com/sinri/PHP-SQL-Parser/commit/eac592a0e19f1df6f420af3777a6d5504837faa7
// as there is no pull request for 279 by the user. His solution works and tested.
if (empty($subtree)) $subtree = array();// as a fix by Sinri 20180528
$remainingExpressions = array();
$ignoredKeys = array('BRACKET', 'SELECT', 'FROM');
$subtreeKeys = array_keys($subtree);
foreach($subtreeKeys as $key) {
if(!in_array($key, $ignoredKeys)) {
$remainingExpressions[$key] = $subtree[$key];
}
}
return $remainingExpressions;
}
}
?>

View File

@@ -30,10 +30,8 @@
* DAMAGE.
*/
require_once dirname(__FILE__) . '/AbstractProcessor.php';
require_once dirname(__FILE__) . '/ReferenceDefinitionProcessor.php';
require_once dirname(__FILE__) . '/ExpressionListProcessor.php';
require_once dirname(__FILE__) . '/../utils/ExpressionType.php';
namespace PHPSQLParser\processors;
use PHPSQLParser\utils\ExpressionType;
/**
*
@@ -45,17 +43,20 @@ require_once dirname(__FILE__) . '/../utils/ExpressionType.php';
class ColumnDefinitionProcessor extends AbstractProcessor {
protected function processExpressionList($parsed) {
$processor = new ExpressionListProcessor();
return $processor->process($parsed);
$processor = new ExpressionListProcessor($this->options);
$expr = $this->removeParenthesisFromStart($parsed);
$expr = $this->splitSQLIntoTokens($expr);
$expr = $this->removeComma($expr);
return $processor->process($expr);
}
protected function processReferenceDefinition($parsed) {
$processor = new ReferenceDefinitionProcessor();
$processor = new ReferenceDefinitionProcessor($this->options);
return $processor->process($parsed);
}
protected function removeComma($tokens) {
$res = array();
$res = array();
foreach ($tokens as $token) {
if (trim($token) !== ',') {
$res[] = $token;
@@ -63,7 +64,7 @@ class ColumnDefinitionProcessor extends AbstractProcessor {
}
return $res;
}
protected function buildColDef($expr, $base_expr, $options, $refs, $key) {
$expr = array('expr_type' => ExpressionType::COLUMN_TYPE, 'base_expr' => $base_expr, 'sub_tree' => $expr);
@@ -134,24 +135,32 @@ class ColumnDefinitionProcessor extends AbstractProcessor {
continue 2;
case 'UNSIGNED':
$last = array_pop($expr);
$last['unsigned'] = true;
$expr[] = $last;
foreach (array_reverse(array_keys($expr)) as $i) {
if (isset($expr[$i]['expr_type']) && (ExpressionType::DATA_TYPE === $expr[$i]['expr_type'])) {
$expr[$i]['unsigned'] = true;
break;
}
}
$options['sub_tree'][] = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim);
continue 2;
case 'ZEROFILL':
$last = array_pop($expr);
$last['zerofill'] = true;
$expr[] = $last;
$options['sub_tree'][] = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim);
continue 2;
case 'BIT':
case 'TINYBIT':
case 'TINYINT':
case 'SMALLINT':
case 'MEDIUMINT':
case 'INT':
case 'INTEGER':
case 'BIGINT':
case 'BOOL':
case 'BOOLEAN':
$expr[] = array('expr_type' => ExpressionType::DATA_TYPE, 'base_expr' => $trim, 'unsigned' => false,
'zerofill' => false, 'length' => false);
$currCategory = 'SINGLE_PARAM_PARENTHESIS';
@@ -194,11 +203,16 @@ class ColumnDefinitionProcessor extends AbstractProcessor {
$prevCategory = $upper;
continue 2;
case 'YEAR':
$expr[] = array('expr_type' => ExpressionType::DATA_TYPE, 'base_expr' => $trim, 'length' => false);
$currCategory = 'SINGLE_PARAM_PARENTHESIS';
$prevCategory = $upper;
continue 2;
case 'DATE':
case 'TIME':
case 'TIMESTAMP':
case 'DATETIME':
case 'YEAR':
case 'TINYBLOB':
case 'BLOB':
case 'MEDIUMBLOB':
@@ -217,7 +231,6 @@ class ColumnDefinitionProcessor extends AbstractProcessor {
continue 2;
case 'ENUM':
case 'SET':
$currCategory = 'MULTIPLE_PARAM_PARENTHESIS';
$prevCategory = 'TEXT';
$expr[] = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim, 'sub_tree' => false);
@@ -238,40 +251,24 @@ class ColumnDefinitionProcessor extends AbstractProcessor {
continue 2;
case 'CHARACTER':
if ($prevCategory === 'TEXT') {
$parsed = array('expr_type' => ExpressionType::DATA_TYPE, 'base_expr' => $trim);
$expr[] = array('expr_type' => ExpressionType::CHARSET, 'base_expr' => substr($base_expr, 0, -1),
'sub_tree' => $parsed);
$base_expr = $token;
$currCategory = 'CHARSET';
continue 2;
}
// else ?
break;
$currCategory = 'CHARSET';
$options['sub_tree'][] = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim);
continue 2;
case 'SET':
if ($currCategory === 'CHARSET') {
// TODO: is it necessary to set special properties like the name or collation?
$parsed = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim);
$last = array_pop($expr);
$last['sub_tree'][] = $parsed;
$expr[] = $last;
continue 2;
}
// else ?
break;
if ($currCategory == 'CHARSET') {
$options['sub_tree'][] = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim);
} else {
$currCategory = 'MULTIPLE_PARAM_PARENTHESIS';
$prevCategory = 'TEXT';
$expr[] = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim, 'sub_tree' => false);
}
continue 2;
case 'COLLATE':
if ($prevCategory === 'TEXT') {
$parsed = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim);
$expr[] = array('expr_type' => ExpressionType::COLLATE, 'base_expr' => substr($base_expr, 0, -1),
'sub_tree' => $parsed);
$base_expr = $token;
$currCategory = 'COLLATION';
continue 2;
}
// else ?
break;
$currCategory = $upper;
$options['sub_tree'][] = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim);
continue 2;
case 'NOT':
case 'NULL':
@@ -361,32 +358,24 @@ class ColumnDefinitionProcessor extends AbstractProcessor {
case 'COLLATE':
// this is the collation name
$parsed = array('expr_type' => ExpressionType::CONSTANT, 'base_expr' => $trim);
$last = array_pop($expr);
$last['sub_tree'][] = $parsed;
$t = $base_expr;
$base_expr = $last['base_expr'] . $base_expr;
$last['base_expr'] = $t;
$options['sub_tree'][] = array('expr_type' => ExpressionType::COLLATE, 'base_expr' => $trim);
$options['collate'] = $trim;
$currCategory = $prevCategory;
break;
case 'CHARSET':
// this is the character set name
$parsed = array('expr_type' => ExpressionType::CONSTANT, 'base_expr' => $trim);
$last = array_pop($expr);
$last['sub_tree'][] = $parsed;
$t = $base_expr;
$base_expr = $last['base_expr'] . $base_expr;
$last['base_expr'] = $t;
$options['sub_tree'][] = array('expr_type' => ExpressionType::CHARSET, 'base_expr' => $trim);
$options['charset'] = $trim;
$currCategory = $prevCategory;
break;
break;
case 'SINGLE_PARAM_PARENTHESIS':
$parsed = $this->removeParenthesisFromStart($trim);
$parsed = array('expr_type' => ExpressionType::CONSTANT, 'base_expr' => trim($parsed));
$last = array_pop($expr);
$last['length'] = $parsed['base_expr'];
$expr[] = $last;
$expr[] = array('expr_type' => ExpressionType::BRACKET_EXPRESSION, 'base_expr' => $trim,
'sub_tree' => array($parsed));
@@ -395,17 +384,12 @@ class ColumnDefinitionProcessor extends AbstractProcessor {
case 'TWO_PARAM_PARENTHESIS':
// maximum of two parameters
$parsed = $this->removeParenthesisFromStart($trim);
$parsed = $this->splitSQLIntoTokens($parsed);
$parsed = $this->removeComma($parsed);
$parsed = $this->processExpressionList($parsed);
// TODO: check that
$parsed = $this->processExpressionList($trim);
$last = array_pop($expr);
$last['length'] = $parsed[0]['base_expr'];
$last['decimals'] = isset($parsed[1]) ? $parsed[1]['base_expr'] : false;
$expr[] = $last;
$expr[] = array('expr_type' => ExpressionType::BRACKET_EXPRESSION, 'base_expr' => $trim,
'sub_tree' => $parsed);
@@ -414,14 +398,17 @@ class ColumnDefinitionProcessor extends AbstractProcessor {
case 'MULTIPLE_PARAM_PARENTHESIS':
// some parameters
$parsed = $this->removeParenthesisFromStart($trim);
$parsed = $this->splitSQLIntoTokens($parsed);
$parsed = $this->removeComma($parsed);
$this->processExpressionList($parsed);
$parsed = $this->processExpressionList($trim);
$last = array_pop($expr);
$last['sub_tree'] = array('expr_type' => ExpressionType::BRACKET_EXPRESSION, 'base_expr' => $trim,
'sub_tree' => $parsed);
$subTree = array('expr_type' => ExpressionType::BRACKET_EXPRESSION, 'base_expr' => $trim,
'sub_tree' => $parsed);
if ($this->options->getConsistentSubtrees()) {
$subTree = array($subTree);
}
$last['sub_tree'] = $subTree;
$expr[] = $last;
$currCategory = $prevCategory;
break;

View File

@@ -30,8 +30,8 @@
* DAMAGE.
*/
require_once dirname(__FILE__) . '/AbstractProcessor.php';
require_once dirname(__FILE__) . '/../utils/ExpressionType.php';
namespace PHPSQLParser\processors;
use PHPSQLParser\utils\ExpressionType;
/**
*

View File

@@ -4,54 +4,80 @@
*
* This file implements the processor for the create definition within the TABLE statements.
*
* Copyright (c) 2010-2012, Justin Swanhart
* with contributions by André Rothe <arothe@phosco.info, phosco@gmx.de>
* PHP version 5
*
* LICENSE:
* Copyright (c) 2010-2014 Justin Swanhart and André Rothe
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author André Rothe <andre.rothe@phosco.info>
* @copyright 2010-2014 Justin Swanhart and André Rothe
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version SVN: $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
require_once dirname(__FILE__) . '/AbstractProcessor.php';
require_once dirname(__FILE__) . '/ColumnDefinitionProcessor.php';
require_once dirname(__FILE__) . '/IndexColumnListProcessor.php';
require_once dirname(__FILE__) . '/ReferenceDefinitionProcessor.php';
require_once dirname(__FILE__) . '/../utils/ExpressionType.php';
namespace PHPSQLParser\processors;
use PHPSQLParser\utils\ExpressionType;
/**
*
* This class processes the create definition of the TABLE statements.
*
* @author arothe
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class CreateDefinitionProcessor extends AbstractProcessor {
protected function processExpressionList($parsed) {
$processor = new ExpressionListProcessor($this->options);
return $processor->process($parsed);
}
protected function processIndexColumnList($parsed) {
$processor = new IndexColumnListProcessor($this->options);
return $processor->process($parsed);
}
protected function processColumnDefinition($parsed) {
$processor = new ColumnDefinitionProcessor($this->options);
return $processor->process($parsed);
}
protected function processReferenceDefinition($parsed) {
$processor = new ReferenceDefinitionProcessor($this->options);
return $processor->process($parsed);
}
protected function correctExpressionType(&$expr) {
$type = ExpressionType::EXPRESSION;
if (!isset($expr[0]) || !isset($expr[0]['expr_type'])) {
return $type;
}
# replace the constraint type with a more descriptive one
// replace the constraint type with a more descriptive one
switch ($expr[0]['expr_type']) {
case ExpressionType::CONSTRAINT:
@@ -74,9 +100,9 @@ class CreateDefinitionProcessor extends AbstractProcessor {
public function process($tokens) {
$base_expr = "";
$prevCategory = "";
$currCategory = "";
$base_expr = '';
$prevCategory = '';
$currCategory = '';
$expr = array();
$result = array();
$skip = 0;
@@ -91,7 +117,7 @@ class CreateDefinitionProcessor extends AbstractProcessor {
continue;
}
if ($trim === "") {
if ($trim === '') {
continue;
}
@@ -110,36 +136,36 @@ class CreateDefinitionProcessor extends AbstractProcessor {
continue 2;
case 'FOREIGN':
if ($prevCategory === "" || $prevCategory === "CONSTRAINT") {
if ($prevCategory === '' || $prevCategory === 'CONSTRAINT') {
$expr[] = array('expr_type' => ExpressionType::FOREIGN_KEY, 'base_expr' => $trim);
$currCategory = $upper;
continue 2;
}
# else ?
// else ?
break;
case 'PRIMARY':
if ($prevCategory === "" || $prevCategory === "CONSTRAINT") {
# next one is KEY
if ($prevCategory === '' || $prevCategory === 'CONSTRAINT') {
// next one is KEY
$expr[] = array('expr_type' => ExpressionType::PRIMARY_KEY, 'base_expr' => $trim);
$currCategory = $upper;
continue 2;
}
# else ?
// else ?
break;
case 'UNIQUE':
if ($prevCategory === "" || $prevCategory === "CONSTRAINT") {
# next one is KEY
if ($prevCategory === '' || $prevCategory === 'CONSTRAINT' || $prevCategory === 'INDEX_COL_LIST') {
// next one is KEY
$expr[] = array('expr_type' => ExpressionType::UNIQUE_IDX, 'base_expr' => $trim);
$currCategory = $upper;
continue 2;
}
# else ?
// else ?
break;
case 'KEY':
# the next one is an index name
// the next one is an index name
if ($currCategory === 'PRIMARY' || $currCategory === 'FOREIGN' || $currCategory === 'UNIQUE') {
$expr[] = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim);
continue 2;
@@ -173,7 +199,7 @@ class CreateDefinitionProcessor extends AbstractProcessor {
continue 2;
case 'WITH':
# starts an index option
// starts an index option
if ($currCategory === 'INDEX_COL_LIST') {
$option = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim);
$expr[] = array('expr_type' => ExpressionType::INDEX_PARSER,
@@ -186,7 +212,7 @@ class CreateDefinitionProcessor extends AbstractProcessor {
break;
case 'KEY_BLOCK_SIZE':
# starts an index option
// starts an index option
if ($currCategory === 'INDEX_COL_LIST') {
$option = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim);
$expr[] = array('expr_type' => ExpressionType::INDEX_SIZE,
@@ -199,7 +225,7 @@ class CreateDefinitionProcessor extends AbstractProcessor {
break;
case 'USING':
# starts an index option
// starts an index option
if ($currCategory === 'INDEX_COL_LIST' || $currCategory === 'PRIMARY') {
$option = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim);
$expr[] = array('base_expr' => substr($base_expr, 0, -strlen($token)), 'trim' => $trim,
@@ -208,18 +234,18 @@ class CreateDefinitionProcessor extends AbstractProcessor {
$currCategory = 'INDEX_TYPE';
continue 2;
}
# else ?
// else ?
break;
case 'REFERENCES':
if ($currCategory === 'INDEX_COL_LIST' && $prevCategory === 'FOREIGN') {
$processor = new ReferenceDefinitionProcessor();
$refs = $processor->process(array_slice($tokens, $k - 1, null, true));
$refs = $this->processReferenceDefinition(array_slice($tokens, $k - 1, null, true));
$skip = $refs['till'] - $k;
unset($refs['till']);
$expr[] = $refs;
$currCategory = $upper;
}
// else ?
break;
case 'BTREE':
@@ -231,16 +257,16 @@ class CreateDefinitionProcessor extends AbstractProcessor {
'sub_tree' => $last['sub_tree']);
$base_expr = $last['base_expr'] . $base_expr;
# FIXME: it could be wrong for index_type within index_option
// FIXME: it could be wrong for index_type within index_option
$currCategory = $last['category'];
continue 2;
}
#else
// else ?
break;
case '=':
if ($currCategory === 'INDEX_SIZE') {
# the optional character between KEY_BLOCK_SIZE and the numeric constant
// the optional character between KEY_BLOCK_SIZE and the numeric constant
$last = array_pop($expr);
$last['sub_tree'][] = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim);
$expr[] = $last;
@@ -255,16 +281,16 @@ class CreateDefinitionProcessor extends AbstractProcessor {
$expr[] = $last;
continue 2;
}
# else?
// else ?
break;
case ',':
# this starts the next definition
// this starts the next definition
$type = $this->correctExpressionType($expr);
$result['create-def'][] = array('expr_type' => $type,
'base_expr' => trim(substr($base_expr, 0, -strlen($token))),
'sub_tree' => $expr);
$base_expr = "";
$base_expr = '';
$expr = array();
break;
@@ -272,36 +298,34 @@ class CreateDefinitionProcessor extends AbstractProcessor {
switch ($currCategory) {
case 'LIKE':
# this is the tablename after LIKE
// this is the tablename after LIKE
$expr[] = array('expr_type' => ExpressionType::TABLE, 'table' => $trim, 'base_expr' => $trim,
'no_quotes' => $this->revokeQuotation($trim));
break;
case 'PRIMARY':
if ($upper[0] === '(' && substr($upper, -1) === ')') {
# the column list
$processor = new IndexColumnListProcessor();
$cols = $processor->process($this->removeParenthesisFromStart($trim));
// the column list
$cols = $this->processIndexColumnList($this->removeParenthesisFromStart($trim));
$expr[] = array('expr_type' => ExpressionType::COLUMN_LIST, 'base_expr' => $trim,
'sub_tree' => $cols);
$prevCategory = $currCategory;
$currCategory = "INDEX_COL_LIST";
$currCategory = 'INDEX_COL_LIST';
continue 3;
}
# else?
// else?
break;
case 'FOREIGN':
if ($upper[0] === '(' && substr($upper, -1) === ')') {
$processor = new IndexColumnListProcessor();
$cols = $processor->process($this->removeParenthesisFromStart($trim));
$cols = $this->processIndexColumnList($this->removeParenthesisFromStart($trim));
$expr[] = array('expr_type' => ExpressionType::COLUMN_LIST, 'base_expr' => $trim,
'sub_tree' => $cols);
$prevCategory = $currCategory;
$currCategory = "INDEX_COL_LIST";
$currCategory = 'INDEX_COL_LIST';
continue 3;
}
# index name
// index name
$expr[] = array('expr_type' => ExpressionType::CONSTANT, 'base_expr' => $trim);
continue 3;
@@ -309,20 +333,19 @@ class CreateDefinitionProcessor extends AbstractProcessor {
case 'UNIQUE':
case 'INDEX':
if ($upper[0] === '(' && substr($upper, -1) === ')') {
$processor = new IndexColumnListProcessor();
$cols = $processor->process($this->removeParenthesisFromStart($trim));
$cols = $this->processIndexColumnList($this->removeParenthesisFromStart($trim));
$expr[] = array('expr_type' => ExpressionType::COLUMN_LIST, 'base_expr' => $trim,
'sub_tree' => $cols);
$prevCategory = $currCategory;
$currCategory = "INDEX_COL_LIST";
$currCategory = 'INDEX_COL_LIST';
continue 3;
}
# index name
// index name
$expr[] = array('expr_type' => ExpressionType::CONSTANT, 'base_expr' => $trim);
continue 3;
case 'CONSTRAINT':
# constraint name
// constraint name
$last = array_pop($expr);
$last['base_expr'] = $base_expr;
$last['sub_tree'] = array('expr_type' => ExpressionType::CONSTANT, 'base_expr' => $trim);
@@ -330,7 +353,7 @@ class CreateDefinitionProcessor extends AbstractProcessor {
continue 3;
case 'INDEX_PARSER':
# index parser name
// index parser name
$last = array_pop($expr);
$last['sub_tree'][] = array('expr_type' => ExpressionType::CONSTANT, 'base_expr' => $trim);
$expr[] = array('expr_type' => ExpressionType::INDEX_PARSER, 'base_expr' => $base_expr,
@@ -340,7 +363,7 @@ class CreateDefinitionProcessor extends AbstractProcessor {
continue 3;
case 'INDEX_SIZE':
# index key block size numeric constant
// index key block size numeric constant
$last = array_pop($expr);
$last['sub_tree'][] = array('expr_type' => ExpressionType::CONSTANT, 'base_expr' => $trim);
$expr[] = array('expr_type' => ExpressionType::INDEX_SIZE, 'base_expr' => $base_expr,
@@ -351,28 +374,26 @@ class CreateDefinitionProcessor extends AbstractProcessor {
case 'CHECK':
if ($upper[0] === '(' && substr($upper, -1) === ')') {
$processor = new ExpressionListProcessor();
$unparsed = $this->splitSQLIntoTokens($this->removeParenthesisFromStart($trim));
$parsed = $processor->process($unparsed);
$parsed = $this->splitSQLIntoTokens($this->removeParenthesisFromStart($trim));
$parsed = $this->processExpressionList($parsed);
$expr[] = array('expr_type' => ExpressionType::BRACKET_EXPRESSION, 'base_expr' => $trim,
'sub_tree' => $parsed);
}
# else?
// else?
break;
case '':
# if the currCategory is empty, we have an unknown token,
# which is a column reference
// if the currCategory is empty, we have an unknown token,
// which is a column reference
$expr[] = array('expr_type' => ExpressionType::COLREF, 'base_expr' => $trim,
'no_quotes' => $this->revokeQuotation($trim));
$currCategory = 'COLUMN_NAME';
continue 3;
case 'COLUMN_NAME':
# the column-definition
# it stops on a comma or on a parenthesis
$processor = new ColumnDefinitionProcessor();
$parsed = $processor->process(array_slice($tokens, $k, null, true), $expr);
// the column-definition
// it stops on a comma or on a parenthesis
$parsed = $this->processColumnDefinition(array_slice($tokens, $k, null, true));
$skip = $parsed['till'] - $k;
unset($parsed['till']);
$expr[] = $parsed;
@@ -380,7 +401,7 @@ class CreateDefinitionProcessor extends AbstractProcessor {
break;
default:
# ?
// ?
break;
}
break;
@@ -394,4 +415,4 @@ class CreateDefinitionProcessor extends AbstractProcessor {
return $result;
}
}
?>
?>

View File

@@ -2,81 +2,112 @@
/**
* CreateProcessor.php
*
* This file implements the processor for the CREATE TABLE statements.
* This file implements the processor for the CREATE statements.
*
* Copyright (c) 2010-2012, Justin Swanhart
* with contributions by André Rothe <arothe@phosco.info, phosco@gmx.de>
* PHP version 5
*
* LICENSE:
* Copyright (c) 2010-2014 Justin Swanhart and André Rothe
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author André Rothe <andre.rothe@phosco.info>
* @copyright 2010-2014 Justin Swanhart and André Rothe
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version SVN: $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
require_once dirname(__FILE__) . '/AbstractProcessor.php';
require_once dirname(__FILE__) . '/../utils/ExpressionType.php';
namespace PHPSQLParser\processors;
use PHPSQLParser\utils\ExpressionType;
/**
*
* This class processes the CREATE statements.
*
* @author arothe
*
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class CreateProcessor extends AbstractProcessor {
public function process($tokens) {
$result = array();
$result = $expr = array();
$base_expr = "";
foreach ($tokens as $token) {
$trim = strtoupper(trim($token));
$trim = trim($token);
$base_expr .= $token;
if ($trim === "") {
continue;
}
switch ($trim) {
$upper = strtoupper($trim);
switch ($upper) {
case 'TEMPORARY':
// CREATE TEMPORARY TABLE
$result['expr_type'] = ExpressionType::TEMPORARY_TABLE;
$result['not-exists'] = false;
$expr[] = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim);
break;
case 'TABLE':
// CREATE TABLE
$result['expr_type'] = ExpressionType::TABLE;
$result['not-exists'] = false;
$expr[] = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim);
break;
case 'INDEX':
// CREATE INDEX
$result['expr_type'] = ExpressionType::INDEX;
$expr[] = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim);
break;
case 'UNIQUE':
case 'FULLTEXT':
case 'SPATIAL':
// options of CREATE INDEX
$result['base_expr'] = $result['expr_type'] = false;
$result['constraint'] = $upper;
$expr[] = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim);
break;
case 'IF':
// option of CREATE TABLE
$expr[] = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim);
break;
case 'NOT':
// option of CREATE TABLE
$expr[] = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim);
break;
case 'EXISTS':
// option of CREATE TABLE
$result['not-exists'] = true;
$expr[] = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim);
break;

View File

@@ -4,62 +4,85 @@
*
* This file implements the processor the unparsed sql string given by the user.
*
* Copyright (c) 2010-2012, Justin Swanhart
* with contributions by André Rothe <arothe@phosco.info, phosco@gmx.de>
* PHP version 5
*
* LICENSE:
* Copyright (c) 2010-2014 Justin Swanhart and André Rothe
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author André Rothe <andre.rothe@phosco.info>
* @copyright 2010-2014 Justin Swanhart and André Rothe
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version SVN: $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
require_once(dirname(__FILE__) . '/AbstractProcessor.php');
require_once(dirname(__FILE__) . '/UnionProcessor.php');
require_once(dirname(__FILE__) . '/SQLProcessor.php');
namespace PHPSQLParser\processors;
/**
*
* This class processes the incoming sql string.
*
* @author arothe
*
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class DefaultProcessor extends AbstractProcessor {
protected function isUnion($tokens) {
return UnionProcessor::isUnion($tokens);
}
protected function processUnion($tokens) {
// this is the highest level lexical analysis. This is the part of the
// code which finds UNION and UNION ALL query parts
$processor = new UnionProcessor($this->options);
return $processor->process($tokens);
}
protected function processSQL($tokens) {
$processor = new SQLProcessor($this->options);
return $processor->process($tokens);
}
public function process($sql) {
$inputArray = $this->splitSQLIntoTokens($sql);
// this is the highest level lexical analysis. This is the part of the
// code which finds UNION and UNION ALL query parts
$processor = new UnionProcessor();
$queries = $processor->process($inputArray);
$queries = $this->processUnion($inputArray);
// If there was no UNION or UNION ALL in the query, then the query is
// stored at $queries[0].
if (!$processor->isUnion($queries)) {
$processor = new SQLProcessor();
$queries = $processor->process($queries[0]);
if (!empty($queries) && !$this->isUnion($queries)) {
$queries = $this->processSQL($queries[0]);
}
return $queries;
}
public function revokeQuotation($sql) {
return parent::revokeQuotation($sql);
}
}
?>
?>

View File

@@ -2,42 +2,52 @@
/**
* DeleteProcessor.php
*
* This file implements the processor for the DELETE statements.
* Processes the DELETE statement parts and splits multi-table deletes.
*
* Copyright (c) 2010-2012, Justin Swanhart
* with contributions by André Rothe <arothe@phosco.info, phosco@gmx.de>
* PHP version 5
*
* LICENSE:
* Copyright (c) 2010-2014 Justin Swanhart and André Rothe
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author André Rothe <andre.rothe@phosco.info>
* @copyright 2010-2014 Justin Swanhart and André Rothe
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version SVN: $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
require_once(dirname(__FILE__) . '/AbstractProcessor.php');
namespace PHPSQLParser\processors;
/**
*
* This class processes the DELETE statements.
*
* @author arothe
*
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class DeleteProcessor extends AbstractProcessor {
@@ -46,19 +56,29 @@ class DeleteProcessor extends AbstractProcessor {
$del = $tokens['DELETE'];
foreach ($tokens['DELETE'] as $expression) {
if ($expression !== 'DELETE' && trim($expression, ' .*') !== "" && !$this->isCommaToken($expression)) {
$tables[] = trim($expression, '.* ');
if (strtoupper($expression) !== 'DELETE' && trim($expression, " \t\n\r\0\x0B.*") !== ""
&& !$this->isCommaToken($expression)) {
$tables[] = trim($expression, " \t\n\r\0\x0B.*");
}
}
if (empty($tables)) {
if (empty($tables) && isset($tokens['USING'])) {
foreach ($tokens['FROM'] as $table) {
$tables[] = $table['table'];
$tables[] = trim($table['table'], " \t\n\r\0\x0B.*");
}
$tokens['FROM'] = $tokens['USING'];
unset($tokens['USING']);
}
$tokens['DELETE'] = array('TABLES' => $tables);
$options = array();
if (isset($tokens['OPTIONS'])) {
$options = $tokens['OPTIONS'];
unset($tokens['OPTIONS']);
}
$tokens['DELETE'] = array('options' => (empty($options) ? false : $options),
'tables' => (empty($tables) ? false : $tables));
return $tokens;
}
}
?>
?>

View File

@@ -30,7 +30,7 @@
* DAMAGE.
*/
require_once(dirname(__FILE__) . '/ExplainProcessor.php');
namespace PHPSQLParser\processors;
/**
*
@@ -45,4 +45,4 @@ class DescProcessor extends ExplainProcessor {
return parent::isStatement($keys, $needle);
}
}
?>
?>

View File

@@ -1,43 +1,52 @@
<?php
/**
* DescribeProcessor.php
* ExplainProcessor.php
*
* This file implements the processor for the DESCRIBE statements.
*
* Copyright (c) 2010-2012, Justin Swanhart
* with contributions by André Rothe <arothe@phosco.info, phosco@gmx.de>
* PHP version 5
*
* LICENSE:
* Copyright (c) 2010-2014 Justin Swanhart and André Rothe
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author André Rothe <andre.rothe@phosco.info>
* @copyright 2010-2014 Justin Swanhart and André Rothe
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version SVN: $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
require_once(dirname(__FILE__) . '/ExplainProcessor.php');
namespace PHPSQLParser\processors;
/**
*
* This class processes the DESCRIBE statements.
*
* @author arothe
*
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class DescribeProcessor extends ExplainProcessor {
@@ -45,4 +54,5 @@ class DescribeProcessor extends ExplainProcessor {
return parent::isStatement($keys, $needle);
}
}
?>
?>

View File

@@ -4,105 +4,139 @@
*
* This file implements the processor for the DROP statements.
*
* Copyright (c) 2010-2012, Justin Swanhart
* with contributions by André Rothe <arothe@phosco.info, phosco@gmx.de>
* PHP version 5
*
* LICENSE:
* Copyright (c) 2010-2014 Justin Swanhart and André Rothe
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author André Rothe <andre.rothe@phosco.info>
* @copyright 2010-2014 Justin Swanhart and André Rothe
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version SVN: $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
require_once(dirname(__FILE__) . '/../utils/ExpressionToken.php');
require_once(dirname(__FILE__) . '/../utils/ExpressionType.php');
require_once(dirname(__FILE__) . '/AbstractProcessor.php');
namespace PHPSQLParser\processors;
use PHPSQLParser\utils\ExpressionType;
/**
*
* This class processes the DROP statements.
*
* @author arothe
*
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class DropProcessor extends AbstractProcessor {
// TODO: we should enhance it to get the positions for the IF EXISTS keywords
// look into the CreateProcessor to get an idea.
public function process($tokenList) {
$skip = 0;
$warning = true;
$base_expr = "";
$expr_type = false;
$exists = false;
$base_expr = '';
$objectType = '';
$subTree = array();
$option = false;
$resultList = array();
foreach ($tokenList as $k => $v) {
$token = new ExpressionToken($k, $v);
foreach ($tokenList as $token) {
$base_expr .= $token;
$trim = trim($token);
if ($token->isWhitespaceToken()) {
if ($trim === '') {
continue;
}
if ($skip > 0) {
$skip --;
continue;
}
switch ($token->getUpper()) {
$upper = strtoupper($trim);
switch ($upper) {
case 'VIEW':
case 'SCHEMA':
case 'DATABASE':
case 'TABLE':
$expr_type = strtolower($token->getTrim());
if ($objectType === '') {
$objectType = constant('PHPSQLParser\utils\ExpressionType::' . $upper);
}
$base_expr = '';
break;
case 'INDEX':
if ( $objectType === '' ) {
$objectType = constant( 'PHPSQLParser\utils\ExpressionType::' . $upper );
}
$base_expr = '';
break;
case 'IF':
$warning = false;
$skip = 1;
case 'EXISTS':
$exists = true;
$base_expr = '';
break;
case 'TEMPORARY':
$expr_type = ExpressionType::TEMPORARY_TABLE;
$skip = 1;
$objectType = ExpressionType::TEMPORARY_TABLE;
$base_expr = '';
break;
case 'RESTRICT':
case 'CASCADE':
$option = $token->getUpper();
$option = $upper;
if (!empty($objectList)) {
$subTree[] = array('expr_type' => ExpressionType::EXPRESSION,
'base_expr' => trim(substr($base_expr, 0, -strlen($token))),
'sub_tree' => $objectList);
$objectList = array();
}
$base_expr = '';
break;
case ',':
$resultList[] = array('expr_type' => $expr_type, 'base_expr' => $base_expr);
$base_expr = "";
break;
$last = array_pop($objectList);
$last['delim'] = $trim;
$objectList[] = $last;
continue 2;
default:
$base_expr .= $token->getToken();
$object = array();
$object['expr_type'] = $objectType;
if ($objectType === ExpressionType::TABLE || $objectType === ExpressionType::TEMPORARY_TABLE) {
$object['table'] = $trim;
$object['no_quotes'] = false;
$object['alias'] = false;
}
$object['base_expr'] = $trim;
$object['no_quotes'] = $this->revokeQuotation($trim);
$object['delim'] = false;
$objectList[] = $object;
continue 2;
}
$subTree[] = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim);
}
if ($base_expr !== "") {
$resultList[] = array('expr_type' => $expr_type, 'base_expr' => $base_expr);
if (!empty($objectList)) {
$subTree[] = array('expr_type' => ExpressionType::EXPRESSION, 'base_expr' => trim($base_expr),
'sub_tree' => $objectList);
}
return array('option' => $option, 'warning' => $warning, 'object_list' => $resultList);
return array('expr_type' => $objectType, 'option' => $option, 'if-exists' => $exists, 'sub_tree' => $subTree);
}
}
?>
?>

View File

@@ -30,20 +30,20 @@
* DAMAGE.
*/
require_once(dirname(__FILE__) . '/SetProcessor.php');
namespace PHPSQLParser\processors;
/**
*
*
* This class processes the DUPLICATE statements.
*
*
* @author arothe
*
*
*/
class DuplicateProcessor extends SetProcessor {
public function process($tokens, $isUpdate = false) {
return parent::process($tokens, false);
return parent::process($tokens, $isUpdate);
}
}
?>
?>

View File

@@ -4,41 +4,50 @@
*
* This file implements the processor for the EXPLAIN statements.
*
* Copyright (c) 2010-2012, Justin Swanhart
* with contributions by André Rothe <arothe@phosco.info, phosco@gmx.de>
* PHP version 5
*
* LICENSE:
* Copyright (c) 2010-2014 Justin Swanhart and André Rothe
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author André Rothe <andre.rothe@phosco.info>
* @copyright 2010-2014 Justin Swanhart and André Rothe
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version SVN: $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
require_once(dirname(__FILE__) . '/AbstractProcessor.php');
require_once(dirname(__FILE__) . '/../utils/ExpressionType.php');
namespace PHPSQLParser\processors;
use PHPSQLParser\utils\ExpressionType;
/**
*
* This class processes the EXPLAIN statements.
*
* @author arothe
*
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class ExplainProcessor extends AbstractProcessor {
@@ -121,12 +130,14 @@ class ExplainProcessor extends AbstractProcessor {
case 'TABLENAME':
$currCategory = 'WILD';
$expr[] = array('expr_type' => ExpressionType::COLREF, 'base_expr' => $trim);
$expr[] = array('expr_type' => ExpressionType::COLREF, 'base_expr' => $trim,
'no_quotes' => $this->revokeQuotation($trim));
break;
case '':
$currCategory = 'TABLENAME';
$expr[] = array('expr_type' => ExpressionType::TABLE, 'base_expr' => $trim);
$expr[] = array('expr_type' => ExpressionType::TABLE, 'table' => $trim,
'no_quotes' => $this->revokeQuotation($trim), 'alias' => false, 'base_expr' => $trim);
break;
default:
@@ -136,4 +147,5 @@ class ExplainProcessor extends AbstractProcessor {
return empty($expr) ? null : $expr;
}
}
?>

View File

@@ -4,43 +4,52 @@
*
* This file implements the processor for expression lists.
*
* Copyright (c) 2010-2012, Justin Swanhart
* with contributions by André Rothe <arothe@phosco.info, phosco@gmx.de>
* PHP version 5
*
* LICENSE:
* Copyright (c) 2010-2014 Justin Swanhart and André Rothe
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author André Rothe <andre.rothe@phosco.info>
* @copyright 2010-2014 Justin Swanhart and André Rothe
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version SVN: $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
require_once(dirname(__FILE__) . '/AbstractProcessor.php');
require_once(dirname(__FILE__) . '/DefaultProcessor.php');
require_once(dirname(__FILE__) . '/../utils/ExpressionToken.php');
require_once(dirname(__FILE__) . '/../utils/ExpressionType.php');
namespace PHPSQLParser\processors;
use PHPSQLParser\utils\ExpressionType;
use PHPSQLParser\utils\ExpressionToken;
use PHPSQLParser\utils\PHPSQLParserConstants;
/**
*
* This class processes expression lists.
*
* @author arothe
*
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class ExpressionListProcessor extends AbstractProcessor {
@@ -51,6 +60,12 @@ class ExpressionListProcessor extends AbstractProcessor {
foreach ($tokens as $k => $v) {
if ($this->isCommentToken($v)) {
$resultList[] = parent::processComment($v);
continue;
}
$curr = new ExpressionToken($k, $v);
if ($curr->isWhitespaceToken()) {
@@ -66,7 +81,7 @@ class ExpressionListProcessor extends AbstractProcessor {
/* is it a subquery? */
if ($curr->isSubQueryToken()) {
$processor = new DefaultProcessor();
$processor = new DefaultProcessor($this->options);
$curr->setSubTree($processor->process($this->removeParenthesisFromStart($curr->getTrim())));
$curr->setTokenType(ExpressionType::SUBQUERY);
@@ -121,7 +136,8 @@ class ExpressionListProcessor extends AbstractProcessor {
$curr->setTokenType(ExpressionType::MATCH_ARGUMENTS);
$prev->setTokenType(ExpressionType::SIMPLE_FUNCTION);
} elseif ($prev->isColumnReference() || $prev->isFunction() || $prev->isAggregateFunction()) {
} elseif ($prev->isColumnReference() || $prev->isFunction() || $prev->isAggregateFunction()
|| $prev->isCustomFunction()) {
// if we have a colref followed by a parenthesis pair,
// it isn't a colref, it is a user-function
@@ -152,7 +168,9 @@ class ExpressionListProcessor extends AbstractProcessor {
}
if (!$curr->getSubTree()) {
$curr->setSubTree($localExprList);
if (!empty($localExprList)) {
$curr->setSubTree($localExprList);
}
} else {
$tmpExprList = $curr->getSubTree();
$curr->setSubTree(array_merge($tmpExprList, $localExprList));
@@ -175,7 +193,9 @@ class ExpressionListProcessor extends AbstractProcessor {
}
if (!$curr->getSubTree()) {
$curr->setSubTree($localExprList);
if (!empty($localExprList)) {
$curr->setSubTree($localExprList);
}
} else {
$tmpExprList = $curr->getSubTree();
$curr->setSubTree(array_merge($tmpExprList, $localExprList));
@@ -183,8 +203,12 @@ class ExpressionListProcessor extends AbstractProcessor {
$prev->setSubTree($curr->getSubTree());
if ($prev->isColumnReference()) {
$prev->setTokenType(ExpressionType::SIMPLE_FUNCTION);
$prev->setNoQuotes(null);
if (PHPSQLParserConstants::getInstance()->isCustomFunction($prev->getUpper())) {
$prev->setTokenType(ExpressionType::CUSTOM_FUNCTION);
} else {
$prev->setTokenType(ExpressionType::SIMPLE_FUNCTION);
}
$prev->setNoQuotes(null, null, $this->options);
}
array_pop($resultList);
@@ -193,14 +217,20 @@ class ExpressionListProcessor extends AbstractProcessor {
// we have parenthesis, but it seems to be an expression
if ($curr->isUnspecified()) {
$tmpExprList = array_values($localTokenList);
$localExprList = $this->process($tmpExprList);
// TODO: the localTokenList could contain commas and further expressions,
// we must handle that like function parameters (see above)!
// this should solve issue 51
$curr->setSubTree($this->process($localTokenList));
$curr->setTokenType(ExpressionType::BRACKET_EXPRESSION);
if (!$curr->getSubTree()) {
if (!empty($localExprList)) {
$curr->setSubTree($localExprList);
}
} else {
$tmpExprList = $curr->getSubTree();
$curr->setSubTree(array_merge($tmpExprList, $localExprList));
}
}
} elseif ($curr->isVariableToken()) {
# a variable
@@ -208,7 +238,7 @@ class ExpressionListProcessor extends AbstractProcessor {
$curr->setTokenType($this->getVariableType($curr->getUpper()));
$curr->setSubTree(false);
$curr->setNoQuotes(trim(trim($curr->getToken()), '@'), "`'\"");
$curr->setNoQuotes(trim(trim($curr->getToken()), '@'), "`'\"", $this->options);
} else {
/* it is either an operator, a colref or a constant */
@@ -227,7 +257,7 @@ class ExpressionListProcessor extends AbstractProcessor {
// then * is an operator
// but if the previous colref ends with a dot, the * is the all-columns-alias
if (!$prev->isColumnReference() && !$prev->isConstant() && !$prev->isExpression()
&& !$prev->isBracketExpression() && !$prev->isAggregateFunction() && !$prev->isVariable()) {
&& !$prev->isBracketExpression() && !$prev->isAggregateFunction() && !$prev->isVariable()) {
$curr->setTokenType(ExpressionType::COLREF);
break;
}
@@ -244,7 +274,6 @@ class ExpressionListProcessor extends AbstractProcessor {
case 'AND':
case '&&':
case 'BETWEEN':
case 'AND':
case 'BINARY':
case '&':
case '~':
@@ -289,8 +318,8 @@ class ExpressionListProcessor extends AbstractProcessor {
$curr->setSubTree(false);
if ($prev->isColumnReference() || $prev->isFunction() || $prev->isAggregateFunction()
|| $prev->isConstant() || $prev->isSubQuery() || $prev->isExpression()
|| $prev->isBracketExpression() || $prev->isVariable()) {
|| $prev->isConstant() || $prev->isSubQuery() || $prev->isExpression()
|| $prev->isBracketExpression() || $prev->isVariable() || $prev->isCustomFunction()) {
$curr->setTokenType(ExpressionType::OPERATOR);
} else {
$curr->setTokenType(ExpressionType::SIGN);
@@ -302,14 +331,20 @@ class ExpressionListProcessor extends AbstractProcessor {
switch ($curr->getToken(0)) {
case "'":
case '"':
// it is a string literal
$curr->setTokenType(ExpressionType::CONSTANT);
break;
case '"':
if (!$this->options->getANSIQuotes()) {
// If we're not using ANSI quotes, this is a string literal.
$curr->setTokenType(ExpressionType::CONSTANT);
break;
}
// Otherwise continue to the next case
case '`':
// it is an escaped colum name
$curr->setTokenType(ExpressionType::COLREF);
$curr->setNoQuotes($curr->getToken());
$curr->setNoQuotes($curr->getToken(), null, $this->options);
break;
default:
@@ -325,7 +360,7 @@ class ExpressionListProcessor extends AbstractProcessor {
}
} else {
$curr->setTokenType(ExpressionType::COLREF);
$curr->setNoQuotes($curr->getToken());
$curr->setNoQuotes($curr->getToken(), null, $this->options);
}
break;
}
@@ -334,48 +369,57 @@ class ExpressionListProcessor extends AbstractProcessor {
/* is a reserved word? */
if (!$curr->isOperator() && !$curr->isInList() && !$curr->isFunction() && !$curr->isAggregateFunction()
&& PHPSQLParserConstants::isReserved($curr->getUpper())) {
&& !$curr->isCustomFunction() && PHPSQLParserConstants::getInstance()->isReserved($curr->getUpper())) {
if (PHPSQLParserConstants::isAggregateFunction($curr->getUpper())) {
$next = isset( $tokens[ $k + 1 ] ) ? new ExpressionToken( $k + 1, $tokens[ $k + 1 ] ) : new ExpressionToken();
$isEnclosedWithinParenthesis = $next->isEnclosedWithinParenthesis();
if ($isEnclosedWithinParenthesis && PHPSQLParserConstants::getInstance()->isCustomFunction($curr->getUpper())) {
$curr->setTokenType(ExpressionType::CUSTOM_FUNCTION);
$curr->setNoQuotes(null, null, $this->options);
} elseif ($isEnclosedWithinParenthesis && PHPSQLParserConstants::getInstance()->isAggregateFunction($curr->getUpper())) {
$curr->setTokenType(ExpressionType::AGGREGATE_FUNCTION);
$curr->setNoQuotes(null);
$curr->setNoQuotes(null, null, $this->options);
} elseif ($curr->getUpper() === 'NULL') {
// it is a reserved word, but we would like to set it as constant
$curr->setTokenType(ExpressionType::CONSTANT);
} else {
if (PHPSQLParserConstants::isParameterizedFunction($curr->getUpper())) {
if ($isEnclosedWithinParenthesis && PHPSQLParserConstants::getInstance()->isParameterizedFunction($curr->getUpper())) {
// issue 60: check functions with parameters
// -> colref (we check parameters later)
// -> if there is no parameter, we leave the colref
$curr->setTokenType(ExpressionType::COLREF);
} elseif (PHPSQLParserConstants::isFunction($curr->getUpper())) {
} elseif ($isEnclosedWithinParenthesis && PHPSQLParserConstants::getInstance()->isFunction($curr->getUpper())) {
$curr->setTokenType(ExpressionType::SIMPLE_FUNCTION);
$curr->setNoQuotes(null);
$curr->setNoQuotes(null, null, $this->options);
} elseif (!$isEnclosedWithinParenthesis && PHPSQLParserConstants::getInstance()->isFunction($curr->getUpper())) {
// Colname using function name.
$curr->setTokenType(ExpressionType::COLREF);
} else {
$curr->setTokenType(ExpressionType::RESERVED);
$curr->setNoQuotes(null);
$curr->setNoQuotes(null, null, $this->options);
}
}
}
// issue 94, INTERVAL 1 MONTH
if ($curr->isConstant() && PHPSQLParserConstants::isParameterizedFunction($prev->getUpper())) {
if ($curr->isConstant() && PHPSQLParserConstants::getInstance()->isParameterizedFunction($prev->getUpper())) {
$prev->setTokenType(ExpressionType::RESERVED);
$prev->setNoQuotes(null);
$prev->setNoQuotes(null, null, $this->options);
}
if ($prev->isConstant() && PHPSQLParserConstants::isParameterizedFunction($curr->getUpper())) {
if ($prev->isConstant() && PHPSQLParserConstants::getInstance()->isParameterizedFunction($curr->getUpper())) {
$curr->setTokenType(ExpressionType::RESERVED);
$curr->setNoQuotes(null);
$curr->setNoQuotes(null, null, $this->options);
}
if ($curr->isUnspecified()) {
$curr->setTokenType(ExpressionType::EXPRESSION);
$curr->setNoQuotes(null);
$curr->setNoQuotes(null, null, $this->options);
$curr->setSubTree($this->process($this->splitSQLIntoTokens($curr->getTrim())));
}
@@ -386,4 +430,4 @@ class ExpressionListProcessor extends AbstractProcessor {
return $this->toArray($resultList);
}
}
?>
?>

View File

@@ -2,48 +2,73 @@
/**
* FromProcessor.php
*
* This file implements the processor for the FROM statements.
* This file implements the processor for the FROM statement.
*
* Copyright (c) 2010-2013, Justin Swanhart
* with contributions by André Rothe <arothe@phosco.info, phosco@gmx.de>
* PHP version 5
*
* LICENSE:
* Copyright (c) 2010-2014 Justin Swanhart and André Rothe
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author André Rothe <andre.rothe@phosco.info>
* @author George Schneeloch <noisecapella@gmail.com>
* @copyright 2010-2014 Justin Swanhart and André Rothe
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version SVN: $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
require_once(dirname(__FILE__) . '/AbstractProcessor.php');
require_once(dirname(__FILE__) . '/ExpressionListProcessor.php');
require_once(dirname(__FILE__) . '/DefaultProcessor.php');
require_once(dirname(__FILE__) . '/../utils/ExpressionType.php');
namespace PHPSQLParser\processors;
use PHPSQLParser\utils\ExpressionType;
/**
*
* This class processes the FROM statements.
*
* @author arothe
*
* This class processes the FROM statement.
*
* @author André Rothe <andre.rothe@phosco.info>
* @author Marco Th. <marco64th@gmail.com>
* @author George Schneeloch <noisecapella@gmail.com>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class FromProcessor extends AbstractProcessor {
protected function processExpressionList($unparsed) {
$processor = new ExpressionListProcessor($this->options);
return $processor->process($unparsed);
}
protected function processColumnList($unparsed) {
$processor = new ColumnListProcessor($this->options);
return $processor->process($unparsed);
}
protected function processSQLDefault($unparsed) {
$processor = new DefaultProcessor($this->options);
return $processor->process($unparsed);
}
protected function initParseInfo($parseInfo = false) {
// first init
if ($parseInfo === false) {
@@ -51,9 +76,9 @@ class FromProcessor extends AbstractProcessor {
}
// loop init
return array('expression' => "", 'token_count' => 0, 'table' => "", 'no_quotes' => "", 'alias' => false,
'join_type' => "", 'next_join_type' => "", 'saved_join_type' => $parseInfo['saved_join_type'],
'ref_type' => false, 'ref_expr' => false, 'base_expr' => false, 'sub_tree' => false,
'subquery' => "");
'hints' => false, 'join_type' => "", 'next_join_type' => "",
'saved_join_type' => $parseInfo['saved_join_type'], 'ref_type' => false, 'ref_expr' => false,
'base_expr' => false, 'sub_tree' => false, 'subquery' => "");
}
protected function processFromExpression(&$parseInfo) {
@@ -65,7 +90,7 @@ class FromProcessor extends AbstractProcessor {
// we have a reg_expr, so we have to parse it
if ($parseInfo['ref_expr'] !== false) {
$unparsed = $this->splitSQLIntoTokens($this->removeParenthesisFromStart($parseInfo['ref_expr']));
$unparsed = $this->splitSQLIntoTokens($parseInfo['ref_expr']);
// here we can get a comma separated list
foreach ($unparsed as $k => $v) {
@@ -73,21 +98,37 @@ class FromProcessor extends AbstractProcessor {
$unparsed[$k] = "";
}
}
$processor = new ExpressionListProcessor();
$parseInfo['ref_expr'] = $processor->process($unparsed);
if ($parseInfo['ref_type'] === 'USING') {
// unparsed has only one entry, the column list
$ref = $this->processColumnList($this->removeParenthesisFromStart($unparsed[0]));
$ref = array(array('expr_type' => ExpressionType::COLUMN_LIST, 'base_expr' => $unparsed[0], 'sub_tree' => $ref));
} else {
$ref = $this->processExpressionList($unparsed);
}
$parseInfo['ref_expr'] = (empty($ref) ? false : $ref);
}
// there is an expression, we have to parse it
if (substr(trim($parseInfo['table']), 0, 1) == '(') {
$parseInfo['expression'] = $this->removeParenthesisFromStart($parseInfo['table']);
if (preg_match("/^\\s*select/i", $parseInfo['expression'])) {
$processor = new DefaultProcessor();
$parseInfo['sub_tree'] = $processor->process($parseInfo['expression']);
if (preg_match("/^\\s*(-- [\\w\\s]+\\n)?\\s*SELECT/i", $parseInfo['expression'])) {
$parseInfo['sub_tree'] = $this->processSQLDefault($parseInfo['expression']);
$res['expr_type'] = ExpressionType::SUBQUERY;
} else {
$tmp = $this->splitSQLIntoTokens($parseInfo['expression']);
$parseInfo['sub_tree'] = $this->process($tmp);
$unionProcessor = new UnionProcessor($this->options);
$unionQueries = $unionProcessor->process($tmp);
// If there was no UNION or UNION ALL in the query, then the query is
// stored at $queries[0].
if (!empty($unionQueries) && !UnionProcessor::isUnion($unionQueries)) {
$sub_tree = $this->process($unionQueries[0]);
}
else {
$sub_tree = $unionQueries;
}
$parseInfo['sub_tree'] = $sub_tree;
$res['expr_type'] = ExpressionType::TABLE_EXPRESSION;
}
} else {
@@ -97,6 +138,7 @@ class FromProcessor extends AbstractProcessor {
}
$res['alias'] = $parseInfo['alias'];
$res['hints'] = $parseInfo['hints'];
$res['join_type'] = $parseInfo['join_type'];
$res['ref_type'] = $parseInfo['ref_type'];
$res['ref_clause'] = $parseInfo['ref_expr'];
@@ -108,6 +150,8 @@ class FromProcessor extends AbstractProcessor {
public function process($tokens) {
$parseInfo = $this->initParseInfo();
$expr = array();
$token_category = '';
$prevToken = '';
$skip_next = false;
$i = 0;
@@ -125,18 +169,48 @@ class FromProcessor extends AbstractProcessor {
}
}
if ($this->isCommentToken($token)) {
$expr[] = parent::processComment($token);
continue;
}
switch ($upper) {
case 'CROSS':
case ',':
case 'INNER':
case 'STRAIGHT_JOIN':
break;
case 'OUTER':
case 'JOIN':
if ($token_category === 'LEFT' || $token_category === 'RIGHT' || $token_category === 'NATURAL') {
$token_category = '';
$parseInfo['next_join_type'] = strtoupper(trim($prevToken)); // it seems to be a join
}
break;
case 'LEFT':
case 'RIGHT':
case 'NATURAL':
case 'CROSS':
case ',':
case 'JOIN':
case 'INNER':
break;
$token_category = $upper;
$prevToken = $token;
$i++;
continue 2;
default:
if ($token_category === 'LEFT' || $token_category === 'RIGHT') {
if ($upper === '') {
$prevToken .= $token;
break;
} else {
$token_category = ''; // it seems to be a function
$parseInfo['expression'] .= $prevToken;
if ($parseInfo['ref_type'] !== false) { // all after ON / USING
$parseInfo['ref_expr'] .= $prevToken;
}
$prevToken = '';
}
}
$parseInfo['expression'] .= $token;
if ($parseInfo['ref_type'] !== false) { // all after ON / USING
$parseInfo['ref_expr'] .= $token;
@@ -144,13 +218,18 @@ class FromProcessor extends AbstractProcessor {
break;
}
if ($upper === '') {
$i++;
continue;
}
switch ($upper) {
case 'AS':
$parseInfo['alias'] = array('as' => true, 'name' => "", 'base_expr' => $token);
$parseInfo['token_count']++;
$n = 1;
$str = "";
while ($str == "") {
while ($str === "" && isset($tokens[$i + $n])) {
$parseInfo['alias']['base_expr'] .= ($tokens[$i + $n] === "" ? " " : $tokens[$i + $n]);
$str = trim($tokens[$i + $n]);
++$n;
@@ -158,14 +237,26 @@ class FromProcessor extends AbstractProcessor {
$parseInfo['alias']['name'] = $str;
$parseInfo['alias']['no_quotes'] = $this->revokeQuotation($str);
$parseInfo['alias']['base_expr'] = trim($parseInfo['alias']['base_expr']);
continue;
break;
case 'IGNORE':
case 'USE':
case 'FORCE':
$token_category = 'IDX_HINT';
$parseInfo['hints'][]['hint_type'] = $upper;
continue 2;
case 'KEY':
case 'INDEX':
if ($token_category == 'CREATE') {
$token_category = $upper;
if ($token_category === 'CREATE') {
$token_category = $upper; // TODO: what is it for a statement?
continue 2;
}
if ($token_category === 'IDX_HINT') {
$cur_hint = (count($parseInfo['hints']) - 1);
$parseInfo['hints'][$cur_hint]['hint_type'] .= " " . $upper;
continue 2;
}
break;
case 'USING':
@@ -174,25 +265,26 @@ class FromProcessor extends AbstractProcessor {
$parseInfo['ref_expr'] = "";
case 'CROSS':
case 'USE':
case 'FORCE':
case 'IGNORE':
case 'INNER':
case 'OUTER':
case 'NATURAL':
$parseInfo['token_count']++;
continue;
break;
case 'FOR':
$parseInfo['token_count']++;
$skip_next = true;
continue;
break;
case 'LEFT':
case 'RIGHT':
case 'STRAIGHT_JOIN':
$parseInfo['next_join_type'] = $upper;
$parseInfo['next_join_type'] = "STRAIGHT_JOIN";
if ($parseInfo['subquery']) {
$parseInfo['sub_tree'] = $this->parse($this->removeParenthesisFromStart($parseInfo['subquery']));
$parseInfo['expression'] = $parseInfo['subquery'];
}
$expr[] = $this->processFromExpression($parseInfo);
$parseInfo = $this->initParseInfo($parseInfo);
break;
case ',':
@@ -209,8 +301,13 @@ class FromProcessor extends AbstractProcessor {
break;
default:
if ($upper === "") {
continue; // ends the switch statement!
// TODO: enhance it, so we can have base_expr to calculate the position of the keywords
// build a subtree under "hints"
if ($token_category === 'IDX_HINT') {
$token_category = '';
$cur_hint = (count($parseInfo['hints']) - 1);
$parseInfo['hints'][$cur_hint]['hint_list'] = $token;
break;
}
if ($parseInfo['token_count'] === 0) {
@@ -218,7 +315,7 @@ class FromProcessor extends AbstractProcessor {
$parseInfo['table'] = $token;
$parseInfo['no_quotes'] = $this->revokeQuotation($token);
}
} elseif ($parseInfo['token_count'] === 1) {
} else if ($parseInfo['token_count'] === 1) {
$parseInfo['alias'] = array('as' => false, 'name' => trim($token),
'no_quotes' => $this->revokeQuotation($token),
'base_expr' => trim($token));
@@ -226,7 +323,7 @@ class FromProcessor extends AbstractProcessor {
$parseInfo['token_count']++;
break;
}
++$i;
$i++;
}
$expr[] = $this->processFromExpression($parseInfo);
@@ -234,4 +331,5 @@ class FromProcessor extends AbstractProcessor {
}
}
?>
?>

View File

@@ -30,7 +30,7 @@
* DAMAGE.
*/
require_once(dirname(__FILE__) . '/OrderByProcessor.php');
namespace PHPSQLParser\processors;
/**
*
@@ -71,4 +71,4 @@ class GroupByProcessor extends OrderByProcessor {
return $out;
}
}
?>
?>

View File

@@ -2,44 +2,78 @@
/**
* HavingProcessor.php
*
* This file implements the processor for the HAVING statements.
* Parses the HAVING statements.
*
* Copyright (c) 2010-2012, Justin Swanhart
* with contributions by André Rothe <arothe@phosco.info, phosco@gmx.de>
* PHP version 5
*
* LICENSE:
* Copyright (c) 2010-2014 Justin Swanhart and André Rothe
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author André Rothe <andre.rothe@phosco.info>
* @copyright 2010-2014 Justin Swanhart and André Rothe
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version SVN: $Id$
*
*/
require_once(dirname(__FILE__) . '/ExpressionListProcessor.php');
namespace PHPSQLParser\processors;
use PHPSQLParser\utils\ExpressionType;
/**
*
* This class processes the HAVING statements.
*
* @author arothe
*
* This class implements the processor for the HAVING statement.
* You can overwrite all functions to achieve another handling.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class HavingProcessor extends ExpressionListProcessor {
public function process($tokens, $select = array()) {
$parsed = parent::process($tokens);
foreach ($parsed as $k => $v) {
if ($v['expr_type'] === ExpressionType::COLREF) {
foreach ($select as $clause) {
if (!isset($clause['alias'])) {
continue;
}
if (!$clause['alias']) {
continue;
}
if ($clause['alias']['no_quotes'] === $v['no_quotes']) {
$parsed[$k]['expr_type'] = ExpressionType::ALIAS;
break;
}
}
}
}
return $parsed;
}
}
?>
?>

View File

@@ -30,8 +30,8 @@
* DAMAGE.
*/
require_once(dirname(__FILE__) . '/AbstractProcessor.php');
require_once(dirname(__FILE__) . '/../utils/ExpressionType.php');
namespace PHPSQLParser\processors;
use PHPSQLParser\utils\ExpressionType;
/**
*

View File

@@ -0,0 +1,306 @@
<?php
/**
* IndexProcessor.php
*
* This file implements the processor for the INDEX statements.
*
* PHP version 5
*
* LICENSE:
* Copyright (c) 2010-2014 Justin Swanhart and André Rothe
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author André Rothe <andre.rothe@phosco.info>
* @copyright 2010-2014 Justin Swanhart and André Rothe
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version SVN: $Id$
*
*/
namespace PHPSQLParser\processors;
use PHPSQLParser\utils\ExpressionType;
/**
* This class processes the INDEX statements.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class IndexProcessor extends AbstractProcessor {
protected function getReservedType($token) {
return array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $token);
}
protected function getConstantType($token) {
return array('expr_type' => ExpressionType::CONSTANT, 'base_expr' => $token);
}
protected function getOperatorType($token) {
return array('expr_type' => ExpressionType::OPERATOR, 'base_expr' => $token);
}
protected function processIndexColumnList($parsed) {
$processor = new IndexColumnListProcessor($this->options);
return $processor->process($parsed);
}
public function process($tokens) {
$currCategory = 'INDEX_NAME';
$result = array('base_expr' => false, 'name' => false, 'no_quotes' => false, 'index-type' => false, 'on' => false,
'options' => false);
$expr = array();
$base_expr = '';
$skip = 0;
foreach ($tokens as $tokenKey => $token) {
$trim = trim($token);
$base_expr .= $token;
if ($skip > 0) {
$skip--;
continue;
}
if ($skip < 0) {
break;
}
if ($trim === '') {
continue;
}
$upper = strtoupper($trim);
switch ($upper) {
case 'USING':
if ($prevCategory === 'CREATE_DEF') {
$expr[] = $this->getReservedType($trim);
$currCategory = 'TYPE_OPTION';
continue 2;
}
if ($prevCategory === 'TYPE_DEF') {
$expr[] = $this->getReservedType($trim);
$currCategory = 'INDEX_TYPE';
continue 2;
}
// else ?
break;
case 'KEY_BLOCK_SIZE':
if ($prevCategory === 'CREATE_DEF') {
$expr[] = $this->getReservedType($trim);
$currCategory = 'INDEX_OPTION';
continue 2;
}
// else ?
break;
case 'WITH':
if ($prevCategory === 'CREATE_DEF') {
$expr[] = $this->getReservedType($trim);
$currCategory = 'INDEX_PARSER';
continue 2;
}
// else ?
break;
case 'PARSER':
if ($currCategory === 'INDEX_PARSER') {
$expr[] = $this->getReservedType($trim);
continue 2;
}
// else ?
break;
case 'COMMENT':
if ($prevCategory === 'CREATE_DEF') {
$expr[] = $this->getReservedType($trim);
$currCategory = 'INDEX_COMMENT';
continue 2;
}
// else ?
break;
case 'ALGORITHM':
case 'LOCK':
if ($prevCategory === 'CREATE_DEF') {
$expr[] = $this->getReservedType($trim);
$currCategory = $upper . '_OPTION';
continue 2;
}
// else ?
break;
case '=':
// the optional operator
if (substr($currCategory, -7, 7) === '_OPTION') {
$expr[] = $this->getOperatorType($trim);
continue 2; // don't change the category
}
// else ?
break;
case 'ON':
if ($prevCategory === 'CREATE_DEF' || $prevCategory === 'TYPE_DEF') {
$expr[] = $this->getReservedType($trim);
$currCategory = 'TABLE_DEF';
continue 2;
}
// else ?
break;
default:
switch ($currCategory) {
case 'COLUMN_DEF':
if ($upper[0] === '(' && substr($upper, -1) === ')') {
$cols = $this->processIndexColumnList($this->removeParenthesisFromStart($trim));
$result['on']['base_expr'] .= $base_expr;
$result['on']['sub_tree'] = array('expr_type' => ExpressionType::COLUMN_LIST,
'base_expr' => $trim, 'sub_tree' => $cols);
}
$expr = array();
$base_expr = '';
$currCategory = 'CREATE_DEF';
break;
case 'TABLE_DEF':
// the table name
$expr[] = $this->getConstantType($trim);
// TODO: the base_expr should contain the column-def too
$result['on'] = array('expr_type' => ExpressionType::TABLE, 'base_expr' => $base_expr,
'name' => $trim, 'no_quotes' => $this->revokeQuotation($trim),
'sub_tree' => false);
$expr = array();
$base_expr = '';
$currCategory = 'COLUMN_DEF';
continue 3;
case 'INDEX_NAME':
$result['base_expr'] = $result['name'] = $trim;
$result['no_quotes'] = $this->revokeQuotation($trim);
$expr = array();
$base_expr = '';
$currCategory = 'TYPE_DEF';
break;
case 'INDEX_PARSER':
// the parser name
$expr[] = $this->getConstantType($trim);
$result['options'][] = array('expr_type' => ExpressionType::INDEX_PARSER,
'base_expr' => trim($base_expr), 'sub_tree' => $expr);
$expr = array();
$base_expr = '';
$currCategory = 'CREATE_DEF';
break;
case 'INDEX_COMMENT':
// the index comment
$expr[] = $this->getConstantType($trim);
$result['options'][] = array('expr_type' => ExpressionType::COMMENT,
'base_expr' => trim($base_expr), 'sub_tree' => $expr);
$expr = array();
$base_expr = '';
$currCategory = 'CREATE_DEF';
break;
case 'INDEX_OPTION':
// the key_block_size
$expr[] = $this->getConstantType($trim);
$result['options'][] = array('expr_type' => ExpressionType::INDEX_SIZE,
'base_expr' => trim($base_expr), 'size' => $upper,
'sub_tree' => $expr);
$expr = array();
$base_expr = '';
$currCategory = 'CREATE_DEF';
break;
case 'INDEX_TYPE':
case 'TYPE_OPTION':
// BTREE or HASH
$expr[] = $this->getReservedType($trim);
if ($currCategory === 'INDEX_TYPE') {
$result['index-type'] = array('expr_type' => ExpressionType::INDEX_TYPE,
'base_expr' => trim($base_expr), 'using' => $upper,
'sub_tree' => $expr);
} else {
$result['options'][] = array('expr_type' => ExpressionType::INDEX_TYPE,
'base_expr' => trim($base_expr), 'using' => $upper,
'sub_tree' => $expr);
}
$expr = array();
$base_expr = '';
$currCategory = 'CREATE_DEF';
break;
case 'LOCK_OPTION':
// DEFAULT|NONE|SHARED|EXCLUSIVE
$expr[] = $this->getReservedType($trim);
$result['options'][] = array('expr_type' => ExpressionType::INDEX_LOCK,
'base_expr' => trim($base_expr), 'lock' => $upper,
'sub_tree' => $expr);
$expr = array();
$base_expr = '';
$currCategory = 'CREATE_DEF';
break;
case 'ALGORITHM_OPTION':
// DEFAULT|INPLACE|COPY
$expr[] = $this->getReservedType($trim);
$result['options'][] = array('expr_type' => ExpressionType::INDEX_ALGORITHM,
'base_expr' => trim($base_expr), 'algorithm' => $upper,
'sub_tree' => $expr);
$expr = array();
$base_expr = '';
$currCategory = 'CREATE_DEF';
break;
default:
break;
}
break;
}
$prevCategory = $currCategory;
$currCategory = '';
}
return $result;
}
}
?>

View File

@@ -4,72 +4,167 @@
*
* This file implements the processor for the INSERT statements.
*
* Copyright (c) 2010-2012, Justin Swanhart
* with contributions by André Rothe <arothe@phosco.info, phosco@gmx.de>
* PHP version 5
*
* LICENSE:
* Copyright (c) 2010-2014 Justin Swanhart and André Rothe
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author André Rothe <andre.rothe@phosco.info>
* @copyright 2010-2014 Justin Swanhart and André Rothe
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version SVN: $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
require_once(dirname(__FILE__) . '/AbstractProcessor.php');
require_once(dirname(__FILE__) . '/ColumnListProcessor.php');
require_once(dirname(__FILE__) . '/../utils/ExpressionType.php');
namespace PHPSQLParser\processors;
use PHPSQLParser\utils\ExpressionType;
/**
*
* This class processes the INSERT statements.
*
* @author arothe
*
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class InsertProcessor extends AbstractProcessor {
public function process($tokenList, $token_category = 'INSERT') {
$table = "";
$cols = array();
protected function processOptions($tokenList) {
if (!isset($tokenList['OPTIONS'])) {
return array();
}
$result = array();
foreach ($tokenList['OPTIONS'] as $token) {
$result[] = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => trim($token));
}
return $result;
}
$into = $tokenList['INTO'];
foreach ($into as $token) {
if ($this->isWhitespaceToken($token))
protected function processKeyword($keyword, $tokenList) {
if (!isset($tokenList[$keyword])) {
return array('', false, array());
}
$table = '';
$cols = false;
$result = array();
foreach ($tokenList[$keyword] as $token) {
$trim = trim($token);
if ($trim === '') {
continue;
if ($table === "") {
$table = $token;
} elseif (empty($cols)) {
$cols[] = $token;
}
$upper = strtoupper($trim);
switch ($upper) {
case 'INTO':
$result[] = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim);
break;
case 'INSERT':
case 'REPLACE':
break;
default:
if ($table === '') {
$table = $trim;
break;
}
if ($cols === false) {
$cols = $trim;
}
break;
}
}
return array($table, $cols, $result);
}
protected function processColumns($cols) {
if ($cols === false) {
return $cols;
}
if ($cols[0] === '(' && substr($cols, -1) === ')') {
$parsed = array('expr_type' => ExpressionType::BRACKET_EXPRESSION, 'base_expr' => $cols,
'sub_tree' => false);
}
$cols = $this->removeParenthesisFromStart($cols);
if (stripos($cols, 'SELECT') === 0) {
$processor = new DefaultProcessor($this->options);
$parsed['sub_tree'] = array(
array('expr_type' => ExpressionType::QUERY, 'base_expr' => $cols,
'sub_tree' => $processor->process($cols)));
} else {
$processor = new ColumnListProcessor($this->options);
$parsed['sub_tree'] = $processor->process($cols);
$parsed['expr_type'] = ExpressionType::COLUMN_LIST;
}
return $parsed;
}
public function process($tokenList, $token_category = 'INSERT') {
$table = '';
$cols = false;
$comments = array();
foreach ($tokenList as $key => &$token) {
if ($key == 'VALUES') {
continue;
}
foreach ($token as &$value) {
if ($this->isCommentToken($value)) {
$comments[] = parent::processComment($value);
$value = '';
}
}
}
if (empty($cols)) {
$cols = false;
} else {
$processor = new ColumnListProcessor();
$cols = $processor->process($this->removeParenthesisFromStart($cols[0]));
$parsed = $this->processOptions($tokenList);
unset($tokenList['OPTIONS']);
list($table, $cols, $key) = $this->processKeyword('INTO', $tokenList);
$parsed = array_merge($parsed, $key);
unset($tokenList['INTO']);
if ($table === '' && in_array($token_category, array('INSERT', 'REPLACE'))) {
list($table, $cols, $key) = $this->processKeyword($token_category, $tokenList);
}
unset($tokenList['INTO']);
$tokenList[$token_category][0] = array('table' => $table, 'columns' => $cols, 'base_expr' => $table,
'no_quotes' => $this->revokeQuotation($table));
$parsed[] = array('expr_type' => ExpressionType::TABLE, 'table' => $table,
'no_quotes' => $this->revokeQuotation($table), 'alias' => false, 'base_expr' => $table);
$cols = $this->processColumns($cols);
if ($cols !== false) {
$parsed[] = $cols;
}
$parsed = array_merge($parsed, $comments);
$tokenList[$token_category] = $parsed;
return $tokenList;
}
}
?>
?>

View File

@@ -30,7 +30,7 @@
* DAMAGE.
*/
require_once(dirname(__FILE__) . '/AbstractProcessor.php');
namespace PHPSQLParser\processors;
/**
*
@@ -56,4 +56,4 @@ class IntoProcessor extends AbstractProcessor {
return $tokenList;
}
}
?>
?>

View File

@@ -4,39 +4,48 @@
*
* This file implements the processor for the LIMIT statements.
*
* Copyright (c) 2010-2012, Justin Swanhart
* with contributions by André Rothe <arothe@phosco.info, phosco@gmx.de>
* PHP version 5
*
* LICENSE:
* Copyright (c) 2010-2014 Justin Swanhart and André Rothe
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author André Rothe <andre.rothe@phosco.info>
* @copyright 2010-2014 Justin Swanhart and André Rothe
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version SVN: $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
require_once(dirname(__FILE__) . '/AbstractProcessor.php');
namespace PHPSQLParser\processors;
/**
*
* This class processes the LIMIT statements.
*
* @author arothe
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class LimitProcessor extends AbstractProcessor {
@@ -47,9 +56,18 @@ class LimitProcessor extends AbstractProcessor {
$comma = -1;
$exchange = false;
$comments = array();
foreach ($tokens as &$token) {
if ($this->isCommentToken($token)) {
$comments[] = parent::processComment($token);
$token = '';
}
}
for ($i = 0; $i < count($tokens); ++$i) {
$trim = trim($tokens[$i]);
$trim = strtoupper(trim($tokens[$i]));
if ($trim === ",") {
$comma = $i;
break;
@@ -77,7 +95,11 @@ class LimitProcessor extends AbstractProcessor {
}
}
return array('offset' => trim($offset), 'rowcount' => trim($rowcount));
$return = array('offset' => trim($offset), 'rowcount' => trim($rowcount));
if (count($comments)) {
$return['comments'] = $comments;
}
return $return;
}
}
?>
?>

View File

@@ -0,0 +1,77 @@
<?php
/**
* OptionsProcessor.php
*
* This file implements the processor for the statement options.
*
* PHP version 5
*
* LICENSE:
* Copyright (c) 2010-2014 Justin Swanhart and André Rothe
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author André Rothe <andre.rothe@phosco.info>
* @copyright 2010-2014 Justin Swanhart and André Rothe
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version SVN: $Id$
*
*/
namespace PHPSQLParser\processors;
use PHPSQLParser\utils\ExpressionType;
/**
* This class processes the statement options.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class OptionsProcessor extends AbstractProcessor {
public function process($tokens) {
$resultList = array();
foreach ($tokens as $token) {
$tokenList = $this->splitSQLIntoTokens($token);
$result = array();
foreach ($tokenList as $reserved) {
$trim = trim($reserved);
if ($trim === '') {
continue;
}
$result[] = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim);
}
$resultList[] = array('expr_type' => ExpressionType::EXPRESSION, 'base_expr' => trim($token),
'sub_tree' => $result);
}
return $resultList;
}
}
?>

View File

@@ -4,49 +4,56 @@
*
* This file implements the processor for the ORDER-BY statements.
*
* Copyright (c) 2010-2012, Justin Swanhart
* with contributions by André Rothe <arothe@phosco.info, phosco@gmx.de>
* PHP version 5
*
* LICENSE:
* Copyright (c) 2010-2014 Justin Swanhart and André Rothe
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author André Rothe <andre.rothe@phosco.info>
* @copyright 2010-2014 Justin Swanhart and André Rothe
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version SVN: $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
require_once(dirname(__FILE__) . '/AbstractProcessor.php');
require_once(dirname(__FILE__) . '/SelectExpressionProcessor.php');
require_once(dirname(__FILE__) . '/../utils/ExpressionType.php');
namespace PHPSQLParser\processors;
use PHPSQLParser\utils\ExpressionType;
/**
*
* This class processes the ORDER-BY statements.
*
* @author arothe
*
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class OrderByProcessor extends AbstractProcessor {
private $selectExpressionProcessor;
public function __construct() {
$this->selectExpressionProcessor = new SelectExpressionProcessor();
protected function processSelectExpression($unparsed) {
$processor = new SelectExpressionProcessor($this->options);
return $processor->process($unparsed);
}
protected function initParseInfo() {
@@ -66,7 +73,7 @@ class OrderByProcessor extends AbstractProcessor {
$parseInfo['no_quotes'] = $this->revokeQuotation($parseInfo['base_expr']);
// search to see if the expression matches an alias
foreach ($select as $clause) {
if (!$clause['alias']) {
if (empty($clause['alias'])) {
continue;
}
@@ -78,7 +85,7 @@ class OrderByProcessor extends AbstractProcessor {
}
if ($parseInfo['expr_type'] === ExpressionType::EXPRESSION) {
$expr = $this->selectExpressionProcessor->process($parseInfo['base_expr']);
$expr = $this->processSelectExpression($parseInfo['base_expr']);
$expr['direction'] = $parseInfo['dir'];
unset($expr['alias']);
return $expr;
@@ -119,6 +126,11 @@ class OrderByProcessor extends AbstractProcessor {
break;
default:
if ($this->isCommentToken($token)) {
$out[] = parent::processComment($token);
break;
}
$parseInfo['base_expr'] .= $token;
}
}
@@ -127,4 +139,4 @@ class OrderByProcessor extends AbstractProcessor {
return $out;
}
}
?>
?>

View File

@@ -0,0 +1,390 @@
<?php
/**
* PartitionDefinitionProcessor.php
*
* This file implements the processor for the PARTITION statements
* within CREATE TABLE.
*
* PHP version 5
*
* LICENSE:
* Copyright (c) 2010-2014 Justin Swanhart and André Rothe
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author André Rothe <andre.rothe@phosco.info>
* @copyright 2010-2014 Justin Swanhart and André Rothe
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version SVN: $Id$
*
*/
namespace PHPSQLParser\processors;
use PHPSQLParser\utils\ExpressionType;
/**
* This class processes the PARTITION statements within CREATE TABLE.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class PartitionDefinitionProcessor extends AbstractProcessor {
protected function processExpressionList($unparsed) {
$processor = new ExpressionListProcessor($this->options);
$expr = $this->removeParenthesisFromStart($unparsed);
$expr = $this->splitSQLIntoTokens($expr);
return $processor->process($expr);
}
protected function processSubpartitionDefinition($unparsed) {
$processor = new SubpartitionDefinitionProcessor($this->options);
$expr = $this->removeParenthesisFromStart($unparsed);
$expr = $this->splitSQLIntoTokens($expr);
return $processor->process($expr);
}
protected function getReservedType($token) {
return array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $token);
}
protected function getConstantType($token) {
return array('expr_type' => ExpressionType::CONSTANT, 'base_expr' => $token);
}
protected function getOperatorType($token) {
return array('expr_type' => ExpressionType::OPERATOR, 'base_expr' => $token);
}
protected function getBracketExpressionType($token) {
return array('expr_type' => ExpressionType::BRACKET_EXPRESSION, 'base_expr' => $token, 'sub_tree' => false);
}
public function process($tokens) {
$result = array();
$prevCategory = '';
$currCategory = '';
$parsed = array();
$expr = array();
$base_expr = '';
$skip = 0;
foreach ($tokens as $tokenKey => $token) {
$trim = trim($token);
$base_expr .= $token;
if ($skip > 0) {
$skip--;
continue;
}
if ($skip < 0) {
break;
}
if ($trim === '') {
continue;
}
$upper = strtoupper($trim);
switch ($upper) {
case 'PARTITION':
if ($currCategory === '') {
$expr[] = $this->getReservedType($trim);
$parsed = array('expr_type' => ExpressionType::PARTITION_DEF, 'base_expr' => trim($base_expr),
'sub_tree' => false);
$currCategory = $upper;
continue 2;
}
// else ?
break;
case 'VALUES':
if ($prevCategory === 'PARTITION') {
$expr[] = array('expr_type' => ExpressionType::PARTITION_VALUES, 'base_expr' => false,
'sub_tree' => false, 'storage' => substr($base_expr, 0, -strlen($token)));
$parsed['sub_tree'] = $expr;
$base_expr = $token;
$expr = array($this->getReservedType($trim));
$currCategory = $upper;
continue 2;
}
// else ?
break;
case 'LESS':
if ($currCategory === 'VALUES') {
$expr[] = $this->getReservedType($trim);
continue 2;
}
// else ?
break;
case 'THAN':
if ($currCategory === 'VALUES') {
// followed by parenthesis and (value-list or expr)
$expr[] = $this->getReservedType($trim);
continue 2;
}
// else ?
break;
case 'MAXVALUE':
if ($currCategory === 'VALUES') {
$expr[] = $this->getConstantType($trim);
$last = array_pop($parsed['sub_tree']);
$last['base_expr'] = $base_expr;
$last['sub_tree'] = $expr;
$base_expr = $last['storage'] . $base_expr;
unset($last['storage']);
$parsed['sub_tree'][] = $last;
$parsed['base_expr'] = trim($base_expr);
$expr = $parsed['sub_tree'];
unset($last);
$currCategory = $prevCategory;
}
// else ?
break;
case 'IN':
if ($currCategory === 'VALUES') {
// followed by parenthesis and value-list
$expr[] = $this->getReservedType($trim);
continue 2;
}
break;
case 'COMMENT':
if ($prevCategory === 'PARTITION') {
$expr[] = array('expr_type' => ExpressionType::PARTITION_COMMENT, 'base_expr' => false,
'sub_tree' => false, 'storage' => substr($base_expr, 0, -strlen($token)));
$parsed['sub_tree'] = $expr;
$base_expr = $token;
$expr = array($this->getReservedType($trim));
$currCategory = $upper;
continue 2;
}
// else ?
break;
case 'STORAGE':
if ($prevCategory === 'PARTITION') {
// followed by ENGINE
$expr[] = array('expr_type' => ExpressionType::ENGINE, 'base_expr' => false, 'sub_tree' => false,
'storage' => substr($base_expr, 0, -strlen($token)));
$parsed['sub_tree'] = $expr;
$base_expr = $token;
$expr = array($this->getReservedType($trim));
$currCategory = $upper;
continue 2;
}
// else ?
break;
case 'ENGINE':
if ($currCategory === 'STORAGE') {
$expr[] = $this->getReservedType($trim);
$currCategory = $upper;
continue 2;
}
if ($prevCategory === 'PARTITION') {
$expr[] = array('expr_type' => ExpressionType::ENGINE, 'base_expr' => false, 'sub_tree' => false,
'storage' => substr($base_expr, 0, -strlen($token)));
$parsed['sub_tree'] = $expr;
$base_expr = $token;
$expr = array($this->getReservedType($trim));
$currCategory = $upper;
continue 2;
}
// else ?
break;
case '=':
if (in_array($currCategory, array('ENGINE', 'COMMENT', 'DIRECTORY', 'MAX_ROWS', 'MIN_ROWS'))) {
$expr[] = $this->getOperatorType($trim);
continue 2;
}
// else ?
break;
case ',':
if ($prevCategory === 'PARTITION' && $currCategory === '') {
// it separates the partition-definitions
$result[] = $parsed;
$parsed = array();
$base_expr = '';
$expr = array();
}
break;
case 'DATA':
case 'INDEX':
if ($prevCategory === 'PARTITION') {
// followed by DIRECTORY
$expr[] = array('expr_type' => constant('PHPSQLParser\utils\ExpressionType::PARTITION_' . $upper . '_DIR'),
'base_expr' => false, 'sub_tree' => false,
'storage' => substr($base_expr, 0, -strlen($token)));
$parsed['sub_tree'] = $expr;
$base_expr = $token;
$expr = array($this->getReservedType($trim));
$currCategory = $upper;
continue 2;
}
// else ?
break;
case 'DIRECTORY':
if ($currCategory === 'DATA' || $currCategory === 'INDEX') {
$expr[] = $this->getReservedType($trim);
$currCategory = $upper;
continue 2;
}
// else ?
break;
case 'MAX_ROWS':
case 'MIN_ROWS':
if ($prevCategory === 'PARTITION') {
$expr[] = array('expr_type' => constant('PHPSQLParser\utils\ExpressionType::PARTITION_' . $upper),
'base_expr' => false, 'sub_tree' => false,
'storage' => substr($base_expr, 0, -strlen($token)));
$parsed['sub_tree'] = $expr;
$base_expr = $token;
$expr = array($this->getReservedType($trim));
$currCategory = $upper;
continue 2;
}
// else ?
break;
default:
switch ($currCategory) {
case 'MIN_ROWS':
case 'MAX_ROWS':
case 'ENGINE':
case 'DIRECTORY':
case 'COMMENT':
$expr[] = $this->getConstantType($trim);
$last = array_pop($parsed['sub_tree']);
$last['sub_tree'] = $expr;
$last['base_expr'] = trim($base_expr);
$base_expr = $last['storage'] . $base_expr;
unset($last['storage']);
$parsed['sub_tree'][] = $last;
$parsed['base_expr'] = trim($base_expr);
$expr = $parsed['sub_tree'];
unset($last);
$currCategory = $prevCategory;
break;
case 'PARTITION':
// that is the partition name
$last = array_pop($expr);
$last['name'] = $trim;
$expr[] = $last;
$expr[] = $this->getConstantType($trim);
$parsed['sub_tree'] = $expr;
$parsed['base_expr'] = trim($base_expr);
break;
case 'VALUES':
// we have parenthesis and have to process an expression/in-list
$last = $this->getBracketExpressionType($trim);
$res = $this->processExpressionList($trim);
$last['sub_tree'] = (empty($res) ? false : $res);
$expr[] = $last;
$last = array_pop($parsed['sub_tree']);
$last['base_expr'] = $base_expr;
$last['sub_tree'] = $expr;
$base_expr = $last['storage'] . $base_expr;
unset($last['storage']);
$parsed['sub_tree'][] = $last;
$parsed['base_expr'] = trim($base_expr);
$expr = $parsed['sub_tree'];
unset($last);
$currCategory = $prevCategory;
break;
case '':
if ($prevCategory === 'PARTITION') {
// last part to process, it is only one token!
if ($upper[0] === '(' && substr($upper, -1) === ')') {
$last = $this->getBracketExpressionType($trim);
$last['sub_tree'] = $this->processSubpartitionDefinition($trim);
$expr[] = $last;
unset($last);
$parsed['base_expr'] = trim($base_expr);
$parsed['sub_tree'] = $expr;
$currCategory = $prevCategory;
break;
}
}
// else ?
break;
default:
break;
}
break;
}
$prevCategory = $currCategory;
$currCategory = '';
}
$result[] = $parsed;
return $result;
}
}
?>

View File

@@ -0,0 +1,361 @@
<?php
/**
* PartitionOptionsProcessor.php
*
* This file implements the processor for the PARTITION BY statements
* within CREATE TABLE.
*
* PHP version 5
*
* LICENSE:
* Copyright (c) 2010-2014 Justin Swanhart and André Rothe
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author André Rothe <andre.rothe@phosco.info>
* @copyright 2010-2014 Justin Swanhart and André Rothe
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version SVN: $Id$
*
*/
namespace PHPSQLParser\processors;
use PHPSQLParser\utils\ExpressionType;
/**
* This class processes the PARTITION BY statements within CREATE TABLE.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class PartitionOptionsProcessor extends AbstractProcessor {
protected function processExpressionList($unparsed) {
$processor = new ExpressionListProcessor($this->options);
$expr = $this->removeParenthesisFromStart($unparsed);
$expr = $this->splitSQLIntoTokens($expr);
return $processor->process($expr);
}
protected function processColumnList($unparsed) {
$processor = new ColumnListProcessor($this->options);
$expr = $this->removeParenthesisFromStart($unparsed);
return $processor->process($expr);
}
protected function processPartitionDefinition($unparsed) {
$processor = new PartitionDefinitionProcessor($this->options);
$expr = $this->removeParenthesisFromStart($unparsed);
$expr = $this->splitSQLIntoTokens($expr);
return $processor->process($expr);
}
protected function getReservedType($token) {
return array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $token);
}
protected function getConstantType($token) {
return array('expr_type' => ExpressionType::CONSTANT, 'base_expr' => $token);
}
protected function getOperatorType($token) {
return array('expr_type' => ExpressionType::OPERATOR, 'base_expr' => $token);
}
protected function getBracketExpressionType($token) {
return array('expr_type' => ExpressionType::BRACKET_EXPRESSION, 'base_expr' => $token, 'sub_tree' => false);
}
public function process($tokens) {
$result = array('partition-options' => array(), 'last-parsed' => false);
$prevCategory = '';
$currCategory = '';
$parsed = array();
$expr = array();
$base_expr = '';
$skip = 0;
foreach ($tokens as $tokenKey => $token) {
$trim = trim($token);
$base_expr .= $token;
if ($skip > 0) {
$skip--;
continue;
}
if ($skip < 0) {
break;
}
if ($trim === '') {
continue;
}
$upper = strtoupper($trim);
switch ($upper) {
case 'PARTITION':
$currCategory = $upper;
$expr[] = $this->getReservedType($trim);
$parsed[] = array('expr_type' => ExpressionType::PARTITION, 'base_expr' => trim($base_expr),
'sub_tree' => false);
break;
case 'SUBPARTITION':
$currCategory = $upper;
$expr[] = $this->getReservedType($trim);
$parsed[] = array('expr_type' => ExpressionType::SUBPARTITION, 'base_expr' => trim($base_expr),
'sub_tree' => false);
break;
case 'BY':
if ($prevCategory === 'PARTITION' || $prevCategory === 'SUBPARTITION') {
$expr[] = $this->getReservedType($trim);
continue 2;
}
break;
case 'PARTITIONS':
case 'SUBPARTITIONS':
$currCategory = 'PARTITION_NUM';
$expr = array('expr_type' => constant('PHPSQLParser\utils\ExpressionType::' . substr($upper, 0, -1) . '_COUNT'),
'base_expr' => false, 'sub_tree' => array($this->getReservedType($trim)),
'storage' => substr($base_expr, 0, -strlen($token)));
$base_expr = $token;
continue 2;
case 'LINEAR':
// followed by HASH or KEY
$currCategory = $upper;
$expr[] = $this->getReservedType($trim);
continue 2;
case 'HASH':
case 'KEY':
$expr[] = array('expr_type' => constant('PHPSQLParser\utils\ExpressionType::' . $prevCategory . '_' . $upper),
'base_expr' => false, 'linear' => ($currCategory === 'LINEAR'), 'sub_tree' => false,
'storage' => substr($base_expr, 0, -strlen($token)));
$last = array_pop($parsed);
$last['by'] = trim($currCategory . ' ' . $upper); // $currCategory will be empty or LINEAR!
$last['sub_tree'] = $expr;
$parsed[] = $last;
$base_expr = $token;
$expr = array($this->getReservedType($trim));
$currCategory = $upper;
continue 2;
case 'ALGORITHM':
if ($currCategory === 'KEY') {
$expr[] = array('expr_type' => constant('PHPSQLParser\utils\ExpressionType::' . $prevCategory . '_KEY_ALGORITHM'),
'base_expr' => false, 'sub_tree' => false,
'storage' => substr($base_expr, 0, -strlen($token)));
$last = array_pop($parsed);
$subtree = array_pop($last['sub_tree']);
$subtree['sub_tree'] = $expr;
$last['sub_tree'][] = $subtree;
$parsed[] = $last;
unset($subtree);
unset($last);
$base_expr = $token;
$expr = array($this->getReservedType($trim));
$currCategory = $upper;
continue 2;
}
break;
case 'RANGE':
case 'LIST':
$expr[] = array('expr_type' => constant('PHPSQLParser\utils\ExpressionType::PARTITION_' . $upper), 'base_expr' => false,
'sub_tree' => false, 'storage' => substr($base_expr, 0, -strlen($token)));
$last = array_pop($parsed);
$last['by'] = $upper;
$last['sub_tree'] = $expr;
$parsed[] = $last;
unset($last);
$base_expr = $token;
$expr = array($this->getReservedType($trim));
$currCategory = $upper . '_EXPR';
continue 2;
case 'COLUMNS':
if ($currCategory === 'RANGE_EXPR' || $currCategory === 'LIST_EXPR') {
$expr[] = $this->getReservedType($trim);
$currCategory = substr($currCategory, 0, -4) . $upper;
continue 2;
}
break;
case '=':
if ($currCategory === 'ALGORITHM') {
// between ALGORITHM and a constant
$expr[] = $this->getOperatorType($trim);
continue 2;
}
break;
default:
switch ($currCategory) {
case 'PARTITION_NUM':
// the number behind PARTITIONS or SUBPARTITIONS
$expr['base_expr'] = trim($base_expr);
$expr['sub_tree'][] = $this->getConstantType($trim);
$base_expr = $expr['storage'] . $base_expr;
unset($expr['storage']);
$last = array_pop($parsed);
$last['count'] = $trim;
$last['sub_tree'][] = $expr;
$last['base_expr'] .= $base_expr;
$parsed[] = $last;
unset($last);
$expr = array();
$base_expr = '';
$currCategory = $prevCategory;
break;
case 'ALGORITHM':
// the number of the algorithm
$expr[] = $this->getConstantType($trim);
$last = array_pop($parsed);
$subtree = array_pop($last['sub_tree']);
$key = array_pop($subtree['sub_tree']);
$key['sub_tree'] = $expr;
$key['base_expr'] = trim($base_expr);
$base_expr = $key['storage'] . $base_expr;
unset($key['storage']);
$subtree['sub_tree'][] = $key;
unset($key);
$expr = $subtree['sub_tree'];
$subtree['sub_tree'] = false;
$subtree['algorithm'] = $trim;
$last['sub_tree'][] = $subtree;
unset($subtree);
$parsed[] = $last;
unset($last);
$currCategory = 'KEY';
continue 3;
case 'LIST_EXPR':
case 'RANGE_EXPR':
case 'HASH':
// parenthesis around an expression
$last = $this->getBracketExpressionType($trim);
$res = $this->processExpressionList($trim);
$last['sub_tree'] = (empty($res) ? false : $res);
$expr[] = $last;
$last = array_pop($parsed);
$subtree = array_pop($last['sub_tree']);
$subtree['base_expr'] = $base_expr;
$subtree['sub_tree'] = $expr;
$base_expr = $subtree['storage'] . $base_expr;
unset($subtree['storage']);
$last['sub_tree'][] = $subtree;
$last['base_expr'] = trim($base_expr);
$parsed[] = $last;
unset($last);
unset($subtree);
$expr = array();
$base_expr = '';
$currCategory = $prevCategory;
break;
case 'LIST_COLUMNS':
case 'RANGE_COLUMNS':
case 'KEY':
// the columnlist
$expr[] = array('expr_type' => ExpressionType::COLUMN_LIST, 'base_expr' => $trim,
'sub_tree' => $this->processColumnList($trim));
$last = array_pop($parsed);
$subtree = array_pop($last['sub_tree']);
$subtree['base_expr'] = $base_expr;
$subtree['sub_tree'] = $expr;
$base_expr = $subtree['storage'] . $base_expr;
unset($subtree['storage']);
$last['sub_tree'][] = $subtree;
$last['base_expr'] = trim($base_expr);
$parsed[] = $last;
unset($last);
unset($subtree);
$expr = array();
$base_expr = '';
$currCategory = $prevCategory;
break;
case '':
if ($prevCategory === 'PARTITION' || $prevCategory === 'SUBPARTITION') {
if ($upper[0] === '(' && substr($upper, -1) === ')') {
// last part to process, it is only one token!
$last = $this->getBracketExpressionType($trim);
$last['sub_tree'] = $this->processPartitionDefinition($trim);
$parsed[] = $last;
break;
}
}
// else ?
break;
default:
break;
}
break;
}
$prevCategory = $currCategory;
$currCategory = '';
}
$result['partition-options'] = $parsed;
if ($result['last-parsed'] === false) {
$result['last-parsed'] = $tokenKey;
}
return $result;
}
}
?>

View File

@@ -2,50 +2,58 @@
/**
* RecordProcessor.php
*
* This file implements the processor for records.
* This file implements a processor, which processes records of data
* for an INSERT statement.
*
* Copyright (c) 2010-2012, Justin Swanhart
* with contributions by André Rothe <arothe@phosco.info, phosco@gmx.de>
* PHP version 5
*
* LICENSE:
* Copyright (c) 2010-2014 Justin Swanhart and André Rothe
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author André Rothe <andre.rothe@phosco.info>
* @copyright 2010-2014 Justin Swanhart and André Rothe
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version SVN: $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
require_once(dirname(__FILE__) . '/AbstractProcessor.php');
require_once(dirname(__FILE__) . '/ExpressionListProcessor.php');
namespace PHPSQLParser\processors;
/**
*
* This class processes records.
*
* @author arothe
*
* This class processes records of an INSERT statement.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class RecordProcessor extends AbstractProcessor {
private $expressionListProcessor;
public function __construct() {
$this->expressionListProcessor = new ExpressionListProcessor();
protected function processExpressionList($unparsed) {
$processor = new ExpressionListProcessor($this->options);
return $processor->process($unparsed);
}
public function process($unparsed) {
@@ -57,8 +65,7 @@ class RecordProcessor extends AbstractProcessor {
$values[$k] = "";
}
}
return $this->expressionListProcessor->process($values);
return $this->processExpressionList($values);
}
}
?>
?>

View File

@@ -30,9 +30,8 @@
* DAMAGE.
*/
require_once(dirname(__FILE__) . '/AbstractProcessor.php');
require_once(dirname(__FILE__) . '/IndexColumnListProcessor.php');
require_once(dirname(__FILE__) . '/../utils/ExpressionType.php');
namespace PHPSQLParser\processors;
use PHPSQLParser\utils\ExpressionType;
/**
*
@@ -155,7 +154,7 @@ class ReferenceDefinitionProcessor extends AbstractProcessor {
case 'REFERENCES':
if ($upper[0] === '(' && substr($upper, -1) === ')') {
# index_col_name list
$processor = new IndexColumnListProcessor();
$processor = new IndexColumnListProcessor($this->options);
$cols = $processor->process($this->removeParenthesisFromStart($trim));
$expr['sub_tree'][] = array('expr_type' => ExpressionType::COLUMN_LIST, 'base_expr' => $trim,
'sub_tree' => $cols);

View File

@@ -4,42 +4,51 @@
*
* This file implements the processor for the RENAME statements.
*
* Copyright (c) 2010-2012, Justin Swanhart
* with contributions by André Rothe <arothe@phosco.info, phosco@gmx.de>
* PHP version 5
*
* LICENSE:
* Copyright (c) 2010-2014 Justin Swanhart and André Rothe
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author André Rothe <andre.rothe@phosco.info>
* @copyright 2010-2014 Justin Swanhart and André Rothe
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version SVN: $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
require_once(dirname(__FILE__) . '/AbstractProcessor.php');
require_once(dirname(__FILE__) . '/../utils/ExpressionToken.php');
require_once(dirname(__FILE__) . '/../utils/ExpressionType.php');
namespace PHPSQLParser\processors;
use PHPSQLParser\utils\ExpressionType;
use PHPSQLParser\utils\ExpressionToken;
/**
*
* This class processes the RENAME statements.
*
* @author arothe
*
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class RenameProcessor extends AbstractProcessor {
@@ -74,6 +83,11 @@ class RenameProcessor extends AbstractProcessor {
$base_expr = "";
break;
case 'TABLE':
$objectType = ExpressionType::TABLE;
$resultList[] = array('expr_type'=>ExpressionType::RESERVED, 'base_expr'=>$token->getTrim());
continue 2;
default:
$base_expr .= $token->getToken();
break;
@@ -87,7 +101,7 @@ class RenameProcessor extends AbstractProcessor {
$resultList[] = $tablePair;
}
return $resultList;
return array('expr_type' => $objectType, 'sub_tree'=>$resultList);
}
}

View File

@@ -2,48 +2,57 @@
/**
* ReplaceProcessor.php
*
* This file implements the processor for the REPLACE statements.
* This file implements the processor for the REPLACE statements.
*
* Copyright (c) 2010-2012, Justin Swanhart
* with contributions by André Rothe <arothe@phosco.info, phosco@gmx.de>
* PHP version 5
*
* LICENSE:
* Copyright (c) 2010-2014 Justin Swanhart and André Rothe
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author André Rothe <andre.rothe@phosco.info>
* @copyright 2010-2014 Justin Swanhart and André Rothe
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version SVN: $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
require_once(dirname(__FILE__) . '/InsertProcessor.php');
namespace PHPSQLParser\processors;
/**
*
* This class processes the REPLACE statements.
*
* @author arothe
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class ReplaceProcessor extends InsertProcessor {
public function process($tokenList, $token_category = 'INSERT') {
return parent::process($tokenList, 'REPLACE');
public function process($tokenList, $token_category = 'REPLACE') {
return parent::process($tokenList, $token_category);
}
}
?>
?>

View File

@@ -4,67 +4,49 @@
*
* This file implements the processor for the SQL chunks.
*
* Copyright (c) 2010-2012, Justin Swanhart
* with contributions by André Rothe <arothe@phosco.info, phosco@gmx.de>
* PHP version 5
*
* LICENSE:
* Copyright (c) 2010-2014 Justin Swanhart and André Rothe
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author André Rothe <andre.rothe@phosco.info>
* @copyright 2010-2014 Justin Swanhart and André Rothe
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version SVN: $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
require_once(dirname(__FILE__) . '/AbstractProcessor.php');
require_once(dirname(__FILE__) . '/FromProcessor.php');
require_once(dirname(__FILE__) . '/RecordProcessor.php');
require_once(dirname(__FILE__) . '/UpdateProcessor.php');
require_once(dirname(__FILE__) . '/DeleteProcessor.php');
require_once(dirname(__FILE__) . '/GroupByProcessor.php');
require_once(dirname(__FILE__) . '/RenameProcessor.php');
require_once(dirname(__FILE__) . '/UsingProcessor.php');
require_once(dirname(__FILE__) . '/DescribeProcessor.php');
require_once(dirname(__FILE__) . '/DescProcessor.php');
require_once(dirname(__FILE__) . '/HavingProcessor.php');
require_once(dirname(__FILE__) . '/ReplaceProcessor.php');
require_once(dirname(__FILE__) . '/ValuesProcessor.php');
require_once(dirname(__FILE__) . '/DropProcessor.php');
require_once(dirname(__FILE__) . '/InsertProcessor.php');
require_once(dirname(__FILE__) . '/SelectExpressionProcessor.php');
require_once(dirname(__FILE__) . '/WhereProcessor.php');
require_once(dirname(__FILE__) . '/DuplicateProcessor.php');
require_once(dirname(__FILE__) . '/IntoProcessor.php');
require_once(dirname(__FILE__) . '/SelectProcessor.php');
require_once(dirname(__FILE__) . '/ExplainProcessor.php');
require_once(dirname(__FILE__) . '/LimitProcessor.php');
require_once(dirname(__FILE__) . '/SetProcessor.php');
require_once(dirname(__FILE__) . '/ExpressionListProcessor.php');
require_once(dirname(__FILE__) . '/OrderByProcessor.php');
require_once(dirname(__FILE__) . '/ShowProcessor.php');
require_once(dirname(__FILE__) . '/CreateProcessor.php');
require_once(dirname(__FILE__) . '/TableProcessor.php');
namespace PHPSQLParser\processors;
/**
*
* This class processes the SQL chunks.
*
* @author arothe
*
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class SQLChunkProcessor extends AbstractProcessor {
@@ -80,107 +62,137 @@ class SQLChunkProcessor extends AbstractProcessor {
if (!$out) {
return false;
}
if (!empty($out['BRACKET'])) {
// TODO: this field should be a global STATEMENT field within the output
// we could add all other categories as sub_tree, it could also work with multipe UNIONs
$processor = new BracketProcessor($this->options);
$processedBracket = $processor->process($out['BRACKET']);
$remainingExpressions = $processedBracket[0]['remaining_expressions'];
unset($processedBracket[0]['remaining_expressions']);
if(!empty($remainingExpressions)) {
foreach($remainingExpressions as $key=>$expression) {
$processedBracket[][$key] = $expression;
}
}
$out['BRACKET'] = $processedBracket;
}
if (!empty($out['CREATE'])) {
$processor = new CreateProcessor();
$processor = new CreateProcessor($this->options);
$out['CREATE'] = $processor->process($out['CREATE']);
}
if (!empty($out['TABLE'])) {
$processor = new TableProcessor();
$processor = new TableProcessor($this->options);
$out['TABLE'] = $processor->process($out['TABLE']);
$this->moveLIKE($out);
}
if (!empty($out['INDEX'])) {
$processor = new IndexProcessor($this->options);
$out['INDEX'] = $processor->process($out['INDEX']);
}
if (!empty($out['EXPLAIN'])) {
$processor = new ExplainProcessor();
$processor = new ExplainProcessor($this->options);
$out['EXPLAIN'] = $processor->process($out['EXPLAIN'], array_keys($out));
}
if (!empty($out['DESCRIBE'])) {
$processor = new DescribeProcessor();
$processor = new DescribeProcessor($this->options);
$out['DESCRIBE'] = $processor->process($out['DESCRIBE'], array_keys($out));
}
if (!empty($out['DESC'])) {
$processor = new DescProcessor();
$processor = new DescProcessor($this->options);
$out['DESC'] = $processor->process($out['DESC'], array_keys($out));
}
if (!empty($out['SELECT'])) {
$processor = new SelectProcessor();
$processor = new SelectProcessor($this->options);
$out['SELECT'] = $processor->process($out['SELECT']);
}
if (!empty($out['FROM'])) {
$processor = new FromProcessor();
$processor = new FromProcessor($this->options);
$out['FROM'] = $processor->process($out['FROM']);
}
if (!empty($out['USING'])) {
$processor = new UsingProcessor();
$processor = new UsingProcessor($this->options);
$out['USING'] = $processor->process($out['USING']);
}
if (!empty($out['UPDATE'])) {
$processor = new UpdateProcessor();
$processor = new UpdateProcessor($this->options);
$out['UPDATE'] = $processor->process($out['UPDATE']);
}
if (!empty($out['GROUP'])) {
// set empty array if we have partial SQL statement
$processor = new GroupByProcessor();
$processor = new GroupByProcessor($this->options);
$out['GROUP'] = $processor->process($out['GROUP'], isset($out['SELECT']) ? $out['SELECT'] : array());
}
if (!empty($out['ORDER'])) {
// set empty array if we have partial SQL statement
$processor = new OrderByProcessor();
$processor = new OrderByProcessor($this->options);
$out['ORDER'] = $processor->process($out['ORDER'], isset($out['SELECT']) ? $out['SELECT'] : array());
}
if (!empty($out['LIMIT'])) {
$processor = new LimitProcessor();
$processor = new LimitProcessor($this->options);
$out['LIMIT'] = $processor->process($out['LIMIT']);
}
if (!empty($out['WHERE'])) {
$processor = new WhereProcessor();
$processor = new WhereProcessor($this->options);
$out['WHERE'] = $processor->process($out['WHERE']);
}
if (!empty($out['HAVING'])) {
$processor = new HavingProcessor();
$out['HAVING'] = $processor->process($out['HAVING']);
$processor = new HavingProcessor($this->options);
$out['HAVING'] = $processor->process($out['HAVING'], isset($out['SELECT']) ? $out['SELECT'] : array());
}
if (!empty($out['SET'])) {
$processor = new SetProcessor();
$processor = new SetProcessor($this->options);
$out['SET'] = $processor->process($out['SET'], isset($out['UPDATE']));
}
if (!empty($out['DUPLICATE'])) {
$processor = new DuplicateProcessor();
$processor = new DuplicateProcessor($this->options);
$out['ON DUPLICATE KEY UPDATE'] = $processor->process($out['DUPLICATE']);
unset($out['DUPLICATE']);
}
if (!empty($out['INSERT'])) {
$processor = new InsertProcessor();
$processor = new InsertProcessor($this->options);
$out = $processor->process($out);
}
if (!empty($out['REPLACE'])) {
$processor = new ReplaceProcessor();
$processor = new ReplaceProcessor($this->options);
$out = $processor->process($out);
}
if (!empty($out['DELETE'])) {
$processor = new DeleteProcessor();
$processor = new DeleteProcessor($this->options);
$out = $processor->process($out);
}
if (!empty($out['VALUES'])) {
$processor = new ValuesProcessor();
$processor = new ValuesProcessor($this->options);
$out = $processor->process($out);
}
if (!empty($out['INTO'])) {
$processor = new IntoProcessor();
$processor = new IntoProcessor($this->options);
$out = $processor->process($out);
}
if (!empty($out['DROP'])) {
$processor = new DropProcessor();
$processor = new DropProcessor($this->options);
$out['DROP'] = $processor->process($out['DROP']);
}
if (!empty($out['RENAME'])) {
$processor = new RenameProcessor();
$processor = new RenameProcessor($this->options);
$out['RENAME'] = $processor->process($out['RENAME']);
}
if (!empty($out['SHOW'])) {
$processor = new ShowProcessor();
$processor = new ShowProcessor($this->options);
$out['SHOW'] = $processor->process($out['SHOW']);
}
if (!empty($out['OPTIONS'])) {
$processor = new OptionsProcessor($this->options);
$out['OPTIONS'] = $processor->process($out['OPTIONS']);
}
if (!empty($out['WITH'])) {
$processor = new WithProcessor($this->options);
$out['WITH'] = $processor->process($out['WITH']);
}
return $out;
}
}
?>
?>

View File

@@ -4,47 +4,56 @@
*
* This file implements the processor for the base SQL statements.
*
* Copyright (c) 2010-2012, Justin Swanhart
* with contributions by André Rothe <arothe@phosco.info, phosco@gmx.de>
* PHP version 5
*
* LICENSE:
* Copyright (c) 2010-2014 Justin Swanhart and André Rothe
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author André Rothe <andre.rothe@phosco.info>
* @copyright 2010-2014 Justin Swanhart and André Rothe
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version SVN: $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
require_once(dirname(__FILE__) . '/AbstractProcessor.php');
require_once(dirname(__FILE__) . '/SQLChunkProcessor.php');
namespace PHPSQLParser\processors;
/**
*
* This class processes the base SQL statements.
*
* @author arothe
*
*
* @author André Rothe <andre.rothe@phosco.info>
* @author Marco Th. <marco64th@gmail.com>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class SQLProcessor extends SQLChunkProcessor {
/*
/**
* This function breaks up the SQL statement into logical sections.
* Some sections are then further handled by specialized processors.
* Some sections are delegated to specialized processors.
*/
public function process($tokens) {
$prev_category = "";
@@ -52,15 +61,24 @@ class SQLProcessor extends SQLChunkProcessor {
$skip_next = 0;
$out = false;
$tokenCount = count($tokens);
// $tokens may come as a numeric indexed array starting with an index greater than 0 (or as a boolean)
$tokenCount = count($tokens);
if ( is_array($tokens) ){
$tokens = array_values($tokens);
}
for ($tokenNumber = 0; $tokenNumber < $tokenCount; ++$tokenNumber) {
// https://github.com/greenlion/PHP-SQL-Parser/issues/279
// https://github.com/sinri/PHP-SQL-Parser/commit/eac592a0e19f1df6f420af3777a6d5504837faa7
// as there is no pull request for 279 by the user. His solution works and tested.
if (!isset($tokens[$tokenNumber])) continue;// as a fix by Sinri 20180528
$token = $tokens[$tokenNumber];
$trim = trim($token); // this removes also \n and \t!
// if it starts with an "(", it should follow a SELECT
if ($trim !== "" && $trim[0] === "(" && $token_category === "") {
$token_category = 'SELECT';
$token_category = 'BRACKET';
$prev_category = $token_category;
}
/*
@@ -68,12 +86,12 @@ class SQLProcessor extends SQLChunkProcessor {
*/
if ($skip_next > 0) {
if ($trim === "") {
if ($token_category !== "") { # is this correct??
if ($token_category !== "") { // is this correct??
$out[$token_category][] = $token;
}
continue;
}
#to skip the token we replace it with whitespace
// to skip the token we replace it with whitespace
$trim = "";
$token = "";
$skip_next--;
@@ -88,7 +106,6 @@ class SQLProcessor extends SQLChunkProcessor {
/* Tokens that get their own sections. These keywords have subclauses. */
case 'SELECT':
case 'ORDER':
case 'DUPLICATE':
case 'VALUES':
case 'GROUP':
case 'HAVING':
@@ -110,6 +127,9 @@ class SQLProcessor extends SQLChunkProcessor {
case 'PURGE':
case 'EXECUTE':
case 'PREPARE':
$token_category = $upper;
break;
case 'DEALLOCATE':
if ($trim === 'DEALLOCATE') {
$skip_next = 1;
@@ -117,6 +137,12 @@ class SQLProcessor extends SQLChunkProcessor {
$token_category = $upper;
break;
case 'DUPLICATE':
if ($token_category !== 'VALUES') {
$token_category = $upper;
}
break;
case 'SET':
if ($token_category !== 'TABLE') {
$token_category = $upper;
@@ -125,21 +151,21 @@ class SQLProcessor extends SQLChunkProcessor {
case 'LIMIT':
case 'PLUGIN':
# no separate section
// no separate section
if ($token_category === 'SHOW') {
continue;
break;
}
$token_category = $upper;
break;
case 'FROM':
# this FROM is different from FROM in other DML (not join related)
// this FROM is different from FROM in other DML (not join related)
if ($token_category === 'PREPARE') {
continue 2;
}
# no separate section
// no separate section
if ($token_category === 'SHOW') {
continue;
break;
}
$token_category = $upper;
break;
@@ -149,7 +175,7 @@ class SQLProcessor extends SQLChunkProcessor {
case 'SHOW':
$token_category = $upper;
break;
case 'DESC':
if ($token_category === '') {
// short version of DESCRIBE
@@ -159,18 +185,16 @@ class SQLProcessor extends SQLChunkProcessor {
break;
case 'RENAME':
// jump over TABLE keyword
$token_category = $upper;
$skip_next = 1;
continue 2;
break;
case 'DATABASE':
case 'SCHEMA':
if ($prev_category === 'DROP') {
continue;
break;
}
if ($prev_category === 'SHOW') {
continue;
break;
}
$token_category = $upper;
break;
@@ -192,10 +216,10 @@ class SQLProcessor extends SQLChunkProcessor {
case 'INTO':
// prevent wrong handling of CACHE within LOAD INDEX INTO CACHE...
if ($prev_category === 'LOAD') {
$out[$prev_category][] = $upper;
$out[$prev_category][] = $trim;
continue 2;
}
$token_category = $upper;
$token_category = $prev_category = $upper;
break;
case 'USER':
@@ -234,60 +258,74 @@ class SQLProcessor extends SQLChunkProcessor {
case 'CHECKSUM':
case 'REPAIR':
case 'RESTORE':
case 'USE':
case 'HELP':
$token_category = $upper;
// set the category in case these get subclauses in a future version of MySQL
$out[$upper][0] = $upper;
$out[$upper][0] = $trim;
continue 2;
case 'REPLACE':
if ($prev_category === 'TABLE') {
# part of the CREATE TABLE statement
$out[$prev_category][] = $upper;
continue 2;
}
// set the category in case these get subclauses in a future version of MySQL
$token_category = $upper;
$out[$upper][0] = $upper;
if ($prev_category === '') {
// set the category in case these get subclauses in a future version of MySQL
$token_category = $upper;
$out[$upper][0] = $trim;
continue 2;
}
// part of the CREATE TABLE statement or a function
$out[$prev_category][] = $trim;
continue 2;
case 'IGNORE':
if ($prev_category === 'TABLE') {
# part of the CREATE TABLE statement
$out[$prev_category][] = $upper;
// part of the CREATE TABLE statement
$out[$prev_category][] = $trim;
continue 2;
}
if ($token_category === 'FROM') {
// part of the FROM statement (index hint)
$out[$token_category][] = $trim;
continue 2;
}
$out['OPTIONS'][] = $upper;
continue 2;
break;
case 'CHECK':
if ($prev_category === 'TABLE') {
$out[$prev_category][] = $upper;
$out[$prev_category][] = $trim;
continue 2;
}
$token_category = $upper;
$out[$upper][0] = $upper;
$out[$upper][0] = $trim;
continue 2;
case 'CREATE':
if ($prev_category === 'SHOW') {
continue;
break;
}
$token_category = $upper;
break;
case 'INDEX':
if ( in_array( $prev_category, array( 'CREATE', 'DROP' ) ) ) {
$out[ $prev_category ][] = $trim;
$token_category = $upper;
}
break;
case 'TABLE':
if ($prev_category === 'CREATE') {
$out[$prev_category][] = $upper;
$out[$prev_category][] = $trim;
$token_category = $upper;
}
if ($prev_category === 'TRUNCATE') {
$out[$prev_category][] = $trim;
$token_category = $upper;
}
break;
case 'TEMPORARY':
if ($prev_category === 'CREATE') {
$out[$prev_category][] = $upper;
$out[$prev_category][] = $trim;
$token_category = $prev_category;
continue 2;
}
@@ -298,7 +336,7 @@ class SQLProcessor extends SQLChunkProcessor {
$token_category = 'CREATE';
$out[$token_category] = array_merge($out[$token_category], $out[$prev_category]);
$out[$prev_category] = array();
$out[$token_category][] = $upper;
$out[$token_category][] = $trim;
$prev_category = $token_category;
continue 2;
}
@@ -307,14 +345,14 @@ class SQLProcessor extends SQLChunkProcessor {
case 'NOT':
if ($prev_category === 'CREATE') {
$token_category = $prev_category;
$out[$prev_category][] = $upper;
$out[$prev_category][] = $trim;
continue 2;
}
break;
case 'EXISTS':
if ($prev_category === 'CREATE') {
$out[$prev_category][] = $upper;
$out[$prev_category][] = $trim;
$prev_category = $token_category = 'TABLE';
continue 2;
}
@@ -322,7 +360,7 @@ class SQLProcessor extends SQLChunkProcessor {
case 'CACHE':
if ($prev_category === "" || $prev_category === 'RESET' || $prev_category === 'FLUSH'
|| $prev_category === 'LOAD') {
|| $prev_category === 'LOAD') {
$token_category = $upper;
continue 2;
}
@@ -332,14 +370,15 @@ class SQLProcessor extends SQLChunkProcessor {
case 'LOCK':
if ($token_category === "") {
$token_category = $upper;
$out[$upper][0] = $upper;
$out[$upper][0] = $trim;
} elseif ($token_category === 'INDEX') {
break;
} else {
$trim = 'LOCK IN SHARE MODE';
$skip_next = 3;
$out['OPTIONS'][] = $trim;
}
continue 2;
break;
case 'USING': /* USING in FROM clause is different from USING w/ prepared statement*/
if ($token_category === 'EXECUTE') {
@@ -362,12 +401,11 @@ class SQLProcessor extends SQLChunkProcessor {
case 'FOR':
if ($prev_category === 'SHOW') {
continue;
break;
}
$skip_next = 1;
$out['OPTIONS'][] = 'FOR UPDATE';
$out['OPTIONS'][] = 'FOR UPDATE'; // TODO: this could be generate problems within the position calculator
continue 2;
break;
case 'UPDATE':
if ($token_category === "") {
@@ -381,22 +419,30 @@ class SQLProcessor extends SQLChunkProcessor {
case 'START':
$trim = "BEGIN";
$out[$upper][0] = $upper;
$out[$upper][0] = $upper; // TODO: this could be generate problems within the position calculator
$skip_next = 1;
break;
/* These tokens are ignored. */
// This token is ignored, except within RENAME
case 'TO':
if ($token_category === 'RENAME') {
break;
}
continue 2;
// This token is ignored, except within CREATE TABLE
case 'BY':
if ($prev_category === 'TABLE') {
break;
}
continue 2;
// These tokens are ignored.
case 'ALL':
case 'SHARE':
case 'MODE':
case ';':
continue 2;
break;
case 'KEY':
if ($token_category === 'DUPLICATE') {
@@ -404,21 +450,43 @@ class SQLProcessor extends SQLChunkProcessor {
}
break;
/* These tokens set particular options for the statement. They never stand alone. */
/* These tokens set particular options for the statement. */
case 'LOW_PRIORITY':
case 'DELAYED':
case 'FORCE':
case 'QUICK':
$out['OPTIONS'][] = $upper;
case 'HIGH_PRIORITY':
$out['OPTIONS'][] = $trim;
continue 2;
case 'USE':
if ($token_category === 'FROM') {
// index hint within FROM clause
$out[$token_category][] = $trim;
continue 2;
}
// set the category in case these get subclauses in a future version of MySQL
$token_category = $upper;
$out[$upper][0] = $trim;
continue 2;
case 'FORCE':
if ($token_category === 'FROM') {
// index hint within FROM clause
$out[$token_category][] = $trim;
continue 2;
}
$out['OPTIONS'][] = $trim;
continue 2;
break;
case 'WITH':
if ($token_category === 'GROUP') {
$skip_next = 1;
$out['OPTIONS'][] = 'WITH ROLLUP';
$out['OPTIONS'][] = 'WITH ROLLUP'; // TODO: this could be generate problems within the position calculator
continue 2;
}
if ($token_category === '') {
$token_category = $upper;
}
break;
case 'AS':
@@ -426,7 +494,6 @@ class SQLProcessor extends SQLChunkProcessor {
case '':
case ',':
case ';':
break;
default:
@@ -445,4 +512,4 @@ class SQLProcessor extends SQLChunkProcessor {
return parent::process($out);
}
}
?>
?>

View File

@@ -30,23 +30,21 @@
* DAMAGE.
*/
require_once(dirname(__FILE__) . '/AbstractProcessor.php');
require_once(dirname(__FILE__) . '/ExpressionListProcessor.php');
require_once(dirname(__FILE__) . '/../utils/ExpressionType.php');
namespace PHPSQLParser\processors;
use PHPSQLParser\utils\ExpressionType;
/**
*
*
* This class processes the SELECT expressions.
*
*
* @author arothe
*
*
*/
class SelectExpressionProcessor extends AbstractProcessor {
private $expressionListProcessor;
public function __construct() {
$this->expressionListProcessor = new ExpressionListProcessor();
protected function processExpressionList($unparsed) {
$processor = new ExpressionListProcessor($this->options);
return $processor->process($unparsed);
}
/**
@@ -62,8 +60,8 @@ class SelectExpressionProcessor extends AbstractProcessor {
}
/*
* Determine if there is an explicit alias after the AS clause.
* If AS is found, then the next non-whitespace token is captured as the alias.
* Determine if there is an explicit alias after the AS clause.
* If AS is found, then the next non-whitespace token is captured as the alias.
* The tokens after (and including) the AS are removed.
*/
$base_expr = "";
@@ -102,7 +100,14 @@ class SelectExpressionProcessor extends AbstractProcessor {
$base_expr .= $token;
}
$stripped = $this->expressionListProcessor->process($stripped);
if ($alias) {
// remove quotation from the alias
$alias['no_quotes'] = $this->revokeQuotation($alias['name']);
$alias['name'] = trim($alias['name']);
$alias['base_expr'] = trim($alias['base_expr']);
}
$stripped = $this->processExpressionList($stripped);
// TODO: the last part can also be a comment, don't use array_pop
@@ -118,34 +123,24 @@ class SelectExpressionProcessor extends AbstractProcessor {
if ($this->isReserved($prev) || $this->isConstant($prev) || $this->isAggregateFunction($prev)
|| $this->isFunction($prev) || $this->isExpression($prev) || $this->isSubQuery($prev)
|| $this->isColumnReference($prev) || $this->isBracketExpression($prev)) {
|| $this->isColumnReference($prev) || $this->isBracketExpression($prev)|| $this->isCustomFunction($prev)) {
$alias = array('as' => false, 'name' => trim($last['base_expr']),
'no_quotes' => $this->revokeQuotation($last['base_expr']),
'base_expr' => trim($last['base_expr']));
// remove the last token
array_pop($tokens);
$base_expr = join("", $tokens);
}
}
if (!$alias) {
$base_expr = join("", $tokens);
} else {
/* remove escape from the alias */
$alias['no_quotes'] = $this->revokeQuotation($alias['name']);
$alias['name'] = trim($alias['name']);
$alias['base_expr'] = trim($alias['base_expr']);
}
$base_expr = $expression;
// TODO: this is always done with $stripped, how we do it twice?
$processed = $this->expressionListProcessor->process($tokens);
$processed = $this->processExpressionList($tokens);
// if there is only one part, we copy the expr_type
// in all other cases we use "expression" as global type
// in all other cases we use "EXPRESSION" as global type
$type = ExpressionType::EXPRESSION;
$no_quotes = $this->revokeQuotation(trim($base_expr));
if (count($processed) === 1) {
if (!$this->isSubQuery($processed[0])) {
$type = $processed[0]['expr_type'];
@@ -162,9 +157,9 @@ class SelectExpressionProcessor extends AbstractProcessor {
if (!empty($no_quotes)) {
$result['no_quotes'] = $no_quotes;
}
$result['sub_tree'] = $processed;
$result['sub_tree'] = (empty($processed) ? false : $processed);
return $result;
}
}
?>
?>

View File

@@ -30,7 +30,7 @@
* DAMAGE.
*/
require_once(dirname(__FILE__) . '/SelectExpressionProcessor.php');
namespace PHPSQLParser\processors;
/**
*
@@ -50,6 +50,8 @@ class SelectProcessor extends SelectExpressionProcessor {
$expression['delim'] = ',';
$expressionList[] = $expression;
$expression = "";
} else if ($this->isCommentToken($token)) {
$expressionList[] = parent::processComment($token);
} else {
switch (strtoupper($token)) {

View File

@@ -30,33 +30,34 @@
* DAMAGE.
*/
require_once(dirname(__FILE__) . '/AbstractProcessor.php');
require_once(dirname(__FILE__) . '/ExpressionListProcessor.php');
require_once(dirname(__FILE__) . '/../utils/ExpressionType.php');
namespace PHPSQLParser\processors;
use PHPSQLParser\utils\ExpressionType;
/**
*
*
* This class processes the SET statements.
*
*
* @author arothe
*
*
*/
class SetProcessor extends AbstractProcessor {
private $expressionListProcessor;
public function __construct() {
$this->expressionListProcessor = new ExpressionListProcessor();
protected function processExpressionList($tokens) {
$processor = new ExpressionListProcessor($this->options);
return $processor->process($tokens);
}
/**
* A SET list is simply a list of key = value expressions separated by comma (,).
* This function produces a list of the key/value expressions.
*/
protected function getAssignment($base_expr) {
$assignment = $this->expressionListProcessor->process($this->splitSQLIntoTokens($base_expr));
protected function processAssignment($base_expr) {
$assignment = $this->processExpressionList($this->splitSQLIntoTokens($base_expr));
// TODO: if the left side of the assignment is a reserved keyword, it should be changed to colref
return array('expr_type' => ExpressionType::EXPRESSION, 'base_expr' => trim($base_expr),
'sub_tree' => $assignment);
'sub_tree' => (empty($assignment) ? false : $assignment));
}
public function process($tokens, $isUpdate = false) {
@@ -66,13 +67,15 @@ class SetProcessor extends AbstractProcessor {
$varType = false;
foreach ($tokens as $token) {
$upper = strtoupper(trim($token));
$trim = trim($token);
$upper = strtoupper($trim);
switch ($upper) {
case 'LOCAL':
case 'SESSION':
case 'GLOBAL':
if (!$isUpdate) {
$result[] = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim);
$varType = $this->getVariableType("@@" . $upper . ".");
$baseExpr = "";
continue 2;
@@ -80,7 +83,7 @@ class SetProcessor extends AbstractProcessor {
break;
case ',':
$assignment = $this->getAssignment($baseExpr);
$assignment = $this->processAssignment($baseExpr);
if (!$isUpdate && $varType !== false) {
$assignment['sub_tree'][0]['expr_type'] = $varType;
}
@@ -95,7 +98,7 @@ class SetProcessor extends AbstractProcessor {
}
if (trim($baseExpr) !== "") {
$assignment = $this->getAssignment($baseExpr);
$assignment = $this->processAssignment($baseExpr);
if (!$isUpdate && $varType !== false) {
$assignment['sub_tree'][0]['expr_type'] = $varType;
}

View File

@@ -30,24 +30,25 @@
* DAMAGE.
*/
require_once(dirname(__FILE__) . '/../utils/PHPSQLParserConstants.php');
require_once(dirname(__FILE__) . '/../utils/ExpressionType.php');
require_once(dirname(__FILE__) . '/LimitProcessor.php');
require_once(dirname(__FILE__) . '/AbstractProcessor.php');
namespace PHPSQLParser\processors;
use PHPSQLParser\Options;
use PHPSQLParser\utils\ExpressionType;
use PHPSQLParser\utils\PHPSQLParserConstants;
/**
*
*
* This class processes the SHOW statements.
*
*
* @author arothe
*
*
*/
class ShowProcessor extends AbstractProcessor {
private $limitProcessor;
public function __construct() {
$this->limitProcessor = new LimitProcessor();
public function __construct(Options $options) {
parent::__construct($options);
$this->limitProcessor = new LimitProcessor($options);
}
public function process($tokens) {
@@ -67,13 +68,14 @@ class ShowProcessor extends AbstractProcessor {
case 'FROM':
$resultList[] = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => trim($token));
if ($prev === 'INDEX' || $prev === 'COLUMNS') {
continue;
break;
}
$category = $upper;
break;
case 'CREATE':
case 'DATABASE':
case 'SCHEMA':
case 'FUNCTION':
case 'PROCEDURE':
case 'ENGINE':
@@ -99,6 +101,7 @@ class ShowProcessor extends AbstractProcessor {
case 'TRIGGERS':
case 'VARIABLES':
case 'DATABASES':
case 'SCHEMAS':
case 'ERRORS':
case 'TABLES':
case 'WARNINGS':
@@ -120,6 +123,7 @@ class ShowProcessor extends AbstractProcessor {
$resultList[] = $limit;
break;
case 'FROM':
case 'SCHEMA':
case 'DATABASE':
$resultList[] = array('expr_type' => ExpressionType::DATABASE, 'name' => $token,
'no_quotes' => $this->revokeQuotation($token), 'base_expr' => $token);
@@ -136,7 +140,7 @@ class ShowProcessor extends AbstractProcessor {
$category = "TABLENAME";
break;
case 'FUNCTION':
if (PHPSQLParserConstants::isAggregateFunction($upper)) {
if (PHPSQLParserConstants::getInstance()->isAggregateFunction($upper)) {
$expr_type = ExpressionType::AGGREGATE_FUNCTION;
} else {
$expr_type = ExpressionType::SIMPLE_FUNCTION;

View File

@@ -0,0 +1,273 @@
<?php
/**
* SubpartitionDefinitionProcessor.php
*
* This file implements the processor for the SUBPARTITION statements
* within CREATE TABLE.
*
* PHP version 5
*
* LICENSE:
* Copyright (c) 2010-2014 Justin Swanhart and André Rothe
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author André Rothe <andre.rothe@phosco.info>
* @copyright 2010-2014 Justin Swanhart and André Rothe
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version SVN: $Id$
*
*/
namespace PHPSQLParser\processors;
use PHPSQLParser\utils\ExpressionType;
/**
* This class processes the SUBPARTITION statements within CREATE TABLE.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class SubpartitionDefinitionProcessor extends AbstractProcessor {
protected function getReservedType($token) {
return array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $token);
}
protected function getConstantType($token) {
return array('expr_type' => ExpressionType::CONSTANT, 'base_expr' => $token);
}
protected function getOperatorType($token) {
return array('expr_type' => ExpressionType::OPERATOR, 'base_expr' => $token);
}
protected function getBracketExpressionType($token) {
return array('expr_type' => ExpressionType::BRACKET_EXPRESSION, 'base_expr' => $token, 'sub_tree' => false);
}
public function process($tokens) {
$result = array();
$prevCategory = '';
$currCategory = '';
$parsed = array();
$expr = array();
$base_expr = '';
$skip = 0;
foreach ($tokens as $tokenKey => $token) {
$trim = trim($token);
$base_expr .= $token;
if ($skip > 0) {
$skip--;
continue;
}
if ($skip < 0) {
break;
}
if ($trim === '') {
continue;
}
$upper = strtoupper($trim);
switch ($upper) {
case 'SUBPARTITION':
if ($currCategory === '') {
$expr[] = $this->getReservedType($trim);
$parsed = array('expr_type' => ExpressionType::SUBPARTITION_DEF, 'base_expr' => trim($base_expr),
'sub_tree' => false);
$currCategory = $upper;
continue 2;
}
// else ?
break;
case 'COMMENT':
if ($prevCategory === 'SUBPARTITION') {
$expr[] = array('expr_type' => ExpressionType::SUBPARTITION_COMMENT, 'base_expr' => false,
'sub_tree' => false, 'storage' => substr($base_expr, 0, -strlen($token)));
$parsed['sub_tree'] = $expr;
$base_expr = $token;
$expr = array($this->getReservedType($trim));
$currCategory = $upper;
continue 2;
}
// else ?
break;
case 'STORAGE':
if ($prevCategory === 'SUBPARTITION') {
// followed by ENGINE
$expr[] = array('expr_type' => ExpressionType::ENGINE, 'base_expr' => false, 'sub_tree' => false,
'storage' => substr($base_expr, 0, -strlen($token)));
$parsed['sub_tree'] = $expr;
$base_expr = $token;
$expr = array($this->getReservedType($trim));
$currCategory = $upper;
continue 2;
}
// else ?
break;
case 'ENGINE':
if ($currCategory === 'STORAGE') {
$expr[] = $this->getReservedType($trim);
$currCategory = $upper;
continue 2;
}
if ($prevCategory === 'SUBPARTITION') {
$expr[] = array('expr_type' => ExpressionType::ENGINE, 'base_expr' => false, 'sub_tree' => false,
'storage' => substr($base_expr, 0, -strlen($token)));
$parsed['sub_tree'] = $expr;
$base_expr = $token;
$expr = array($this->getReservedType($trim));
$currCategory = $upper;
continue 2;
}
// else ?
break;
case '=':
if (in_array($currCategory, array('ENGINE', 'COMMENT', 'DIRECTORY', 'MAX_ROWS', 'MIN_ROWS'))) {
$expr[] = $this->getOperatorType($trim);
continue 2;
}
// else ?
break;
case ',':
if ($prevCategory === 'SUBPARTITION' && $currCategory === '') {
// it separates the subpartition-definitions
$result[] = $parsed;
$parsed = array();
$base_expr = '';
$expr = array();
}
break;
case 'DATA':
case 'INDEX':
if ($prevCategory === 'SUBPARTITION') {
// followed by DIRECTORY
$expr[] = array('expr_type' => constant('PHPSQLParser\utils\ExpressionType::SUBPARTITION_' . $upper . '_DIR'),
'base_expr' => false, 'sub_tree' => false,
'storage' => substr($base_expr, 0, -strlen($token)));
$parsed['sub_tree'] = $expr;
$base_expr = $token;
$expr = array($this->getReservedType($trim));
$currCategory = $upper;
continue 2;
}
// else ?
break;
case 'DIRECTORY':
if ($currCategory === 'DATA' || $currCategory === 'INDEX') {
$expr[] = $this->getReservedType($trim);
$currCategory = $upper;
continue 2;
}
// else ?
break;
case 'MAX_ROWS':
case 'MIN_ROWS':
if ($prevCategory === 'SUBPARTITION') {
$expr[] = array('expr_type' => constant('PHPSQLParser\utils\ExpressionType::SUBPARTITION_' . $upper),
'base_expr' => false, 'sub_tree' => false,
'storage' => substr($base_expr, 0, -strlen($token)));
$parsed['sub_tree'] = $expr;
$base_expr = $token;
$expr = array($this->getReservedType($trim));
$currCategory = $upper;
continue 2;
}
// else ?
break;
default:
switch ($currCategory) {
case 'MIN_ROWS':
case 'MAX_ROWS':
case 'ENGINE':
case 'DIRECTORY':
case 'COMMENT':
$expr[] = $this->getConstantType($trim);
$last = array_pop($parsed['sub_tree']);
$last['sub_tree'] = $expr;
$last['base_expr'] = trim($base_expr);
$base_expr = $last['storage'] . $base_expr;
unset($last['storage']);
$parsed['sub_tree'][] = $last;
$parsed['base_expr'] = trim($base_expr);
$expr = $parsed['sub_tree'];
unset($last);
$currCategory = $prevCategory;
break;
case 'SUBPARTITION':
// that is the subpartition name
$last = array_pop($expr);
$last['name'] = $trim;
$expr[] = $last;
$expr[] = $this->getConstantType($trim);
$parsed['sub_tree'] = $expr;
$parsed['base_expr'] = trim($base_expr);
break;
default:
break;
}
break;
}
$prevCategory = $currCategory;
$currCategory = '';
}
$result[] = $parsed;
return $result;
}
}
?>

View File

@@ -4,41 +4,49 @@
*
* This file implements the processor for the TABLE statements.
*
* Copyright (c) 2010-2012, Justin Swanhart
* with contributions by André Rothe <arothe@phosco.info, phosco@gmx.de>
* PHP version 5
*
* LICENSE:
* Copyright (c) 2010-2014 Justin Swanhart and André Rothe
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author André Rothe <andre.rothe@phosco.info>
* @copyright 2010-2014 Justin Swanhart and André Rothe
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version SVN: $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
require_once(dirname(__FILE__) . '/AbstractProcessor.php');
require_once(dirname(__FILE__) . '/CreateDefinitionProcessor.php');
require_once(dirname(__FILE__) . '/../utils/ExpressionType.php');
namespace PHPSQLParser\processors;
use PHPSQLParser\utils\ExpressionType;
/**
*
* This class processes the TABLE statements.
*
* @author arothe
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class TableProcessor extends AbstractProcessor {
@@ -55,6 +63,16 @@ class TableProcessor extends AbstractProcessor {
return array('expr_type' => ExpressionType::OPERATOR, 'base_expr' => $token);
}
protected function processPartitionOptions($tokens) {
$processor = new PartitionOptionsProcessor($this->options);
return $processor->process($tokens);
}
protected function processCreateDefinition($tokens) {
$processor = new CreateDefinitionProcessor($this->options);
return $processor->process($tokens);
}
protected function clear(&$expr, &$base_expr, &$category) {
$expr = array();
$base_expr = '';
@@ -63,14 +81,14 @@ class TableProcessor extends AbstractProcessor {
public function process($tokens) {
$currCategory = "TABLE_NAME";
$currCategory = 'TABLE_NAME';
$result = array('base_expr' => false, 'name' => false, 'no_quotes' => false, 'create-def' => false,
'options' => false, 'like' => false, 'select-option' => false);
$expr = array();
$base_expr = '';
$skip = 0;
foreach ($tokens as $token) {
foreach ($tokens as $tokenKey => $token) {
$trim = trim($token);
$base_expr .= $token;
@@ -83,7 +101,7 @@ class TableProcessor extends AbstractProcessor {
break;
}
if ($trim === "") {
if ($trim === '') {
continue;
}
@@ -91,12 +109,12 @@ class TableProcessor extends AbstractProcessor {
switch ($upper) {
case ',':
# it is possible to separate the table options with comma!
// it is possible to separate the table options with comma!
if ($prevCategory === 'CREATE_DEF') {
$last = array_pop($result['options']);
$last['delim'] = ',';
$result['options'][] = $last;
$base_expr = "";
$base_expr = '';
}
continue 2;
@@ -109,7 +127,7 @@ class TableProcessor extends AbstractProcessor {
break;
case 'LIKE':
# like without parenthesis
// like without parenthesis
if ($prevCategory === 'TABLE_NAME') {
$currCategory = $upper;
continue 2;
@@ -117,10 +135,10 @@ class TableProcessor extends AbstractProcessor {
break;
case '=':
# the optional operator
// the optional operator
if ($prevCategory === 'TABLE_OPTION') {
$expr[] = $this->getOperatorType($trim);
continue 2; # don't change the category
continue 2; // don't change the category
}
break;
@@ -130,15 +148,16 @@ class TableProcessor extends AbstractProcessor {
$currCategory = 'TABLE_OPTION';
}
if ($prevCategory === 'TABLE_OPTION') {
# add it to the previous DEFAULT
// add it to the previous DEFAULT
$expr[] = $this->getReservedType($trim);
continue 2;
}
break;
case 'SET':
case 'CHARSET':
if ($prevCategory === 'TABLE_OPTION') {
# add it to a previous CHARACTER
// add it to a previous CHARACTER
$expr[] = $this->getReservedType($trim);
$currCategory = 'CHARSET';
continue 2;
@@ -146,8 +165,8 @@ class TableProcessor extends AbstractProcessor {
break;
case 'COLLATE':
if ($prevCategory === 'TABLE_OPTION') {
# add it to the previous DEFAULT
if ($prevCategory === 'TABLE_OPTION' || $prevCategory === 'CREATE_DEF') {
// add it to the previous DEFAULT
$expr[] = $this->getReservedType($trim);
$currCategory = 'COLLATE';
continue 2;
@@ -156,7 +175,7 @@ class TableProcessor extends AbstractProcessor {
case 'DIRECTORY':
if ($currCategory === 'INDEX_DIRECTORY' || $currCategory === 'DATA_DIRECTORY') {
# after INDEX or DATA
// after INDEX or DATA
$expr[] = $this->getReservedType($trim);
continue 2;
}
@@ -212,16 +231,15 @@ class TableProcessor extends AbstractProcessor {
case 'LAST':
case 'DEFAULT':
if ($prevCategory === 'CREATE_DEF') {
# DEFAULT before CHARACTER SET and COLLATE
// DEFAULT before CHARACTER SET and COLLATE
$expr[] = $this->getReservedType($trim);
$currCategory = 'TABLE_OPTION';
}
if ($prevCategory === 'TABLE_OPTION') {
# all assignments with the keywords
// all assignments with the keywords
$expr[] = $this->getReservedType($trim);
$result['options'][] = array('expr_type' => ExpressionType::EXPRESSION,
'base_expr' => trim($base_expr), 'delim' => ' ',
'sub_tree' => $expr);
'base_expr' => trim($base_expr), 'delim' => ' ', 'sub_tree' => $expr);
$this->clear($expr, $base_expr, $currCategory);
}
break;
@@ -244,46 +262,47 @@ class TableProcessor extends AbstractProcessor {
continue 2;
case 'PARTITION':
# TODO: parse partition options
$skip = -1;
if ($prevCategory === 'CREATE_DEF') {
$part = $this->processPartitionOptions(array_slice($tokens, $tokenKey - 1, null, true));
$skip = $part['last-parsed'] - $tokenKey;
$result['partition-options'] = $part['partition-options'];
continue 2;
}
// else
break;
default:
switch ($currCategory) {
case 'CHARSET':
# the charset name
// the charset name
$expr[] = $this->getConstantType($trim);
$result['options'][] = array('expr_type' => ExpressionType::CHARSET,
'base_expr' => trim($base_expr), 'delim' => ' ',
'sub_tree' => $expr);
'base_expr' => trim($base_expr), 'delim' => ' ', 'sub_tree' => $expr);
$this->clear($expr, $base_expr, $currCategory);
break;
case 'COLLATE':
# the collate name
// the collate name
$expr[] = $this->getConstantType($trim);
$result['options'][] = array('expr_type' => ExpressionType::COLLATE,
'base_expr' => trim($base_expr), 'delim' => ' ',
'sub_tree' => $expr);
'base_expr' => trim($base_expr), 'delim' => ' ', 'sub_tree' => $expr);
$this->clear($expr, $base_expr, $currCategory);
break;
case 'DATA_DIRECTORY':
# we have the directory name
// we have the directory name
$expr[] = $this->getConstantType($trim);
$result['options'][] = array('expr_type' => ExpressionType::DIRECTORY, 'kind' => 'DATA',
'base_expr' => trim($base_expr), 'delim' => ' ',
'sub_tree' => $expr);
'base_expr' => trim($base_expr), 'delim' => ' ', 'sub_tree' => $expr);
$this->clear($expr, $base_expr, $prevCategory);
continue 3;
case 'INDEX_DIRECTORY':
# we have the directory name
// we have the directory name
$expr[] = $this->getConstantType($trim);
$result['options'][] = array('expr_type' => ExpressionType::DIRECTORY, 'kind' => 'INDEX',
'base_expr' => trim($base_expr), 'delim' => ' ',
'sub_tree' => $expr);
'base_expr' => trim($base_expr), 'delim' => ' ', 'sub_tree' => $expr);
$this->clear($expr, $base_expr, $prevCategory);
continue 3;
@@ -294,17 +313,16 @@ class TableProcessor extends AbstractProcessor {
break;
case 'LIKE':
$result['like'] = array('expr_type' => ExpressionType::TABLE, 'table' => $trim, 'base_expr' => $trim,
'no_quotes' => $this->revokeQuotation($trim));
$result['like'] = array('expr_type' => ExpressionType::TABLE, 'table' => $trim,
'base_expr' => $trim, 'no_quotes' => $this->revokeQuotation($trim));
$this->clear($expr, $base_expr, $currCategory);
break;
case '':
# after table name
// after table name
if ($prevCategory === 'TABLE_NAME' && $upper[0] === '(' && substr($upper, -1) === ')') {
$unparsed = $this->splitSQLIntoTokens($this->removeParenthesisFromStart($trim));
$processor = new CreateDefinitionProcessor();
$coldef = $processor->process($unparsed);
$coldef = $this->processCreateDefinition($unparsed);
$result['create-def'] = array('expr_type' => ExpressionType::BRACKET_EXPRESSION,
'base_expr' => $base_expr, 'sub_tree' => $coldef['create-def']);
$expr = array();
@@ -314,10 +332,10 @@ class TableProcessor extends AbstractProcessor {
break;
case 'UNION':
# TODO: this token starts and ends with parenthesis
# and contains a list of table names (comma-separated)
# split the token and add the list as subtree
# we must change the DefaultProcessor
// TODO: this token starts and ends with parenthesis
// and contains a list of table names (comma-separated)
// split the token and add the list as subtree
// we must change the DefaultProcessor
$unparsed = $this->splitSQLIntoTokens($this->removeParenthesisFromStart($trim));
$expr[] = array('expr_type' => ExpressionType::BRACKET_EXPRESSION, 'base_expr' => $trim,
@@ -328,11 +346,10 @@ class TableProcessor extends AbstractProcessor {
break;
default:
# strings and numeric constants
// strings and numeric constants
$expr[] = $this->getConstantType($trim);
$result['options'][] = array('expr_type' => ExpressionType::EXPRESSION,
'base_expr' => trim($base_expr), 'delim' => ' ',
'sub_tree' => $expr);
'base_expr' => trim($base_expr), 'delim' => ' ', 'sub_tree' => $expr);
$this->clear($expr, $base_expr, $currCategory);
break;
}
@@ -340,7 +357,7 @@ class TableProcessor extends AbstractProcessor {
}
$prevCategory = $currCategory;
$currCategory = "";
$currCategory = '';
}
if ($result['like'] === false) {

View File

@@ -1,50 +1,66 @@
<?php
/**
* UnionProcessor.php
* WhereProcessor.php
*
* This file implements the processor for the UNION statements.
*
* Copyright (c) 2010-2012, Justin Swanhart
* with contributions by André Rothe <arothe@phosco.info, phosco@gmx.de>
* PHP version 5
*
* LICENSE:
* Copyright (c) 2010-2014 Justin Swanhart and André Rothe
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author André Rothe <andre.rothe@phosco.info>
* @copyright 2010-2014 Justin Swanhart and André Rothe
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version SVN: $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
require_once(dirname(__FILE__) . '/AbstractProcessor.php');
require_once(dirname(__FILE__) . '/SQLProcessor.php');
require_once(dirname(__FILE__) . '/DefaultProcessor.php');
require_once(dirname(__FILE__) . '/../utils/ExpressionType.php');
namespace PHPSQLParser\processors;
/**
*
* This class processes the UNION statements.
*
* @author arothe
*
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class UnionProcessor extends AbstractProcessor {
public function isUnion($queries) {
protected function processDefault($token) {
$processor = new DefaultProcessor($this->options);
return $processor->process($token);
}
protected function processSQL($token) {
$processor = new SQLProcessor($this->options);
return $processor->process($token);
}
public static function isUnion($queries) {
$unionTypes = array('UNION', 'UNION ALL');
foreach ($unionTypes as $unionType) {
if (!empty($queries[$unionType])) {
@@ -81,21 +97,66 @@ class UnionProcessor extends AbstractProcessor {
// starts with "(select"
if (preg_match("/^\\(\\s*select\\s*/i", $token)) {
$processor = new DefaultProcessor();
$queries[$unionType][$key] = $processor->process($this->removeParenthesisFromStart($token));
$queries[$unionType][$key] = $this->processDefault($this->removeParenthesisFromStart($token));
break;
}
$processor = new SQLProcessor();
$queries[$unionType][$key] = $processor->process($queries[$unionType][$key]);
$queries[$unionType][$key] = $this->processSQL($queries[$unionType][$key]);
break;
}
}
}
// it can be parsed or not
return $queries;
}
/**
* Moves the final union query into a separate output, so the remainder (such as ORDER BY) can
* be processed separately.
*/
protected function splitUnionRemainder($queries, $unionType, $outputArray)
{
$finalQuery = [];
//If this token contains a matching pair of brackets at the start and end, use it as the final query
$finalQueryFound = false;
if (count($outputArray) === 1) {
$tokenAsArray = str_split(trim($outputArray[0]));
if ($tokenAsArray[0] == '(' && $tokenAsArray[count($tokenAsArray)-1] == ')') {
$queries[$unionType][] = $outputArray;
$finalQueryFound = true;
}
}
if (!$finalQueryFound) {
foreach ($outputArray as $key => $token) {
if (strtoupper($token) == 'ORDER') {
break;
} else {
$finalQuery[] = $token;
unset($outputArray[$key]);
}
}
}
$finalQueryString = trim(implode($finalQuery));
if (!empty($finalQuery) && $finalQueryString != '') {
$queries[$unionType][] = $finalQuery;
}
$defaultProcessor = new DefaultProcessor($this->options);
$rePrepareSqlString = trim(implode($outputArray));
if (!empty($rePrepareSqlString)) {
$remainingQueries = $defaultProcessor->process($rePrepareSqlString);
$queries[] = $remainingQueries;
}
return $queries;
}
public function process($inputArray) {
$outputArray = array();
@@ -155,7 +216,7 @@ class UnionProcessor extends AbstractProcessor {
// or we don't have an UNION/UNION ALL
if (!empty($outputArray)) {
if ($unionType) {
$queries[$unionType][] = $outputArray;
$queries = $this->splitUnionRemainder($queries, $unionType, $outputArray);
} else {
$queries[] = $outputArray;
}
@@ -163,6 +224,5 @@ class UnionProcessor extends AbstractProcessor {
return $this->processMySQLUnion($queries);
}
}
?>
?>

View File

@@ -4,42 +4,51 @@
*
* This file implements the processor for the UPDATE statements.
*
* Copyright (c) 2010-2012, Justin Swanhart
* with contributions by André Rothe <arothe@phosco.info, phosco@gmx.de>
* PHP version 5
*
* LICENSE:
* Copyright (c) 2010-2014 Justin Swanhart and André Rothe
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author André Rothe <andre.rothe@phosco.info>
* @copyright 2010-2014 Justin Swanhart and André Rothe
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version SVN: $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
require_once(dirname(__FILE__) . '/FromProcessor.php');
namespace PHPSQLParser\processors;
/**
*
* This class processes the UPDATE statements.
*
* @author arothe
*
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class UpdateProcessor extends FromProcessor {
}
?>
?>

View File

@@ -4,42 +4,51 @@
*
* This file implements the processor for the USING statements.
*
* Copyright (c) 2010-2012, Justin Swanhart
* with contributions by André Rothe <arothe@phosco.info, phosco@gmx.de>
* PHP version 5
*
* LICENSE:
* Copyright (c) 2010-2014 Justin Swanhart and André Rothe
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author André Rothe <andre.rothe@phosco.info>
* @copyright 2010-2014 Justin Swanhart and André Rothe
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version SVN: $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
require_once(dirname(__FILE__) . '/FromProcessor.php');
namespace PHPSQLParser\processors;
/**
*
* This class processes the USING statements.
*
* @author arothe
*
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class UsingProcessor extends FromProcessor {
}
?>
?>

View File

@@ -4,76 +4,146 @@
*
* This file implements the processor for the VALUES statements.
*
* Copyright (c) 2010-2012, Justin Swanhart
* with contributions by André Rothe <arothe@phosco.info, phosco@gmx.de>
* PHP version 5
*
* LICENSE:
* Copyright (c) 2010-2014 Justin Swanhart and André Rothe
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author André Rothe <andre.rothe@phosco.info>
* @copyright 2010-2014 Justin Swanhart and André Rothe
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version SVN: $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
require_once(dirname(__FILE__) . '/../utils/ExpressionType.php');
require_once(dirname(__FILE__) . '/RecordProcessor.php');
require_once(dirname(__FILE__) . '/AbstractProcessor.php');
namespace PHPSQLParser\processors;
use PHPSQLParser\utils\ExpressionType;
/**
*
* This class processes the VALUES statements.
*
* @author arothe
*
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class ValuesProcessor extends AbstractProcessor {
private $recordProcessor;
protected function processExpressionList($unparsed) {
$processor = new ExpressionListProcessor($this->options);
return $processor->process($unparsed);
}
public function __construct() {
$this->recordProcessor = new RecordProcessor();
protected function processRecord($unparsed) {
$processor = new RecordProcessor($this->options);
return $processor->process($unparsed);
}
public function process($tokens) {
$unparsed = "";
$currCategory = '';
$parsed = array();
$base_expr = '';
foreach ($tokens['VALUES'] as $k => $v) {
if ($this->isCommentToken($v)) {
$parsed[] = parent::processComment($v);
continue;
}
$base_expr .= $v;
$trim = trim($v);
if ($this->isWhitespaceToken($v)) {
continue;
}
$unparsed .= $v;
$upper = strtoupper($trim);
switch ($upper) {
case 'ON':
if ($currCategory === '') {
$base_expr = trim(substr($base_expr, 0, -strlen($v)));
$parsed[] = array('expr_type' => ExpressionType::RECORD, 'base_expr' => $base_expr,
'data' => $this->processRecord($base_expr), 'delim' => false);
$base_expr = '';
$currCategory = 'DUPLICATE';
$parsed[] = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim);
}
// else ?
break;
case 'DUPLICATE':
case 'KEY':
case 'UPDATE':
if ($currCategory === 'DUPLICATE') {
$parsed[] = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim);
$base_expr = '';
}
// else ?
break;
case ',':
if ($currCategory === 'DUPLICATE') {
$base_expr = trim(substr($base_expr, 0, -strlen($v)));
$res = $this->processExpressionList($this->splitSQLIntoTokens($base_expr));
$parsed[] = array('expr_type' => ExpressionType::EXPRESSION, 'base_expr' => $base_expr,
'sub_tree' => (empty($res) ? false : $res), 'delim' => $trim);
$base_expr = '';
continue 2;
}
$parsed[] = array('expr_type' => ExpressionType::RECORD, 'base_expr' => trim($base_expr),
'data' => $this->processRecord(trim($base_expr)), 'delim' => $trim);
$base_expr = '';
break;
default:
break;
}
}
$values = $this->splitSQLIntoTokens($unparsed);
$parsed = array();
foreach ($values as $k => $v) {
if ($this->isCommaToken($v)) {
unset($values[$k]);
} else {
$processor = new RecordProcessor();
$values[$k] = array('expr_type' => ExpressionType::RECORD, 'base_expr' => $v,
'data' => $this->recordProcessor->process($v));
if (trim($base_expr) !== '') {
if ($currCategory === '') {
$parsed[] = array('expr_type' => ExpressionType::RECORD, 'base_expr' => trim($base_expr),
'data' => $this->processRecord(trim($base_expr)), 'delim' => false);
}
if ($currCategory === 'DUPLICATE') {
$res = $this->processExpressionList($this->splitSQLIntoTokens($base_expr));
$parsed[] = array('expr_type' => ExpressionType::EXPRESSION, 'base_expr' => trim($base_expr),
'sub_tree' => (empty($res) ? false : $res), 'delim' => false);
}
}
$tokens['VALUES'] = array_values($values);
$tokens['VALUES'] = $parsed;
return $tokens;
}
}
?>
?>

View File

@@ -4,42 +4,51 @@
*
* This file implements the processor for the WHERE statements.
*
* Copyright (c) 2010-2012, Justin Swanhart
* with contributions by André Rothe <arothe@phosco.info, phosco@gmx.de>
* PHP version 5
*
* LICENSE:
* Copyright (c) 2010-2014 Justin Swanhart and André Rothe
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author André Rothe <andre.rothe@phosco.info>
* @copyright 2010-2014 Justin Swanhart and André Rothe
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version SVN: $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
require_once(dirname(__FILE__) . '/ExpressionListProcessor.php');
namespace PHPSQLParser\processors;
/**
*
* This class processes the WHERE statements.
*
* @author arothe
*
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class WhereProcessor extends ExpressionListProcessor {
}
?>
?>

View File

@@ -0,0 +1,119 @@
<?php
/**
* WithProcessor.php
*
* This file implements the processor for Oracle's WITH statements.
*
* Copyright (c) 2010-2012, Justin Swanhart
* with contributions by André Rothe <arothe@phosco.info, phosco@gmx.de>
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
namespace PHPSQLParser\processors;
use PHPSQLParser\utils\ExpressionType;
/**
*
* This class processes Oracle's WITH statements.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
class WithProcessor extends AbstractProcessor {
protected function processTopLevel($sql) {
$processor = new DefaultProcessor($this->options);
return $processor->process($sql);
}
protected function buildTableName($token) {
return array('expr_type' => ExpressionType::TEMPORARY_TABLE, 'name'=>$token, 'base_expr' => $token, 'no_quotes' => $this->revokeQuotation($token));
}
public function process($tokens) {
$out = array();
$resultList = array();
$category = '';
$base_expr = '';
$prev = '';
foreach ($tokens as $token) {
$base_expr .= $token;
$upper = strtoupper(trim($token));
if ($this->isWhitespaceToken($token)) {
continue;
}
$trim = trim($token);
switch ($upper) {
case 'AS':
if ($prev !== 'TABLENAME') {
// error or tablename is AS
$resultList[] = $this->buildTableName($trim);
$category = 'TABLENAME';
break;
}
$resultList[] = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => $trim);
$category = $upper;
break;
case ',':
// ignore
$base_expr = '';
break;
default:
switch ($prev) {
case 'AS':
// it follows a parentheses pair
$subtree = $this->processTopLevel($this->removeParenthesisFromStart($token));
$resultList[] = array('expr_type' => ExpressionType::BRACKET_EXPRESSION, 'base_expr' => $trim, 'sub_tree' => $subtree);
$out[] = array('expr_type' => ExpressionType::SUBQUERY_FACTORING, 'base_expr' => trim($base_expr), 'sub_tree' => $resultList);
$resultList = array();
$category = '';
break;
case '':
// we have the name of the table
$resultList[] = $this->buildTableName($trim);
$category = 'TABLENAME';
break;
default:
// ignore
break;
}
break;
}
$prev = $category;
}
return $out;
}
}
?>