Skip to content

Commit a4ad4ea

Browse files
nicodergreenlion
authored andcommitted
fix issue #299: arithmetic operations in brackets are comma separated (#306)
for example the query: ```sql SELECT (1 + 2) AS THREE, (1 + 3) AS FOUR ``` would become: ```sql SELECT (1,+,2) AS THREE, (1 + 3) AS FOUR ``` This is a regression caused by commit 3a9d906, that meant to add missing commas in expressions like `CAST(... AS DECIMAL(16, 2))`, but added too many commas visibly. => revert the parts of that commit that changed the code (leave the added test assertion) => change the `ExpressionListProcessor` code that handles unspecified nodes. That code was added in commit 63d59d7, apparently to solve issue 51: https://code.google.com/archive/p/php-sql-parser/issues/51 It seems to have copied and pasted the code that handles function arguments, just above. But that code does not keep commas, and while the `FunctionBuilder` adds them back, the `SelectBracketExpressionBuilder` does not. The issue51Test test seems to cover the bug described in issue 51, and it still passes. So maybe we do not need the same handling as for function arguments in unspecified nodes? Hopefully someone with a better understanding of this code can have a look.
1 parent c97f346 commit a4ad4ea

6 files changed

Lines changed: 62 additions & 55 deletions

File tree

src/PHPSQLParser/builders/SelectBracketExpressionBuilder.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,7 @@ public function build(array $parsed) {
6666
if ($parsed['expr_type'] !== ExpressionType::BRACKET_EXPRESSION) {
6767
return "";
6868
}
69-
return '('
70-
. $this->buildSubTree(
71-
$parsed,
72-
!empty($parsed['delim']) ? $parsed['delim'] : ' '
73-
)
74-
. ')'
69+
return '(' . $this->buildSubTree($parsed, ' ') . ')'
7570
. $this->buildAlias($parsed);
7671
}
7772
}

