Skip to content

Commit bfdddef

Browse files
authored
Merge pull request diesel-rs#2068 from diesel-rs/sg-nullable-expr-methods
Implement all expression method traits for nullable types
2 parents 3fa88b4 + 4889564 commit bfdddef

4 files changed

Lines changed: 40 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ for Rust libraries in [RFC #1105](https://github.com/rust-lang/rfcs/blob/master/
1515
* Added `DatabaseErrorKind::ReadOnlyTransaction` to allow applications to
1616
handle errors caused by writing when only allowed to read.
1717

18+
* All expression methods can now be called on expressions of nullable types.
19+
1820
### Removed
1921

2022
* All previously deprecated items have been removed.

diesel/src/expression_methods/bool_expression_methods.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use expression::grouped::Grouped;
22
use expression::operators::{And, Or};
33
use expression::{AsExpression, Expression};
4-
use sql_types::Bool;
4+
use sql_types::{Bool, Nullable};
55

66
/// Methods present on boolean expressions
7-
pub trait BoolExpressionMethods: Expression<SqlType = Bool> + Sized {
7+
pub trait BoolExpressionMethods: Expression + Sized {
88
/// Creates a SQL `AND` expression
99
///
1010
/// # Example
@@ -37,8 +37,8 @@ pub trait BoolExpressionMethods: Expression<SqlType = Bool> + Sized {
3737
/// assert_eq!(expected, data);
3838
/// # Ok(())
3939
/// # }
40-
fn and<T: AsExpression<Bool>>(self, other: T) -> And<Self, T::Expression> {
41-
And::new(self.as_expression(), other.as_expression())
40+
fn and<T: AsExpression<Self::SqlType>>(self, other: T) -> And<Self, T::Expression> {
41+
And::new(self, other.as_expression())
4242
}
4343

4444
/// Creates a SQL `OR` expression
@@ -79,9 +79,23 @@ pub trait BoolExpressionMethods: Expression<SqlType = Bool> + Sized {
7979
/// assert_eq!(expected, data);
8080
/// # Ok(())
8181
/// # }
82-
fn or<T: AsExpression<Bool>>(self, other: T) -> Grouped<Or<Self, T::Expression>> {
82+
fn or<T: AsExpression<Self::SqlType>>(self, other: T) -> Grouped<Or<Self, T::Expression>> {
8383
Grouped(Or::new(self, other.as_expression()))
8484
}
8585
}
8686

87-
impl<T: Expression<SqlType = Bool>> BoolExpressionMethods for T {}
87+
impl<T> BoolExpressionMethods for T
88+
where
89+
T: Expression,
90+
T::SqlType: BoolOrNullableBool,
91+
{
92+
}
93+
94+
#[doc(hidden)]
95+
/// Marker trait used to implement `BoolExpressionMethods` on the appropriate
96+
/// types. Once coherence takes associated types into account, we can remove
97+
/// this trait.
98+
pub trait BoolOrNullableBool {}
99+
100+
impl BoolOrNullableBool for Bool {}
101+
impl BoolOrNullableBool for Nullable<Bool> {}

diesel/src/pg/expression/date_and_time.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ use expression::Expression;
22
use pg::Pg;
33
use query_builder::*;
44
use result::QueryResult;
5-
use sql_types::{Date, Timestamp, Timestamptz, VarChar};
5+
use sql_types::{Date, NotNull, Nullable, Timestamp, Timestamptz, VarChar};
66

77
/// Marker trait for types which are valid in `AT TIME ZONE` expressions
88
pub trait DateTimeLike {}
99
impl DateTimeLike for Date {}
1010
impl DateTimeLike for Timestamp {}
1111
impl DateTimeLike for Timestamptz {}
12+
impl<T: NotNull + DateTimeLike> DateTimeLike for Nullable<T> {}
1213

1314
#[derive(Debug, Copy, Clone, QueryId, NonAggregate)]
1415
pub struct AtTimeZone<Ts, Tz> {

diesel/src/pg/expression/expression_methods.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ pub trait PgTimestampExpressionMethods: Expression + Sized {
138138
impl<T: Expression> PgTimestampExpressionMethods for T where T::SqlType: DateTimeLike {}
139139

140140
/// PostgreSQL specific methods present on array expressions.
141-
pub trait PgArrayExpressionMethods<ST>: Expression<SqlType = Array<ST>> + Sized {
141+
pub trait PgArrayExpressionMethods: Expression + Sized {
142142
/// Creates a PostgreSQL `&&` expression.
143143
///
144144
/// This operator returns whether two arrays have common elements.
@@ -300,7 +300,21 @@ pub trait PgArrayExpressionMethods<ST>: Expression<SqlType = Array<ST>> + Sized
300300
}
301301
}
302302

303-
impl<T, ST> PgArrayExpressionMethods<ST> for T where T: Expression<SqlType = Array<ST>> {}
303+
impl<T> PgArrayExpressionMethods for T
304+
where
305+
T: Expression,
306+
T::SqlType: ArrayOrNullableArray,
307+
{
308+
}
309+
310+
#[doc(hidden)]
311+
/// Marker trait used to implement `ArrayExpressionMethods` on the appropriate
312+
/// types. Once coherence takes associated types into account, we can remove
313+
/// this trait.
314+
pub trait ArrayOrNullableArray {}
315+
316+
impl<T> ArrayOrNullableArray for Array<T> {}
317+
impl<T> ArrayOrNullableArray for Nullable<Array<T>> {}
304318

305319
use expression::operators::{Asc, Desc};
306320

0 commit comments

Comments
 (0)