Trait rocket_db_pools::diesel::prelude::Selectable

source ·
pub trait Selectable<DB>
where DB: Backend,
{ type SelectExpression: Expression; // Required method fn construct_selection() -> Self::SelectExpression; }
Expand description

Trait indicating that a record can be selected and queried from the database.

Types which implement Selectable represent the select clause of a SQL query. Use SelectableHelper::as_select() to construct the select clause. Once you called .select(YourType::as_select()) we enforce at the type system level that you use the same type to load the query result into.

The constructed select clause can contain arbitrary expressions coming from different tables. The corresponding derive provides a simple way to construct a select clause matching fields to the corresponding table columns.

§Examples

If you just want to construct a select clause using an existing struct, you can use #[derive(Selectable)], See #[derive(Selectable)] for details.

use schema::users;

#[derive(Queryable, PartialEq, Debug, Selectable)]
struct User {
    id: i32,
    name: String,
}

let first_user = users.select(User::as_select()).first(connection)?;
let expected = User { id: 1, name: "Sean".into() };
assert_eq!(expected, first_user);

Alternatively, we can implement the trait for our struct manually.

use schema::users;
use diesel::prelude::{Queryable, Selectable};
use diesel::backend::Backend;

#[derive(Queryable, PartialEq, Debug)]
struct User {
    id: i32,
    name: String,
}

impl<DB> Selectable<DB> for User
where
    DB: Backend
{
    type SelectExpression = (users::id, users::name);

    fn construct_selection() -> Self::SelectExpression {
        (users::id, users::name)
    }
}

let first_user = users.select(User::as_select()).first(connection)?;
let expected = User { id: 1, name: "Sean".into() };
assert_eq!(expected, first_user);

When selecting from joined tables, you can select from a composition of types that implement Selectable. The simplest way is to use a tuple of all the types you wish to select.

use schema::{users, posts};

#[derive(Debug, PartialEq, Queryable, Selectable)]
struct User {
    id: i32,
    name: String,
}

#[derive(Debug, PartialEq, Queryable, Selectable)]
struct Post {
    id: i32,
    user_id: i32,
    title: String,
}

let (first_user, first_post) = users::table
    .inner_join(posts::table)
    .select(<(User, Post)>::as_select())
    .first(connection)?;

let expected_user = User { id: 1, name: "Sean".into() };
assert_eq!(expected_user, first_user);

let expected_post = Post { id: 1, user_id: 1, title: "My first post".into() };
assert_eq!(expected_post, first_post);

If you want to load only a subset of fields, you can create types with those fields and use them in the composition.

use schema::{users, posts};

#[derive(Debug, PartialEq, Queryable, Selectable)]
struct User {
    id: i32,
    name: String,
}

#[derive(Debug, PartialEq, Queryable, Selectable)]
#[diesel(table_name = posts)]
struct PostTitle {
    title: String,
}

let (first_user, first_post_title) = users::table
    .inner_join(posts::table)
    .select(<(User, PostTitle)>::as_select())
    .first(connection)?;

let expected_user = User { id: 1, name: "Sean".into() };
assert_eq!(expected_user, first_user);

let expected_post_title = PostTitle { title: "My first post".into() };
assert_eq!(expected_post_title, first_post_title);

You are not limited to using only tuples to build the composed type. The Selectable derive macro allows you to embed other types. This is useful when you want to implement methods or traits on the composed type.

use schema::{users, posts};

#[derive(Debug, PartialEq, Queryable, Selectable)]
struct User {
    id: i32,
    name: String,
}

#[derive(Debug, PartialEq, Queryable, Selectable)]
#[diesel(table_name = posts)]
struct PostTitle {
    title: String,
}

#[derive(Debug, PartialEq, Queryable, Selectable)]
struct UserPost {
    #[diesel(embed)]
    user: User,
    #[diesel(embed)]
    post_title: PostTitle,
}

let first_user_post = users::table
    .inner_join(posts::table)
    .select(UserPost::as_select())
    .first(connection)?;

let expected_user_post = UserPost {
    user: User {
        id: 1,
        name: "Sean".into(),
    },
    post_title: PostTitle {
        title: "My first post".into(),
    },
};
assert_eq!(expected_user_post, first_user_post);

If you want to avoid nesting types, you can use the Selectable derive macro’s select_expression and select_expression_type attributes to flatten the fields.

use schema::{users, posts};

#[derive(Debug, PartialEq, Queryable, Selectable)]
struct User {
    id: i32,
    name: String,
}

#[derive(Debug, PartialEq, Queryable, Selectable)]
#[diesel(table_name = posts)]
struct PostTitle {
    title: String,
}

#[derive(Debug, PartialEq, Queryable, Selectable)]
struct UserPost {
    #[diesel(select_expression = users::columns::id)]
    #[diesel(select_expression_type = users::columns::id)]
    id: i32,
    #[diesel(select_expression = users::columns::name)]
    #[diesel(select_expression_type = users::columns::name)]
    name: String,
    #[diesel(select_expression = posts::columns::title)]
    #[diesel(select_expression_type = posts::columns::title)]
    title: String,
}

let first_user_post = users::table
    .inner_join(posts::table)
    .select(UserPost::as_select())
    .first(connection)?;

let expected_user_post = UserPost {
    id: 1,
    name: "Sean".into(),
    title: "My first post".into(),
};
assert_eq!(expected_user_post, first_user_post);

Required Associated Types§

source

type SelectExpression: Expression

The expression you’d like to select.

This is typically a tuple of corresponding to the table columns of your struct’s fields.

Required Methods§

source

fn construct_selection() -> Self::SelectExpression

Construct an instance of the expression

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, __DB> Selectable<__DB> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15)
where __DB: Backend, T0: Selectable<__DB>, T1: Selectable<__DB>, T2: Selectable<__DB>, T3: Selectable<__DB>, T4: Selectable<__DB>, T5: Selectable<__DB>, T6: Selectable<__DB>, T7: Selectable<__DB>, T8: Selectable<__DB>, T9: Selectable<__DB>, T10: Selectable<__DB>, T11: Selectable<__DB>, T12: Selectable<__DB>, T13: Selectable<__DB>, T14: Selectable<__DB>, T15: Selectable<__DB>,

