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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
use std::{fmt, rc};
use std::mem;
use std::marker;

use super::from;
use super::expression::{self, UntypedExpression};
use super::predicate::{self, ToOrPredicate, ToAndPredicate, ToExcludePredicate};
use super::sql;
use super::order_by;
use super::join;
use super::distinct;
use super::group_by;

#[derive(Clone, Debug)]
pub enum Select {
    Only(Vec<expression::SharedExpression>),
    All
}

pub trait AbstractSelectQuery: sql::ToSql {

}

pub trait ToSelectQuery: sql::ToSql {
    fn upcast(&self) -> SharedSelectQuery;
}

impl<T> ToSelectQuery for T where T: AbstractSelectQuery + Clone + 'static {
    fn upcast(&self) -> SharedSelectQuery {
        rc::Rc::new(Box::new(self.clone()))
    }
}

#[derive(Clone, Copy, Debug)]
pub enum SelectFor {
    Update,
    UpdateNoWait,
    Share,
    ShareNoWait
}

#[derive(Clone, Copy, Debug)]
pub struct LimitOne;

#[derive(Clone, Copy, Debug)]
pub struct LimitTwo;

#[derive(Clone, Copy, Debug)]
pub struct LimitMany;

#[derive(Clone, Copy, Debug)]
pub struct NoResult;

macro_rules! set_predicate {
    ($s:ident, $getter:ident, $setter:ident, $pr:expr, $w:ident, $new_pr:expr) => ({
        let mut query = $s.clone();
        match $s.$getter() {
            &Some(ref $w) => {
                query.$setter($new_pr);
            },
            &None => {
                query.$setter($pr);
            }
        }
        query
    })
}

macro_rules! predicate_trait {
    (
        $name:ident,
        $getter:ident,
        $setter:ident,
        $unset:ident,
        $implicit_and:ident,
        $explicit_and:ident,
        $or:ident,
        $exclude:ident,
        $and_exclude:ident,
        $or_exclude:ident
    ) => (

        pub trait $name: Clone {
            fn $getter(&self) -> &Option<predicate::SharedPredicate>;
            fn $setter(&mut self, predicate: predicate::SharedPredicate);
            fn $unset(&mut self);

            fn $implicit_and(&self, predicate: predicate::SharedPredicate) -> Self {
                set_predicate!(self, $getter, $setter, predicate, w, w.and(predicate))
            }

            fn $or(&self, predicate: predicate::SharedPredicate) -> Self {
                set_predicate!(self, $getter, $setter, predicate, w, w.or(predicate))
            }

            fn $explicit_and(&self, predicate: predicate::SharedPredicate) -> Self {
                self.$implicit_and(predicate)
            }

            fn $exclude(&self, predicate: predicate::SharedPredicate) -> Self {
               set_predicate!(self, $getter, $setter, predicate.exclude(), w, w.and(predicate.exclude()))
            }

            fn $and_exclude(&self, predicate: predicate::SharedPredicate) -> Self {
               self.$exclude(predicate)
            }

            fn $or_exclude(&self, predicate: predicate::SharedPredicate) -> Self {
               set_predicate!(self, $getter, $setter, predicate.exclude(), w, w.or(predicate.exclude()))
            }
        }

    )
}

predicate_trait!(
    Queryable,
    get_where,
    set_where,
    unset_where,
    where_,
    and,
    or,
    exclude,
    and_exclude,
    or_exclude
);

predicate_trait!(
    HasHaving,
    get_having,
    set_having,
    unset_having,
    having,
    and_having,
    or_having,
    exclude_having,
    and_exclude_having,
    or_exclude_having
);

pub trait Orderable: Clone {
    fn get_order_by_mut(&mut self) -> &mut Vec<order_by::OrderBy>;
    fn set_order_by(&mut self, order_by: Vec<order_by::OrderBy>);

    fn order_by(&self, field: &UntypedExpression) -> Self {
        with_clone!(self, query, query.set_order_by(
            vec![order_by::OrderBy::by(field)]
        ))
    }

    fn order_by_fields(&self, fields: &[&UntypedExpression]) -> Self {
        with_clone!(self, query, query.set_order_by(
            fields.iter().map(|f| order_by::OrderBy::by(*f)).collect()
        ))
    }

