1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#[macro_export]
#[doc(hidden)]
macro_rules! infix_predicate_body {
    ($name:ident, $operator:expr, $return_type:ty) => {
        #[derive(Debug, Clone, Copy)]
        pub struct $name<T, U> {
            left: T,
            right: U,
        }

        impl<T, U> $name<T, U> {
            pub fn new(left: T, right: U) -> Self {
                $name {
                    left: left,
                    right: right,
                }
            }
        }

        impl_query_id!($name<T, U>);

        impl<T, U> $crate::expression::Expression for $name<T, U> where
            T: $crate::expression::Expression,
            U: $crate::expression::Expression,
        {
            type SqlType = $return_type;
        }

        impl<T, U, QS> $crate::expression::SelectableExpression<QS> for $name<T, U> where
            T: $crate::expression::SelectableExpression<QS>,
            U: $crate::expression::SelectableExpression<QS>,
        {
            type SqlTypeForSelect = Self::SqlType;
        }

        impl<T, U> $crate::expression::NonAggregate for $name<T, U> where
            T: $crate::expression::NonAggregate,
            U: $crate::expression::NonAggregate,
        {
        }
    }
}

#[macro_export]
#[doc(hidden)]
macro_rules! global_infix_predicate_to_sql {
    ($name:ident, $operator:expr) => {
        impl<T, U, DB> $crate::query_builder::QueryFragment<DB> for $name<T, U> where
            DB: $crate::backend::Backend,
            T: $crate::query_builder::QueryFragment<DB>,
            U: $crate::query_builder::QueryFragment<DB>,
        {
            fn to_sql(&self, out: &mut DB::QueryBuilder) -> $crate::query_builder::BuildQueryResult {
                try!(self.left.to_sql(out));
                out.push_sql($operator);
                self.right.to_sql(out)
            }

            fn collect_binds(&self, out: &mut DB::BindCollector) -> $crate::result::QueryResult<()> {
                try!(self.left.collect_binds(out));
                try!(self.right.collect_binds(out));
                Ok(())
            }

            fn is_safe_to_cache_prepared(&self) -> bool {
                self.left.is_safe_to_cache_prepared() &&
                    self.right.is_safe_to_cache_prepared()
            }
        }
    }
}

#[macro_export]
#[doc(hidden)]
macro_rules! backend_specific_infix_predicate_to_sql {
    ($name:ident, $operator:expr, $backend:ty) => {
        impl<T, U> $crate::query_builder::QueryFragment<$backend> for $name<T, U> where
            T: $crate::query_builder::QueryFragment<$backend>,
            U: $crate::query_builder::QueryFragment<$backend>,
        {
            fn to_sql(&self, out: &mut <$backend as $crate::backend::Backend>::QueryBuilder)
                -> $crate::query_builder::BuildQueryResult
            {
                use $crate::query_builder::QueryBuilder;
                try!(self.left.to_sql(out));
                out.push_sql($operator);
                self.right.to_sql(out)
            }

            fn collect_binds(&self, out: &mut <$backend as $crate::backend::Backend>::BindCollector)
                -> $crate::result::QueryResult<()>
            {
                try!(self.left.collect_binds(out));
                try!(self.right.collect_binds(out));
                Ok(())
            }

            fn is_safe_to_cache_prepared(&self) -> bool {
                self.left.is_safe_to_cache_prepared() &&
                    self.right.is_safe_to_cache_prepared()
            }
        }

        impl<T, U> $crate::query_builder::QueryFragment<$crate::backend::Debug>
            for $name<T, U> where
                T: $crate::query_builder::QueryFragment<$crate::backend::Debug>,
                U: $crate::query_builder::QueryFragment<$crate::backend::Debug>,
        {
            fn to_sql(
                &self,
                out: &mut <$crate::backend::Debug as $crate::backend::Backend>::QueryBuilder,
            ) -> $crate::query_builder::BuildQueryResult {
                use $crate::query_builder::QueryBuilder;
                try!(self.left.to_sql(out));
                out.push_sql($operator);
                self.right.to_sql(out)
            }

            fn collect_binds(
                &self,
                out: &mut <$crate::backend::Debug as $crate::backend::Backend>::BindCollector,
            ) -> $crate::result::QueryResult<()> {
                try!(self.left.collect_binds(out));
                try!(self.right.collect_binds(out));
                Ok(())
            }

            fn is_safe_to_cache_prepared(&self) -> bool {
                self.left.is_safe_to_cache_prepared() &&
                    self.right.is_safe_to_cache_prepared()
            }
        }
    }
}

