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
601 stars 156 forks source link

Build User Varaiable Expression in FunctionBuilder #340

Closed Aramics closed 2 years ago

Aramics commented 3 years ago

Implement UserVariableBuilder in FunctionBuilder to build for custom variable.

This changes solve issues of subtree with user_variable expr_type when building parsed query.

Solve this error while building query. Message: unknown [expr_type] = user_variable in "function subtree

To reproduce:

$sql = 'select  demoid as id, parent_id as pid, name from    (select demoid, parent_id, d.name as name from tbldemos as d order by d.parent_id, d.demoid) demos_sorted, (select @pv := "0") initialisation where find_in_set(parent_id, @pv) and     length(@pv := concat(@pv, ",", demoid))';

$parser = new PHPSQLParser($sql);
$parsed  = $parser->parsed;

//building sql will fail without this commit.
$creator = new PHPSQLCreator($parsed);
$newSql = $creator->created;