Skip to main content

Crate utopia_query

Crate utopia_query 

Source
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.

PHPRust
QueryQuery
MethodMethod
Type, CursorDirection, OrderDirection, NullsPositionType, CursorDirection, OrderDirection, NullsPosition
CompilerCompiler
QuotesIdentifiersquote, quote_literal
Builder\ParsedQueryParsedQuery
Exception, Exception\ValidationException, Exception\UnsupportedExceptionError::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 Query lists, 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). Tenant and Map are 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).
ParsedQuery
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.
TimeBucket
A groupByTimeBucket query: the attribute and the bucket size.

Enums§

CursorDirection
Which side of a cursor row a page is on.
Error
Why a query could not be parsed, validated or compiled.
Method
What a Query does: filter, order, paginate, select, aggregate, join, combine, or carry raw SQL.
NullsPosition
Where NULLs sort.
Object
An object among a query’s values.
OrderDirection
A sort direction.
QueryOrJson
What Query::fingerprint accepts: a query, or its JSON.
StringLiteral
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 groupByTimeBucket accepts.

Traits§

Compiler
Something that compiles queries: a builder.
IntoValue
Something usable as one PHP value: integers, floats, booleans, strings, arrays, queries, None for null.
IntoValues
Something usable as a PHP array of values: a Rust array, a Vec or a slice of IntoValues (a list), or an Array (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.

Type Aliases§

Array
A PHP array of Values.
Result
The result of the library’s fallible operations.
Value
A value a query or a statement carries: a PHP value (bytes, integers distinct from floats, ordered arrays with integer and string keys, stdClass), where objects are Objects.