Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Selectable trait #2709

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions diesel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,8 @@ pub mod prelude {
#[doc(inline)]
pub use crate::query_builder::DecoratableTarget;
#[doc(inline)]
pub use crate::query_builder::Selectable;
#[doc(inline)]
pub use crate::query_dsl::{
BelongingToDsl, CombineDsl, JoinOnDsl, QueryDsl, RunQueryDsl, SaveChangesDsl,
};
Expand Down
16 changes: 15 additions & 1 deletion diesel/src/query_builder/delete_statement/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ use crate::expression::{AppearsOnTable, SelectableExpression};
use crate::query_builder::returning_clause::*;
use crate::query_builder::where_clause::*;
use crate::query_builder::*;
use crate::query_dsl::load_dsl::LoadIntoDsl;
use crate::query_dsl::methods::{BoxedDsl, FilterDsl};
use crate::query_dsl::RunQueryDsl;
use crate::query_dsl::{LoadQuery, RunQueryDsl};
use crate::query_source::Table;
use crate::result::QueryResult;
use crate::Connection;

#[derive(Debug, Clone, Copy, QueryId)]
#[must_use = "Queries are only executed when calling `load`, `get_result` or similar."]
Expand Down Expand Up @@ -231,3 +233,15 @@ impl<T, U> DeleteStatement<T, U, NoReturningClause> {
}
}
}

impl<T, U, Conn, S> LoadIntoDsl<Conn, S> for DeleteStatement<T, U, NoReturningClause>
where
Conn: Connection,
S: Selectable<Conn::Backend>,
S::SelectExpression: SelectableExpression<T>,
DeleteStatement<T, U, ReturningClause<S::SelectExpression>>: Query + LoadQuery<Conn, S>,
{
fn load_into(self, conn: &Conn) -> QueryResult<Vec<S>> {
self.returning(S::selection()).internal_load(conn)
}
}
17 changes: 15 additions & 2 deletions diesel/src/query_builder/insert_statement/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::fmt::{self, Debug, Display};
use std::marker::PhantomData;

use super::returning_clause::*;
use super::select_clause::Selectable;
use crate::backend::Backend;
use crate::expression::grouped::Grouped;
use crate::expression::operators::Eq;
Expand All @@ -18,14 +19,14 @@ use crate::insertable::*;
#[cfg(feature = "mysql")]
use crate::mysql::Mysql;
use crate::query_builder::*;
use crate::query_dsl::load_dsl::LoadIntoDsl;
#[cfg(feature = "sqlite")]
use crate::query_dsl::methods::ExecuteDsl;
use crate::query_dsl::RunQueryDsl;
use crate::query_dsl::{LoadQuery, RunQueryDsl};
use crate::query_source::{Column, Table};
use crate::result::QueryResult;
#[cfg(feature = "sqlite")]
use crate::sqlite::Sqlite;
#[cfg(feature = "sqlite")]
use crate::Connection;

/// The structure returned by [`insert_into`].
Expand Down Expand Up @@ -204,6 +205,18 @@ where
}
}

impl<T, U, Op, Conn, S> LoadIntoDsl<Conn, S> for InsertStatement<T, U, Op>
where
Conn: Connection,
S: Selectable<Conn::Backend>,
S::SelectExpression: SelectableExpression<T>,
InsertStatement<T, U, Op, ReturningClause<S::SelectExpression>>: Query + LoadQuery<Conn, S>,
{
fn load_into(self, conn: &Conn) -> QueryResult<Vec<S>> {
self.returning(S::selection()).internal_load(conn)
}
}

