From 9206c46258bfa6d365d127161323b3c317283192 Mon Sep 17 00:00:00 2001 From: Nicolas Dermine Date: Thu, 11 Aug 2022 02:50:58 +0200 Subject: [PATCH] Fix issue 335 multiplication operator wrongly parsed as colref (#336) * #335 tidy * fix issue #335 multiplication operator was parsed as `'colref'` instead of `'operator'` (if it was positioned after the function expression, as in the second test) --- .../processors/ExpressionListProcessor.php | 11 ++- tests/cases/parser/issue335Test.php | 92 +++++++++++++++++++ 2 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 tests/cases/parser/issue335Test.php diff --git a/src/PHPSQLParser/processors/ExpressionListProcessor.php b/src/PHPSQLParser/processors/ExpressionListProcessor.php index b0353f76..f8d72cf7 100644 --- a/src/PHPSQLParser/processors/ExpressionListProcessor.php +++ b/src/PHPSQLParser/processors/ExpressionListProcessor.php @@ -256,8 +256,15 @@ public function process($tokens) { // if the last token is colref, const or expression // 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()) { + if ( + !$prev->isColumnReference() + && !$prev->isConstant() + && !$prev->isExpression() + && !$prev->isBracketExpression() + && !$prev->isAggregateFunction() + && !$prev->isVariable() + && !$prev->isFunction() + ) { $curr->setTokenType(ExpressionType::COLREF); break; } diff --git a/tests/cases/parser/issue335Test.php b/tests/cases/parser/issue335Test.php new file mode 100644 index 00000000..7dd5a869 --- /dev/null +++ b/tests/cases/parser/issue335Test.php @@ -0,0 +1,92 @@ + + * @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\Test\Parser; + +use PHPSQLParser\PHPSQLParser; + +class issue335Test extends \PHPUnit_Framework_TestCase +{ + /** + * @test + */ + public function multiplication_operator_is_correctly_parsed_before_function_expression() + { + $parser = new PHPSQLParser(); + + $parser->parse('SELECT 15 * ROUND(3, 1) FROM dual'); + $multiplicationOperator = $parser->parsed['SELECT'][0]['sub_tree'][1]; + $this->assertEquals('*', $multiplicationOperator['base_expr']); + $this->assertEquals('operator', $multiplicationOperator['expr_type']); + } + + /** + * @test + */ + public function multiplication_operator_is_correctly_parsed_after_function_expression() + { + $parser = new PHPSQLParser(); + + $parser->parse('SELECT ROUND(3, 1) * 15 FROM dual'); + $multiplicationOperator = $parser->parsed['SELECT'][0]['sub_tree'][1]; + $this->assertEquals('*', $multiplicationOperator['base_expr']); + $this->assertEquals('operator', $multiplicationOperator['expr_type']); + } + + /** + * @test + */ + public function star_is_parsed_as_colref() + { + $parser = new PHPSQLParser(); + + $parser->parse('SELECT * FROM a_table'); + $star = $parser->parsed['SELECT'][0]; + $this->assertEquals('*', $star['base_expr']); + $this->assertEquals('colref', $star['expr_type']); + + $parser->parse('SELECT ROUND(3, 1), * FROM a_table'); + $star = $parser->parsed['SELECT'][1]; + $this->assertEquals('*', $star['base_expr']); + $this->assertEquals('colref', $star['expr_type']); + + $parser->parse('SELECT ROUND(3, 1), a_table.* FROM a_table'); + $star = $parser->parsed['SELECT'][1]; + $this->assertEquals('a_table.*', $star['base_expr']); + $this->assertEquals('colref', $star['expr_type']); + } +}