    fn reverse_by(&self, field: &UntypedExpression) -> Self {
        with_clone!(self, query, query.set_order_by(
            vec![order_by::OrderBy::reverse_by(field)]
        ))
    }

    fn reverse_by_fields(&self, fields: &[&UntypedExpression]) -> Self {
        with_clone!(self, query, query.set_order_by(
            fields.iter().map(|f| order_by::OrderBy::reverse_by(*f)).collect()
        ))
    }

    fn order_append(&self, field: &UntypedExpression) -> Self {
        with_clone!(self, query, query.get_order_by_mut().push(order_by::OrderBy::by(field)))
    }

    fn order_prepend(&self, field: &UntypedExpression) -> Self {
        with_clone!(self, query, query.get_order_by_mut().insert(0, order_by::OrderBy::by(field)))
    }

    fn reverse_append(&self, field: &UntypedExpression) -> Self {
        with_clone!(self, query, query.get_order_by_mut().push(order_by::OrderBy::reverse_by(field)))
    }

    fn reverse_prepend(&self, field: &UntypedExpression) -> Self {
        with_clone!(self, query, query.get_order_by_mut().insert(0, order_by::OrderBy::reverse_by(field)))
    }

    fn unorder(&self) -> Self {
        with_clone!(self, query, query.set_order_by(vec![]))
    }

}

#[derive(Clone, Debug)]
pub struct SelectQuery<T, L, M> {
    distinct: Option<distinct::Distinct>,
    select: Select,
    from: from::SharedFrom,
    joins: Vec<join::Join>,
    where_: Option<predicate::SharedPredicate>,
    group_by: Option<group_by::GroupBy>,
    having: Option<predicate::SharedPredicate>,
    limit: Option<usize>,
    offset: Option<usize>,
    order_by: Vec<order_by::OrderBy>,
    for_: Option<SelectFor>,

    _marker_t: marker::PhantomData<T>,
    _marker_l: marker::PhantomData<L>,
    _marker_m: marker::PhantomData<M>
}

impl<T, L, M> SelectQuery<T, L, M> {
    // GETTERS
    pub fn get_distinct(&self) -> &Option<distinct::Distinct> { &self.distinct }
    pub fn get_select(&self) -> &Select { &self.select }
    pub fn get_from(&self) -> &from::SharedFrom { &self.from }
    pub fn get_joins(&self) -> &Vec<join::Join> { &self.joins }
    pub fn get_where(&self) -> &Option<predicate::SharedPredicate> { &self.where_ }
    pub fn get_group_by(&self) -> &Option<group_by::GroupBy> { &self.group_by }
    pub fn get_having(&self) -> &Option<predicate::SharedPredicate> { &self.having }
    pub fn get_limit(&self) -> &Option<usize> { &self.limit }
    pub fn get_offset(&self) -> &Option<usize> { &self.offset }
    pub fn get_order_by(&self) -> &Vec<order_by::OrderBy> { &self.order_by }
    pub fn get_for(&self) -> &Option<SelectFor> { &self.for_ }
}



impl<T: Clone, L: Clone, M: Clone> SelectQuery<T, L, M> {

    // METHODS

    pub fn new(select: Select, from: from::SharedFrom) -> SelectQuery<T, L, M> {
        SelectQuery {
            distinct: None,
            select: select,
            from: from,
            joins: vec![],
            where_: None,
            group_by: None,
            having: None,
            limit: None,
            offset: None,
            order_by: vec![],
            for_: None,

            _marker_t: marker::PhantomData,
            _marker_l: marker::PhantomData,
            _marker_m: marker::PhantomData,
        }
    }

    pub fn distinct(&self, ) -> SelectQuery<T, L, M> {
        with_clone!(self, query, query.distinct = Some(distinct::Distinct::new()))
    }

    pub fn distinct_on(&self, fields: &[&UntypedExpression]) -> SelectQuery<T, L, M> {
        with_clone!(self, query, query.distinct = Some(distinct::Distinct::on(fields)))
    }

