[][src]Trait diesel::expression::BoxableExpression

pub trait BoxableExpression<QS, DB> where
    DB: Backend,
    Self: Expression,
    Self: SelectableExpression<QS>,
    Self: NonAggregate,
    Self: QueryFragment<DB>, 
{ }

Helper trait used when boxing expressions.

In Rust you cannot create a trait object with more than one trait. This type has all of the additional traits you would want when using Box<Expression> as a single trait object.

This is typically used as the return type of a function. For cases where you want to dynamically construct a query, boxing the query is usually more ergonomic.

Examples

use diesel::sql_types::Bool;

enum Search {
    Id(i32),
    Name(String),
}

type DB = diesel::sqlite::Sqlite;

fn find_user(search: Search) -> Box<BoxableExpression<users::table, DB, SqlType = Bool>> {
    match search {
        Search::Id(id) => Box::new(users::id.eq(id)),
        Search::Name(name) => Box::new(users::name.eq(name)),
    }
}

let user_one = users::table
    .filter(find_user(Search::Id(1)))
    .first(&conn)?;
assert_eq!((1, String::from("Sean")), user_one);

let tess = users::table
    .filter(find_user(Search::Name("Tess".into())))
    .first(&conn)?;
assert_eq!((2, String::from("Tess")), tess);

Trait Implementations

impl<'a, QS, ST, DB> QueryId for dyn BoxableExpression<QS, DB, SqlType = ST> + 'a
[src]

type QueryId = ()

A type which uniquely represents Self in a SQL query. Read more

fn query_id() -> Option<TypeId>
[src]

Returns the type id of Self::QueryId if Self::HAS_STATIC_QUERY_ID. Returns None otherwise. Read more

Implementors

impl<QS, T, DB> BoxableExpression<QS, DB> for T where
    DB: Backend,
    T: Expression,
    T: SelectableExpression<QS>,
    T: NonAggregate,
    T: QueryFragment<DB>, 
[src]

Loading content...