Trait diesel::prelude::Selectable
source · pub trait Selectable<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§
sourcetype SelectExpression: Expression
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§
sourcefn construct_selection() -> Self::SelectExpression
fn construct_selection() -> Self::SelectExpression
Construct an instance of the expression