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

Parser breaks on nexted subqueries ending in )) #25

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
If I try to parse a complex query like this:

SELECT * FROM contacts WHERE contacts.id IN (SELECT email_addr_bean_rel.bean_id 
FROM email_addr_bean_rel, email_addresses WHERE email_addresses.id = 
email_addr_bean_rel.email_address_id AND email_addr_bean_rel.deleted = 0 AND 
email_addr_bean_rel.bean_module = 'Contacts' AND email_addresses.email_address 
IN ("test@example.com"))

then the query parser does not parse the internal subquery properly. The reason 
for that is that trim functions which are used on encompassing subquery do 
trim($sql, " ()") and thus when trimming they remove both enclosing parentheses 
for the subquery and the ones that belong to inner IN, which they should not be 
doing. I would propose to use something like this:

private function trimSubquery($sq)
    {
        $sq = trim($sq);
        if(empty($sq)) return '';
            while($sq[0] == '(' && substr($sq, -1) == ')') {
                $sq = substr($sq, 1, -1);
                $sq = trim($sq);
            }
            return $sq;
    }

to keep trims balanced and use this functions instead of regular trim with ' 
()'. 

Original issue reported on code.google.com by smalys...@gmail.com on 15 Feb 2012 at 1:13

GoogleCodeExporter commented 8 years ago
Have you tried the latest version on 
https://www.phosco.info/publicsvn/php-sql-parser/
?

Original comment by pho...@gmx.de on 15 Feb 2012 at 9:11

GoogleCodeExporter commented 8 years ago
No, didn't try it yet. Could be useful to update this repo or publish the link 
to the current version here - I had no idea the version available is not the 
latest one.

Original comment by smalys...@gmail.com on 15 Feb 2012 at 5:44

GoogleCodeExporter commented 8 years ago
I have contact to the developer, but at the moment I have no write access to 
his repository. I'm waiting for an answer, but he reads his mails not really 
often. Please write further problems here on this page, so all users can see, 
what happens.

Thanks for using the parser.

Original comment by pho...@gmx.de on 15 Feb 2012 at 5:50

GoogleCodeExporter commented 8 years ago
I have added a working test case to the current version on Google.Code.

Original comment by pho...@gmx.de on 13 Mar 2012 at 12:14