source§

impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, __DB> Selectable<__DB> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14)
where __DB: Backend, T0: Selectable<__DB>, T1: Selectable<__DB>, T2: Selectable<__DB>, T3: Selectable<__DB>, T4: Selectable<__DB>, T5: Selectable<__DB>, T6: Selectable<__DB>, T7: Selectable<__DB>, T8: Selectable<__DB>, T9: Selectable<__DB>, T10: Selectable<__DB>, T11: Selectable<__DB>, T12: Selectable<__DB>, T13: Selectable<__DB>, T14: Selectable<__DB>,

source§

impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, __DB> Selectable<__DB> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13)
where __DB: Backend, T0: Selectable<__DB>, T1: Selectable<__DB>, T2: Selectable<__DB>, T3: Selectable<__DB>, T4: Selectable<__DB>, T5: Selectable<__DB>, T6: Selectable<__DB>, T7: Selectable<__DB>, T8: Selectable<__DB>, T9: Selectable<__DB>, T10: Selectable<__DB>, T11: Selectable<__DB>, T12: Selectable<__DB>, T13: Selectable<__DB>,

source§

impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, __DB> Selectable<__DB> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)
where __DB: Backend, T0: Selectable<__DB>, T1: Selectable<__DB>, T2: Selectable<__DB>, T3: Selectable<__DB>, T4: Selectable<__DB>, T5: Selectable<__DB>, T6: Selectable<__DB>, T7: Selectable<__DB>, T8: Selectable<__DB>, T9: Selectable<__DB>, T10: Selectable<__DB>, T11: Selectable<__DB>, T12: Selectable<__DB>,

source§

impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, __DB> Selectable<__DB> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)
where __DB: Backend, T0: Selectable<__DB>, T1: Selectable<__DB>, T2: Selectable<__DB>, T3: Selectable<__DB>, T4: Selectable<__DB>, T5: Selectable<__DB>, T6: Selectable<__DB>, T7: Selectable<__DB>, T8: Selectable<__DB>, T9: Selectable<__DB>, T10: Selectable<__DB>, T11: Selectable<__DB>,

