simha95 / php-sql-parser

Automatically exported from code.google.com/p/php-sql-parser
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

PHPSQLParser incorrectly parses * as colref instead of operator inside certain expressions. #82

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
INCORRECT
$parser = new PHPSQLParser;
$parsed = $parser->parse('SELECT SUM(1) * 100 as number');
//The * results in 
        array(3) {
          ["expr_type"]=>
          string(6) "colref"
          ["base_expr"]=>
          string(1) "*"
          ["sub_tree"]=>
          bool(false)
        }

CORRECT
$parser = new PHPSQLParser;
$parsed = $parser->parse('SELECT 100 * SUM(1)  as number');
//The * results in: 
        array(3) {
          ["expr_type"]=>
          string(8) "operator"
          ["base_expr"]=>
          string(1) "*"
          ["sub_tree"]=>
          bool(false)
        }

For some reason switching the order makes a difference.

using r346.

Original issue reported on code.google.com by cag...@gmail.com on 12 Dec 2012 at 12:29

GoogleCodeExporter commented 8 years ago
Any time * is the first item in an expression it should be treated as a colref, 
otherwise it should be treated as an operator.  This will make select * and 
select count(*) treat * as a colref, but select a * b will treat it as an 
operator.

Original comment by greenlion@gmail.com on 13 Dec 2012 at 8:34

GoogleCodeExporter commented 8 years ago
Had a similar issue - in 
https://code.google.com/p/php-sql-parser/source/detail?r=346, 
php-sql-parser.php, line 1286 (case '*'...) I modified

if (!$prev->isColumnReference() && !$prev->isConstant() && 
!$prev->isExpression()
                                && !$prev->isBracketExpression()) {
to

if (!$prev->isColumnReference() && !$prev->isConstant() && 
!$prev->isExpression()
                                && !$prev->isBracketExpression() && !$prev->isFunction() && !$prev->isAggregateFunction()) {

Original comment by chrisw...@gmx.de on 21 May 2013 at 3:03

GoogleCodeExporter commented 8 years ago
Chris, I have added your code to the repository. Thanks a lot, good job.

Original comment by pho...@gmx.de on 23 Oct 2013 at 10:31