sql_query_builder 2.6.2

Write SQL queries in a simple and composable way
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
use crate::{
  behavior::TransactionQuery,
  concat::Concat,
  fmt,
  structure::{CreateIndex, CreateIndexParams},
  utils::push_unique,
};

impl TransactionQuery for CreateIndex {}

impl CreateIndex {
  /// Gets the current state of the [CreateIndex] and returns it as string
  ///
  /// ### Example
  ///
  /// ```
  /// # #[cfg(any(feature = "postgresql", feature = "sqlite", feature = "mysql"))]
  /// # {
  /// # use sql_query_builder as sql;
  /// let query = sql::CreateIndex::new()
  ///   .create_index("users_name_idx")
  ///   .as_string();
  ///
  /// # let expected = "CREATE INDEX users_name_idx";
  /// # assert_eq!(expected, query);
  /// # }
  /// ```
  ///
  /// Output
  ///
  /// ```sql
  /// CREATE INDEX users_name_idx
  /// ```
  pub fn as_string(&self) -> String {
    let fmts = fmt::one_line();
    self.concat(&fmts)
  }

  /// Defines the column of the table used to create the index
  ///
  /// ### Example
  ///
  ///```
  /// # #[cfg(any(feature = "postgresql", feature = "sqlite", feature = "mysql"))]
  /// # {
  /// # use sql_query_builder as sql;
  /// let query = sql::CreateIndex::new()
  ///   .on("users")
  ///   .column("login")
  ///   .column("name")
  ///   .as_string();
  ///
  /// # let expected = "ON users (login, name)";
  /// # assert_eq!(expected, query);
  /// # }
  /// ```
  ///
  /// Outputs
  ///
  /// ```sql
  /// ON users (login, name)
  /// ```
  pub fn column(mut self, column_name: &str) -> Self {
    push_unique(&mut self._column, column_name.trim().to_string());
    self
  }

  /// Defines a create index parameter, this method overrides the previous value
  ///
  /// ### Example
  ///
  ///```
  /// # #[cfg(any(feature = "postgresql", feature = "sqlite", feature = "mysql"))]
  /// # {
  /// # use sql_query_builder as sql;
  /// let query = sql::CreateIndex::new()
  ///   .create_index("users_name_idx")
  ///   .create_index("orders_product_name_idx")
  ///   .as_string();
  ///
  /// # let expected = "CREATE INDEX orders_product_name_idx";
  /// # assert_eq!(expected, query);
  /// # }
  /// ```
  ///
  /// Outputs
  ///
  /// ```sql
  /// CREATE INDEX orders_product_name_idx
  /// ```
  pub fn create_index(mut self, index_name: &str) -> Self {
    self._index_name = index_name.trim().to_string();
    self._create_index = true;
    self
  }

  /// Prints the current state of the [CreateIndex] to the standard output in a more ease to read version.
  /// This method is useful to debug complex queries or just print the generated SQL while you type
  ///
  /// ### Example
  ///
  /// ```
  /// # #[cfg(any(feature = "postgresql", feature = "sqlite", feature = "mysql"))]
  /// # {
  /// # use sql_query_builder as sql;
  /// let query = sql::CreateIndex::new()
  ///   .create_index("users_name_idx")
  ///   .on("users")
  ///   .column("name")
  ///   .debug()
  ///   .as_string();
  ///
  /// # let expected = "\
  /// #   CREATE INDEX users_name_idx \
  /// #   ON users (name)\
  /// # ";
  /// # assert_eq!(expected, query);
  /// # }
  /// ```
  ///
  /// Prints to the standard output
  ///
  /// ```sql
  /// -- ------------------------------------------------------------------------------
  /// CREATE INDEX users_name_idx
  /// ON users (name)
  /// -- ------------------------------------------------------------------------------
  /// ```
  pub fn debug(self) -> Self {
    let fmts = fmt::multiline();
    println!("{}", fmt::format(self.concat(&fmts), &fmts));
    self
  }

  /// Creates instance of the [CreateIndex] command
  pub fn new() -> Self {
    Self::default()
  }