#[cfg(feature = "sqlite")]
impl<'a, T, U, Op, C> ExecuteDsl<C, Sqlite> for InsertStatement<T, BatchInsert<'a, U, T>, Op>
where
Expand Down
2 changes: 1 addition & 1 deletion diesel/src/query_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub use self::insert_statement::{
pub use self::query_id::QueryId;
#[doc(inline)]
pub use self::select_clause::{
IntoBoxedSelectClause, SelectClauseExpression, SelectClauseQueryFragment,
IntoBoxedSelectClause, SelectClauseExpression, SelectClauseQueryFragment, Selectable,
};
#[doc(hidden)]
pub use self::select_statement::{BoxedSelectStatement, SelectStatement};
Expand Down
29 changes: 29 additions & 0 deletions diesel/src/query_builder/select_clause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,35 @@ pub struct DefaultSelectClause;
#[derive(Debug, Clone, Copy, QueryId)]
pub struct SelectClause<T>(pub T);

/// This trait represents a type that can be used to construct a
/// select or returning clause via
/// [`RunQueryDsl::load_into`](crate::query_dsl::RunQueryDsl::load_into)
///
/// This trait [can be derived](derive@Selectable)
pub trait Selectable<DB: Backend> {
/// The type of the select expression
type SelectExpression: Expression;

/// Construct the select expression
fn selection() -> Self::SelectExpression;
}

impl<T, DB> Selectable<DB> for Option<T>
where
DB: Backend,
T: Selectable<DB>,
crate::expression::nullable::Nullable<T::SelectExpression>: Expression,
{
type SelectExpression = crate::expression::nullable::Nullable<T::SelectExpression>;

fn selection() -> Self::SelectExpression {
crate::expression::nullable::Nullable::new(T::selection())
}
}

#[doc(inline)]
pub use diesel_derives::Selectable;

/// Specialised variant of `Expression` for select clause types
///
/// The difference to the normal `Expression` trait is the query source (`QS`)
Expand Down
15 changes: 15 additions & 0 deletions diesel/src/query_builder/select_statement/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ use crate::query_builder::offset_clause::OffsetClause;
use crate::query_builder::order_clause::OrderClause;
use crate::query_builder::where_clause::*;
use crate::query_builder::*;
use crate::query_dsl::load_dsl::LoadIntoDsl;
use crate::query_dsl::methods::*;
use crate::query_dsl::*;
use crate::query_source::joins::*;
use crate::query_source::{QuerySource, Table};
use crate::result::QueryResult;
use crate::sql_types::{BigInt, BoolOrNullableBool, IntoNullable};
use crate::Connection;

#[allow(missing_debug_implementations)]
pub struct BoxedSelectStatement<'a, ST, QS, DB, GB = ()> {
Expand Down Expand Up @@ -401,6 +403,19 @@ where
}
}

impl<'a, Conn, U, ST, QS, DB, GB> LoadIntoDsl<Conn, U> for BoxedSelectStatement<'a, ST, QS, DB, GB>
where
DB: Backend,
Conn: Connection<Backend = DB>,
U: Selectable<DB>,
Self: SelectDsl<U::SelectExpression>,
crate::dsl::Select<Self, U::SelectExpression>: LoadQuery<Conn, U>,
{
fn load_into(self, conn: &Conn) -> crate::QueryResult<Vec<U>> {
<_ as SelectDsl<_>>::select(self, U::selection()).internal_load(conn)
}
}

#[cfg(test)]
mod tests {
use crate::prelude::*;
Expand Down
15 changes: 15 additions & 0 deletions diesel/src/query_builder/select_statement/dsl_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ use crate::query_builder::{
AsQuery, IntoBoxedClause, Query, QueryFragment, SelectQuery, SelectStatement,
};
use crate::query_dsl::boxed_dsl::BoxedDsl;
use crate::query_dsl::load_dsl::LoadIntoDsl;
use crate::query_dsl::methods::*;
use crate::query_dsl::order_dsl::ValidOrderingForDistinct;
use crate::query_dsl::*;
use crate::query_source::joins::{Join, JoinOn, JoinTo};
use crate::query_source::QuerySource;
use crate::sql_types::{BigInt, BoolOrNullableBool};
use crate::Connection;

impl<F, S, D, W, O, LOf, G, LC, Rhs, Kind, On> InternalJoinDsl<Rhs, Kind, On>
for SelectStatement<F, S, D, W, O, LOf, G, LC>
Expand Down Expand Up @@ -536,3 +538,16 @@ where
CombinationClause::new(Except, All, self, rhs.as_query())
}
}

