Skip to content

Commit 37c82db

Browse files
authored
Merge pull request diesel-rs#709 from diesel-rs/sg-selectable-expression-associated-type
Use associated types for `SelectableExpression`
2 parents 400ab94 + a4f49dd commit 37c82db

38 files changed

Lines changed: 252 additions & 207 deletions

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ for Rust libraries in [RFC #1105](https://github.com/rust-lang/rfcs/blob/master/
4646

4747
[transaction-0.11.0]: http://docs.diesel.rs/diesel/connection/trait.Connection.html#method.transaction
4848

49+
* The way tuples of columns from the right side of left outer joins interact
50+
with `.select` has changed. If you are deserializing into an option of a tuple
51+
(instead of a tuple of options), you will need to explicitly call
52+
`.nullable()`. (e.g. `.select(users::name, (posts::title,
53+
posts::body).nullable())`)
54+
4955
### Removed
5056

5157
* `result::TransactionError`

diesel/src/expression/aliased.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ impl<'a, Expr> Aliased<'a, Expr> {
2020
}
2121
}
2222

23-
#[derive(Debug, Copy, Clone)]
24-
pub struct FromEverywhere;
25-
2623
impl<'a, T> Expression for Aliased<'a, T> where
2724
T: Expression,
2825
{
@@ -57,7 +54,9 @@ impl<'a, T> QueryId for Aliased<'a, T> {
5754
// FIXME This is incorrect, should only be selectable from WithQuerySource
5855
impl<'a, T, QS> SelectableExpression<QS> for Aliased<'a, T> where
5956
Aliased<'a, T>: Expression,
57+
T: SelectableExpression<QS>,
6058
{
59+
type SqlTypeForSelect = T::SqlTypeForSelect;
6160
}
6261

6362
impl<'a, T: Expression + Copy> QuerySource for Aliased<'a, T> {

diesel/src/expression/array_comparison.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,15 @@ impl<T, U, QS> SelectableExpression<QS> for In<T, U> where
5757
T: SelectableExpression<QS>,
5858
U: SelectableExpression<QS>,
5959
{
60+
type SqlTypeForSelect = Self::SqlType;
6061
}
6162

6263
impl<T, U, QS> SelectableExpression<QS> for NotIn<T, U> where
6364
NotIn<T, U>: Expression,
6465
T: SelectableExpression<QS>,
6566
U: SelectableExpression<QS>,
6667
{
68+
type SqlTypeForSelect = Self::SqlType;
6769
}
6870

6971
impl<T, U> NonAggregate for In<T, U> where
@@ -163,9 +165,10 @@ pub trait MaybeEmpty {
163165
fn is_empty(&self) -> bool;
164166
}
165167

166-
impl<ST, S, F, W, O, L, Of> AsInExpression<ST>
167-
for SelectStatement<ST, S, F, W, O, L, Of> where
168-
Subselect<SelectStatement<ST, S, F, W, O, L, Of>, ST>: Expression<SqlType=ST>,
168+
impl<ST, S, F, W, O, L, Of, G> AsInExpression<ST>
169+
for SelectStatement<S, F, W, O, L, Of, G> where
170+
SelectStatement<S, F, W, O, L, Of, G>: Query<SqlType=ST>,
171+
Subselect<SelectStatement<S, F, W, O, L, Of>, ST>: Expression,
169172
{
170173
type InExpression = Subselect<Self, ST>;
171174

@@ -202,6 +205,7 @@ impl<T, QS> SelectableExpression<QS> for Many<T> where
202205
Many<T>: Expression,
203206
T: SelectableExpression<QS>,
204207
{
208+
type SqlTypeForSelect = T::SqlTypeForSelect;
205209
}
206210

207211
impl<T, DB> QueryFragment<DB> for Many<T> where
@@ -251,6 +255,7 @@ impl<T, ST, QS> SelectableExpression<QS> for Subselect<T, ST> where
251255
Subselect<T, ST>: Expression,
252256
T: Query,
253257
{
258+
type SqlTypeForSelect = ST;
254259
}
255260

256261
impl<T, ST, DB> QueryFragment<DB> for Subselect<T, ST> where

diesel/src/expression/bound.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ impl<T: QueryId, U> QueryId for Bound<T, U> {
6666
impl<T, U, QS> SelectableExpression<QS> for Bound<T, U> where
6767
Bound<T, U>: Expression,
6868
{
69+
type SqlTypeForSelect = T;
6970
}
7071

7172
impl<T, U> NonAggregate for Bound<T, U> where

diesel/src/expression/coerce.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ impl<T, ST> Expression for Coerce<T, ST> where
4141
impl<T, ST, QS> SelectableExpression<QS> for Coerce<T, ST> where
4242
T: SelectableExpression<QS>,
4343
{
44+
type SqlTypeForSelect = Self::SqlType;
4445
}
4546

4647
impl<T, ST, DB> QueryFragment<DB> for Coerce<T, ST> where

diesel/src/expression/count.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ impl<T: QueryFragment<DB>, DB: Backend> QueryFragment<DB> for Count<T> {
8080
impl_query_id!(Count<T>);
8181

8282
impl<T: Expression, QS> SelectableExpression<QS> for Count<T> {
83+
type SqlTypeForSelect = BigInt;
8384
}
8485

8586
#[derive(Debug, Clone, Copy)]
@@ -106,6 +107,7 @@ impl<DB: Backend> QueryFragment<DB> for CountStar {
106107
}
107108

108109
impl<QS> SelectableExpression<QS> for CountStar {
110+
type SqlTypeForSelect = BigInt;
109111
}
110112

111113
impl_query_id!(CountStar);

diesel/src/expression/exists.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ impl<T> Expression for Exists<T> where
5252
impl<T, QS> SelectableExpression<QS> for Exists<T> where
5353
Exists<T>: Expression,
5454
{
55+
type SqlTypeForSelect = Bool;
5556
}
5657

5758
impl<T> NonAggregate for Exists<T> {

diesel/src/expression/expression_methods/global_expression_methods.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ pub trait ExpressionMethods: Expression + Sized {
307307
/// # fn main() {
308308
/// # use self::users::dsl::*;
309309
/// # let order = "name";
310-
/// let ordering: Box<BoxableExpression<users, (), DB, SqlType=()>> =
310+
/// let ordering: Box<BoxableExpression<users, DB, SqlType=(), SqlTypeForSelect=()>> =
311311
/// if order == "name" {
312312
/// Box::new(name.desc())
313313
/// } else {

diesel/src/expression/functions/aggregate_folding.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,11 @@ macro_rules! fold_function {
5151

5252
impl_query_id!($type_name<T>);
5353

54-
impl<ST, T, QS> SelectableExpression<QS> for $type_name<T> where
55-
ST: Foldable,
56-
T: Expression<SqlType=ST>,
54+
impl<T, QS> SelectableExpression<QS> for $type_name<T> where
55+
$type_name<T>: Expression,
56+
T: SelectableExpression<QS>,
5757
{
58+
type SqlTypeForSelect = Self::SqlType;
5859
}
5960
}
6061
}

diesel/src/expression/functions/aggregate_ordering.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ macro_rules! ord_function {
5252

5353
impl<T, QS> SelectableExpression<QS> for $type_name<T> where
5454
$type_name<T>: Expression,
55+
T: SelectableExpression<QS>,
5556
{
57+
type SqlTypeForSelect = Self::SqlType;
5658
}
5759
}
5860
}

0 commit comments

Comments
 (0)