Skip to main content

Module expr

Module expr 

Source
Expand description

SQL types, columns, and the typed expression AST.

Expr<Req, S> carries two purely phantom compile-time tags: Req (the flat cons-list of tables this expression touches — see scope::Superset) and S (its SQL type). The actual payload, ExprKind, is a plain closed enum with no generics at all, so the renderer is never re-monomorphized per query shape.

Structs§

Agg
The row key an aggregate over C is filed under: distinct per function and per column, so sum(a) and sum(b) don’t collide, and named after the column so a DTO field or a CTE column can match it.
Avg
avg(column). NULL over zero rows.
BigInt
Bool
Bytes
Column
A column reference, identified entirely by its ColumnKey. Generated per-field by #[derive(Table)] as a pub const NAME: Column<..> inside each table’s module (e.g. users::id). A plain, Copy value — not tied to any particular query — which is what lets it be reused across queries and passed as an ordinary function argument instead of through a scope-bound cursor closure.
Count
The identity a selected count(*) is filed under in a row.
CountOf
count(column) — non-NULL values, unlike count()’s count(*) rows.
Date
Expr
A typed SQL expression. Req is the (possibly empty) flat list of tables this expression references — see the module docs and scope::Superset for how that’s checked against a query’s actual scope at the point the expression is used, not at the point it’s built. This is what lets orders::user_id.eq(users::id) be a plain, portable value with no dependency on which query it’ll eventually be used in.
Integer
Keyed
An expression that carries its own row key K: count() and the window functions, whose identity is the function that produced them. Selecting two of the same one into a row is what row::Row::get rejects, and what LabelExt::label exists to resolve.
Labeled
A selected item filed under a LabelKey instead of under its own identity, and rendered with that name as its AS.
Max
max(column). NULL over zero rows.
Min
min(column). NULL over zero rows.
Numeric
RawSlot
What a slot holds, opaque outside this crate: the RawArg impls are the only way to make one, so a slot always holds something the renderer can write.
Real
Sum
sum(column). NULL over zero rows, so the result is always nullable.
Text
Timestamptz
Uuid

Enums§

BinOp
SortDir
Sort direction. Shared by ORDER BY (select::OrderKey) and window functions’ OVER (.. ORDER BY ..) (window::Window), hence living here rather than in either module.
Value
A closed, non-generic sum of every literal value the crate can bind as a query parameter. Deliberately not Box<dyn ToSql>: a plain enum keeps the renderer free of vtable dispatch and lets it stay a single non-generic function no matter how many query shapes exist.

Traits§

AssignsTo
What an expression can be assigned to. The value’s type is Self and the column’s is the parameter, which is the direction assignment runs in: a Text value goes into a Nullable<Text> column and a narrower number into a wider one, never the reverse. (Comparable is the symmetric, nullability-blind relation, and a comparison is symmetric.)
BoolLike
What a WHERE/HAVING/ON clause accepts. Nullable<Bool> belongs here because SQL takes it: a NULL condition selects no row, which is the same answer IS NOT TRUE would give.
ColumnKey
A column’s compile-time identity. One implementor per column in the schema, generated by #[derive(Table)] (and by with!{} for a CTE’s pseudo-columns), which is what lets a column be a key — two columns of the same table and SQL type are still distinct types here, so a row can be indexed by column without ambiguity.
Comparable
Which SQL types may be compared with each other. A nullable column and a non-nullable one hold the same values, so users::manager_id.eq(users::id) is an ordinary join predicate; without this relation the two would be unrelated types and every optional foreign key would be unwritable.
ExprMethods
Comparison/boolean-combinator methods, blanket-implemented for anything convertible to a typed expression (columns, literals, and Expr itself). Kept separate from IntoExpr so one blanket impl can serve all three.
HasCount
The identity a selected count(*) is filed under in a row.
IntoExpr
Converts a value into a typed expression, tagging it with the set of tables it references (Nil for a plain literal, Cons<T, Nil> for a bare column, or whatever Req an already-built Expr carries).
LabelExt
Files a selected item under a declared name: the way to select the same expression twice, and the way out of two tables’ same-named columns colliding.
LabelKey
A caller-declared output-column name, generated by label!{}. Being a marker over Named is what keeps label!{} the only way to make one, and Named::NAME the only place the name is written.
NullValue
A base SQL type’s typed NULL — see Value::NullI32 etc. for why this can’t just be a single untyped Value::Null.
Ordered
What min/max accept: a type the databases order. Its own marker for the reason Summable is one — WrapNullable<MaybeNull>, which stood here before, is implemented for every leaf type, so it gated nothing and max(bool_column) rendered SQL Postgres has no aggregate for.
RawArg
One ? slot of a sql!{} fragment: every expression, plus the Option a request field already holds — a slot is the one place a NULL arrives as data rather than as a written null::<..>(). A slot that isn’t one reports IntoExpr, since that is the bound this one is built on.
RawArgs
The whole slot list of one sql!{}, whose Req is every table its slots name.
SqlType
A SQL scalar type. Implemented only by the closed set of leaf types declared via sql_leaf_type! below, plus Nullable<T>.
Summable
What sum(..) of a column decodes to. sum is NULL over zero rows, so every result is nullable however the column was declared. CAST keeps the widened type a database picks for a sum inside the closed set of types this crate has: Postgres returns numeric for sum(bigint) and avg(int), neither of which has a native here.
TextLike
What LIKE accepts: a text expression, nullable or not. Its own marker rather than Comparable<Text> so the failure says what the operator needs instead of talking about comparison.
Writable
A column a statement may write to: #[derive(Table)] emits this for every column except the generated and primary-key ones, which are the same set *Update leaves out.

Functions§

all_of
True when all of them are. An empty collection matches everything, which is what a WHERE with no conditions does.
any_of
True when any of the conditions is. Takes a runtime-length collection, the way is_in takes a runtime-length list of values, so the OR a search box needs doesn’t have to be folded by hand — folding one by one grows Req and stops type-checking after the first pair. An empty collection matches nothing, which is what is_in([]) says too.
avg
avg(column). NULL over zero rows.
count
Counts rows. count_of(column) counts that column’s non-NULL values, which is the different question a LEFT JOIN makes visible.
count_of
count(column) — non-NULL values, unlike count()’s count(*) rows.
max
max(column). NULL over zero rows.
min
min(column). NULL over zero rows.
null
A typed SQL NULL, for the one position an Option can’t say it: SET column = NULL assigns, and an assignment has no Option to be None. null::<Text>() is Nullable<Text>, so only a nullable column accepts it.
sum
sum(column). NULL over zero rows, so the result is always nullable.

Type Aliases§

Declared
An expression that has stated what it decodes to but has no name: the anonymous Keyed, and so selectable, labellable and usable in a slot on exactly the same terms as any other keyed expression — except that Anon is not Spelled, so it can’t be looked up or matched by name.