  /// Defines the `on table_name` clause, this method overrides the previous value
  ///
  /// ### Example
  ///
  ///```
  /// # #[cfg(any(feature = "postgresql", feature = "sqlite", feature = "mysql"))]
  /// # {
  /// # use sql_query_builder as sql;
  /// let query = sql::CreateIndex::new()
  ///   .on("users")
  ///   .on("orders")
  ///   .as_string();
  ///
  /// # let expected = "ON orders";
  /// # assert_eq!(expected, query);
  /// # }
  /// ```
  ///
  /// Outputs
  ///
  /// ```sql
  /// ON orders
  /// ```
  pub fn on(mut self, table_name: &str) -> Self {
    self._on = table_name.trim().to_string();
    self
  }

  /// Prints the current state of the [CreateIndex] to the standard output similar to debug method,
  /// the difference is that this method prints in one line.
  pub fn print(self) -> Self {
    let fmts = fmt::one_line();
    println!("{}", fmt::format(self.concat(&fmts), &fmts));
    self
  }

  /// Adds at the beginning a raw SQL query. Is useful to create a more complex create index command.
  ///
  /// ### Example
  ///
  /// ```
  /// # #[cfg(any(feature = "postgresql", feature = "sqlite", feature = "mysql"))]
  /// # {
  /// # use sql_query_builder as sql;
  /// let create_index_query = sql::CreateIndex::new()
  ///   .raw("/* start index command */")
  ///   .create_index("users_name_idx")
  ///   .as_string();
  ///
  /// # let expected = "/* start index command */ CREATE INDEX users_name_idx";
  /// # assert_eq!(expected, create_index_query);
  /// # }
  /// ```
  ///
  /// Output
  ///
  /// ```sql
  /// /* start index command */ CREATE INDEX users_name_idx
  /// ```
  pub fn raw(mut self, raw_sql: &str) -> Self {
    push_unique(&mut self._raw, raw_sql.trim().to_string());
    self
  }

  /// Adds a raw SQL query after a specified parameter.
  ///
  /// The `CreateIndexParams::CreateIndex` works both to `.create_index` and `.create_index_if_not_exists` methods.
  ///
  /// ### Example
  ///
  /// ```
  /// # #[cfg(any(feature = "postgresql", feature = "sqlite", feature = "mysql"))]
  /// # {
  /// # use sql_query_builder as sql;
  /// let raw = "/* after create index */";
  ///
  /// let query = sql::CreateIndex::new()
  ///   .create_index("users_name_idx")
  ///   .raw_after(sql::CreateIndexParams::CreateIndex, raw)
  ///   .on("users")
  ///   .column("name")
  ///   .as_string();
  ///
  /// # let expected = "\
  /// #   CREATE INDEX users_name_idx \
  /// #   /* after create index */ \
  /// #   ON users (name)\
  /// # ";
  /// # assert_eq!(expected, query);
  /// # }
  /// ```
  ///
  /// Output
  ///
  /// ```sql
  /// CREATE INDEX users_name_idx
  /// /* after create index */
  /// ON users (name)
  /// ```
  pub fn raw_after(mut self, param: CreateIndexParams, raw_sql: &str) -> Self {
    self._raw_after.push((param, raw_sql.trim().to_string()));
    self
  }

  /// Adds a raw SQL query before a specified parameter.
  ///
  /// The `CreateIndexParams::CreateIndex` works both to `.create_index` and `.create_index_if_not_exists` methods.
  ///
  /// ### Example
  ///
  /// ```
  /// # #[cfg(any(feature = "postgresql", feature = "sqlite", feature = "mysql"))]
  /// # {
  /// # use sql_query_builder as sql;
  /// let raw = "/* before create index */";
  ///
  /// let query = sql::CreateIndex::new()
  ///   .raw_before(sql::CreateIndexParams::CreateIndex, raw)
  ///   .create_index("users_name_idx")
  ///   .on("users")
  ///   .column("name")
  ///   .as_string();
  ///
  /// # let expected = "\
  /// #   /* before create index */ \
  /// #   CREATE INDEX users_name_idx \
  /// #   ON users (name)\
  /// # ";
  /// # assert_eq!(expected, query);
  /// # }
  /// ```
  ///
  /// Output
  ///
  /// ```sql
  /// /* before create index */
  /// CREATE INDEX users_name_idx
  /// ON users (name)
  /// ```
  pub fn raw_before(mut self, param: CreateIndexParams, raw_sql: &str) -> Self {
    self._raw_before.push((param, raw_sql.trim().to_string()));
    self
  }