src/PHPSQLParser/processors/ExpressionListProcessor.php

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -217,45 +217,7 @@ public function process($tokens) {
217217

218218
// we have parenthesis, but it seems to be an expression
219219
if ($curr->isUnspecified()) {
220-
221-
$localExpr = new ExpressionToken();
222-
$tmpExprList = array();
223-
224-
foreach ($localTokenList as $k => $v) {
225-
$tmpToken = new ExpressionToken($k, $v);
226-
if (!$tmpToken->isCommaToken()) {
227-
$localExpr->addToken($v);
228-
$tmpExprList[] = $v;
229-
} else {
230-
// an expression could have multiple parts split by operands
231-
// if we have a comma, it is a split-point for expressions
232-
$tmpExprList = array_values($tmpExprList);
233-
$localExprList = $this->process($tmpExprList);
234-
235-
if (count($localExprList) > 1) {
236-
$localExpr->setSubTree($localExprList);
237-
$localExpr->setTokenType(ExpressionType::EXPRESSION);
238-
$localExprList = $localExpr->toArray();
239-
$localExprList['alias'] = false;
240-
$localExprList = array($localExprList);
241-
}
242-
243-
if (!$curr->getSubTree()) {
244-
if (!empty($localExprList)) {
245-
$curr->setSubTree($localExprList);
246-
}
247-
} else {
248-
$tmpExprList = $curr->getSubTree();
249-
$curr->setSubTree(array_merge($tmpExprList, $localExprList));
250-
}
251-
$curr->setDelim(',');
252-
253-
$tmpExprList = array();
254-
$localExpr = new ExpressionToken();
255-
}
256-
}
257-
258-
$tmpExprList = array_values($tmpExprList);
220+
$tmpExprList = array_values($localTokenList);
259221
$localExprList = $this->process($tmpExprList);
260222

261223
$curr->setTokenType(ExpressionType::BRACKET_EXPRESSION);

src/PHPSQLParser/utils/ExpressionToken.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ class ExpressionToken {
1515
private $trim;
1616
private $upper;
1717
private $noQuotes;
18-
private $delim;
1918

2019
public function __construct($key = "", $token = "") {
2120
$this->subTree = false;
@@ -65,10 +64,6 @@ public function setTokenType($type) {
6564
$this->tokenType = $type;
6665
}
6766

68-
public function setDelim($delim) {
69-
$this->delim = $delim;
70-
}
71-
7267
public function endsWith($needle) {
7368
$length = strlen($needle);
7469
if ($length == 0) {
@@ -160,9 +155,6 @@ public function toArray() {
160155
$result['no_quotes'] = $this->noQuotes;
161156
}
162157
$result['sub_tree'] = $this->subTree;
163-
if ($this->delim) {
164-
$result['delim'] = $this->delim;
165-
}
166158
return $result;
167159
}
168160
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* issue299.php
4+
*
5+
* Test case for PHPSQLParser.
6+
*
7+
* PHP version 5
8+
*
9+
* LICENSE:
10+
* Copyright (c) 2010-2014 Justin Swanhart and André Rothe
11+
* All rights reserved.
12+
*
13+
* Redistribution and use in source and binary forms, with or without
14+
* modification, are permitted provided that the following conditions
15+
* are met:
16+
* 1. Redistributions of source code must retain the above copyright
17+
* notice, this list of conditions and the following disclaimer.
18+
* 2. Redistributions in binary form must reproduce the above copyright
19+
* notice, this list of conditions and the following disclaimer in the
20+
* documentation and/or other materials provided with the distribution.
21+
* 3. The name of the author may not be used to endorse or promote products
22+
* derived from this software without specific prior written permission.
23+
*
24+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25+
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26+
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27+
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29+
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33+
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34+
*
35+
* @author André Rothe <andre.rothe@phosco.info>
36+
* @copyright 2010-2014 Justin Swanhart and André Rothe
37+
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
38+
* @version SVN: $Id$
39+
*
40+
*/
41+
namespace PHPSQLParser\Test\Parser;
42+
use PHPSQLParser\PHPSQLParser;
43+
use PHPSQLParser\PHPSQLCreator;
44+
45+
class issue299Test extends \PHPUnit_Framework_TestCase
46+
{
47+
public function testIssue299()
48+
{
49+
$parser = new PHPSQLParser();
50+
$sql = 'SELECT (1 + 2) AS THREE, (1 + 3) AS FOUR';
51+
$parser->parse($sql, true);
52+
$creator = new PHPSQLCreator($parser->parsed);
53+
$this->assertEquals(
54+
'SELECT (1 + 2) AS THREE, (1 + 3) AS FOUR',
55+
$creator->created
56+
);
57+
}
58+
}

tests/cases/parser/issue51Test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function testIssue51() {
5656
$this->assertEquals($expected, $p, 'should not die if query contains cast expression');
5757

5858
$creator = new PHPSQLCreator($p);
59-
$this->assertEquals('SELECT CAST(12 AS decimal (9,3))', $creator->created);
59+
$this->assertEquals('SELECT CAST(12 AS decimal (9 , 3))', $creator->created);
6060
}
6161
}
6262

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
a:1:{s:6:"SELECT";a:1:{i:0;a:6:{s:9:"expr_type";s:8:"function";s:5:"alias";b:0;s:9:"base_expr";s:4:"CAST";s:8:"sub_tree";a:1:{i:0;a:5:{s:9:"expr_type";s:10:"expression";s:9:"base_expr";s:21:"12 AS decimal( 9, 3 )";s:8:"sub_tree";a:4:{i:0;a:4:{s:9:"expr_type";s:5:"const";s:9:"base_expr";s:2:"12";s:8:"sub_tree";b:0;s:8:"position";i:13;}i:1;a:4:{s:9:"expr_type";s:8:"reserved";s:9:"base_expr";s:2:"AS";s:8:"sub_tree";b:0;s:8:"position";i:16;}i:2;a:4:{s:9:"expr_type";s:8:"reserved";s:9:"base_expr";s:7:"decimal";s:8:"sub_tree";b:0;s:8:"position";i:19;}i:3;a:5:{s:9:"expr_type";s:18:"bracket_expression";s:9:"base_expr";s:8:"( 9, 3 )";s:8:"sub_tree";a:2:{i:0;a:4:{s:9:"expr_type";s:5:"const";s:9:"base_expr";s:1:"9";s:8:"sub_tree";b:0;s:8:"position";i:28;}i:1;a:4:{s:9:"expr_type";s:5:"const";s:9:"base_expr";s:1:"3";s:8:"sub_tree";b:0;s:8:"position";i:31;}}s:5:"delim";s:1:",";s:8:"position";i:26;}}s:5:"alias";b:0;s:8:"position";i:13;}}s:5:"delim";b:0;s:8:"position";i:7;}}}
1+
a:1:{s:6:"SELECT";a:1:{i:0;a:6:{s:9:"expr_type";s:8:"function";s:5:"alias";b:0;s:9:"base_expr";s:4:"CAST";s:8:"sub_tree";a:1:{i:0;a:5:{s:9:"expr_type";s:10:"expression";s:9:"base_expr";s:21:"12 AS decimal( 9, 3 )";s:8:"sub_tree";a:4:{i:0;a:4:{s:9:"expr_type";s:5:"const";s:9:"base_expr";s:2:"12";s:8:"sub_tree";b:0;s:8:"position";i:13;}i:1;a:4:{s:9:"expr_type";s:8:"reserved";s:9:"base_expr";s:2:"AS";s:8:"sub_tree";b:0;s:8:"position";i:16;}i:2;a:4:{s:9:"expr_type";s:8:"reserved";s:9:"base_expr";s:7:"decimal";s:8:"sub_tree";b:0;s:8:"position";i:19;}i:3;a:4:{s:9:"expr_type";s:18:"bracket_expression";s:9:"base_expr";s:8:"( 9, 3 )";s:8:"sub_tree";a:3:{i:0;a:4:{s:9:"expr_type";s:5:"const";s:9:"base_expr";s:1:"9";s:8:"sub_tree";b:0;s:8:"position";i:28;}i:1;a:5:{s:9:"expr_type";s:6:"colref";s:9:"base_expr";s:1:",";s:9:"no_quotes";a:2:{s:5:"delim";b:0;s:5:"parts";a:1:{i:0;s:1:",";}}s:8:"sub_tree";b:0;s:8:"position";i:29;}i:2;a:4:{s:9:"expr_type";s:5:"const";s:9:"base_expr";s:1:"3";s:8:"sub_tree";b:0;s:8:"position";i:31;}}s:8:"position";i:26;}}s:5:"alias";b:0;s:8:"position";i:13;}}s:5:"delim";b:0;s:8:"position";i:7;}}}

0 commit comments

Comments
 (0)