Trait diesel::prelude::BoxedDsl [] [src]

pub trait BoxedDsl: Sized {
    fn into_boxed<'a, DB>(self) -> Self::Output where DB: Backend, Self: InternalBoxedDsl<'a, DB> { ... }
}

Boxes the pieces of a query into a single type. This is useful for cases where you want to conditionally modify a query, but need the type to remain the same. The backend must be specified as part of this. It is not possible to box a query and have it be useable on multiple backends.

A boxed query will incur a minor performance penalty, as the query builder can no longer be inlined by the compiler. For most applications this cost will be minimal.

Example

let mut query = users::table.into_boxed();
if let Some(name) = params.get("name") {
    query = query.filter(users::name.eq(name));
}
let users = query.load(&connection);

Diesel queries also have a similar problem to Iterator, where returning them from a function requires exposing the implementation of that function. The helper_types module exists to help with this, but you might want to hide the return type or have it conditionally change. Boxing can achieve both.

Example

fn users_by_name<'a>(name: &'a str) -> users::BoxedQuery<'a, DB> {
    users::table.filter(users::name.eq(name)).into_boxed()
}

assert_eq!(Ok(1), users_by_name("Sean").select(users::id).first(&connection));
assert_eq!(Ok(2), users_by_name("Tess").select(users::id).first(&connection));

Provided Methods

Implementors