greenlion/PHP-SQL-Parser

Error during building parsed sub subquery

Closed this issue · 3 comments

I am trying to parse this simplified piece of sql query.
SELECT (SELECT SUM(1) FROM table1 WHERE ((SELECT SUM(2) FROM table2))) FROM table2
Parsing is successful, but during buiding sql from parsed array it shows this ErrorException:

unknown [expr_type] = subquery in "WHERE expression subtree" [0]

It seems it cannot be parsed because of double brackets in WHERE clause. Can it be fixed, please? Thank you.

Exception is on the line:

\src\PHPSQLParser\builders\WhereBracketExpressionBuilder.php:114

Same here, it's failing because of subquery:
...LEFT JOIN pd_table AS d ON d.id = (SELECT MAX(id) FROM pd_table WHERE supplierId = c.id AND type = 5)...

Uncaught exception (PHPSQLParser\exceptions\UnableToCreateSQLException): unknown [expr_type] = subquery in "expression ref_clause" [2]
in file /data/project/vendor/greenlion/php-sql-parser/src/PHPSQLParser/builders/RefClauseBuilder.php:45

Same here, it's failing because of subquery:
...LEFT JOIN pd_table AS d ON d.id = (SELECT MAX(id) FROM pd_table WHERE supplierId = c.id AND type = 5)...

Uncaught exception (PHPSQLParser\exceptions\UnableToCreateSQLException): unknown [expr_type] = subquery in "expression ref_clause" [2]
in file /data/project/vendor/greenlion/php-sql-parser/src/PHPSQLParser/builders/RefClauseBuilder.php:45

I am not sure how I fixed it temporarily any more, but I think when I modified \PHPSQLParser\builders\WhereBracketExpressionBuilder.php => buildWhereExpression method like this:

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

it may work, give a feedback if you try to resolve this. Thank you.

you can fix this bug by just adding in the WhereBracketExpressionBuilder.php :
protected function buildSubQuery($parsed) {
$builder = new SubQueryBuilder();
return $builder->build($parsed);
}

and in the build function add :
$sql .= $this->buildSubQuery($v);