Skip to main content

Expr

Enum Expr 

Source
#[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
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

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.

Fields

§left: Box<Expr>
§right: Box<Expr>
§

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.

Fields

§args: Vec<Expr>
§

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.

Fields

§branches: Vec<CaseBranch>
§default: Option<Box<Expr>>
§

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.

Fields

§column: Option<&'static str>
§table: &'static str
§correlation: 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.

Fields

§alias: &'static str
§column: &'static str
§

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.

Fields

§expr: Box<Expr>
§

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.

Fields

§source: Box<Expr>
§as_text: bool

Implementations§

Source§

impl Expr

Source

pub fn col(name: &'static str) -> Self

Build a column-reference expression. Sugar for Expr::Column.

Source

pub fn binop(self, op: BinOp, rhs: impl Into<Expr>) -> Self

Build self <op> rhs. Boxes both sides for you.

Source

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.

Source

pub fn as_literal(&self) -> Option<&SqlValue>

The inner SqlValue when self is Literal, else None.

Trait Implementations§

Source§

impl<R: Into<Expr>> Add<R> for Expr

Source§

type Output = Expr

The resulting type after applying the + operator.
Source§

fn add(self, rhs: R) -> Expr

Performs the + operation. Read more
Source§

impl<R: Into<Expr>> BitAnd<R> for Expr

Source§

type Output = Expr

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: R) -> Expr

Performs the & operation. Read more
Source§

impl<R: Into<Expr>> BitOr<R> for Expr

Source§

type Output = Expr

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: R) -> Expr

Performs the | operation. Read more
Source§

impl<R: Into<Expr>> BitXor<R> for Expr

Source§

type Output = Expr

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: R) -> Expr

Performs the ^ operation. Read more
Source§

impl Clone for Expr

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Expr

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<R: Into<Expr>> Div<R> for Expr

Source§

type Output = Expr

The resulting type after applying the / operator.
Source§

fn div(self, rhs: R) -> Expr

Performs the / operation. Read more
Source§

impl From<&'static str> for Expr

Source§

fn from(v: &'static str) -> Self

Converts to this type from the input type.
Source§

impl From<CaseBuilder> for Expr

Source§

fn from(b: CaseBuilder) -> Self

Converts to this type from the input type.
Source§

impl From<DateTime<Utc>> for Expr

Source§

fn from(v: DateTime<Utc>) -> Self

Converts to this type from the input type.
Source§

impl From<F> for Expr

Source§

fn from(f: F) -> Self

Converts to this type from the input type.
Source§

impl From<NaiveDate> for Expr

Source§

fn from(v: NaiveDate) -> Self

Converts to this type from the input type.
Source§

impl From<Point> for Expr

Source§

fn from(p: Point) -> Self

Converts to this type from the input type.
Source§

impl From<SqlValue> for Expr

Source§

fn from(v: SqlValue) -> Self

Converts to this type from the input type.
Source§

impl From<String> for Expr

Source§

fn from(v: String) -> Self

Converts to this type from the input type.
Source§

impl From<Uuid> for Expr

Source§

fn from(v: Uuid) -> Self

Converts to this type from the input type.
Source§

impl From<Value> for Expr

Source§

fn from(v: Value) -> Self

Converts to this type from the input type.
Source§

impl From<WindowBuilder> for Expr

Source§

fn from(b: WindowBuilder) -> Self

Converts to this type from the input type.
Source§

impl From<bool> for Expr

Source§

fn from(v: bool) -> Self

Converts to this type from the input type.
Source§

impl From<f32> for Expr

Source§

fn from(v: f32) -> Self

Converts to this type from the input type.
Source§

impl From<f64> for Expr

Source§

fn from(v: f64) -> Self

Converts to this type from the input type.
Source§

impl From<i16> for Expr

Source§

fn from(v: i16) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for Expr

Source§

fn from(v: i32) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for Expr

Source§

fn from(v: i64) -> Self

Converts to this type from the input type.
Source§

impl<R: Into<Expr>> Mul<R> for Expr

Source§

type Output = Expr

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: R) -> Expr

Performs the * operation. Read more
Source§

impl PartialEq for Expr

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<R: Into<Expr>> Rem<R> for Expr

Source§

type Output = Expr

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: R) -> Expr

Performs the % operation. Read more
Source§

impl<R: Into<Expr>> Shl<R> for Expr

Source§

type Output = Expr

The resulting type after applying the << operator.
Source§

fn shl(self, rhs: R) -> Expr

Performs the << operation. Read more
Source§

impl<R: Into<Expr>> Shr<R> for Expr

Source§

type Output = Expr

The resulting type after applying the >> operator.
Source§

fn shr(self, rhs: R) -> Expr

Performs the >> operation. Read more
Source§

impl StructuralPartialEq for Expr

Source§

impl<R: Into<Expr>> Sub<R> for Expr

Source§

type Output = Expr

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: R) -> Expr

Performs the - operation. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T, Rhs, Output> NumOps<Rhs, Output> for T
where T: Sub<Rhs, Output = Output> + Mul<Rhs, Output = Output> + Div<Rhs, Output = Output> + Add<Rhs, Output = Output> + Rem<Rhs, Output = Output>,

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more