impl<Conn, U, F, S, D, W, O, LOf, G, LC> LoadIntoDsl<Conn, U>
for SelectStatement<F, S, D, W, O, LOf, G, LC>
where
Conn: Connection,
U: Selectable<Conn::Backend>,
Self: SelectDsl<U::SelectExpression>,
crate::dsl::Select<Self, U::SelectExpression>: LoadQuery<Conn, U>,
{
fn load_into(self, conn: &Conn) -> crate::QueryResult<Vec<U>> {
<_ as SelectDsl<_>>::select(self, U::selection()).internal_load(conn)
}
}
17 changes: 16 additions & 1 deletion diesel/src/query_builder/update_statement/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ use crate::expression::{
use crate::query_builder::returning_clause::*;
use crate::query_builder::where_clause::*;
use crate::query_builder::*;
use crate::query_dsl::load_dsl::LoadIntoDsl;
use crate::query_dsl::methods::{BoxedDsl, FilterDsl};
use crate::query_dsl::RunQueryDsl;
use crate::query_dsl::{LoadQuery, RunQueryDsl};
use crate::query_source::Table;
use crate::result::Error::QueryBuilderError;
use crate::result::QueryResult;
use crate::Connection;

impl<T, U> UpdateStatement<T, U, SetNotCalled> {
pub(crate) fn new(target: UpdateTarget<T, U>) -> Self {
Expand Down Expand Up @@ -286,3 +288,16 @@ impl<T, U, V> UpdateStatement<T, U, V, NoReturningClause> {
/// Indicates that you have not yet called `.set` on an update statement
#[derive(Debug, Clone, Copy)]
pub struct SetNotCalled;

impl<T, U, V, Conn, S> LoadIntoDsl<Conn, S> for UpdateStatement<T, U, V>
where
Conn: Connection,
S: Selectable<Conn::Backend>,
T: Table,
S::SelectExpression: SelectableExpression<T>,
UpdateStatement<T, U, V, ReturningClause<S::SelectExpression>>: Query + LoadQuery<Conn, S>,
{
fn load_into(self, conn: &Conn) -> QueryResult<Vec<S>> {
self.returning(S::selection()).internal_load(conn)
}
}
15 changes: 15 additions & 0 deletions diesel/src/query_dsl/load_dsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::deserialize::FromSqlRow;
use crate::expression::QueryMetadata;
use crate::query_builder::{AsQuery, QueryFragment, QueryId};
use crate::result::QueryResult;
use crate::Table;

/// The `load` method
///
Expand All @@ -18,6 +19,10 @@ pub trait LoadQuery<Conn, U>: RunQueryDsl<Conn> {
fn internal_load(self, conn: &Conn) -> QueryResult<Vec<U>>;
}

pub trait LoadIntoDsl<Conn, U>: RunQueryDsl<Conn> {
fn load_into(self, conn: &Conn) -> QueryResult<Vec<U>>;
}

impl<Conn, T, U> LoadQuery<Conn, U> for T
where
Conn: Connection,
Expand All @@ -31,6 +36,16 @@ where
}
}

impl<Conn, T, U> LoadIntoDsl<Conn, U> for T
where
T: Table + AsQuery,
T::Query: LoadIntoDsl<Conn, U>,
{
fn load_into(self, conn: &Conn) -> QueryResult<Vec<U>> {
<_ as LoadIntoDsl<Conn, U>>::load_into(self.as_query(), conn)
}
}

/// The `execute` method
///
/// This trait should not be relied on directly by most apps. Its behavior is
Expand Down
Loading