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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
use crate::expr::Expr;
use crate::table_expr::TableExpr;
use crate::item::Field;
use crate::item::Table;
use crate::item::Ident;
use crate::item::Order;
use crate::item::Sort;

/// Make an alias out of an expression.
///
/// # Examples
///
/// ```
/// use xql::as_field;
///
/// assert_eq!(as_field(1, "num").to_string(), "1 AS num");
/// ```
#[inline]
pub fn as_field<'a, E, A>(expr: E, alias: A) -> Field<'a>
where
    E: Into<Expr<'a>>,
    A: Into<Ident<'a>>,
{
    Field {
        expr: expr.into(),
        alias: Some(alias.into()),
    }
}

/// Make an alias out of a table expression.
///
/// # Examples
///
/// ```
/// use xql::as_table;
///
/// assert_eq!(as_table("user", "person").to_string(), "user AS person");
/// ```
#[inline]
pub fn as_table<'a, T, A>(table: T, alias: A) -> Table<'a>
where
    T: Into<TableExpr<'a>>,
    A: Into<Ident<'a>>,
{
    Table {
        table: table.into(),
        alias: Some(alias.into()),
    }
}

/// Group an expression with parenthesis.
///
/// # Examples
///
/// ```
/// use xql::{paren, not, and};
///
/// assert_eq!(not(paren(and(true, false))).to_string(), "NOT (true AND false)");
/// ```
#[inline]
pub fn paren<'a, E>(expr: E) -> Expr<'a>
where
    E: Into<Expr<'a>>,
{
    Expr::Paren(Box::new(expr.into()))
}

/// Make an ascending sort out of an expression.
///
/// # Examples
///
/// ```
/// use xql::asc;
///
/// assert_eq!(asc("id").to_string(), "id ASC");
/// assert_eq!(asc(("user", "id")).to_string(), "user.id ASC");
/// ```
#[inline]
pub fn asc<'a, E: Into<Expr<'a>>>(expr: E) -> Order<'a> {
    Order(expr.into(), Some(Sort::Asc))
}

/// Make a descending sort out of an expression.
///
/// # Examples
///
/// ```
/// use xql::desc;
///
/// assert_eq!(desc("id").to_string(), "id DESC");
/// assert_eq!(desc(("user", "id")).to_string(), "user.id DESC");
/// ```
#[inline]
pub fn desc<'a, E: Into<Expr<'a>>>(expr: E) -> Order<'a> {
    Order(expr.into(), Some(Sort::Desc))
}

/// Construct a binary operation on expression.
///
/// # Examples
///
/// ```
/// use xql::binop;
///
/// assert_eq!(binop(1, "+", 2).to_string(), "1 + 2");
/// ```
#[inline]
pub fn binop<'a, L, R>(left: L, op: &'static str, right: R) -> Expr<'a>
where
    L: Into<Expr<'a>>,
    R: Into<Expr<'a>>,
{
    Expr::Infix(Box::new(left.into()), op, Box::new(right.into()))
}

#[inline]
/// Construct a unary prefix operation on expression.
///
/// # Examples
///
/// ```
/// use xql::preop;
///
/// assert_eq!(preop("NOT", true).to_string(), "NOT true");
/// ```
pub fn preop<'a, E>(op: &'static str, expr: E) -> Expr<'a>
where
    E: Into<Expr<'a>>,
{
    Expr::Prefix(op, Box::new(expr.into()))
}

#[inline]
/// Construct a unary postfix operation on expression.
///
/// # Examples
///
/// ```
/// use xql::postop;
///
/// assert_eq!(postop("id", "ISNULL").to_string(), "id ISNULL");
/// ```
pub fn postop<'a, E>(expr: E, op: &'static str) -> Expr<'a>
where
    E: Into<Expr<'a>>,
{
    Expr::Postfix(Box::new(expr.into()), op)
}