  /// Defines the `unique` parameter
  ///
  /// ### Example
  ///
  /// ```
  /// # #[cfg(any(feature = "postgresql", feature = "sqlite", feature = "mysql"))]
  /// # {
  /// # use sql_query_builder as sql;
  /// let query = sql::CreateIndex::new()
  ///   .create_index("users_name_idx")
  ///   .unique()
  ///   .to_string();
  ///
  /// # let expected = "CREATE UNIQUE INDEX users_name_idx";
  /// # assert_eq!(expected, query);
  /// # }
  /// ```
  ///
  /// Outputs
  ///
  /// ```sql
  /// CREATE UNIQUE INDEX users_name_idx
  /// ```
  pub fn unique(mut self) -> Self {
    self._unique = true;

    #[cfg(feature = "mysql")]
    {
      self._fulltext = false;
      self._spatial = false;
    }
    self
  }
}

#[cfg(any(feature = "postgresql", feature = "sqlite"))]
use crate::structure::LogicalOperator;

#[cfg(any(doc, feature = "postgresql", feature = "sqlite"))]
#[cfg_attr(docsrs, doc(cfg(feature = "postgresql")))]
#[cfg_attr(docsrs, doc(cfg(feature = "sqlite")))]
impl CreateIndex {
  /// Defines a create index parameter with the `if not exists` modifier, this method overrides the previous value
  ///
  /// ### Example
  ///
  /// ```
  /// # #[cfg(any(feature = "postgresql", feature = "sqlite"))]
  /// # {
  /// # use sql_query_builder as sql;
  /// let query = sql::CreateIndex::new()
  ///   .create_index("users_name_idx")
  ///   .create_index_if_not_exists("orders_product_name_idx")
  ///   .to_string();
  ///
  /// # let expected = "CREATE INDEX IF NOT EXISTS orders_product_name_idx";
  /// # assert_eq!(expected, query);
  /// # }
  /// ```
  ///
  /// Outputs
  ///
  /// ```sql
  /// CREATE INDEX IF NOT EXISTS orders_product_name_idx
  /// ```
  pub fn create_index_if_not_exists(mut self, index_name: &str) -> Self {
    self._index_name = index_name.trim().to_string();
    self._create_index = true;
    self._if_not_exists = true;
    self
  }

  /// The method will concatenate multiples calls using the `and` operator. This method is un alias of `where_clause`.
  ///
  /// # Example
  ///
  /// ```
  /// # #[cfg(any(feature = "postgresql", feature = "sqlite"))]
  /// # {
  /// # use sql_query_builder as sql;
  /// let select_query = sql::CreateIndex::new()
  ///   .create_index("users_name_idx")
  ///   .on("users")
  ///   .column("name")
  ///   .where_and("created_at >= $1")
  ///   .as_string();
  ///
  /// # let expected = "\
  /// #   CREATE INDEX users_name_idx \
  /// #   ON users (name) \
  /// #   WHERE created_at >= $1\
  /// # ";
  /// # assert_eq!(select_query, expected);
  /// # }
  /// ```
  ///
  /// Outputs
  ///
  /// ```sql
  /// CREATE INDEX users_name_idx
  /// ON users (name)
  /// WHERE created_at >= $1
  /// ```
  pub fn where_and(self, condition: &str) -> Self {
    self.where_clause(condition)
  }

