rickywu-posh / 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

DISTINCT causes error in PHPSQLCreator #110

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
The DISTINCT keyword doesn't appear to be handled in the PHPSQLCreator. An 
exception is thrown instead.

This code reproduces the problem:
$parser = new PHPSQLParser();
$creator = new PHPSQLCreator();

$tree = $parser->parse("SELECT DISTINCT a FROM b");
print($creator->create($tree));

Stack trace:
PHP Fatal error:  Uncaught exception 'UnableToCreateSQLException' with message 
'unknown expr_type in SELECT[0] reserved' in 
/home/george/Projects/svn/php-sql-parser-read-only/src/builders/SelectBuilder.ph
p:94
Stack trace:
#0 
/home/george/Projects/svn/php-sql-parser-read-only/src/builders/SelectStatementB
uilder.php(61): SelectBuilder->build(Array)
#1 
/home/george/Projects/svn/php-sql-parser-read-only/src/builders/SelectStatementB
uilder.php(90): SelectStatementBuilder->buildSELECT(Array)
#2 
/home/george/Projects/svn/php-sql-parser-read-only/src/PHPSQLCreator.php(75): 
SelectStatementBuilder->build(Array)
#3 /home/george/Projects/svn/php-sql-parser-read-only/test.php(13): 
PHPSQLCreator->create(Array)
#4 {main}
  thrown in /home/george/Projects/svn/php-sql-parser-read-only/src/builders/SelectBuilder.php on line 94

This was run against the latest in SVN, but the error happens with the latest 
download as well (php-sql-parser-20131130.zip)

Would it be sufficient to do something like this?

function processReserved($parsed) {
            if ($parsed['expr_type'] !== ExpressionType::RESERVED) {
                return "";
            }
            $sql = $parsed['base_expr'];
            return $sql;
}

Original issue reported on code.google.com by noisecap...@gmail.com on 3 Jan 2014 at 8:52

GoogleCodeExporter commented 8 years ago
I have changed some things for SELECT options like DISTINCT within the parser. 
The r897 handles the reserved keywords within the select clause, but there is a 
comma behind the DISTINCT within the generated SQL statement. The parser must 
be enhanced to create a new output property, which holds the concatenation 
character.

Original comment by pho...@gmx.de on 6 Jan 2014 at 8:28

GoogleCodeExporter commented 8 years ago
Fixed in r901.

Original comment by pho...@gmx.de on 6 Jan 2014 at 9:47

GoogleCodeExporter commented 8 years ago
Thanks for your quick response!

Original comment by noisecap...@gmail.com on 6 Jan 2014 at 4:04