    pub fn group_by(&self, fields: &[&UntypedExpression]) -> SelectQuery<T, L, M> {
        with_clone!(self, query, query.group_by = Some(group_by::GroupBy::by(fields)))
    }

    pub fn limit(&self, limit: usize) -> SelectQuery<T, LimitOne, M> {
        let mut query = self.clone();
        query.limit = Some(limit);
        unsafe{ mem::transmute(query) }
    }

    pub fn first(&self) -> SelectQuery<T, LimitOne, M> {
        let mut query = self.clone();
        query.limit = Some(1);
        unsafe{ mem::transmute(query) }
    }

    pub fn offset(&self, offset: usize) -> SelectQuery<T, L, M> {
        with_clone!(self, query, query.offset = Some(offset))
    }

    pub fn alias(&self, alias: &str) -> from::FromSelect<T, L, M> {
        from::FromSelect { select: self.clone(), alias: alias.to_string() }
    }

    pub fn from_as(&self, alias: &str) -> from::FromSelect<T, L, M> {
        self.alias(alias)
    }

    pub fn for_update(&self) -> SelectQuery<T, L, M> {
        with_clone!(self, query, query.for_ = Some(SelectFor::Update))
    }

    pub fn for_update_nowait(&self) -> SelectQuery<T, L, M> {
        with_clone!(self, query, query.for_ = Some(SelectFor::UpdateNoWait))
    }

    pub fn for_share(&self) -> SelectQuery<T, L, M> {
        with_clone!(self, query, query.for_ = Some(SelectFor::Share))
    }

    pub fn for_share_nowait(&self) -> SelectQuery<T, L, M> {
        with_clone!(self, query, query.for_ = Some(SelectFor::ShareNoWait))
    }

    pub fn inner_join(&self, from: &from::From, on: predicate::SharedPredicate) -> SelectQuery<T, L, M> {
        with_clone!(self, query, query.joins.push(join::Join::inner_join(from.upcast_from(), on)))
    }

    pub fn full_outer_join(&self, from: &from::From, on: predicate::SharedPredicate) -> SelectQuery<T, L, M> {
        with_clone!(self, query, query.joins.push(join::Join::full_outer_join(from.upcast_from(), on)))
    }

    pub fn right_outer_join(&self, from: &from::From, on: predicate::SharedPredicate) -> SelectQuery<T, L, M> {
        with_clone!(self, query, query.joins.push(join::Join::right_outer_join(from.upcast_from(), on)))
    }

    pub fn left_outer_join(&self, from: &from::From, on: predicate::SharedPredicate) -> SelectQuery<T, L, M> {
        with_clone!(self, query, query.joins.push(join::Join::left_outer_join(from.upcast_from(), on)))
    }

    pub fn full_join(&self, from: &from::From, on: predicate::SharedPredicate) -> SelectQuery<T, L, M> {
        with_clone!(self, query, query.joins.push(join::Join::full_join(from.upcast_from(), on)))
    }

    pub fn left_join(&self, from: &from::From, on: predicate::SharedPredicate) -> SelectQuery<T, L, M> {
        with_clone!(self, query, query.joins.push(join::Join::left_join(from.upcast_from(), on)))
    }

    pub fn right_join(&self, from: &from::From, on: predicate::SharedPredicate) -> SelectQuery<T, L, M> {
        with_clone!(self, query, query.joins.push(join::Join::right_join(from.upcast_from(), on)))
    }

    pub fn natural_join(&self, from: &from::From) -> SelectQuery<T, L, M> {
        with_clone!(self, query, query.joins.push(join::Join::natural_join(from.upcast_from())))
    }

    pub fn natural_left_join(&self, from: &from::From) -> SelectQuery<T, L, M> {
        with_clone!(self, query, query.joins.push(join::Join::natural_left_join(from.upcast_from())))
    }

    pub fn natural_right_join(&self, from: &from::From) -> SelectQuery<T, L, M> {
        with_clone!(self, query, query.joins.push(join::Join::natural_right_join(from.upcast_from())))
    }

    pub fn natural_full_join(&self, from: &from::From) -> SelectQuery<T, L, M> {
        with_clone!(self, query, query.joins.push(join::Join::natural_full_join(from.upcast_from())))
    }

