Skip to main content

Module ast

Module ast 

Source
Expand description

The SQL syntax tree: a parser from tokens, serializers per dialect, a walker and visitors that rewrite trees.

Parser reads the tokens of a Tokenizer into a Select; Walker applies a Visitor to every node (validating columns, injecting a filter, renaming tables); Serializer writes the tree back as SQL. Nodes are plain values with public fields: Expression is the enum of every expression node.

PHP: Utopia\Query\AST\*.

PHPRust
AST\Expression (interface)Expression
AST\Call\FuncFunc
AST\GrammarGrammar
AST\Expression\Aliased, Between, Binary, CaseWhen, Cast, Conditional, Exists, In, Subquery, Unary, WindowAliased, Between, Binary, CaseWhen, Cast, Conditional, Exists, In (with InList), Subquery, Unary, Window
AST\Literal, AST\Placeholder, AST\Raw, AST\StarLiteral, Placeholder, Raw, Star
AST\Reference\Column, AST\Reference\TableColumn, Table
AST\SubquerySource, Table|SubquerySourceSubquerySource, TableSource
AST\JoinClause, AST\OrderByItemJoinClause, OrderByItem
AST\Definition\Cte, AST\Definition\WindowCte, WindowDefinition
AST\Specification\WindowWindowSpecification
AST\Statement\Select (with(): struct update syntax)Select
AST\ParserParser
AST\Serializer, Serializer\MySQL, MariaDB, ClickHouse, PostgreSQL, SQLiteSerializer::new, Serializer::mysql, Serializer::mariadb, Serializer::clickhouse, Serializer::postgresql, Serializer::sqlite
AST\WalkerWalker
AST\Visitor (interface)Visitor
AST\Visitor\ColumnValidator, FilterInjector, TableRenamerColumnValidator, FilterInjector, TableRenamer
use utopia_query::ast::{Binary, Column, FilterInjector, Literal, Parser, Serializer, Visitor};

let select = Parser::new().parse_sql("SELECT * FROM orders WHERE total > 100")?;
let mut tenant = FilterInjector::new(Binary::new(Column::new("tenant_id"), "=", Literal::new(42)));
let select = tenant.visit_select(select)?;
assert_eq!(
    Serializer::new().serialize(&select)?,
    "SELECT * FROM `orders` WHERE `total` > 100 AND `tenant_id` = 42"
);
use Utopia\Query\AST\Expression\Binary;
use Utopia\Query\AST\Literal;
use Utopia\Query\AST\Parser;
use Utopia\Query\AST\Reference\Column;
use Utopia\Query\AST\Serializer;
use Utopia\Query\AST\Visitor\FilterInjector;
use Utopia\Query\Tokenizer\Tokenizer;

$select = (new Parser())->parse(Tokenizer::filter((new Tokenizer())->tokenize('SELECT * FROM orders WHERE total > 100')));
$select = (new FilterInjector(new Binary(new Column('tenant_id'), '=', new Literal(42))))->visitSelect($select);
echo (new Serializer())->serialize($select), "\n"; // SELECT * FROM `orders` WHERE `total` > 100 AND `tenant_id` = 42

Structs§

Aliased
An expression with an alias: expression AS alias.
Between
expression [NOT] BETWEEN low AND high.
Binary
A binary operation: left operator right.
CaseWhen
One WHEN condition THEN result of a CASE.
Cast
CAST(expression AS type) (or PostgreSQL’s expression::type).
Column
A column reference: [schema.][table.]name.
ColumnValidator
Rejects columns outside an allow-list, and * unless allowed.
Conditional
CASE [operand] WHEN ... THEN ... [ELSE ...] END.
Cte
A common table expression: name [(columns)] AS (query).
Exists
[NOT] EXISTS (subquery).
FilterInjector
Adds a condition to the WHERE of statements (AND-ed with an existing one).
Func
A function call: NAME([DISTINCT] args) [FILTER (WHERE ...)].
Grammar
The lexical rules for function names and cast types, which the Serializer writes verbatim, so a quoted identifier cannot carry SQL into them.
In
expression [NOT] IN (list).
JoinClause
A join: TYPE source [ON condition].
Literal
A literal: a string, an integer, a float, a boolean or NULL.
OrderByItem
One ORDER BY item: expression ASC|DESC [NULLS FIRST|LAST].
Parser
A recursive-descent parser of SELECT statements.
Placeholder
A bound parameter placeholder, written as is (?, :name, $1).
Raw
SQL written as is.
Select
A SELECT statement.
Serializer
Writes a syntax tree as SQL, quoting identifiers the way one dialect does: backticks (MySQL, MariaDB, ClickHouse, and the base serializer) or double quotes (PostgreSQL, SQLite), with the quote doubled inside.
Star
*, table.* or schema.table.*.
Subquery
A scalar subquery: (SELECT ...).
SubquerySource
A subquery used as a table: (SELECT ...) AS alias.
Table
A table reference: [schema.]name [AS alias].
TableRenamer
Renames tables: table references (name and alias) and the table qualifier of columns and table.*.
Unary
A unary operation: operator (operand) when prefix, else operand operator (IS NULL, IS NOT NULL).
Walker
Applies a Visitor to every node of a statement, bottom-up: the children of a node first, then the node. Statements nested in subqueries, IN, EXISTS, derived tables and CTEs are walked the same way, and each gets Visitor::visit_select.
Window
A window function call: function OVER name or function OVER (specification).
WindowDefinition
A named window: WINDOW name AS (specification).
WindowSpecification
A window: [PARTITION BY ...] [ORDER BY ...] [ROWS|RANGE frame].

Enums§

Expression
An expression: a column, a literal, an operation, a function call, a subquery.
InList
What an IN tests against.
TableSource
What a FROM or a join reads: a table or a subquery.

Traits§

Visitor
Inspects or rewrites the nodes a Walker visits: each method gets a node (its children already walked) and returns it, or its replacement. The defaults keep every node.