source§

impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, __DB> Selectable<__DB> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)
where __DB: Backend, T0: Selectable<__DB>, T1: Selectable<__DB>, T2: Selectable<__DB>, T3: Selectable<__DB>, T4: Selectable<__DB>, T5: Selectable<__DB>, T6: Selectable<__DB>, T7: Selectable<__DB>, T8: Selectable<__DB>, T9: Selectable<__DB>, T10: Selectable<__DB>,

source§

impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, __DB> Selectable<__DB> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9)
where __DB: Backend, T0: Selectable<__DB>, T1: Selectable<__DB>, T2: Selectable<__DB>, T3: Selectable<__DB>, T4: Selectable<__DB>, T5: Selectable<__DB>, T6: Selectable<__DB>, T7: Selectable<__DB>, T8: Selectable<__DB>, T9: Selectable<__DB>,

source§

impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, __DB> Selectable<__DB> for (T0, T1, T2, T3, T4, T5, T6, T7, T8)
where __DB: Backend, T0: Selectable<__DB>, T1: Selectable<__DB>, T2: Selectable<__DB>, T3: Selectable<__DB>, T4: Selectable<__DB>, T5: Selectable<__DB>, T6: Selectable<__DB>, T7: Selectable<__DB>, T8: Selectable<__DB>,

source§

impl<T0, T1, T2, T3, T4, T5, T6, T7, __DB> Selectable<__DB> for (T0, T1, T2, T3, T4, T5, T6, T7)
where __DB: Backend, T0: Selectable<__DB>, T1: Selectable<__DB>, T2: Selectable<__DB>, T3: Selectable<__DB>, T4: Selectable<__DB>, T5: Selectable<__DB>, T6: Selectable<__DB>, T7: Selectable<__DB>,

source§

impl<T0, T1, T2, T3, T4, T5, T6, __DB> Selectable<__DB> for (T0, T1, T2, T3, T4, T5, T6)
where __DB: Backend, T0: Selectable<__DB>, T1: Selectable<__DB>, T2: Selectable<__DB>, T3: Selectable<__DB>, T4: Selectable<__DB>, T5: Selectable<__DB>, T6: Selectable<__DB>,

source§

impl<T0, T1, T2, T3, T4, T5, __DB> Selectable<__DB> for (T0, T1, T2, T3, T4, T5)
where __DB: Backend, T0: Selectable<__DB>, T1: Selectable<__DB>, T2: Selectable<__DB>, T3: Selectable<__DB>, T4: Selectable<__DB>, T5: Selectable<__DB>,

source§

impl<T0, T1, T2, T3, T4, __DB> Selectable<__DB> for (T0, T1, T2, T3, T4)
where __DB: Backend, T0: Selectable<__DB>, T1: Selectable<__DB>, T2: Selectable<__DB>, T3: Selectable<__DB>, T4: Selectable<__DB>,

source§

impl<T0, T1, T2, T3, __DB> Selectable<__DB> for (T0, T1, T2, T3)
where __DB: Backend, T0: Selectable<__DB>, T1: Selectable<__DB>, T2: Selectable<__DB>, T3: Selectable<__DB>,

source§

impl<T0, T1, T2, __DB> Selectable<__DB> for (T0, T1, T2)
where __DB: Backend, T0: Selectable<__DB>, T1: Selectable<__DB>, T2: Selectable<__DB>,

source§

impl<T0, T1, __DB> Selectable<__DB> for (T0, T1)
where __DB: Backend, T0: Selectable<__DB>, T1: Selectable<__DB>,

source§

impl<T0, __DB> Selectable<__DB> for (T0,)
where __DB: Backend, T0: Selectable<__DB>,

source§

impl<T, DB> Selectable<DB> for Option<T>
where DB: Backend, T: Selectable<DB>, Nullable<<T as Selectable<DB>>::SelectExpression>: Expression,

Implementors§