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

SQL creator fails to parse simple query #92

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Here's a simple example:

    $parser = new PHPSQLParser();
    $creator = new PHPSQLCreator();
    $in = "SELECT cid*2 FROM cities ORDER BY country";
    $ast = $parser->parse($in);
    print_r($ast);
    $out = @$creator->create($ast);
    echo $out;

This gives back:

    Array
    (
        [SELECT] => Array
            (
                [0] => Array
                    (
                        [expr_type] => expression
                        [alias] => 
                        [base_expr] => cid*2
                        [sub_tree] => Array
                            (
                                [0] => Array
                                    (
                                        [expr_type] => colref
                                        [base_expr] => cid
                                        [sub_tree] => 
                                    )

                                [1] => Array
                                    (
                                        [expr_type] => operator
                                        [base_expr] => *
                                        [sub_tree] => 
                                    )

                                [2] => Array
                                    (
                                        [expr_type] => const
                                        [base_expr] => 2
                                        [sub_tree] => 
                                    )

                            )

                    )

            )

        [FROM] => Array
            (
                [0] => Array
                    (
                        [expr_type] => table
                        [table] => cities
                        [alias] => 
                        [join_type] => JOIN
                        [ref_type] => 
                        [ref_clause] => 
                        [base_expr] => cities
                        [sub_tree] => 
                    )

            )

        [ORDER] => Array
            (
                [0] => Array
                    (
                        [expr_type] => colref
                        [base_expr] => country
                        [sub_tree] => 
                        [direction] => ASC
                    )

            )

    )

    Fatal error:  Uncaught exception 'UnableToCreateSQLException' with message 'unknown expr_type in expression subtree[0] colref' in ...\PHP-SQL-Parser\php-sql-creator.php:452
    Stack trace:
    #0 ...\PHP-SQL-Parser\php-sql-creator.php(424): PHPSQLCreator->processSubTree(Array, ' ')
    #1 ...\PHP-SQL-Parser\php-sql-creator.php(119): PHPSQLCreator->processSelectExpression(Array)
    #2 ...\PHP-SQL-Parser\php-sql-creator.php(72): PHPSQLCreator->processSELECT(Array)
    #3 ...\PHP-SQL-Parser\php-sql-creator.php(53): PHPSQLCreator->processSelectStatement(Array)
    #4 ...\controllers\MainController.php(69): PHPSQLCreator->create(Array)
    #5 [internal function]: MainController->sortColumn()
    #6 ...\public\router.php(51): call_user_func(Array)
    #7 {main}
      thrown in <b>...\PHP-SQL-Parser\php-sql-creator.php</b> on line <b>452</b><br />

i.e., it can parse the SQL but it can't take the tree it generated exactly 
as-is and turn it back into SQL.

I was hoping to manipulate the ORDER statement a little before converting it 
back to SQL, but it won't even take its own output.

Original issue reported on code.google.com by mnbaya...@gmail.com on 25 Apr 2013 at 2:37

GoogleCodeExporter commented 8 years ago
The creator could not handle colref expression types. I have added a call to 
the processColRef() method.

Original comment by pho...@gmx.de on 23 Oct 2013 at 8:13