#[non_exhaustive]pub enum Expr {
Show 14 variants
Literal(SqlValue),
Column(&'static str),
BinOp {
left: Box<Expr>,
op: BinOp,
right: Box<Expr>,
},
Function {
kind: ScalarFn,
args: Vec<Expr>,
},
Case {
branches: Vec<CaseBranch>,
default: Option<Box<Expr>>,
},
Subquery(Box<SelectQuery>),
AggregateSubquery(Box<AggregateQuery>),
RelAggregate {
kind: RelAggKind,
column: Option<&'static str>,
table: &'static str,
correlation: RelCorrelation,
},
OuterRef(&'static str),
AliasedColumn {
alias: &'static str,
column: &'static str,
},
Window(Box<WindowExpr>),
Aggregate(Box<AggregateExpr>),
Cast {
expr: Box<Expr>,
ty: FieldType,
},
JsonPath {
source: Box<Expr>,
path: Vec<JsonPathStep>,
as_text: bool,
},
}Expand description
RHS expression — literal, column reference, arithmetic tree, function call, and more.
The writer renders an Expr to the right of = in an UPDATE
assignment, and to the right of a column predicate in a WHERE
clause. The variants are recursive, so arithmetic and calls nest
as deep as you need.
Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
Literal(SqlValue)
Bound value. Emitter pushes a parameter.
Column(&'static str)
Column reference. Emitter writes the quoted identifier.
&'static str matches how the rest of the IR names columns.
BinOp
Binary arithmetic: left <op> right. Recursive — either side
may be a nested BinOp.
Function
Scalar function call — FN(arg, arg, …). Every arity folds
into args, so the writer never switches on argument count.
See crate::core::funcs for the builders.
Case
CASE WHEN c1 THEN t1 [WHEN c2 THEN t2 …] [ELSE d] END.
Standard SQL, emitted the same way on all three dialects.
branches holds the WHEN arms in order; default is the
optional ELSE, left out of the SQL when None.
Build it with crate::core::case::case() — the chain reads
closer to the SQL and does the boxing for you.
Subquery(Box<SelectQuery>)
Scalar subquery — (SELECT col FROM … LIMIT 1). Fits any slot
that wants one value: set_expr, eq_expr, a WHERE rhs.
The inner SelectQuery comes from QuerySet::compile(), so
schema errors surface when you build it, and one compiled
subquery can be reused across statements.
Shaping the inner query to one row and one column is on you.
AggregateSubquery(Box<AggregateQuery>)
Scalar aggregate subquery — (SELECT COUNT(*) FROM … WHERE …).
Like Self::Subquery, but the inner query is an
AggregateQuery, so it projects one aggregate instead of the
child model’s columns.
Backs crate::query::QuerySet::where_has_count. The writer
pushes the inner model’s scope frame, so any Self::OuterRef
inside points at the parent row, the same on all three dialects.
Build it with crate::core::subquery::reverse_has_count.
RelAggregate
Correlated aggregate over a raw relation table — the M2M /
GFK counterpart of AggregateSubquery
for tables with no ModelSchema. Emits
(SELECT <kind>(<column> | *) FROM <table> WHERE <correlation>).
column is None for COUNT(*), Some(col) otherwise. The
link back to the outer row lives in
RelCorrelation.
OuterRef(&'static str)
A reference to an outer query’s column from inside a correlated
subquery. Emitted as "<outer_table>"."<col>"; the writer
threads the outer table through at emit time, so nested
EXISTS, IN (SELECT …) and scalar subqueries can all read
the outer row.
Build it with crate::core::subquery::outer_ref.
AliasedColumn
Column qualified by an explicit table alias —
"<alias>"."<column>". Needed in JOIN ON predicates, where
either side may name a column on another table and a bare
Column(name) would resolve against the wrong one.
Build it with crate::core::joins::aliased.
Window(Box<WindowExpr>)
Window function — <fn>(args) OVER (PARTITION BY … ORDER BY … [frame]). Boxed to keep Expr small, since WindowExpr
carries its own Vec<Expr>.
Build it with crate::core::window (row_number, rank,
dense_rank, lag, lead, first_value, last_value,
ntile).
Aggregate(Box<AggregateExpr>)
An aggregate lifted into the Expr tree, so it can sit in a
HAVING predicate through
super::query::WhereExpr::ExprCompare. PG needs the whole
expression there, not the SELECT alias. It also composes
inside Case, Coalesce and set_expr when the query
aggregates. Boxed to keep Expr small.
Cast
CAST(<expr> AS <ty>) — explicit type coercion. ty is a
dialect-neutral crate::core::FieldType; the writer maps it
to the dialect’s SQL token via
crate::sql::Dialect::null_cast. Only that token differs
between PG / MySQL / SQLite.
JsonPath
JSON path extraction — <source> -> 'k1' -> 'k2' ->> 'k3' on
PG, JSON_UNQUOTE(JSON_EXTRACT(<source>, '$.k1.k2.k3')) on
MySQL, json_extract(<source>, '$.k1.k2.k3') on SQLite.
as_text = true asks for the unwrapped text form; false
keeps the JSON-typed form for further chaining.
With as_text, a JSON null reads as SQL NULL on every backend.
Implementations§
Source§impl Expr
impl Expr
Sourcepub fn col(name: &'static str) -> Self
pub fn col(name: &'static str) -> Self
Build a column-reference expression. Sugar for Expr::Column.
Sourcepub fn binop(self, op: BinOp, rhs: impl Into<Expr>) -> Self
pub fn binop(self, op: BinOp, rhs: impl Into<Expr>) -> Self
Build self <op> rhs. Boxes both sides for you.
Sourcepub fn is_literal(&self) -> bool
pub fn is_literal(&self) -> bool
true when this is a plain literal: no column refs, no
arithmetic. Writers use it as a fast path.
Sourcepub fn as_literal(&self) -> Option<&SqlValue>
pub fn as_literal(&self) -> Option<&SqlValue>
The inner SqlValue when self is Literal, else None.
Trait Implementations§
Source§impl From<CaseBuilder> for Expr
impl From<CaseBuilder> for Expr
Source§fn from(b: CaseBuilder) -> Self
fn from(b: CaseBuilder) -> Self
Source§impl From<WindowBuilder> for Expr
impl From<WindowBuilder> for Expr
Source§fn from(b: WindowBuilder) -> Self
fn from(b: WindowBuilder) -> Self
impl StructuralPartialEq for Expr
Auto Trait Implementations§
impl Freeze for Expr
impl RefUnwindSafe for Expr
impl Send for Expr
impl Sync for Expr
impl Unpin for Expr
impl UnsafeUnpin for Expr
impl UnwindSafe for Expr
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more