Expand description
Queries as data, and everything that turns them into statements.
A Query is one filter, order, page, selection, aggregate, join or
union, as data: built in code (Query::equal(...)) or parsed from the
JSON clients send (Query::parse). Builders compile lists of queries
into statements for MySQL, MariaDB, PostgreSQL, SQLite, ClickHouse and
MongoDB (builder), with parameter bindings, joins, aggregates,
window functions, upserts and dialect features. Schema builders write
DDL (schema). A SQL tokenizer (tokenizer), parser and serializer
(ast) and a statement classifier (classifier) read SQL back.
Values are PHP values (Value, from php-std): queries and statements
are shared with PHP, so integers stay distinct from floats and arrays
keep their keys and order.
PHP: ports utopia-php/query.
| PHP | Rust |
|---|---|
Query | Query |
Method | Method |
Type, CursorDirection, OrderDirection, NullsPosition | Type, CursorDirection, OrderDirection, NullsPosition |
Compiler | Compiler |
QuotesIdentifiers | quote, quote_literal |
Builder\ParsedQuery | ParsedQuery |
Exception, Exception\ValidationException, Exception\UnsupportedException | Error::Query, Error::Validation, Error::Unsupported |
Builder\* | builder |
Hook\* | hook |
Schema\* | schema |
Tokenizer\* | tokenizer |
AST\* | ast |
Classifier\* | classifier |
use utopia_query::Query;
use utopia_query::builder::MySql;
let statement = MySql::new()
.from("users", "")
.select(["name", "email"])
.filter([Query::equal("status", ["active"]), Query::greater_than("age", 18)])
.limit(25)
.build()?;
assert_eq!(statement.query, "SELECT `name`, `email` FROM `users` WHERE `status` IN (?) AND `age` > ? LIMIT ?");use Utopia\Query\Builder\MySQL;
use Utopia\Query\Query;
$statement = (new MySQL())
->from('users')
->select(['name', 'email'])
->filter([Query::equal('status', ['active']), Query::greaterThan('age', 18)])
->limit(25)
->build();
echo $statement->query, "\n"; // SELECT `name`, `email` FROM `users` WHERE `status` IN (?) AND `age` > ? LIMIT ?Modules§
- ast
- The SQL syntax tree: a parser from tokens, serializers per dialect, a walker and visitors that rewrite trees.
- builder
- Query builders: compile
Querylists, and the builder’s own clauses, into statements for each dialect. - classifier
- Statement classifiers: whether a wire protocol message reads, writes or controls a transaction, so a proxy can route it to a replica or the primary.
- hook
- Hooks: code a builder runs while compiling, to scope every query
(
Filter,JoinFilter) or map attribute names to columns (Attribute).TenantandMapare the hooks the library ships. - schema
- Schema builders: DDL for tables, columns, indexes, views, procedures and triggers, per dialect.
- tokenizer
- SQL tokenizers: SQL text to
Tokens, per dialect.
Structs§
- Code
- An exception code: an integer, or a string PHP turns into one (a numeric
string is cast, anything else is
0). - Parsed
Query - The queries grouped by what they do, as builders compile them.
- Query
- One query: a
Method, the attribute it applies to, its values, and the alias a join or an aggregate is exposed under. - Time
Bucket - A
groupByTimeBucketquery: the attribute and the bucket size.
Enums§
- Cursor
Direction - Which side of a cursor row a page is on.
- Error
- Why a query could not be parsed, validated or compiled.
- Method
- What a
Querydoes: filter, order, paginate, select, aggregate, join, combine, or carry raw SQL. - Nulls
Position - Where
NULLs sort. - Object
- An object among a query’s values.
- Order
Direction - A sort direction.
- Query
OrJson - What
Query::fingerprintaccepts: a query, or its JSON. - String
Literal - The rules a dialect’s string literals follow.
- Type
- What kind of statement a classifier found.
Constants§
- DEFAULT_
ALIAS - The alias a builder gives its main table when none is set.
- GROUP_
BY_ TIME_ BUCKET_ INTERVALS - The bucket sizes
groupByTimeBucketaccepts.
Traits§
- Compiler
- Something that compiles queries: a builder.
- Into
Value - Something usable as one PHP value: integers, floats, booleans, strings,
arrays, queries,
Nonefornull. - Into
Values - Something usable as a PHP array of values: a Rust array, a
Vecor a slice ofIntoValues (a list), or anArray(keys kept).
Functions§
- list
- A list of values.
- quote
- Quotes a possibly qualified identifier with
wrap: each dot-separated segment is wrapped (the wrap character doubled inside), a final*stays bare, and*alone is returned as is. Control characters are refused. - quote_
literal - Quotes one identifier without treating dots as qualifiers (a column named
meta.key, as ClickHouse nested columns are). - string
- A string value.