  /// The `where` clause, this method will concatenate multiples calls using the `and` operator.
  /// If you intended to use the `or` operator you should use the [where_or](CreateIndex::where_or) method
  ///
  /// # Example
  ///
  /// ```
  /// # #[cfg(any(feature = "postgresql", feature = "sqlite"))]
  /// # {
  /// # use sql_query_builder as sql;
  /// let select_query = sql::CreateIndex::new()
  ///   .create_index("users_name_idx")
  ///   .on("users")
  ///   .column("name")
  ///   .where_clause("status = 'active'")
  ///   .as_string();
  ///
  /// # let expected = "\
  /// #   CREATE INDEX users_name_idx \
  /// #   ON users (name) \
  /// #   WHERE status = 'active'\
  /// # ";
  /// # assert_eq!(select_query, expected);
  /// # }
  /// ```
  ///
  /// Outputs
  ///
  /// ```sql
  /// CREATE INDEX users_name_idx
  /// ON users (name)
  /// WHERE status = 'active'
  /// ```
  pub fn where_clause(mut self, condition: &str) -> Self {
    push_unique(&mut self._where, (LogicalOperator::And, condition.trim().to_string()));
    self
  }

  /// The `where` clause that concatenate multiples calls using the OR operator.
  /// If you intended to use the `and` operator you should use the [where_clause](CreateIndex::where_clause) method
  ///
  /// # Example
  ///
  /// ```
  /// # #[cfg(any(feature = "postgresql", feature = "sqlite"))]
  /// # {
  /// # use sql_query_builder as sql;
  /// let select_query = sql::CreateIndex::new()
  ///   .create_index("users_name_idx")
  ///   .on("users")
  ///   .column("name")
  ///   .where_clause("created_at >= $1")
  ///   .where_or("status = 'active'")
  ///   .as_string();
  ///
  /// # let expected = "\
  /// #   CREATE INDEX users_name_idx \
  /// #   ON users (name) \
  /// #   WHERE \
  /// #     created_at >= $1 \
  /// #     OR status = 'active'\
  /// # ";
  /// # assert_eq!(select_query, expected);
  /// # }
  /// ```
  ///
  /// Outputs
  ///
  /// ```sql
  /// CREATE INDEX users_name_idx
  /// ON users (name)
  /// WHERE
  ///   created_at >= $1
  ///   OR status = 'active'
  /// ```
  pub fn where_or(mut self, condition: &str) -> Self {
    push_unique(&mut self._where, (LogicalOperator::Or, condition.trim().to_string()));
    self
  }
}

#[cfg(any(doc, feature = "postgresql", feature = "mysql"))]
#[cfg_attr(docsrs, doc(cfg(feature = "postgresql")))]
#[cfg_attr(docsrs, doc(cfg(feature = "mysql")))]
impl CreateIndex {
  /// Defines the index algorithm to be used to create the index, this method overrides the previous value
  ///
  /// ### Example
  ///
  ///```
  /// # #[cfg(any(feature = "postgresql", feature = "mysql"))]
  /// # {
  /// # use sql_query_builder as sql;
  /// let query = sql::CreateIndex::new()
  ///   .using("btree")
  ///   .as_string();
  ///
  /// # let expected = "USING btree";
  /// # assert_eq!(expected, query);
  /// # }
  /// ```
  ///
  /// Outputs
  ///
  /// ```sql
  /// USING btree
  /// ```
  pub fn using(mut self, index_method: &str) -> Self {
    self._using = index_method.trim().to_string();
    self
  }
}

#[cfg(any(doc, feature = "postgresql"))]
#[cfg_attr(docsrs, doc(cfg(feature = "postgresql")))]
impl CreateIndex {
  /// Defines the `concurrently` parameter
  ///
  /// ### Example
  ///
  /// ```
  /// # #[cfg(feature = "postgresql")]
  /// # {
  /// # use sql_query_builder as sql;
  /// let query = sql::CreateIndex::new()
  ///   .create_index("users_name_idx")
  ///   .concurrently()
  ///   .to_string();
  ///
  /// # let expected = "CREATE INDEX CONCURRENTLY users_name_idx";
  /// # assert_eq!(expected, query);
  /// # }
  /// ```
  ///
  /// Outputs
  ///
  /// ```sql
  /// CREATE INDEX CONCURRENTLY users_name_idx
  /// ```
  pub fn concurrently(mut self) -> Self {
    self._concurrently = true;
    self
  }

