pub trait Expr<'a, V: SQLParam>: ToSQL<'a, V> + ExprSources {
type SQLType: DataType;
type Nullable: Nullability;
type Aggregate: AggregateKind;
// Provided methods
fn to_expr_sql(&self) -> SQL<'a, V> { ... }
fn into_expr_sql(self) -> SQL<'a, V>
where Self: Sized { ... }
fn to_condition_sql(&self) -> Option<SQL<'a, V>> { ... }
fn into_condition_sql(self) -> Option<SQL<'a, V>>
where Self: Sized { ... }
}Expand description
A typed SQL expression.
Columns, Rust literals, SQLExpr values and the results of the functions
in this module implement Expr. The associated types describe the result:
SQLType: the SQL data type, such as the dialect’s integer or text type;Nullable:NonNullorNull;Aggregate:ScalarorAgg.
V is the dialect’s value type (for example SQLiteValue or
PostgresValue), and 'a is the lifetime of borrowed values inside the
expression. The table macros implement this trait for generated columns;
you rarely implement it yourself.
§Examples
A helper that accepts any integer expression:
fn is_adult<'a, E>(age: E) -> impl Expr<'a, Value>
where
E: Expr<'a, Value, SQLType = Int>,
{
gt(age, 18)
}
assert_eq!(is_adult(users.age).into_expr_sql().sql(), r#""users"."age" > ?"#);Required Associated Types§
Sourcetype Nullable: Nullability
type Nullable: Nullability
Whether this expression can be NULL.
Sourcetype Aggregate: AggregateKind
type Aggregate: AggregateKind
Whether this is an aggregate (COUNT, SUM) or scalar expression.
Provided Methods§
Sourcefn to_expr_sql(&self) -> SQL<'a, V>
fn to_expr_sql(&self) -> SQL<'a, V>
Renders this value as a scalar expression, borrowing it.
Most expressions use their ToSQL implementation. A few Rust container
types, notably byte buffers, need expression-specific rendering because
their generic ToSQL form is a comma-separated list.
Sourcefn into_expr_sql(self) -> SQL<'a, V>where
Self: Sized,
fn into_expr_sql(self) -> SQL<'a, V>where
Self: Sized,
Renders this value as a scalar expression, consuming it.
Sourcefn to_condition_sql(&self) -> Option<SQL<'a, V>>
fn to_condition_sql(&self) -> Option<SQL<'a, V>>
Renders this value as one element of a ConditionList.
None means the element contributes no condition and is dropped from
the combined SQL. Only Option::None does this; every other expression
renders through Expr::to_expr_sql.
Sourcefn into_condition_sql(self) -> Option<SQL<'a, V>>where
Self: Sized,
fn into_condition_sql(self) -> Option<SQL<'a, V>>where
Self: Sized,
Consuming counterpart of Expr::to_condition_sql.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementations on Foreign Types§
Source§impl<'a, V, T0, T1, T2, T3, T4, T5, T6, T7> Expr<'a, V> for (T0, T1, T2, T3, T4, T5, T6, T7)
impl<'a, V, T0, T1, T2, T3, T4, T5, T6, T7> Expr<'a, V> for (T0, T1, T2, T3, T4, T5, T6, T7)
type SQLType = Conjunction
type Nullable = <(T0, T1, T2, T3, T4, T5, T6, T7) as ConditionList<'a, V>>::Nullable
type Aggregate = <(T0, T1, T2, T3, T4, T5, T6, T7) as ConditionList<'a, V>>::Aggregate
fn to_expr_sql(&self) -> SQL<'a, V>
fn into_expr_sql(self) -> SQL<'a, V>
Source§impl<'a, V, T0, T1, T2, T3, T4, T5, T6> Expr<'a, V> for (T0, T1, T2, T3, T4, T5, T6)
impl<'a, V, T0, T1, T2, T3, T4, T5, T6> Expr<'a, V> for (T0, T1, T2, T3, T4, T5, T6)
type SQLType = Conjunction
type Nullable = <(T0, T1, T2, T3, T4, T5, T6) as ConditionList<'a, V>>::Nullable
type Aggregate = <(T0, T1, T2, T3, T4, T5, T6) as ConditionList<'a, V>>::Aggregate
fn to_expr_sql(&self) -> SQL<'a, V>
fn into_expr_sql(self) -> SQL<'a, V>
Source§impl<'a, V, T0, T1, T2, T3, T4, T5> Expr<'a, V> for (T0, T1, T2, T3, T4, T5)
impl<'a, V, T0, T1, T2, T3, T4, T5> Expr<'a, V> for (T0, T1, T2, T3, T4, T5)
type SQLType = Conjunction
type Nullable = <(T0, T1, T2, T3, T4, T5) as ConditionList<'a, V>>::Nullable
type Aggregate = <(T0, T1, T2, T3, T4, T5) as ConditionList<'a, V>>::Aggregate
fn to_expr_sql(&self) -> SQL<'a, V>
fn into_expr_sql(self) -> SQL<'a, V>
Source§impl<'a, V, T0, T1, T2, T3, T4> Expr<'a, V> for (T0, T1, T2, T3, T4)
impl<'a, V, T0, T1, T2, T3, T4> Expr<'a, V> for (T0, T1, T2, T3, T4)
type SQLType = Conjunction
type Nullable = <(T0, T1, T2, T3, T4) as ConditionList<'a, V>>::Nullable
type Aggregate = <(T0, T1, T2, T3, T4) as ConditionList<'a, V>>::Aggregate
fn to_expr_sql(&self) -> SQL<'a, V>
fn into_expr_sql(self) -> SQL<'a, V>
Source§impl<'a, V, T0, T1, T2, T3> Expr<'a, V> for (T0, T1, T2, T3)
impl<'a, V, T0, T1, T2, T3> Expr<'a, V> for (T0, T1, T2, T3)
type SQLType = Conjunction
type Nullable = <(T0, T1, T2, T3) as ConditionList<'a, V>>::Nullable
type Aggregate = <(T0, T1, T2, T3) as ConditionList<'a, V>>::Aggregate
fn to_expr_sql(&self) -> SQL<'a, V>
fn into_expr_sql(self) -> SQL<'a, V>
Source§impl<'a, V, T0, T1, T2> Expr<'a, V> for (T0, T1, T2)
impl<'a, V, T0, T1, T2> Expr<'a, V> for (T0, T1, T2)
type SQLType = Conjunction
type Nullable = <(T0, T1, T2) as ConditionList<'a, V>>::Nullable
type Aggregate = <(T0, T1, T2) as ConditionList<'a, V>>::Aggregate
fn to_expr_sql(&self) -> SQL<'a, V>
fn into_expr_sql(self) -> SQL<'a, V>
Source§impl<'a, V, T0, T1> Expr<'a, V> for (T0, T1)
impl<'a, V, T0, T1> Expr<'a, V> for (T0, T1)
type SQLType = Conjunction
type Nullable = <(T0, T1) as ConditionList<'a, V>>::Nullable
type Aggregate = <(T0, T1) as ConditionList<'a, V>>::Aggregate
fn to_expr_sql(&self) -> SQL<'a, V>
fn into_expr_sql(self) -> SQL<'a, V>
Source§impl<'a, V, T0> Expr<'a, V> for (T0,)
impl<'a, V, T0> Expr<'a, V> for (T0,)
type SQLType = Conjunction
type Nullable = <(T0,) as ConditionList<'a, V>>::Nullable
type Aggregate = <(T0,) as ConditionList<'a, V>>::Aggregate
fn to_expr_sql(&self) -> SQL<'a, V>
fn into_expr_sql(self) -> SQL<'a, V>
Source§impl<'a, V, T> Expr<'a, V> for &T
impl<'a, V, T> Expr<'a, V> for &T
type SQLType = <T as Expr<'a, V>>::SQLType
type Nullable = <T as Expr<'a, V>>::Nullable
type Aggregate = <T as Expr<'a, V>>::Aggregate
fn to_expr_sql(&self) -> SQL<'a, V>
fn into_expr_sql(self) -> SQL<'a, V>
fn to_condition_sql(&self) -> Option<SQL<'a, V>>
fn into_condition_sql(self) -> Option<SQL<'a, V>>
Source§impl<'a, V, T> Expr<'a, V> for Option<T>
impl<'a, V, T> Expr<'a, V> for Option<T>
Source§fn to_condition_sql(&self) -> Option<SQL<'a, V>>
fn to_condition_sql(&self) -> Option<SQL<'a, V>>
A missing optional condition contributes nothing to a condition list.
Source§fn into_condition_sql(self) -> Option<SQL<'a, V>>
fn into_condition_sql(self) -> Option<SQL<'a, V>>
A missing optional condition contributes nothing to a condition list.