greenlion / PHP-SQL-Parser

A pure PHP SQL (non validating) parser w/ focus on MySQL dialect of SQL
BSD 3-Clause "New" or "Revised" License
608 stars 155 forks source link

PHPSQLCreator unknown [expr_type] = in-list in "expression subtree" #225

Open kannan84 opened 7 years ago

kannan84 commented 7 years ago

Found an issue with PHPSQLCreator when using in-list expression in expression's subtree.

$sql="SELECT name,IF(player_type IN ('National','International'), 'Recognized', 'Unrecognized') AS 'Status' from players"; $parser = new PHPSQLParser($sql, true); print_r($parser->parsed); $creator = new PHPSQLCreator($parser->parsed); echo $creator->created;

While Parsing we got the following array for the second column,

Array ( [expr_type] => function [alias] => Array ( [as] => 1 [name] => 'Status' [base_expr] => AS 'Status' [no_quotes] => Array ( [delim] => [parts] => Array ( [0] => Status )

                            )

                        [position] => 89
                    )

                [base_expr] => IF
                [sub_tree] => Array
                    (
                        [0] => Array
                            (
                                [expr_type] => expression
                                [base_expr] => player_type IN ('National','International')
                                [sub_tree] => Array
                                    (
                                        [0] => Array
                                            (
                                                [expr_type] => colref
                                                [base_expr] => player_type
                                                [no_quotes] => Array
                                                    (
                                                        [delim] => 
                                                        [parts] => Array
                                                            (
                                                                [0] => player_type
                                                            )

                                                    )

                                                [sub_tree] => 
                                                [position] => 15
                                            )

                                        [1] => Array
                                            (
                                                [expr_type] => operator
                                                [base_expr] => IN
                                                [sub_tree] => 
                                                [position] => 27
                                            )

                                        [2] => Array
                                            (
                                                [expr_type] => in-list
                                                [base_expr] => ('National','International')
                                                [sub_tree] => Array
                                                    (
                                                        [0] => Array
                                                            (
                                                                [expr_type] => const
                                                                [base_expr] => 'National'
                                                                [sub_tree] => 
                                                                [position] => 31
                                                            )

                                                        [1] => Array
                                                            (
                                                                [expr_type] => const
                                                                [base_expr] => 'International'
                                                                [sub_tree] => 
                                                                [position] => 42
                                                            )

                                                    )

                                                [position] => 30
                                            )

                                    )

                                [alias] => 
                                [position] => 15
                            )

PHPSQLCreator throws the following exception while trying to build from this array element,

exception 'PHPSQLParser\exceptions\UnableToCreateSQLException' with message 'unknown [expr_type] = in-list in "expression subtree" [2] '

tomershay commented 7 years ago

I got a very similar error - did you follow up on this? Did you find a solution? Thank you!

soleromel commented 6 years ago

in SubTreeBuilder.php is missing the protected function buildInList() so add the following method in it

protected function buildInList($parsed) {
    $builder = new InListBuilder();
    return $builder->build($parsed);
}