Skip to content

Commit

Permalink
Preparations to allow boxed dynamic select clauses
Browse files Browse the repository at this point in the history
  • Loading branch information
weiznich committed Jan 3, 2020
1 parent 924710b commit 5a93ca6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 34 deletions.
4 changes: 3 additions & 1 deletion diesel/src/query_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ pub use self::insert_statement::{
};
pub use self::query_id::QueryId;
#[doc(inline)]
pub use self::select_clause::{SelectClauseExpression, SelectClauseQueryFragment};
pub use self::select_clause::{
IntoBoxedSelectClause, SelectClauseExpression, SelectClauseQueryFragment,
};
#[doc(hidden)]
pub use self::select_statement::{BoxedSelectStatement, SelectStatement};
pub use self::sql_query::{BoxedSqlQuery, SqlQuery};
Expand Down
38 changes: 38 additions & 0 deletions diesel/src/query_builder/select_clause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,41 @@ where
source.default_selection().walk_ast(pass)
}
}

/// An internal helper trait to convert different select clauses
/// into their boxed counter part.
///
/// You normally don't need this trait, at least as long as you
/// don't implement your own select clause representation
pub trait IntoBoxedSelectClause<'a, DB, QS> {
/// The sql type of the select clause
type SqlType;

/// Convert the select clause into a the boxed representation
fn into_boxed(self, source: &QS) -> Box<dyn QueryFragment<DB> + 'a>;
}

impl<'a, DB, T, QS> IntoBoxedSelectClause<'a, DB, QS> for SelectClause<T>
where
T: QueryFragment<DB> + SelectableExpression<QS> + 'a,
DB: Backend,
{
type SqlType = T::SqlType;

fn into_boxed(self, _source: &QS) -> Box<dyn QueryFragment<DB> + 'a> {
Box::new(self.0)
}
}

impl<'a, DB, QS> IntoBoxedSelectClause<'a, DB, QS> for DefaultSelectClause
where
QS: QuerySource,
QS::DefaultSelection: QueryFragment<DB> + 'a,
DB: Backend,
{
type SqlType = <QS::DefaultSelection as Expression>::SqlType;

fn into_boxed(self, source: &QS) -> Box<dyn QueryFragment<DB> + 'a> {
Box::new(source.default_selection())
}
}
36 changes: 3 additions & 33 deletions diesel/src/query_builder/select_statement/dsl_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,12 +339,11 @@ impl<F, S, D, W, O, L, Of, G, LC, LM, Modifier> ModifyLockDsl<Modifier>
}
}

impl<'a, F, S, D, W, O, L, Of, G, DB> BoxedDsl<'a, DB>
for SelectStatement<F, SelectClause<S>, D, W, O, L, Of, G>
impl<'a, F, S, D, W, O, L, Of, G, DB> BoxedDsl<'a, DB> for SelectStatement<F, S, D, W, O, L, Of, G>
where
Self: AsQuery,
DB: Backend,
S: QueryFragment<DB> + SelectableExpression<F> + 'a,
S: IntoBoxedSelectClause<'a, DB, F>,
D: QueryFragment<DB> + 'a,
W: Into<BoxedWhereClause<'a, DB>>,
O: Into<Option<Box<dyn QueryFragment<DB> + 'a>>>,
Expand All @@ -356,36 +355,7 @@ where

fn internal_into_boxed(self) -> Self::Output {
BoxedSelectStatement::new(
Box::new(self.select.0),
self.from,
Box::new(self.distinct),
self.where_clause.into(),
self.order.into(),
Box::new(self.limit),
Box::new(self.offset),
Box::new(self.group_by),
)
}
}

impl<'a, F, D, W, O, L, Of, G, DB> BoxedDsl<'a, DB>
for SelectStatement<F, DefaultSelectClause, D, W, O, L, Of, G>
where
Self: AsQuery,
DB: Backend,
F: QuerySource,
F::DefaultSelection: QueryFragment<DB> + 'a,
D: QueryFragment<DB> + 'a,
W: Into<BoxedWhereClause<'a, DB>>,
O: Into<Option<Box<dyn QueryFragment<DB> + 'a>>>,
L: QueryFragment<DB> + 'a,
Of: QueryFragment<DB> + 'a,
G: QueryFragment<DB> + 'a,
{
type Output = BoxedSelectStatement<'a, <F::DefaultSelection as Expression>::SqlType, F, DB>;
fn internal_into_boxed(self) -> Self::Output {
BoxedSelectStatement::new(
Box::new(self.from.default_selection()),
self.select.into_boxed(&self.from),
self.from,
Box::new(self.distinct),
self.where_clause.into(),
Expand Down

0 comments on commit 5a93ca6

Please sign in to comment.