#[macro_export]
/// Useful for libraries adding support for new SQL types. Apps should never
/// need to call this
///
/// # Example
///
/// ```ignore
/// infix_predicate!(Matches, " @@ ");
/// infix_predicate!(Concat, " || ", TsVector);
/// infix_predicate!(And, " && ", TsQuery);
/// infix_predicate!(Or, " || ", TsQuery);
/// infix_predicate!(Contains, " @> ");
/// infix_predicate!(ContainedBy, " @> ");
/// ```
macro_rules! infix_predicate {
    ($name:ident, $operator:expr) => {
        infix_predicate!($name, $operator, $crate::types::Bool);
    };

    ($name:ident, $operator:expr, $return_type:ty) => {
        global_infix_predicate_to_sql!($name, $operator);
        infix_predicate_body!($name, $operator, $return_type);
    };

    ($name:ident, $operator:expr, backend: $backend:ty) => {
        infix_predicate!($name, $operator, $backend, $crate::types::Bool);
    };

    ($name:ident, $operator:expr, $backend:ty, $return_type:ty) => {
        backend_specific_infix_predicate_to_sql!($name, $operator, $backend);
        infix_predicate_body!($name, $operator, $return_type);
    };
}

/// Alias for `infix_predicate!`
macro_rules! infix_expression {
    ($($args:tt)*) => { infix_predicate!($($args)*); }
}

#[macro_export]
#[doc(hidden)]
macro_rules! postfix_predicate_body {
    ($name:ident, $operator:expr, $return_type:ty) => {
        #[derive(Debug, Clone, Copy)]
        pub struct $name<T> {
            expr: T,
        }

        impl<T> $name<T> {
            pub fn new(expr: T) -> Self {
                $name {
                    expr: expr,
                }
            }
        }

        impl_query_id!($name<T>);

        impl<T> $crate::expression::Expression for $name<T> where
            T: $crate::expression::Expression,
        {
            type SqlType = $return_type;
        }

        impl<T, DB> $crate::query_builder::QueryFragment<DB> for $name<T> where
            DB: $crate::backend::Backend,
            T: $crate::query_builder::QueryFragment<DB>,
        {
            fn to_sql(&self, out: &mut DB::QueryBuilder) -> $crate::query_builder::BuildQueryResult {
                try!(self.expr.to_sql(out));
                out.push_sql($operator);
                Ok(())
            }

            fn collect_binds(&self, out: &mut DB::BindCollector) -> $crate::result::QueryResult<()> {
                try!(self.expr.collect_binds(out));
                Ok(())
            }

            fn is_safe_to_cache_prepared(&self) -> bool {
                self.expr.is_safe_to_cache_prepared()
            }
        }

        impl<T, QS> $crate::expression::SelectableExpression<QS> for $name<T> where
            T: $crate::expression::SelectableExpression<QS>,
        {
            type SqlTypeForSelect = Self::SqlType;
        }

        impl<T> $crate::expression::NonAggregate for $name<T> where
            T: $crate::expression::NonAggregate,
        {
        }
    }
}

#[macro_export]
/// Useful for libraries adding support for new SQL types. Apps should never
/// need to call this.
macro_rules! postfix_predicate {
    ($name:ident, $operator:expr) => {
        postfix_expression!($name, $operator, $crate::types::Bool);
    };
}

#[macro_export]
macro_rules! postfix_expression {
    ($name:ident, $operator:expr, $return_type:ty) => {
        postfix_predicate_body!($name, $operator, $return_type);
    }
}

infix_predicate!(And, " AND ");
infix_predicate!(Between, " BETWEEN ");
infix_predicate!(Escape, " ESCAPE ");
infix_predicate!(Eq, " = ");
infix_predicate!(Gt, " > ");
infix_predicate!(GtEq, " >= ");
infix_predicate!(Like, " LIKE ");
infix_predicate!(Lt, " < ");
infix_predicate!(LtEq, " <= ");
infix_predicate!(NotBetween, " NOT BETWEEN ");
infix_predicate!(NotEq, " != ");
infix_predicate!(NotLike, " NOT LIKE ");
infix_predicate!(Or, " OR ");

postfix_predicate!(IsNull, " IS NULL");
postfix_predicate!(IsNotNull, " IS NOT NULL");
postfix_expression!(Asc, " ASC", ());
postfix_expression!(Desc, " DESC", ());

use backend::Backend;
use query_source::Column;
use query_builder::*;
use result::QueryResult;
use super::SelectableExpression;

impl<T, U, DB> Changeset<DB> for Eq<T, U> where
    DB: Backend,
    T: Column,
    U: SelectableExpression<T::Table> + QueryFragment<DB>,
{
    fn is_noop(&self) -> bool {
        false
    }

    fn to_sql(&self, out: &mut DB::QueryBuilder) -> BuildQueryResult {
        try!(out.push_identifier(T::name()));
        out.push_sql(" = ");
        QueryFragment::to_sql(&self.right, out)
    }

    fn collect_binds(&self, out: &mut DB::BindCollector) -> QueryResult<()> {
        QueryFragment::collect_binds(&self.right, out)
    }
}

impl<T, U> AsChangeset for Eq<T, U> where
    T: Column,
    U: SelectableExpression<T::Table>,
{
    type Target = T::Table;
    type Changeset = Self;

    fn as_changeset(self) -> Self {
        self
    }
}