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,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);
}
}
?>
?>