Rust

Crate root

Module
What utopia_query defines at its root.

Modules

Contents

use utopia_query::Code;
Rust
pub struct Code(pub i64);

An exception code: an integer, or a string PHP turns into one (a numeric string is cast, anything else is 0).

Also implements From<i64>, From<&str>, Display.
use utopia_query::ParsedQuery;
Rust
pub struct ParsedQuery {
    pub filters: Vec<Query>,
    pub selections: Vec<Query>,
    pub aggregations: Vec<Query>,
    pub group_by: Vec<Value>,
    pub having: Vec<Query>,
    pub distinct: bool,
    pub joins: Vec<Query>,
    pub unions: Vec<Query>,
    pub limit: Option<i64>,
    pub offset: Option<i64>,
    pub cursor: Option<Value>,
    pub cursor_direction: Option<CursorDirection>,
    pub time_buckets: Vec<TimeBucket>,
}

The queries grouped by what they do, as builders compile them.

PHP: Utopia\Query\Builder\ParsedQuery.

use utopia_query::Query;
Rust
pub struct Query {
    // private fields
}

One query: a Method, the attribute it applies to, its values, and the alias a join or an aggregate is exposed under.

Values are PHP values (Value); nested queries (the children of and, or, having, the conditions of a join) are values too.

PHP: Utopia\Query\Query.

Rust
use utopia_query::{Method, Query};

