Skip to main content

Expr

Trait Expr 

Source
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: NonNull or Null;
  • Aggregate: Scalar or Agg.

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§

Source

type SQLType: DataType

The SQL data type this expression evaluates to.

Source

type Nullable: Nullability

Whether this expression can be NULL.

Source

type Aggregate: AggregateKind

Whether this is an aggregate (COUNT, SUM) or scalar expression.

Provided Methods§

Source

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.

Source

fn into_expr_sql(self) -> SQL<'a, V>
where Self: Sized,

Renders this value as a scalar expression, consuming it.

Source

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.

Source

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)
where V: SQLParam + 'a, Self: ConditionList<'a, V> + ToSQL<'a, V>,

Source§

impl<'a, V, T0, T1, T2, T3, T4, T5, T6> Expr<'a, V> for (T0, T1, T2, T3, T4, T5, T6)
where V: SQLParam + 'a, Self: ConditionList<'a, V> + ToSQL<'a, V>,

Source§

impl<'a, V, T0, T1, T2, T3, T4, T5> Expr<'a, V> for (T0, T1, T2, T3, T4, T5)
where V: SQLParam + 'a, Self: ConditionList<'a, V> + ToSQL<'a, V>,

Source§

impl<'a, V, T0, T1, T2, T3, T4> Expr<'a, V> for (T0, T1, T2, T3, T4)
where V: SQLParam + 'a, Self: ConditionList<'a, V> + ToSQL<'a, V>,

Source§

impl<'a, V, T0, T1, T2, T3> Expr<'a, V> for (T0, T1, T2, T3)
where V: SQLParam + 'a, Self: ConditionList<'a, V> + ToSQL<'a, V>,

Source§

impl<'a, V, T0, T1, T2> Expr<'a, V> for (T0, T1, T2)
where V: SQLParam + 'a, Self: ConditionList<'a, V> + ToSQL<'a, V>,

Source§

impl<'a, V, T0, T1> Expr<'a, V> for (T0, T1)
where V: SQLParam + 'a, Self: ConditionList<'a, V> + ToSQL<'a, V>,

Source§

impl<'a, V, T0> Expr<'a, V> for (T0,)
where V: SQLParam + 'a, Self: ConditionList<'a, V> + ToSQL<'a, V>,

Source§

impl<'a, V, T> Expr<'a, V> for &T
where V: SQLParam + 'a, T: Expr<'a, V>, T::Nullable: Nullability,

Source§

type SQLType = <T as Expr<'a, V>>::SQLType

Source§

type Nullable = <T as Expr<'a, V>>::Nullable

Source§

type Aggregate = <T as Expr<'a, V>>::Aggregate

Source§

fn to_expr_sql(&self) -> SQL<'a, V>

Source§

fn into_expr_sql(self) -> SQL<'a, V>

Source§

fn to_condition_sql(&self) -> Option<SQL<'a, V>>

Source§

fn into_condition_sql(self) -> Option<SQL<'a, V>>

Source§

impl<'a, V, T> Expr<'a, V> for Option<T>
where V: SQLParam + 'a, T: Expr<'a, V>, T::Nullable: Nullability,

Source§

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>>

A missing optional condition contributes nothing to a condition list.

Source§

type SQLType = <T as Expr<'a, V>>::SQLType

Source§

type Nullable = Null

Source§

type Aggregate = <T as Expr<'a, V>>::Aggregate

Source§

impl<'a, V, const N: usize> Expr<'a, V> for [char; N]
where V: SQLParam + 'a + From<Self> + From<char> + Into<Cow<'a, V>>, Self: ValueTypeForDialect<V::DialectMarker>,

Source§

impl<'a, V, const N: usize> Expr<'a, V> for [u8; N]
where V: SQLParam + 'a + From<&'a [u8]> + From<Vec<u8>> + From<u8> + Into<Cow<'a, V>>,

Source§

impl<'a, V> Expr<'a, V> for &'a [u8]
where V: SQLParam + 'a + From<&'a [u8]> + From<Vec<u8>> + From<u8> + Into<Cow<'a, V>>,

Source§

impl<'a, V> Expr<'a, V> for &'a str
where V: SQLParam + 'a + From<&'a str> + Into<Cow<'a, V>>, Self: ValueTypeForDialect<V::DialectMarker>,

Source§

impl<'a, V> Expr<'a, V> for Cow<'a, [u8]>
where V: SQLParam + 'a + From<&'a [u8]> + From<Vec<u8>> + Into<Cow<'a, V>>,

Source§

impl<'a, V> Expr<'a, V> for String
where V: SQLParam + 'a + From<Self> + Into<Cow<'a, V>>, Self: ValueTypeForDialect<V::DialectMarker>,

Source§

impl<'a, V> Expr<'a, V> for Vec<u8>
where V: SQLParam + 'a + From<&'a [u8]> + From<Vec<u8>> + From<u8> + Into<Cow<'a, V>>,

Source§