    pub fn cross_join(&self, from: &from::From) -> SelectQuery<T, L, M> {
        with_clone!(self, query, query.joins.push(join::Join::cross_join(from.upcast_from())))
    }

    pub fn unjoin(&self) -> SelectQuery<T, L, M> {
        with_clone!(self, query, query.joins = vec![])
    }
}

pub trait Selectable<M: Clone>: from::From {
    // FIXME: Unify select_N after [generics](https://github.com/rust-lang/rfcs/issues/376)

    fn select_1<T: Clone>(&self, field: &expression::Expression<T>) -> SelectQuery<(T,), LimitMany, M> {
        SelectQuery::new(Select::Only(vec![field.upcast_expression()]), self.upcast_from())
    }

    fn select_2<T1: Clone, T2: Clone>(&self, field1: &expression::Expression<T1>, field2: &expression::Expression<T2>) -> SelectQuery<(T1, T2), LimitMany, M> {
        SelectQuery::new(Select::Only(vec![field1.upcast_expression(), field2.upcast_expression()]), self.upcast_from())
    }

    fn select(&self, fields: &[&UntypedExpression]) -> SelectQuery<(), LimitMany, M> {
        SelectQuery::new(Select::Only(fields.iter().map(|f| f.upcast_expression()).collect()), self.upcast_from())
    }

    fn select_all(&self) -> SelectQuery<(), LimitMany, M> {
        SelectQuery::new(Select::All, self.upcast_from())
    }

    fn exists(&self) -> SelectQuery<(), LimitMany, M> {
        SelectQuery::new(Select::Only(vec![expression::RawExpression::new("1").upcast_expression()]), self.upcast_from())
    }
}

impl<T: Clone, L: Clone, M: Clone> Queryable for SelectQuery<T, L, M> {
    fn get_where(&self) -> &Option<predicate::SharedPredicate> { &self.where_ }
    fn set_where(&mut self, predicate: predicate::SharedPredicate) { self.where_ = Some(predicate); }
    fn unset_where(&mut self) { self.where_ = None; }
}

impl<T: Clone, L: Clone, M: Clone> HasHaving for SelectQuery<T, L, M> {
    fn get_having(&self) -> &Option<predicate::SharedPredicate> { &self.having }
    fn set_having(&mut self, predicate: predicate::SharedPredicate) { self.having = Some(predicate); }
    fn unset_having(&mut self) { self.having = None; }
}

impl<T: Clone, L: Clone, M: Clone> Orderable for SelectQuery<T, L, M> {
    fn get_order_by_mut(&mut self) -> &mut Vec<order_by::OrderBy> { &mut self.order_by }
    fn set_order_by(&mut self, order_by: Vec<order_by::OrderBy>) { self.order_by = order_by }
}

impl<T: Clone, L: Clone, M: Clone> AbstractSelectQuery for SelectQuery<T, L, M> { }

pub type BoxedSelectQuery = Box<AbstractSelectQuery + 'static>;
pub type SharedSelectQuery = rc::Rc<BoxedSelectQuery>;

impl<T: Clone + 'static + fmt::Debug, L: Clone + 'static + fmt::Debug, M: Clone + 'static + fmt::Debug> UntypedExpression for SelectQuery<T, L, M> {
    fn expression_as_sql(&self) -> &sql::ToSql {
                self
    }

    fn upcast_expression(&self) -> expression::SharedExpression {
        rc::Rc::new(Box::new(self.clone()) as expression::BoxedExpression)
    }
}

impl<M: Clone + 'static + fmt::Debug, T: Clone + 'static + fmt::Debug> expression::Expression<T> for SelectQuery<(T,), LimitOne, M> { }
impl<M: Clone + 'static + fmt::Debug, T: Clone + 'static + fmt::Debug> expression::ListExpression<T> for SelectQuery<(T,), LimitMany, M> { }

impl<M: Clone + 'static + fmt::Debug, T: Clone + 'static + fmt::Debug> expression::ToExpression<T> for SelectQuery<(T,), LimitOne, M> { }
impl<M: Clone + 'static + fmt::Debug, T: Clone + 'static + fmt::Debug> expression::ToListExpression<T> for SelectQuery<(T,), LimitMany, M> { }