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

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

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

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

Can the SQL generated by Self be uniquely identified by its type? Read more

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

Implementors