impl<'a, V> Expr<'a, V> for bool
where V: SQLParam + 'a + From<Self> + Into<Cow<'a, V>>, Self: ValueTypeForDialect<V::DialectMarker>,

Source§

impl<'a, V> Expr<'a, V> for f32
where V: SQLParam + 'a + From<Self> + Into<Cow<'a, V>>, Self: ValueTypeForDialect<V::DialectMarker>,

Source§

impl<'a, V> Expr<'a, V> for f64
where V: SQLParam + 'a + From<Self> + Into<Cow<'a, V>>, Self: ValueTypeForDialect<V::DialectMarker>,

Source§

impl<'a, V> Expr<'a, V> for i8
where V: SQLParam + 'a + From<Self> + Into<Cow<'a, V>>, Self: ValueTypeForDialect<V::DialectMarker>,

Source§

impl<'a, V> Expr<'a, V> for i16
where V: SQLParam + 'a + From<Self> + Into<Cow<'a, V>>, Self: ValueTypeForDialect<V::DialectMarker>,

Source§

impl<'a, V> Expr<'a, V> for i32
where V: SQLParam + 'a + From<Self> + Into<Cow<'a, V>>, Self: ValueTypeForDialect<V::DialectMarker>,

Source§

impl<'a, V> Expr<'a, V> for i64
where V: SQLParam + 'a + From<Self> + Into<Cow<'a, V>>, Self: ValueTypeForDialect<V::DialectMarker>,

Source§

impl<'a, V> Expr<'a, V> for isize
where V: SQLParam + 'a + From<Self> + Into<Cow<'a, V>>, Self: ValueTypeForDialect<V::DialectMarker>,

Source§

impl<'a, V> Expr<'a, V> for u8
where V: SQLParam + 'a + From<Self> + Into<Cow<'a, V>>, Self: ValueTypeForDialect<V::DialectMarker>,

Source§

impl<'a, V> Expr<'a, V> for u16
where V: SQLParam + 'a + From<Self> + Into<Cow<'a, V>>, Self: ValueTypeForDialect<V::DialectMarker>,

Source§

impl<'a, V> Expr<'a, V> for u32
where V: SQLParam + 'a + From<Self> + Into<Cow<'a, V>>, Self: ValueTypeForDialect<V::DialectMarker>,

Source§

impl<'a, V> Expr<'a, V> for u64
where V: SQLParam + 'a + From<Self> + Into<Cow<'a, V>>, Self: ValueTypeForDialect<V::DialectMarker>,

Source§

impl<'a, V> Expr<'a, V> for usize
where V: SQLParam + 'a + From<Self> + Into<Cow<'a, V>>, Self: ValueTypeForDialect<V::DialectMarker>,

Implementors§

Source§

impl<'a, V, C> Expr<'a, V> for Excluded<C>

Source§

type SQLType = <C as Expr<'a, V>>::SQLType

Source§

type Nullable = <C as Expr<'a, V>>::Nullable

Source§

type Aggregate = <C as Expr<'a, V>>::Aggregate

Source§

impl<'a, V, E, Name> Expr<'a, V> for NamedExpr<E, Name>
where V: SQLParam + 'a, E: Expr<'a, V>, Name: Tag,

Source§

type SQLType = <E as Expr<'a, V>>::SQLType

Source§

type Nullable = <E as Expr<'a, V>>::Nullable

Source§

type Aggregate = <E as Expr<'a, V>>::Aggregate

Source§

impl<'a, V, E> Expr<'a, V> for AliasedExpr<E>
where V: SQLParam + 'a, E: Expr<'a, V>,

Source§

type SQLType = <E as Expr<'a, V>>::SQLType

Source§

type Nullable = <E as Expr<'a, V>>::Nullable

Source§

type Aggregate = <E as Expr<'a, V>>::Aggregate

Source§

impl<'a, V, Name, Output> Expr<'a, V> for DerivedField<Name, Output>
where V: SQLParam + 'a, Name: Tag, Output: ProjectionOutput + Expr<'a, V>,

Source§

type SQLType = <Output as Expr<'a, V>>::SQLType

Source§

type Nullable = <Output as Expr<'a, V>>::Nullable

Source§

type Aggregate = Scalar

Source§

impl<'a, V: SQLParam + 'a, T: DataType, N: Nullability> Expr<'a, V> for TypedPlaceholder<T, N>

Source§

impl<'a, V: SQLParam + 'a> Expr<'a, V> for Placeholder

Source§

impl<'a, V: SQLParam, T: DataType, N: Nullability, A: AggregateKind, S> Expr<'a, V> for SQLExpr<'a, V, T, N, A, S>

Source§

impl<'a, V> Expr<'a, V> for SQL<'a, V>
where V: SQLParam + 'a,

Source§

impl<'a, V> Expr<'a, V> for SQLBytes<'a>
where V: SQLParam + 'a + From<&'a [u8]> + From<Vec<u8>> + Into<Cow<'a, V>>,