Skip to content

Commit

Permalink
fix issue greenlion#294: missing comma in cast
Browse files Browse the repository at this point in the history
without this change, parsing and then re-creating a SQL query casting a
field as a decimal loses the comma in the decimal

for example `DECIMAL (16,2)` became `DECIMAL (16 2)`

added a `delim` in `ExpressionListProcessor`, when have a comma token,
as is done in `SelectProcessor#process`.

not sure if this is the right way to proceed,
and if it is it might need to be done in other places.
  • Loading branch information
nicoder committed Nov 6, 2018
1 parent c58ac6c commit 3a9d906
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/PHPSQLParser/builders/SelectBracketExpressionBuilder.php
Expand Up @@ -66,7 +66,13 @@ public function build(array $parsed) {
if ($parsed['expr_type'] !== ExpressionType::BRACKET_EXPRESSION) {
return "";
}
return "(" . $this->buildSubTree($parsed, " ") . ")" . $this->buildAlias($parsed);
return '('
. $this->buildSubTree(
$parsed,
!empty($parsed['delim']) ? $parsed['delim'] : ' '
)
. ')'
. $this->buildAlias($parsed);
}
}
?>
1 change: 1 addition & 0 deletions src/PHPSQLParser/processors/ExpressionListProcessor.php
Expand Up @@ -248,6 +248,7 @@ public function process($tokens) {
$tmpExprList = $curr->getSubTree();
$curr->setSubTree(array_merge($tmpExprList, $localExprList));
}
$curr->setDelim(',');

$tmpExprList = array();
$localExpr = new ExpressionToken();
Expand Down
8 changes: 8 additions & 0 deletions src/PHPSQLParser/utils/ExpressionToken.php
Expand Up @@ -15,6 +15,7 @@ class ExpressionToken {
private $trim;
private $upper;
private $noQuotes;
private $delim;

public function __construct($key = "", $token = "") {
$this->subTree = false;
Expand Down Expand Up @@ -64,6 +65,10 @@ public function setTokenType($type) {
$this->tokenType = $type;
}

public function setDelim($delim) {
$this->delim = $delim;
}

public function endsWith($needle) {
$length = strlen($needle);
if ($length == 0) {
Expand Down Expand Up @@ -155,6 +160,9 @@ public function toArray() {
$result['no_quotes'] = $this->noQuotes;
}
$result['sub_tree'] = $this->subTree;
if ($this->delim) {
$result['delim'] = $this->delim;
}
return $result;
}
}
Expand Down
2 changes: 2 additions & 0 deletions tests/cases/parser/issue51Test.php
Expand Up @@ -55,6 +55,8 @@ public function testIssue51() {
$expected = getExpectedValue(dirname(__FILE__), 'issue51.serialized');
$this->assertEquals($expected, $p, 'should not die if query contains cast expression');

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

2 changes: 1 addition & 1 deletion tests/expected/parser/issue51.serialized
@@ -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:4:{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:8:"position";i:26;}}s:5:"alias";b:0;s:8:"position";i:13;}}s:5:"delim";b:0;s:8:"position";i:7;}}}
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;}}}

0 comments on commit 3a9d906

Please sign in to comment.