vikramkakar / 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 produces errors on query parse #31

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
Parse the following query:
SELECT  sp.level,
        CASE sp.level
            WHEN 'bronze' THEN 0
            WHEN 'silver' THEN 1
            WHEN 'gold' THEN 2
            ELSE -1
        END AS levelnum,
        sp.alt_en,
        sp.alt_pl,
        DATE_FORMAT(sp.vu_start,'%Y-%m-%d %T') AS vu_start,
        DATE_FORMAT(sp.vu_stop,'%Y-%m-%d %T') AS vu_stop,
        ABS(TO_DAYS(now()) - TO_DAYS(sp.vu_start)) AS frdays,
        ABS(TO_DAYS(now()) - TO_DAYS(sp.vu_stop)) AS todays,
        IF(ISNULL(TO_DAYS(sp.vu_start)) OR ISNULL(TO_DAYS(sp.vu_stop))
            , 1
            , IF(TO_DAYS(now()) < TO_DAYS(sp.vu_start)
                , TO_DAYS(now()) - TO_DAYS(sp.vu_start)
                , IF(TO_DAYS(now()) > TO_DAYS(sp.vu_stop)
                    , TO_DAYS(now()) - TO_DAYS(sp.vu_stop)
                    , 0))) AS status,
        st.id,
        SUM(IF(st.type='view',1,0)) AS view,
        SUM(IF(st.type='click',1,0)) AS click
FROM    stats AS st,
        sponsor AS sp
WHERE   st.id=sp.id
GROUP BY st.id
ORDER BY sp.alt_en asc, sp.alt_pl asc

What is the expected output? What do you see instead?
Query should parse... (it runs on MySQL 5.x)
I get this:
Notice: Uninitialized string offset: 0 in 
/media/sf_swjozef2/vendor/sqlparser/php-sql-parser.php on line 677 Notice: 
Undefined variable: processed in 
/media/sf_swjozef2/vendor/sqlparser/php-sql-parser.php on line 1241 Notice: 
Undefined variable: processed in 
/media/sf_swjozef2/vendor/sqlparser/php-sql-parser.php on line 1243 Notice: 
Undefined variable: expr_type in 
/media/sf_swjozef2/vendor/sqlparser/php-sql-parser.php on line 1247 Warning: 
array_pop() expects parameter 1 to be array, string given in 
/media/sf_swjozef2/vendor/sqlparser/php-sql-parser.php on line 717 Notice: 
Uninitialized string offset: 0 in 
/media/sf_swjozef2/vendor/sqlparser/php-sql-parser.php on line 744 Notice: 
Undefined variable: processed in 
/media/sf_swjozef2/vendor/sqlparser/php-sql-parser.php on line 1241 Notice: 
Undefined variable: processed in 
/media/sf_swjozef2/vendor/sqlparser/php-sql-parser.php on line 1243 Notice: 
Undefined variable: expr_type in 
/media/sf_swjozef2/vendor/sqlparser/php-sql-parser.php on line 1247 Notice: 
Uninitialized string offset: 0 in 
/media/sf_swjozef2/vendor/sqlparser/php-sql-parser.php on line 762 Fatal error: 
Cannot use string offset as an array in 
/media/sf_swjozef2/vendor/sqlparser/php-sql-parser.php on line 762

What version of the product are you using? On what operating system?
Downloaded on 2/29/2012, running on ubuntu Linux 64-bit, PHP 5.3.5

Please provide any additional information below.

Original issue reported on code.google.com by boa...@gmail.com on 1 Mar 2012 at 10:04

GoogleCodeExporter commented 8 years ago
Try current version on

https://www.phosco.info/publicsvn/php-sql-parser/tags/20120301

This version doesn't generate an error, but the output is not valid, because of 
a keyword "view", which you use as alias. I will look into the code as soon as 
possible.

Original comment by pho...@gmx.de on 1 Mar 2012 at 10:22

GoogleCodeExporter commented 8 years ago
Thanks for the quick response! This version looks to have quite a few changes 
from the one I had before...
As you said, the new one does not error out, but it still produces a notice:
PHP Notice:  Uninitialized string offset: 0 in 
/media/sf_test/php-sql-parser.php on line 879
When I change the view/click aliases to views/clicks, no notice.

Original comment by boa...@gmail.com on 1 Mar 2012 at 10:52

GoogleCodeExporter commented 8 years ago
Oh, this is because the alias "view" is not available as alias (it is 
recognized as keyword). It is an aftereffect.

Original comment by pho...@gmx.de on 4 Mar 2012 at 8:05

GoogleCodeExporter commented 8 years ago
I have fixed that specific problem. The parser will check now the previous 
tokens, if the token VIEW occurs. There must be ALTER, CREATE or DROP just 
before VIEW. If you use other keywords, some other problems will occur. The 
only way to solve that more generic, is to develop a validating parser, which 
uses a grammar. This parser uses a brute force method to find keywords and 
split the statement, so every "wrong used" keyword breaks the parser. Check out 
the bleeding edge on

https://www.phosco.info/publicsvn/php-sql-parser/trunk

Original comment by pho...@gmx.de on 4 Mar 2012 at 8:40

GoogleCodeExporter commented 8 years ago
There is a test case for this issue (see current version on Google.Code).

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