Skip to content

Commit 39a7e05

Browse files
authored
Merge pull request diesel-rs#946 from diesel-rs/sg-refactor-operators
Refactor `infix_predicate!`, add a prefix version
2 parents 8cd340d + b13b74d commit 39a7e05

18 files changed

Lines changed: 446 additions & 315 deletions

File tree

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ for Rust libraries in [RFC #1105](https://github.com/rust-lang/rfcs/blob/master/
1616
[pg-network-0.13.1]: https://www.postgresql.org/docs/9.6/static/datatype-net-types.html
1717
[not-0.14.0]: http://docs.diesel.rs/diesel/expression/dsl/fn.not.html
1818

19+
* Added `diesel_prefix_operator!` which behaves identically to
20+
`diesel_postfix_operator!` (previously `postfix_predicate!`), but for
21+
operators like `NOT` which use prefix notation.
22+
23+
### Changed
24+
25+
* `infix_predicate!` and `infix_expression!` have been renamed to
26+
`diesel_infix_operator!`.
27+
28+
* `postfix_predicate!` and `postfix_expression!` have been renamed to
29+
`diesel_postfix_operator!`.
30+
1931
## [0.13.0] - 2017-05-15
2032

2133
### Added

diesel/src/expression/helper_types.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//! AsExpr<Rhs, Lhs>>`. Since we often need to return concrete types, instead of
33
//! a boxed trait object, these can be useful for writing concise return types.
44
use super::{Expression, AsExpression};
5+
use super::grouped::Grouped;
56
use types;
67

78
pub type SqlTypeOf<Expr> = <Expr as Expression>::SqlType;
@@ -10,11 +11,11 @@ pub type AsExprOf<Item, Type> = <Item as AsExpression<Type>>::Expression;
1011

1112
macro_rules! gen_helper_type {
1213
($name:ident) => {
13-
pub type $name<Lhs, Rhs> = super::predicates::$name<Lhs, AsExpr<Rhs, Lhs>>;
14+
pub type $name<Lhs, Rhs> = super::operators::$name<Lhs, AsExpr<Rhs, Lhs>>;
1415
};
1516

1617
($name:ident, $tpe:ident) => {
17-
pub type $name<Lhs, Rhs> = super::predicates::$name<
18+
pub type $name<Lhs, Rhs> = super::operators::$name<
1819
Lhs,
1920
<Rhs as AsExpression<types::$tpe>>::Expression,
2021
>;
@@ -31,15 +32,15 @@ gen_helper_type!(And, Bool);
3132
gen_helper_type!(Like, VarChar);
3233
gen_helper_type!(NotLike, VarChar);
3334

34-
pub type Between<Lhs, Rhs> = super::predicates::Between<Lhs,
35-
super::predicates::And<AsExpr<Rhs, Lhs>, AsExpr<Rhs, Lhs>>>;
36-
pub type NotBetween<Lhs, Rhs> = super::predicates::NotBetween<Lhs,
37-
super::predicates::And<AsExpr<Rhs, Lhs>, AsExpr<Rhs, Lhs>>>;
35+
pub type Between<Lhs, Rhs> = super::operators::Between<Lhs,
36+
super::operators::And<AsExpr<Rhs, Lhs>, AsExpr<Rhs, Lhs>>>;
37+
pub type NotBetween<Lhs, Rhs> = super::operators::NotBetween<Lhs,
38+
super::operators::And<AsExpr<Rhs, Lhs>, AsExpr<Rhs, Lhs>>>;
3839
/// The return type of `not(expr)`
39-
pub type Not<Expr> = super::not::Not<AsExprOf<Expr, types::Bool>>;
40+
pub type Not<Expr> = super::operators::Not<Grouped<AsExprOf<Expr, types::Bool>>>;
4041

4142
#[doc(inline)]
42-
pub use super::predicates::{IsNull, IsNotNull, Asc, Desc};
43+
pub use super::operators::{IsNull, IsNotNull, Asc, Desc};
4344
#[doc(inline)]
4445
pub use super::array_comparison::EqAny;
4546

diesel/src/expression/mod.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ mod not;
3939
pub mod nullable;
4040
#[doc(hidden)]
4141
#[macro_use]
42-
pub mod predicates;
42+
pub mod operators;
4343
pub mod sql_literal;
4444
mod unchecked_bind;
4545

@@ -64,11 +64,12 @@ pub use self::sql_literal::SqlLiteral;
6464

6565
use backend::Backend;
6666

67-
/// Represents a typed fragment of SQL. Apps should not need to implement this
68-
/// type directly, but it may be common to use this as type boundaries.
69-
/// Libraries should consider using
70-
/// [`infix_predicate!`](../macro.infix_predicate.html) or
71-
/// [`postfix_predicate!`](../macro.postfix_predicate.html) instead of
67+
/// Represents a typed fragment of SQL.
68+
///
69+
/// Apps should not need to implement this type directly, but it may be common
70+
/// to use this in where clauses. Libraries should consider using
71+
/// [`diesel_infix_operator!`](../macro.diesel_infix_operator.html) or
72+
/// [`diesel_postfix_operator!`](../macro.diesel_postfix_operator.html) instead of
7273
/// implementing this directly.
7374
pub trait Expression {
7475
type SqlType;

diesel/src/expression/not.rs

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use expression::*;
2-
use query_builder::*;
3-
use result::QueryResult;
1+
use expression::AsExpression;
2+
use expression::helper_types::Not;
3+
use expression::grouped::Grouped;
44
use types::Bool;
55

66
/// Creates a SQL `NOT` expression
@@ -31,42 +31,6 @@ use types::Bool;
3131
/// assert_eq!(Ok(2), users_not_with_name.first(&connection));
3232
/// # }
3333
/// ```
34-
pub fn not<T: AsExpression<Bool>>(expr: T) -> Not<T::Expression> {
35-
Not(expr.as_expression())
34+
pub fn not<T: AsExpression<Bool>>(expr: T) -> Not<T> {
35+
super::operators::Not::new(Grouped(expr.as_expression()))
3636
}
37-
38-
#[doc(hidden)]
39-
#[derive(Debug, Clone, Copy)]
40-
pub struct Not<T>(T);
41-
42-
impl<T: Expression<SqlType=Bool>> Expression for Not<T> {
43-
type SqlType = Bool;
44-
}
45-
46-
impl<T, QS> AppearsOnTable<QS> for Not<T> where
47-
T: AppearsOnTable<QS>,
48-
Not<T>: Expression,
49-
{
50-
}
51-
52-
impl<T, QS> SelectableExpression<QS> for Not<T> where
53-
T: SelectableExpression<QS>,
54-
Not<T>: AppearsOnTable<QS>,
55-
{
56-
}
57-
58-
impl<T: NonAggregate> NonAggregate for Not<T> {}
59-
60-
impl<T, DB> QueryFragment<DB> for Not<T> where
61-
DB: Backend,
62-
T: QueryFragment<DB>,
63-
{
64-
fn walk_ast(&self, mut out: AstPass<DB>) -> QueryResult<()> {
65-
out.push_sql("NOT (");
66-
self.0.walk_ast(out.reborrow())?;
67-
out.push_sql(")");
68-
Ok(())
69-
}
70-
}
71-
72-
impl_query_id!(Not<T>);

0 commit comments

Comments
 (0)