tideorm-sql-builder 0.1.1

SQL builder for TideORM
Documentation
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
 TideORM's SQL builder, re-exported by the engine as `tideorm::orm::sql_builder`.

 ## Usage

 Table of Content

 1. Basics

     1. [Iden]#iden
     1. [Expression]#expression
     1. [Condition]#condition
     1. [Statement Builders]#statement-builders

 1. Query Statement

     1. [Query Select]#query-select
     1. [Query Insert]#query-insert
     1. [Query Update]#query-update
     1. [Query Delete]#query-delete

 1. Advanced
     1. [Aggregate Functions]#aggregate-functions
     1. [Casting]#casting
     1. [Custom Function]#custom-function

 ## Motivation

 Why would you want to use a dynamic query builder?

 ### 1. Parameter bindings

 One of the headaches when using raw SQL is parameter binding. Here you can inject parameters
 right alongside the expression, and the $N sequencing will be handled for you. No more "off by one" errors!

 ```
 # use sql_builder::*;
 assert_eq!(
     Query::select()
         .expr(Expr::col("size_w").add(1).mul(2))
         .from("glyph")
         .and_where(Expr::col("image").like("A"))
         .and_where(Expr::col("id").is_in([3, 4, 5]))
         .build(PostgresQueryBuilder).unwrap(),
     (
         r#"SELECT ("size_w" + $1) * $2 FROM "glyph" WHERE "image" LIKE $3 AND "id" IN ($4, $5, $6)"#
             .to_owned(),
         Values(vec![
             1.into(),
             2.into(),
             "A".to_owned().into(),
             3.into(),
             4.into(),
             5.into(),
         ])
     )
 );
 ```

 If you need an "escape hatch" to construct complex queries, you can use custom expressions,
 and still have the benefit of sequentially-binded parameters.

 ```
 # use sql_builder::*;
 assert_eq!(
     Query::select()
         .columns(["size_w", "size_h"])
         .from("character")
         .and_where(Expr::col("id").eq(1)) // this is $1
         // custom expressions only need to define local parameter sequence.
         // its global sequence will be re-written.
         // here, we flip the order of $2 & $1 to make it look tricker!
         .and_where(Expr::cust_with_values(r#""size_w" = $2 * $1"#, [3, 2], &PostgresQueryBuilder).unwrap())
         .and_where(Expr::col("size_h").gt(4)) // this is $N?
         .build(PostgresQueryBuilder).unwrap(),
     (
         r#"SELECT "size_w", "size_h" FROM "character" WHERE "id" = $1 AND ("size_w" = $2 * $3) AND "size_h" > $4"#
             .to_owned(),
         Values(vec![1.into(), 2.into(), 3.into(), 4.into()])
     )
 );
 ```

 ### 2. Dynamic query

 You can construct the query at runtime based on user inputs with a fluent interface,
 so you don't have to append `WHERE` or `AND` conditionally.

 ```
 # use sql_builder::*;
 fn query(a: Option<i32>, b: Option<char>) -> SelectStatement {
     Query::select()
         .column("id")
         .from("character")
         .apply_if(a, |q, v| {
             q.and_where(Expr::col("font_id").eq(v));
         })
         .apply_if(b, |q, v| {
             q.and_where(Expr::col("ascii").like(v));
         })
         .take()
 }

 assert_eq!(
     query(Some(5), Some('A')).to_string(MysqlQueryBuilder).unwrap(),
     "SELECT `id` FROM `character` WHERE `font_id` = 5 AND `ascii` LIKE 'A'"
 );
 assert_eq!(
     query(Some(5), None).to_string(MysqlQueryBuilder).unwrap(),
     "SELECT `id` FROM `character` WHERE `font_id` = 5"
 );
 assert_eq!(
     query(None, None).to_string(MysqlQueryBuilder).unwrap(),
     "SELECT `id` FROM `character`"
 );
 ```

 Conditions can be arbitrarily complex, thanks to the builder's internal AST:

 ```
 # use sql_builder::*;
 assert_eq!(
     Query::select()
         .column("id")
         .from("glyph")
         .cond_where(
             Cond::any()
                 .add(
                     Cond::all()
                         .add(Expr::col("aspect").is_null())
                         .add(Expr::col("image").is_null())
                 )
                 .add(
                     Cond::all()
                         .add(Expr::col("aspect").is_in([3, 4]))
                         .add(Expr::col("image").like("A%"))
                 )
         )
         .to_string(PostgresQueryBuilder).unwrap(),
     [
         r#"SELECT "id" FROM "glyph""#,
         r#"WHERE"#,
         r#"("aspect" IS NULL AND "image" IS NULL)"#,
         r#"OR"#,
         r#"("aspect" IN (3, 4) AND "image" LIKE 'A%')"#,
     ]
     .join(" ")
 );
 ```

 There is no superfluous parentheses `((((` cluttering the query, because the builder respects
 operator precedence when injecting them.

 ### 3. Cross database support

 You can target multiple database backends while maintaining a single source of query logic.

 ```
 # use sql_builder::*;
 let query = Query::insert()
     .into_table("glyph")
     .columns(["aspect", "image"])
     .values_panic([
         2.into(),
         3.into(),
     ])
     .on_conflict(
         OnConflict::column("id")
             .update_columns(["aspect", "image"])
             .to_owned(),
     )
     .to_owned();

 assert_eq!(
     query.to_string(MysqlQueryBuilder).unwrap(),
     r#"INSERT INTO `glyph` (`aspect`, `image`) VALUES (2, 3) ON DUPLICATE KEY UPDATE `aspect` = VALUES(`aspect`), `image` = VALUES(`image`)"#
 );
 assert_eq!(
     query.to_string(PostgresQueryBuilder).unwrap(),
     r#"INSERT INTO "glyph" ("aspect", "image") VALUES (2, 3) ON CONFLICT ("id") DO UPDATE SET "aspect" = "excluded"."aspect", "image" = "excluded"."image""#
 );
 assert_eq!(
     query.to_string(SqliteQueryBuilder).unwrap(),
     r#"INSERT INTO "glyph" ("aspect", "image") VALUES (2, 3) ON CONFLICT ("id") DO UPDATE SET "aspect" = "excluded"."aspect", "image" = "excluded"."image""#
 );
 ```

 ## Basics

 ### Iden

 `Iden` is a trait for identifiers used in any query statement.

 Commonly implemented by Enum where each Enum represents a table found in a database,
 and its variants include table name and column name.

 You can use the `Iden` derive macro to implement it.

 ```rust
 # use sql_builder::Iden;
 #[derive(Iden)]
 enum Character {
     Table,
     Id,
     FontId,
     FontSize,
 }

 assert_eq!(Character::Table.to_string(), "character");
 assert_eq!(Character::Id.to_string(), "id");
 assert_eq!(Character::FontId.to_string(), "font_id");
 assert_eq!(Character::FontSize.to_string(), "font_size");

 #[derive(Iden)]
 struct Glyph;
 assert_eq!(Glyph.to_string(), "glyph");
 ```

 ### Expression

 Use [`Expr`] constructors and [`ExprTrait`] methods
 to construct `SELECT`, `JOIN`, `WHERE` and `HAVING` expression in query.

 ```rust
 # use sql_builder::*;
 assert_eq!(
     Query::select()
         .column("char_code")
         .from("character")
         .and_where(
             Expr::col("size_w")
                 .add(1)
                 .mul(2)
                 .eq(Expr::col("size_h").div(2).sub(1))
         )
         .and_where(
             Expr::col("size_w").in_subquery(
                 Query::select()
                     .expr(Expr::cust_with_values("ln($1 ^ $2)", [2.4, 1.2], &PostgresQueryBuilder).unwrap())
                     .take()
             )
         )
         .and_where(
             Expr::col("char_code")
                 .like("D")
                 .and(Expr::col("char_code").like("E"))
         )
         .to_string(PostgresQueryBuilder).unwrap(),
     [
         r#"SELECT "char_code" FROM "character""#,
         r#"WHERE ("size_w" + 1) * 2 = ("size_h" / 2) - 1"#,
         r#"AND "size_w" IN (SELECT ln(2.4 ^ 1.2))"#,
         r#"AND ("char_code" LIKE 'D' AND "char_code" LIKE 'E')"#,
     ]
     .join(" ")
 );
 ```

 ### Condition

 If you have complex conditions to express, you can use the [`Condition`] builder,
 usable for [`ConditionalStatement::cond_where`] and [`SelectStatement::cond_having`].

 ```
 # use sql_builder::*;
 assert_eq!(
     Query::select()
         .column("id")
         .from("glyph")
         .cond_where(
             Cond::any()
                 .add(
                     Cond::all()
                         .add(Expr::col("aspect").is_null())
                         .add(Expr::col("image").is_null())
                 )
                 .add(
                     Cond::all()
                         .add(Expr::col("aspect").is_in([3, 4]))
                         .add(Expr::col("image").like("A%"))
                 )
         )
         .to_string(PostgresQueryBuilder).unwrap(),
     [
         r#"SELECT "id" FROM "glyph""#,
         r#"WHERE"#,
         r#"("aspect" IS NULL AND "image" IS NULL)"#,
         r#"OR"#,
         r#"("aspect" IN (3, 4) AND "image" LIKE 'A%')"#,
     ]
     .join(" ")
 );
 ```

 There is also the [`any!`] and [`all!`] macro at your convenience:

 ```
 # use sql_builder::*;
 Query::select().cond_where(any![
     Expr::col("aspect").is_in([3, 4]),
     all![
         Expr::col("aspect").is_null(),
         Expr::col("image").like("A%")
     ]
 ]);
 ```

 ### Statement Builders

 Query statements are serialized into SQL with [`QueryStatementBuilder`]:

 ```rust
 # use sql_builder::{*};
 # trait ExampleQueryBuilder {
 fn build<T: QueryBuilder>(&self, query_builder: T) -> (String, Values);

 fn to_string<T: QueryBuilder>(&self, query_builder: T) -> String;
 # }
 ```

 `build` builds a SQL statement as string and parameters to be passed to the database driver
 through the binary protocol. This is the preferred way as it has less overhead and is more secure.

 `to_string` builds a SQL statement as string with parameters injected. This is good for testing
 and debugging.

 ## Query Statement

 ### Query Select

 ```rust
 # use sql_builder::*;
 let query = Query::select()
     .column("char_code")
     .column(("font", "name"))
     .from("character")
     .left_join("font", Expr::col(("character", "font_id")).equals(("font", "id")))
     .and_where(Expr::col("size_w").is_in([3, 4]))
     .and_where(Expr::col("char_code").like("A%"))
     .to_owned();

 assert_eq!(
     query.to_string(MysqlQueryBuilder).unwrap(),
     r#"SELECT `char_code`, `font`.`name` FROM `character` LEFT JOIN `font` ON `character`.`font_id` = `font`.`id` WHERE `size_w` IN (3, 4) AND `char_code` LIKE 'A%'"#
 );
 assert_eq!(
     query.to_string(PostgresQueryBuilder).unwrap(),
     r#"SELECT "char_code", "font"."name" FROM "character" LEFT JOIN "font" ON "character"."font_id" = "font"."id" WHERE "size_w" IN (3, 4) AND "char_code" LIKE 'A%'"#
 );
 assert_eq!(
     query.to_string(SqliteQueryBuilder).unwrap(),
     r#"SELECT "char_code", "font"."name" FROM "character" LEFT JOIN "font" ON "character"."font_id" = "font"."id" WHERE "size_w" IN (3, 4) AND "char_code" LIKE 'A%'"#
 );
 ```

 ### Query Insert

 ```rust
 # use sql_builder::*;
 let query = Query::insert()
     .into_table("glyph")
     .columns(["aspect", "image"])
     .values_panic([5.15.into(), "12A".into()])
     .values_panic([4.21.into(), "123".into()])
     .to_owned();

 assert_eq!(
     query.to_string(MysqlQueryBuilder).unwrap(),
     r#"INSERT INTO `glyph` (`aspect`, `image`) VALUES (5.15, '12A'), (4.21, '123')"#
 );
 assert_eq!(
     query.to_string(PostgresQueryBuilder).unwrap(),
     r#"INSERT INTO "glyph" ("aspect", "image") VALUES (5.15, '12A'), (4.21, '123')"#
 );
 assert_eq!(
     query.to_string(SqliteQueryBuilder).unwrap(),
     r#"INSERT INTO "glyph" ("aspect", "image") VALUES (5.15, '12A'), (4.21, '123')"#
 );
 ```

 ### Query Update

 ```rust
 # use sql_builder::*;
 let query = Query::update()
     .table("glyph")
     .values([("aspect", 1.23.into()), ("image", "123".into())])
     .and_where(Expr::col("id").eq(1))
     .to_owned();

 assert_eq!(
     query.to_string(MysqlQueryBuilder).unwrap(),
     r#"UPDATE `glyph` SET `aspect` = 1.23, `image` = '123' WHERE `id` = 1"#
 );
 assert_eq!(
     query.to_string(PostgresQueryBuilder).unwrap(),
     r#"UPDATE "glyph" SET "aspect" = 1.23, "image" = '123' WHERE "id" = 1"#
 );
 assert_eq!(
     query.to_string(SqliteQueryBuilder).unwrap(),
     r#"UPDATE "glyph" SET "aspect" = 1.23, "image" = '123' WHERE "id" = 1"#
 );
 ```

 ### Query Delete

 ```rust
 # use sql_builder::*;
 let query = Query::delete()
     .from_table("glyph")
     .cond_where(
         Cond::any()
             .add(Expr::col("id").lt(1))
             .add(Expr::col("id").gt(10)),
     )
     .to_owned();

 assert_eq!(
     query.to_string(MysqlQueryBuilder).unwrap(),
     r#"DELETE FROM `glyph` WHERE `id` < 1 OR `id` > 10"#
 );
 assert_eq!(
     query.to_string(PostgresQueryBuilder).unwrap(),
     r#"DELETE FROM "glyph" WHERE "id" < 1 OR "id" > 10"#
 );
 assert_eq!(
     query.to_string(SqliteQueryBuilder).unwrap(),
     r#"DELETE FROM "glyph" WHERE "id" < 1 OR "id" > 10"#
 );
 ```

 ## Advanced

 ### Aggregate Functions

 `max`, `min`, `sum`, `avg`, `count` etc

 ```rust
 # use sql_builder::*;
 let query = Query::select()
     .expr(Func::sum(Expr::col(("character", "size_h"))))
     .from("character")
     .to_owned();
 assert_eq!(
     query.to_string(MysqlQueryBuilder).unwrap(),
     r#"SELECT SUM(`character`.`size_h`) FROM `character`"#
 );
 assert_eq!(
     query.to_string(PostgresQueryBuilder).unwrap(),
     r#"SELECT SUM("character"."size_h") FROM "character""#
 );
 assert_eq!(
     query.to_string(SqliteQueryBuilder).unwrap(),
     r#"SELECT SUM("character"."size_h") FROM "character""#
 );
 ```

 ### Casting

 ```rust
 # use sql_builder::*;
 let query = Query::select()
     .expr(Func::cast_as("hello", "MyType"))
     .to_owned();

 assert_eq!(
     query.to_string(MysqlQueryBuilder).unwrap(),
     r#"SELECT CAST('hello' AS MyType)"#
 );
 assert_eq!(
     query.to_string(PostgresQueryBuilder).unwrap(),
     r#"SELECT CAST('hello' AS MyType)"#
 );
 assert_eq!(
     query.to_string(SqliteQueryBuilder).unwrap(),
     r#"SELECT CAST('hello' AS MyType)"#
 );
 ```

 ### Custom Function

 ```rust
 # use sql_builder::*;
 struct MyFunction;

 impl Iden for MyFunction {
     fn unquoted(&self) -> &str {
         "MY_FUNCTION"
     }
 }

 let query = Query::select()
     .expr(Func::cust(MyFunction).arg(Expr::val("hello")))
     .to_owned();

 assert_eq!(
     query.to_string(MysqlQueryBuilder).unwrap(),
     r#"SELECT MY_FUNCTION('hello')"#
 );
 assert_eq!(
     query.to_string(PostgresQueryBuilder).unwrap(),
     r#"SELECT MY_FUNCTION('hello')"#
 );
 assert_eq!(
     query.to_string(SqliteQueryBuilder).unwrap(),
     r#"SELECT MY_FUNCTION('hello')"#
 );
 ```