  /// Defines the include parameter
  ///
  /// ### Example
  ///
  ///```
  /// # #[cfg(feature = "postgresql")]
  /// # {
  /// # use sql_query_builder as sql;
  /// let query = sql::CreateIndex::new()
  ///   .include("login")
  ///   .include("name")
  ///   .as_string();
  ///
  /// # let expected = "INCLUDE (login, name)";
  /// # assert_eq!(expected, query);
  /// # }
  /// ```
  ///
  /// Outputs
  ///
  /// ```sql
  /// INCLUDE (login, name)
  /// ```
  pub fn include(mut self, column_name: &str) -> Self {
    push_unique(&mut self._include, column_name.trim().to_string());
    self
  }

  /// Defines the `only` parameter
  ///
  /// ### Example
  ///
  /// ```
  /// # #[cfg(feature = "postgresql")]
  /// # {
  /// # use sql_query_builder as sql;
  /// let query = sql::CreateIndex::new()
  ///   .on("users")
  ///   .only()
  ///   .to_string();
  ///
  /// # let expected = "ON ONLY users";
  /// # assert_eq!(expected, query);
  /// # }
  /// ```
  ///
  /// Outputs
  ///
  /// ```sql
  /// ON ONLY users
  /// ```
  pub fn only(mut self) -> Self {
    self._only = true;
    self
  }
}

#[cfg(any(doc, feature = "mysql"))]
#[cfg_attr(docsrs, doc(cfg(feature = "mysql")))]
impl CreateIndex {
  /// Defines the `fulltext` parameter of the create index
  ///
  /// ### Example
  ///
  /// ```
  /// # #[cfg(feature = "mysql")]
  /// # {
  /// # use sql_query_builder as sql;
  /// let query = sql::CreateIndex::new()
  ///   .create_index("idx_users")
  ///   .fulltext()
  ///   .to_string();
  ///
  /// # let expected = "CREATE FULLTEXT INDEX idx_users";
  /// # assert_eq!(expected, query);
  /// # }
  /// ```
  ///
  /// Outputs
  ///
  /// ```sql
  /// CREATE FULLTEXT INDEX idx_users
  /// ```
  pub fn fulltext(mut self) -> Self {
    self._fulltext = true;
    self._unique = false;
    self._spatial = false;
    self
  }

  /// Defines the `lock` option
  ///
  /// ### Example
  ///
  /// ```
  /// # #[cfg(feature = "mysql")]
  /// # {
  /// # use sql_query_builder as sql;
  /// let query = sql::CreateIndex::new()
  ///   .create_index("idx_users")
  ///   .lock("exclusive")
  ///   .to_string();
  ///
  /// # let expected = "CREATE INDEX idx_users LOCK exclusive";
  /// # assert_eq!(expected, query);
  /// # }
  /// ```
  ///
  /// Outputs
  ///
  /// ```sql
  /// CREATE INDEX idx_users LOCK exclusive
  /// ```
  pub fn lock(mut self, lock_option: &str) -> Self {
    self._lock = lock_option.trim().to_string();
    self
  }

  /// Defines the `spatial` parameter of the create index
  ///
  /// ### Example
  ///
  /// ```
  /// # #[cfg(feature = "mysql")]
  /// # {
  /// # use sql_query_builder as sql;
  /// let query = sql::CreateIndex::new()
  ///   .create_index("idx_users")
  ///   .spatial()
  ///   .to_string();
  ///
  /// # let expected = "CREATE SPATIAL INDEX idx_users";
  /// # assert_eq!(expected, query);
  /// # }
  /// ```
  ///
  /// Outputs
  ///
  /// ```sql
  /// CREATE SPATIAL INDEX idx_users
  /// ```
  pub fn spatial(mut self) -> Self {
    self._spatial = true;
    self._unique = false;
    self._fulltext = false;
    self
  }
}

impl std::fmt::Display for CreateIndex {
  fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
    write!(f, "{}", self.as_string())
  }
}

impl std::fmt::Debug for CreateIndex {
  fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
    let fmts = fmt::multiline();
    write!(f, "{}", fmt::format(self.concat(&fmts), &fmts))
  }
}