Skip to content

Commit 9206c46

Browse files
authored
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)
1 parent 4232d06 commit 9206c46

File tree

2 files changed

+101
-2
lines changed

2 files changed

+101
-2
lines changed

src/PHPSQLParser/processors/ExpressionListProcessor.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,15 @@ public function process($tokens) {
256256
// if the last token is colref, const or expression
257257
// then * is an operator
258258
// but if the previous colref ends with a dot, the * is the all-columns-alias
259-
if (!$prev->isColumnReference() && !$prev->isConstant() && !$prev->isExpression()
260-
&& !$prev->isBracketExpression() && !$prev->isAggregateFunction() && !$prev->isVariable()) {
259+
if (
260+
!$prev->isColumnReference()
261+
&& !$prev->isConstant()
262+
&& !$prev->isExpression()
263+
&& !$prev->isBracketExpression()
264+
&& !$prev->isAggregateFunction()
265+
&& !$prev->isVariable()
266+
&& !$prev->isFunction()
267+
) {
261268
$curr->setTokenType(ExpressionType::COLREF);
262269
break;
263270
}

tests/cases/parser/issue335Test.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
/**
3+
* Test case for PHPSQLParser.
4+
*
5+
* PHP version 5
6+
*
7+
* LICENSE:
8+
* Copyright (c) 2010-2014 Justin Swanhart and André Rothe
9+
* All rights reserved.
10+
*
11+
* Redistribution and use in source and binary forms, with or without
12+
* modification, are permitted provided that the following conditions
13+
* are met:
14+
* 1. Redistributions of source code must retain the above copyright
15+
* notice, this list of conditions and the following disclaimer.
16+
* 2. Redistributions in binary form must reproduce the above copyright
17+
* notice, this list of conditions and the following disclaimer in the
18+
* documentation and/or other materials provided with the distribution.
19+
* 3. The name of the author may not be used to endorse or promote products
20+
* derived from this software without specific prior written permission.
21+
*
22+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23+
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24+
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25+
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27+
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31+
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32+
*
33+
* @author André Rothe <[email protected]>
34+
* @copyright 2010-2014 Justin Swanhart and André Rothe
35+
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
36+
* @version SVN: $Id$
37+
*/
38+
namespace PHPSQLParser\Test\Parser;
39+
40+
use PHPSQLParser\PHPSQLParser;
41+
42+
class issue335Test extends \PHPUnit_Framework_TestCase
43+
{
44+
/**
45+
* @test
46+
*/
47+
public function multiplication_operator_is_correctly_parsed_before_function_expression()
48+
{
49+
$parser = new PHPSQLParser();
50+
51+
$parser->parse('SELECT 15 * ROUND(3, 1) FROM dual');
52+
$multiplicationOperator = $parser->parsed['SELECT'][0]['sub_tree'][1];
53+
$this->assertEquals('*', $multiplicationOperator['base_expr']);
54+
$this->assertEquals('operator', $multiplicationOperator['expr_type']);
55+
}
56+
57+
/**
58+
* @test
59+
*/
60+
public function multiplication_operator_is_correctly_parsed_after_function_expression()
61+
{
62+
$parser = new PHPSQLParser();
63+
64+
$parser->parse('SELECT ROUND(3, 1) * 15 FROM dual');
65+
$multiplicationOperator = $parser->parsed['SELECT'][0]['sub_tree'][1];
66+
$this->assertEquals('*', $multiplicationOperator['base_expr']);
67+
$this->assertEquals('operator', $multiplicationOperator['expr_type']);
68+
}
69+
70+
/**
71+
* @test
72+
*/
73+
public function star_is_parsed_as_colref()
74+
{
75+
$parser = new PHPSQLParser();
76+
77+
$parser->parse('SELECT * FROM a_table');
78+
$star = $parser->parsed['SELECT'][0];
79+
$this->assertEquals('*', $star['base_expr']);
80+
$this->assertEquals('colref', $star['expr_type']);
81+
82+
$parser->parse('SELECT ROUND(3, 1), * FROM a_table');
83+
$star = $parser->parsed['SELECT'][1];
84+
$this->assertEquals('*', $star['base_expr']);
85+
$this->assertEquals('colref', $star['expr_type']);
86+
87+
$parser->parse('SELECT ROUND(3, 1), a_table.* FROM a_table');
88+
$star = $parser->parsed['SELECT'][1];
89+
$this->assertEquals('a_table.*', $star['base_expr']);
90+
$this->assertEquals('colref', $star['expr_type']);
91+
}
92+
}

0 commit comments

Comments
 (0)