macro_rules! generate_binop_funcs {
    ($trait:ident {$($(#[$comment:meta])* $name:ident: $op:expr),+}) => {
        $(
            $(#[$comment])*
            #[inline]
            pub fn $name<'a, L, R>(left: L, right: R) -> $crate::expr::Expr<'a>
            where L: Into<$crate::expr::Expr<'a>>,
                  R: Into<$crate::expr::Expr<'a>>,
            {
                crate::expr::Expr::Infix(
                    Box::new(left.into()),
                    $op,
                    Box::new(right.into()),
                )
            }
        )+
    };
}

generate_binop_funcs!(BinOps {
    /// Construct an `addition` operation between two expression.
    ///
    /// # Examples
    ///
    /// ```
    /// use xql::add;
    ///
    /// assert_eq!(add(1, 2).to_string(), "1 + 2");
    /// ```
    add: "+",
    /// Construct a `multiplication` operation between two expression.
    ///
    /// # Examples
    ///
    /// ```
    /// use xql::sub;
    ///
    /// assert_eq!(sub(1, 2).to_string(), "1 - 2");
    /// ```
    sub: "-",
    /// Construct a `division` operation between two expression.
    ///
    /// # Examples
    ///
    /// ```
    /// use xql::mul;
    ///
    /// assert_eq!(mul(1, 2).to_string(), "1 * 2");
    /// ```
    mul: "*",
    /// Construct a `modulo` operation between two expression.
    ///
    /// # Examples
    ///
    /// ```
    /// use xql::div;
    ///
    /// assert_eq!(div(1, 2).to_string(), "1 / 2");
    /// ```
    div: "/",
    /// Construct an `equal` comparison between two expression.
    ///
    /// # Examples
    ///
    /// ```
    /// use xql::rem;
    ///
    /// assert_eq!(rem(1, 2).to_string(), "1 % 2");
    /// ```
    rem: "%",
    /// Construct a `not equal` comparison between two expression.
    ///
    /// # Examples
    ///
    /// ```
    /// use xql::eq;
    ///
    /// assert_eq!(eq(1, 2).to_string(), "1 = 2");
    /// ```
    eq: "=",
    /// Construct a `greater than` comparison between two expression.
    ///
    /// # Examples
    ///
    /// ```
    /// use xql::ne;
    ///
    /// assert_eq!(ne(1, 2).to_string(), "1 <> 2");
    /// ```
    ne: "<>",
    /// Construct a `greater or equal` comparison between two expression.
    ///
    /// # Examples
    ///
    /// ```
    /// use xql::gt;
    ///
    /// assert_eq!(gt(1, 2).to_string(), "1 > 2");
    /// ```
    gt: ">",
    /// Construct a `less than` comparison between two expression.
    ///
    /// # Examples
    ///
    /// ```
    /// use xql::ge;
    ///
    /// assert_eq!(ge(1, 2).to_string(), "1 >= 2");
    /// ```
    ge: ">=",
    /// Construct a `less or equal` comparison between two expression.
    ///
    /// # Examples
    ///
    /// ```
    /// use xql::lt;
    ///
    /// assert_eq!(lt(1, 2).to_string(), "1 < 2");
    /// ```
    lt: "<",
    /// Construct a `less or equal` comparison between two expression.
    ///
    /// # Examples
    ///
    /// ```
    /// use xql::le;
    ///
    /// assert_eq!(le(1, 2).to_string(), "1 <= 2");
    /// ```
    le: "<=",
    /// Construct a `boolean and` operation between two expression.
    ///
    /// # Examples
    ///
    /// ```
    /// use xql::and;
    ///
    /// assert_eq!(and(true, false).to_string(), "true AND false");
    /// ```
    and: "AND",
    /// Construct a `boolean or` operation between two expression.
    ///
    /// # Examples
    ///
    /// ```
    /// use xql::or;
    ///
    /// assert_eq!(or(true, false).to_string(), "true OR false");
    /// ```
    or: "OR",
    /// Construct a `LIKE` operation between two expression.
    ///
    /// # Examples
    ///
    /// ```
    /// use xql::like;
    ///
    /// assert_eq!(like("name", &"%name".to_string()).to_string(), "name LIKE '%name'");
    /// ```
    like: "LIKE",
    /// Construct a `ILIKE` operation between two expression.
    ///
    /// # Examples
    ///
    /// ```
    /// use xql::ilike;
    ///
    /// assert_eq!(ilike("name", &"%name".to_string()).to_string(), "name ILIKE '%name'");
    /// ```
    ilike: "ILIKE"
});

/// Construct a `boolean not` operation on an expression.
///
/// # Examples
///
/// ```
/// use xql::not;
///
/// assert_eq!(not(true).to_string(), "NOT true");
/// ```
#[inline]
pub fn not<'a, E>(expr: E) -> Expr<'a>
where
    E: Into<Expr<'a>>,
{
    preop("NOT", expr)
}

/// Construct an `ISNULL` operation on an expression.
///
/// # Examples
///
/// ```
/// use xql::isnull;
///
/// assert_eq!(isnull(None::<i32>).to_string(), "null ISNULL");
/// ```
#[inline]
pub fn isnull<'a, E>(expr: E) -> Expr<'a>
where
    E: Into<Expr<'a>>,
{
    postop(expr, "ISNULL")
}

macro_rules! generate_join_funcs {
    ($(#[$comment:meta])* $join:ident $fn:ident) => {
        $(#[$comment])*
        #[inline]
        pub fn $fn<'a, L, R>(left: L, right: R) -> crate::table_expr::TableExpr<'a>
        where
            L: Into<crate::table_expr::TableExpr<'a>>,
            R: Into<crate::table_expr::TableExpr<'a>>,
        {
            crate::table_expr::TableExpr::$join(Box::new(left.into()), Box::new(right.into()))
        }
    };
    ($(#[$comment:meta])* $join:ident $fn:ident cond) => {
        $(#[$comment])*
        #[inline]
        pub fn $fn<'a, L, R, E>(left: L, right: R, cond: E) -> crate::table_expr::TableExpr<'a>
        where
            L: Into<crate::table_expr::TableExpr<'a>>,
            R: Into<crate::table_expr::TableExpr<'a>>,
            E: Into<crate::expr::Expr<'a>>,
        {
            crate::table_expr::TableExpr::$join(
                Box::new(left.into()),
                Box::new(right.into()),
                cond.into(),
            )
        }
    };
}

generate_join_funcs!(
    /// Construct a `JOIN` operation on a table expression.
    /// 
    /// # Examples
    /// 
    /// ```
    /// use xql::eq;
    /// use xql::join;
    /// 
    /// assert_eq!(
    ///     join("category", "book", eq(("category", "id"), ("book", "category_id"))).to_string(),
    ///     "category JOIN book ON category.id = book.category_id",
    /// );
    /// ```
    Join join cond);
generate_join_funcs!(
    /// Construct a `LEFT JOIN` operation on a table expression.
    /// 
    /// # Examples
    /// 
    /// ```
    /// use xql::eq;
    /// use xql::left_join;
    /// 
    /// assert_eq!(
    ///     left_join("category", "book", eq(("category", "id"), ("book", "category_id"))).to_string(),
    ///     "category LEFT JOIN book ON category.id = book.category_id",
    /// );
    /// ```
    LeftJoin left_join cond);
generate_join_funcs!(
    /// Construct a `RIGHT JOIN` operation on a table expression.
    /// 
    /// # Examples
    /// 
    /// ```
    /// use xql::eq;
    /// use xql::right_join;
    /// 
    /// assert_eq!(
    ///     right_join("category", "book", eq(("category", "id"), ("book", "category_id"))).to_string(),
    ///     "category RIGHT JOIN book ON category.id = book.category_id",
    /// );
    /// ```
    RightJoin right_join cond);
generate_join_funcs!(
    /// Construct a `FULL JOIN` operation on a table expression.
    /// 
    /// # Examples
    /// 
    /// ```
    /// use xql::eq;
    /// use xql::full_join;
    /// 
    /// assert_eq!(
    ///     full_join("category", "book", eq(("category", "id"), ("book", "category_id"))).to_string(),
    ///     "category FULL JOIN book ON category.id = book.category_id",
    /// );
    /// ```
    FullJoin full_join cond);
generate_join_funcs!(
    /// Construct a `NATURAL JOIN` operation on a table expression.
    /// 
    /// # Examples
    /// 
    /// ```
    /// use xql::natural_join;
    /// 
    /// assert_eq!(
    ///     natural_join("category", "book").to_string(),
    ///     "category NATURAL JOIN book",
    /// );
    /// ```
    NaturalJoin natural_join);
generate_join_funcs!(
    /// Construct a `NATURAL LEFT JOIN` operation on a table expression.
    /// 
    /// # Examples
    /// 
    /// ```
    /// use xql::natural_left_join;
    /// 
    /// assert_eq!(
    ///     natural_left_join("category", "book").to_string(),
    ///     "category NATURAL LEFT JOIN book",
    /// );
    /// ```
    NaturalLeftJoin natural_left_join);
generate_join_funcs!(
    /// Construct a `NATURAL RIGHT JOIN` operation on a table expression.
    /// 
    /// # Examples
    /// 
    /// ```
    /// use xql::natural_right_join;
    /// 
    /// assert_eq!(
    ///     natural_right_join("category", "book").to_string(),
    ///     "category NATURAL RIGHT JOIN book",
    /// );
    /// ```
    NaturalRightJoin natural_right_join);
generate_join_funcs!(
    /// Construct a `NATURAL FULL JOIN` operation on a table expression.
    /// 
    /// # Examples
    /// 
    /// ```
    /// use xql::natural_full_join;
    /// 
    /// assert_eq!(
    ///     natural_full_join("category", "book").to_string(),
    ///     "category NATURAL FULL JOIN book",
    /// );
    /// ```
    NaturalFullJoin natural_full_join);
generate_join_funcs!(
    /// Construct a `CROSS JOIN` operation on a table expression.
    /// 
    /// # Examples
    /// 
    /// ```
    /// use xql::cross_join;
    /// 
    /// assert_eq!(
    ///     cross_join("category", "book").to_string(),
    ///     "category CROSS JOIN book",
    /// );
    /// ```
    CrossJoin cross_join);