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\*.
| PHP | Rust |
|---|---|
AST\Expression (interface) | Expression |
AST\Call\Func | Func |
AST\Grammar | Grammar |
AST\Expression\Aliased, Between, Binary, CaseWhen, Cast, Conditional, Exists, In, Subquery, Unary, Window | Aliased, Between, Binary, CaseWhen, Cast, Conditional, Exists, In (with InList), Subquery, Unary, Window |
AST\Literal, AST\Placeholder, AST\Raw, AST\Star | Literal, Placeholder, Raw, Star |
AST\Reference\Column, AST\Reference\Table | Column, Table |
AST\SubquerySource, Table|SubquerySource | SubquerySource, TableSource |
AST\JoinClause, AST\OrderByItem | JoinClause, OrderByItem |
AST\Definition\Cte, AST\Definition\Window | Cte, WindowDefinition |
AST\Specification\Window | WindowSpecification |
AST\Statement\Select (with(): struct update syntax) | Select |
AST\Parser | Parser |
AST\Serializer, Serializer\MySQL, MariaDB, ClickHouse, PostgreSQL, SQLite | Serializer::new, Serializer::mysql, Serializer::mariadb, Serializer::clickhouse, Serializer::postgresql, Serializer::sqlite |
AST\Walker | Walker |
AST\Visitor (interface) | Visitor |
AST\Visitor\ColumnValidator, FilterInjector, TableRenamer | ColumnValidator, 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` = 42Structs§
- Aliased
- An expression with an alias:
expression AS alias. - Between
expression [NOT] BETWEEN low AND high.- Binary
- A binary operation:
left operator right. - Case
When - One
WHEN condition THEN resultof aCASE. - Cast
CAST(expression AS type)(or PostgreSQL’sexpression::type).- Column
- A column reference:
[schema.][table.]name. - Column
Validator - 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).- Filter
Injector - Adds a condition to the
WHEREof 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
Serializerwrites verbatim, so a quoted identifier cannot carry SQL into them. - In
expression [NOT] IN (list).- Join
Clause - A join:
TYPE source [ON condition]. - Literal
- A literal: a string, an integer, a float, a boolean or
NULL. - Order
ByItem - One
ORDER BYitem:expression ASC|DESC [NULLS FIRST|LAST]. - Parser
- A recursive-descent parser of
SELECTstatements. - Placeholder
- A bound parameter placeholder, written as is (
?,:name,$1). - Raw
- SQL written as is.
- Select
- A
SELECTstatement. - 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.*orschema.table.*.- Subquery
- A scalar subquery:
(SELECT ...). - Subquery
Source - A subquery used as a table:
(SELECT ...) AS alias. - Table
- A table reference:
[schema.]name [AS alias]. - Table
Renamer - Renames tables: table references (name and alias) and the table
qualifier of columns and
table.*. - Unary
- A unary operation:
operator (operand)whenprefix, elseoperand operator(IS NULL,IS NOT NULL). - Walker
- Applies a
Visitorto 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 getsVisitor::visit_select. - Window
- A window function call:
function OVER nameorfunction OVER (specification). - Window
Definition - A named window:
WINDOW name AS (specification). - Window
Specification - 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
INtests against. - Table
Source - What a
FROMor a join reads: a table or a subquery.