sulheru / 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

Partial Query Parsing #55

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I've noticed that the parser is "almost" able to properly deal with partial 
queries. This is actually a very useful, even if unintended feature.

Examples that do work:

$parser = new PHPSQLParser("WHERE a=5 AND b=5 and (c LIKE '%something%' ");
print_r($parser->parsed);

$parser = new PHPSQLParser("LIMIT 5, 10");
print_r($parser->parsed);

$parser = new PHPSQLParser("FROM `schema`.`table` as `alias` JOIN `a`.`b` as 
`c` LEFT JOIN p");
print_r($parser->parsed);

There are only 2 cases where it glitches at the moment: Line 883 and line 886 
in the parser where processing of group and order is dependent on select part 
being present.

From what I can see in the code, it tires to find if the expression refers to 
an alias defined in select. 

Personally, I'm not sure if this is useful/required for anything. In my opinion 
it's almost a form of validation, which I think is beyond parser's scope.

So, I would like the parser to support partial queries (as a feature) and I 
would like to be able to execute these 2: 
$parser = new PHPSQLParser("GROUP BY a, b, table.c");
print_r($parser->parsed);

$parser = new PHPSQLParser("ORDER BY a ASC, b DESC, table.c ASC");
print_r($parser->parsed);

Examples above spit a number of warnings.

PS. 
Thank you for this tool, it's really great piece of software that opens many 
possibilities for me. PHP was i need of a good SQL parser for a long time.

-------

What version of the product are you using? On what operating system?

php 5.3.10, ubuntu linux 12.04

Please provide any additional information below.

Original issue reported on code.google.com by akay...@gmail.com on 7 May 2012 at 6:44

GoogleCodeExporter commented 9 years ago
We need the SELECT part to set 'alias' instead of 'expression', if the 
order/group clause contains known aliases (which are part of the SELECT 
clause). To solve your problem, I have set an empty array for the select 
clause, so the parser runs correctly and returns a result. The REV 268 (trunk) 
has a test case for your issue.

He, your "PS" makes my day :-) I hope, greenlion reads it too.

Original comment by pho...@gmx.de on 8 May 2012 at 12:34