I have already asked a question here PHP Sql expression to AST, but my problem is not resolved. I have tried using the project https://github.com/phpmyadmin/sql-parser to parse my SQL expressions, but I am not able to get an AST of them. What am I doing wrong?
<?php
require_once(__DIR__ . '/vendor/autoload.php');
$expression = " SUM ( order_part.amount * 0.8 ) AS `total` ";
echo $expression . "\n";
$parser = new \PhpMyAdmin\SqlParser\Parser($expression);
$parser->parse();
// ------------------------------------
$tokens = \array_values(
\array_filter(
$parser->list->tokens,
function (\PhpMyAdmin\SqlParser\Token $token): bool {
return $token->type !== \PhpMyAdmin\SqlParser\Token::TYPE_WHITESPACE;
}
)
);
$expression = \PhpMyAdmin\SqlParser\Components\Expression::parse(
$parser,
new \PhpMyAdmin\SqlParser\TokensList($tokens),
[
'breakOnAlias' => true,
]
);
echo \json_encode($expression) . "\n";
I am only getting this as an output:
{"database":null,"table":null,"column":null,"expr":"SUM(order_part.amount*0.8)","alias":null,"function":"SUM","subquery":null}
I see that "function":"SUM" and that is correct but I cannot go "deeper" into the expression, I dont see its parameters, sub-expressions, etc.
To be clear, I would like to get an AST of the expression.