let query = Query::equal("status", ["active", "pending"]);
assert_eq!(query.method(), Method::Equal);
assert_eq!(query.to_string()?, r#"{"method":"equal","attribute":"status","values":["active","pending"]}"#);
PHP
use Utopia\Query\Query;

$query = Query::equal('status', ['active', 'pending']);
echo $query->getMethod()->value, "\n"; // equal
echo $query->toString(), "\n";         // {"method":"equal","attribute":"status","values":["active","pending"]}
Also implements From<Query>.
use utopia_query::TimeBucket;
Rust
pub struct TimeBucket {
    pub attribute: String,
    pub interval: Value,
}

A groupByTimeBucket query: the attribute and the bucket size.

use utopia_query::CursorDirection;
Rust
pub enum CursorDirection {
    After,
    Before,
}

Which side of a cursor row a page is on.

PHP: Utopia\Query\CursorDirection.

Also implements Display.
use utopia_query::Error;
Rust
pub enum Error {
    Query {
        message: String,
        code: i64,
        previous: Option<Arc<dyn Error + Send + Sync>>,
        raw: Option<Arc<[u8]>>,
    },
    Validation(String),
    Unsupported(String),
    InvalidArgument(String),
    InvalidValue(String),
    Type(String),
    NoExecutor(String),
    Engine(String),
    Json {
        message: String,
        code: i64,
    },
}

Why a query could not be parsed, validated or compiled.

PHP: one variant per exception class the library throws, with the same message (Error::php_class names the class).

Also implements PartialEq.
use utopia_query::Method;
Rust
pub enum Method {
    Equal,
    NotEqual,
    LessThan,
    LessThanEqual,
    GreaterThan,
    GreaterThanEqual,
    Contains,
    ContainsAny,
    NotContains,
    Search,
    NotSearch,
    IsNull,
    IsNotNull,
    Between,
    NotBetween,
    StartsWith,
    NotStartsWith,
    EndsWith,
    NotEndsWith,
    Regex,
    Exists,
    NotExists,
    Crosses,
    NotCrosses,
    DistanceEqual,
    DistanceNotEqual,
    DistanceGreaterThan,
    DistanceLessThan,
    Intersects,
    NotIntersects,
    Overlaps,
    NotOverlaps,
    Touches,
    NotTouches,
    VectorDot,
    VectorCosine,
    VectorEuclidean,
    Select,
    OrderDesc,
    OrderAsc,
    OrderRandom,
    Limit,
    Offset,
    CursorAfter,
    CursorBefore,
    And,
    Or,
    ContainsAll,
    ElemMatch,
    Relationship,
    Count,
    CountDistinct,
    Sum,
    Avg,
    Min,
    Max,
    Stddev,
    StddevPop,
    StddevSamp,
    Variance,
    VarPop,
    VarSamp,
    BitAnd,
    BitOr,
    BitXor,
    GroupBy,
    GroupByTimeBucket,
    Having,
    Distinct,
    Join,
    LeftJoin,
    RightJoin,
    CrossJoin,
    FullOuterJoin,
    NaturalJoin,
    On,
    Union,
    UnionAll,
    JsonContains,
    JsonNotContains,
    JsonOverlaps,
    JsonPath,
    OrderVectorDistance,
    Covers,
    NotCovers,
    SpatialEquals,
    NotSpatialEquals,
    Raw,
}

What a Query does: filter, order, paginate, select, aggregate, join, combine, or carry raw SQL.

PHP: Utopia\Query\Method.

Also implements Display.
use utopia_query::NullsPosition;
Rust
pub enum NullsPosition {
    First,
    Last,
}

Where NULLs sort.

PHP: Utopia\Query\NullsPosition.

Also implements Display, From<NullsPosition>.
use utopia_query::Object;
Rust
pub enum Object {
    Query(Box<Query>),
    Nulls(NullsPosition),
    Row {
        class: String,
        fields: Array,
    },
}

An object among a query's values.

Also implements From<NullsPosition>, Extension.
use utopia_query::OrderDirection;
Rust
pub enum OrderDirection {
    Asc,
    Desc,
    Random,
}

A sort direction.

PHP: Utopia\Query\OrderDirection.

Also implements Display.
use utopia_query::QueryOrJson;
Rust
pub enum QueryOrJson<'a> {
    Query(&'a Query),
    Json(&'a str),
}

What Query::fingerprint accepts: a query, or its JSON.

use utopia_query::StringLiteral;
Rust
pub enum StringLiteral {
    Backslash,
    BackslashUtf8,
    Extended,
    Standard,
}

The rules a dialect's string literals follow.

PHP: Utopia\Query\StringLiteral.

use utopia_query::Type;
Rust
pub enum Type {
    Read,
    Write,
    TransactionBegin,
    TransactionEnd,
    Transaction,
    Unknown,
}

What kind of statement a classifier found.

PHP: Utopia\Query\Type.

Also implements Display.
use utopia_query::Compiler;
Rust
pub trait Compiler

Something that compiles queries: a builder.

PHP: Utopia\Query\Compiler.

Implemented byBuilder
use utopia_query::IntoValue;
Rust
pub trait IntoValue

Something usable as one PHP value: integers, floats, booleans, strings, arrays, queries, None for null.

Implemented byNullsPositionQuery
use utopia_query::IntoValues;
Rust
pub trait 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).

use utopia_query::list;
Rust
pub fn list<V: Into<Value>>(items: impl IntoIterator<Item = V>) -> Array

A list of values.

use utopia_query::quote;
Rust
pub fn quote(identifier: &str, wrap: char) -> Result<String>

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.

PHP: QuotesIdentifiers::quote() (with $wrapChar).

Rust
use utopia_query::quote;

assert_eq!(quote("schema.users", '`')?, "`schema`.`users`");
assert_eq!(quote("users.*", '"')?, "\"users\".*");
PHP
use Utopia\Query\Builder\MySQL;

echo (new MySQL())->from('schema.users')->build()->query, "\n"; // SELECT * FROM `schema`.`users`
use utopia_query::quote_literal;
Rust
pub fn quote_literal(identifier: &str, wrap: char) -> Result<String>

Quotes one identifier without treating dots as qualifiers (a column named meta.key, as ClickHouse nested columns are).

PHP: QuotesIdentifiers::quoteLiteral().

Rust
use utopia_query::quote_literal;

assert_eq!(quote_literal("meta.key", '`')?, "`meta.key`");
PHP
use Utopia\Query\Schema\ClickHouse;

echo (new ClickHouse())->table('t')->string('meta.key')->create()->query, "\n";
use utopia_query::string;
Rust
pub fn string(s: impl AsRef<[u8]>) -> Value

A string value.

Rust
pub type Array = Array<Object>;

A PHP array of Values.

Rust
pub type Result<T, E = Error> = Result<T, E>;

The result of the library's fallible operations.

Rust
pub type Value = Value<Object>;

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.

Rust
pub const DEFAULT_ALIAS: &str = "main";

The alias a builder gives its main table when none is set.

PHP: Query::DEFAULT_ALIAS.

Rust
pub const GROUP_BY_TIME_BUCKET_INTERVALS: [&str; 7] = _;

The bucket sizes groupByTimeBucket accepts.

PHP: Query::GROUP_BY_TIME_BUCKET_INTERVALS.