turso-sql 0.1.1

SQL AST and query builder for the Turso / SQLite dialect
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
//! DML statement builders: `SELECT`, `INSERT`, `UPDATE` and `DELETE`.
//!
//! Each builder is a plain data structure with chainable setters; nothing is
//! validated or rendered until the writer runs, so a partially built
//! statement can be stored, cloned and completed later — which is how
//! `turso-orm` assembles entity queries in several passes. The fields are
//! `pub(crate)` rather than private so the writer in `crate::writer` can
//! read them directly without a mirror of accessors.
//!
//! The module owns the shape of DML statements only. Expressions and
//! conditions come from `crate::expr`, identifiers from `crate::iden`, and
//! the SQL text is produced by `crate::writer`.
//!
//! - [`Query`]: the entry point, with one constructor per statement
//!   (`Query::select()` and friends);
//! - [`Select`], [`Insert`], [`Update`], [`Delete`]: the four statements;
//! - [`Join`], [`JoinType`], [`OnConflict`], [`Returning`], [`SelectItem`]:
//!   the clauses they are built from.

use crate::expr::{Condition, Expr, IntoCondition, Order};
use crate::iden::{ColumnRef, Ident, IntoIden, TableRef};
use crate::value::Value;

/// The entry point to the DML builders, mirroring the `Query::select()`
/// style.
#[derive(Debug)]
pub struct Query;

impl Query {
    /// Starts a `SELECT`.
    pub fn select() -> Select {
        Select::default()
    }

    /// Starts an `INSERT`.
    pub fn insert() -> Insert {
        Insert::default()
    }

    /// Starts an `UPDATE`.
    pub fn update() -> Update {
        Update::default()
    }

    /// Starts a `DELETE`.
    pub fn delete() -> Delete {
        Delete::default()
    }
}

/// An item of a select list.
#[derive(Clone, Debug, PartialEq)]
pub enum SelectItem {
    /// Any expression, possibly aliased with [`Expr::alias`].
    Expr(Expr),
}

/// The join kinds.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum JoinType {
    /// An `INNER JOIN`.
    Inner,
    /// A `LEFT JOIN`.
    Left,
    /// A `CROSS JOIN`.
    Cross,
}

/// A join clause.
#[derive(Clone, Debug, PartialEq)]
pub struct Join {
    /// The join kind.
    pub kind: JoinType,
    /// The joined table.
    pub table: TableRef,
    /// The `ON` condition, absent for a `CROSS JOIN`.
    pub on: Option<Expr>,
}

/// A `RETURNING` clause.
#[derive(Clone, Debug, Default, PartialEq)]
pub enum Returning {
    /// No `RETURNING` clause.
    #[default]
    None,
    /// `RETURNING *`.
    All,
    /// `RETURNING a, b`.
    Columns(Vec<ColumnRef>),
}

/// A `SELECT` statement.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Select {
    /// Whether `DISTINCT` is set.
    pub(crate) distinct: bool,
    /// The select list; empty renders as `*`.
    pub(crate) items: Vec<SelectItem>,
    /// The `FROM` tables.
    pub(crate) from: Vec<TableRef>,
    /// A derived table `FROM (subquery) AS alias`, appended after `from`.
    pub(crate) from_subquery: Option<(Box<Select>, Ident)>,
    /// The join clauses, in order.
    pub(crate) joins: Vec<Join>,
    /// The `WHERE` condition; empty renders as nothing.
    pub(crate) r#where: Condition,
    /// The `GROUP BY` expressions.
    pub(crate) group_by: Vec<Expr>,
    /// The `HAVING` condition; empty renders as nothing.
    pub(crate) having: Condition,
    /// The `ORDER BY` terms.
    pub(crate) order_by: Vec<(Expr, Order)>,
    /// The `LIMIT`, bound as a parameter.
    pub(crate) limit: Option<u64>,
    /// The `OFFSET`, bound as a parameter.
    pub(crate) offset: Option<u64>,
}

impl Select {
    /// A new empty select whose conditions default to `Condition::all()`.
    pub fn new() -> Self {
        Self {
            r#where: Condition::all(),
            having: Condition::all(),
            ..Default::default()
        }
    }

    /// Sets `SELECT DISTINCT`.
    #[must_use]
    pub fn distinct(mut self) -> Self {
        self.distinct = true;
        self
    }

    /// Selects a column.
    #[must_use]
    pub fn column(mut self, column: impl Into<ColumnRef>) -> Self {
        self.items
            .push(SelectItem::Expr(Expr::Column(column.into())));
        self
    }

    /// Selects several columns.
    #[must_use]
    pub fn columns<C: Into<ColumnRef>>(mut self, columns: impl IntoIterator<Item = C>) -> Self {
        for c in columns {
            self = self.column(c);
        }
        self
    }

    /// Selects an expression.
    #[must_use]
    pub fn expr(mut self, expr: Expr) -> Self {
        self.items.push(SelectItem::Expr(expr));
        self
    }

    /// Selects an aliased expression.
    #[must_use]
    pub fn expr_as(self, expr: Expr, alias: impl IntoIden) -> Self {
        self.expr(expr.alias(alias))
    }

    /// Whether any item has been selected; without one the statement renders
    /// `SELECT *`.
    pub fn has_items(&self) -> bool {
        !self.items.is_empty()
    }

    /// Removes all selected items.
    #[must_use]
    pub fn clear_items(mut self) -> Self {
        self.items.clear();
        self
    }

    /// Adds a `FROM table`.
    #[must_use]
    pub fn from(mut self, table: impl Into<TableRef>) -> Self {
        self.from.push(table.into());
        self
    }

    /// Sets `FROM (subquery) AS alias`.
    #[must_use]
    pub fn from_subquery(mut self, subquery: Select, alias: impl IntoIden) -> Self {
        self.from_subquery = Some((Box::new(subquery), alias.into_iden()));
        self
    }

    /// Adds a `JOIN table ON cond` of the given kind.
    #[must_use]
    pub fn join(
        mut self,
        kind: JoinType,
        table: impl Into<TableRef>,
        on: impl IntoCondition,
    ) -> Self {
        self.joins.push(Join {
            kind,
            table: table.into(),
            on: on.into_condition().into_expr(),
        });
        self
    }

    /// Adds an `INNER JOIN`.
    #[must_use]
    pub fn inner_join(self, table: impl Into<TableRef>, on: impl IntoCondition) -> Self {
        self.join(JoinType::Inner, table, on)
    }

    /// Adds a `LEFT JOIN`.
    #[must_use]
    pub fn left_join(self, table: impl Into<TableRef>, on: impl IntoCondition) -> Self {
        self.join(JoinType::Left, table, on)
    }

    /// Adds a condition to `WHERE` with `AND`.
    #[must_use]
    pub fn and_where(mut self, cond: impl IntoCondition) -> Self {
        self.r#where = std::mem::take(&mut self.r#where).add(cond);
        self
    }

    /// Replaces the whole `WHERE` condition.
    #[must_use]
    pub fn cond_where(mut self, cond: impl IntoCondition) -> Self {
        self.r#where = cond.into_condition();
        self
    }

    /// Adds a `GROUP BY` expression.
    #[must_use]
    pub fn group_by(mut self, expr: Expr) -> Self {
        self.group_by.push(expr);
        self
    }

    /// Adds a condition to `HAVING` with `AND`.
    #[must_use]
    pub fn and_having(mut self, cond: impl IntoCondition) -> Self {
        self.having = std::mem::take(&mut self.having).add(cond);
        self
    }

    /// Adds an `ORDER BY column`.
    #[must_use]
    pub fn order_by(mut self, column: impl Into<ColumnRef>, order: Order) -> Self {
        self.order_by.push((Expr::Column(column.into()), order));
        self
    }

    /// Adds an `ORDER BY expr`.
    #[must_use]
    pub fn order_by_expr(mut self, expr: Expr, order: Order) -> Self {
        self.order_by.push((expr, order));
        self
    }

    /// Removes the ordering.
    #[must_use]
    pub fn clear_order_by(mut self) -> Self {
        self.order_by.clear();
        self
    }

    /// Sets `LIMIT`.
    #[must_use]
    pub fn limit(mut self, limit: u64) -> Self {
        self.limit = Some(limit);
        self
    }

    /// Removes `LIMIT`.
    #[must_use]
    pub fn reset_limit(mut self) -> Self {
        self.limit = None;
        self
    }

    /// Sets `OFFSET`.
    #[must_use]
    pub fn offset(mut self, offset: u64) -> Self {
        self.offset = Some(offset);
        self
    }

    /// Removes `OFFSET`.
    #[must_use]
    pub fn reset_offset(mut self) -> Self {
        self.offset = None;
        self
    }

    /// The `FROM` tables.
    pub fn from_tables(&self) -> &[TableRef] {
        &self.from
    }

    /// The join clauses, in order.
    pub fn joins(&self) -> &[Join] {
        &self.joins
    }
}

/// An `ON CONFLICT` clause for inserts.
#[derive(Clone, Debug, PartialEq)]
pub struct OnConflict {
    /// The conflict target columns; empty renders a bare `ON CONFLICT`.
    pub(crate) target: Vec<Ident>,
    /// What to do on conflict.
    pub(crate) action: ConflictAction,
}

/// The action part of an `ON CONFLICT` clause.
#[derive(Clone, Debug, PartialEq)]
pub(crate) enum ConflictAction {
    /// `DO NOTHING`.
    Nothing,
    /// `DO UPDATE SET column = expr, ...`.
    Update(Vec<(Ident, Expr)>),
}

impl OnConflict {
    /// Builds `ON CONFLICT(columns) DO NOTHING`.
    pub fn do_nothing<C: IntoIden>(columns: impl IntoIterator<Item = C>) -> Self {
        Self {
            target: columns.into_iter().map(IntoIden::into_iden).collect(),
            action: ConflictAction::Nothing,
        }
    }

    /// Builds `ON CONFLICT(columns) DO UPDATE SET c = excluded.c, ...` for
    /// every column in `update`.
    pub fn update_columns<C: IntoIden, U: IntoIden>(
        columns: impl IntoIterator<Item = C>,
        update: impl IntoIterator<Item = U>,
    ) -> Self {
        let sets = update
            .into_iter()
            .map(|c| {
                let c = c.into_iden();
                let excluded =
                    Expr::Column(ColumnRef::TableColumn("excluded".into_iden(), c.clone()));
                (c, excluded)
            })
            .collect();
        Self {
            target: columns.into_iter().map(IntoIden::into_iden).collect(),
            action: ConflictAction::Update(sets),
        }
    }

    /// Builds `ON CONFLICT(columns) DO UPDATE SET` with explicit expressions.
    pub fn update_exprs<C: IntoIden, U: IntoIden>(
        columns: impl IntoIterator<Item = C>,
        sets: impl IntoIterator<Item = (U, Expr)>,
    ) -> Self {
        Self {
            target: columns.into_iter().map(IntoIden::into_iden).collect(),
            action: ConflictAction::Update(
                sets.into_iter().map(|(c, e)| (c.into_iden(), e)).collect(),
            ),
        }
    }
}

/// An `INSERT` statement.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Insert {
    /// The target table.
    pub(crate) table: Option<TableRef>,
    /// The column list.
    pub(crate) columns: Vec<Ident>,
    /// The `VALUES` rows, one expression per column.
    pub(crate) rows: Vec<Vec<Expr>>,
    /// Whether to render `DEFAULT VALUES` instead of rows.
    pub(crate) default_values: bool,
    /// The `ON CONFLICT` clause.
    pub(crate) on_conflict: Option<OnConflict>,
    /// The `RETURNING` clause.
    pub(crate) returning: Returning,
    /// A `SELECT` source rendered instead of `VALUES` when set.
    pub(crate) select: Option<Box<Select>>,
}

impl Insert {
    /// Sets `INSERT INTO table`.
    #[must_use]
    pub fn into_table(mut self, table: impl Into<TableRef>) -> Self {
        self.table = Some(table.into());
        self
    }

    /// Sets the column list.
    #[must_use]
    pub fn columns<C: IntoIden>(mut self, columns: impl IntoIterator<Item = C>) -> Self {
        self.columns = columns.into_iter().map(IntoIden::into_iden).collect();
        self
    }

    /// Adds a row of values, one per column.
    #[must_use]
    pub fn values<V: Into<Expr>>(mut self, row: impl IntoIterator<Item = V>) -> Self {
        self.rows.push(row.into_iter().map(Into::into).collect());
        self
    }

    /// Sets `INSERT INTO table DEFAULT VALUES`.
    #[must_use]
    pub fn default_values(mut self) -> Self {
        self.default_values = true;
        self
    }

    /// Sets `INSERT INTO table (...) SELECT ...`.
    #[must_use]
    pub fn select_from(mut self, select: Select) -> Self {
        self.select = Some(Box::new(select));
        self
    }

    /// Sets the `ON CONFLICT` clause.
    #[must_use]
    pub fn on_conflict(mut self, on_conflict: OnConflict) -> Self {
        self.on_conflict = Some(on_conflict);
        self
    }

    /// Sets the `RETURNING` clause.
    #[must_use]
    pub fn returning(mut self, returning: Returning) -> Self {
        self.returning = returning;
        self
    }

    /// Sets `RETURNING *`.
    #[must_use]
    pub fn returning_all(self) -> Self {
        self.returning(Returning::All)
    }

    /// The number of rows added so far.
    pub fn row_count(&self) -> usize {
        self.rows.len()
    }
}

/// An `UPDATE` statement.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Update {
    /// The target table.
    pub(crate) table: Option<TableRef>,
    /// The `SET` assignments, in order.
    pub(crate) sets: Vec<(Ident, Expr)>,
    /// The `WHERE` condition.
    pub(crate) r#where: Option<Condition>,
    /// The `RETURNING` clause.
    pub(crate) returning: Returning,
    /// The `LIMIT`, written inline since it is not a user value.
    pub(crate) limit: Option<u64>,
}

impl Update {
    /// Sets `UPDATE table`.
    #[must_use]
    pub fn table(mut self, table: impl Into<TableRef>) -> Self {
        self.table = Some(table.into());
        self
    }

    /// Adds `SET column = value`.
    #[must_use]
    pub fn value(mut self, column: impl IntoIden, value: impl Into<Expr>) -> Self {
        self.sets.push((column.into_iden(), value.into()));
        self
    }

    /// Adds several `SET` assignments.
    #[must_use]
    pub fn values<C: IntoIden, V: Into<Expr>>(
        mut self,
        sets: impl IntoIterator<Item = (C, V)>,
    ) -> Self {
        for (c, v) in sets {
            self = self.value(c, v);
        }
        self
    }

    /// Adds a condition to `WHERE` with `AND`.
    #[must_use]
    pub fn and_where(mut self, cond: impl IntoCondition) -> Self {
        let current = self.r#where.take().unwrap_or_else(Condition::all);
        self.r#where = Some(current.add(cond));
        self
    }

    /// Sets the `RETURNING` clause.
    #[must_use]
    pub fn returning(mut self, returning: Returning) -> Self {
        self.returning = returning;
        self
    }

    /// Sets `RETURNING *`.
    #[must_use]
    pub fn returning_all(self) -> Self {
        self.returning(Returning::All)
    }

    /// Whether any `SET` assignment was added.
    pub fn has_sets(&self) -> bool {
        !self.sets.is_empty()
    }
}

/// A `DELETE` statement.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Delete {
    /// The target table.
    pub(crate) table: Option<TableRef>,
    /// The `WHERE` condition.
    pub(crate) r#where: Option<Condition>,
    /// The `RETURNING` clause.
    pub(crate) returning: Returning,
}

impl Delete {
    /// Sets `DELETE FROM table`.
    #[must_use]
    pub fn from_table(mut self, table: impl Into<TableRef>) -> Self {
        self.table = Some(table.into());
        self
    }

    /// Adds a condition to `WHERE` with `AND`.
    #[must_use]
    pub fn and_where(mut self, cond: impl IntoCondition) -> Self {
        let current = self.r#where.take().unwrap_or_else(Condition::all);
        self.r#where = Some(current.add(cond));
        self
    }

    /// Sets the `RETURNING` clause.
    #[must_use]
    pub fn returning(mut self, returning: Returning) -> Self {
        self.returning = returning;
        self
    }
}

impl From<Value> for SelectItem {
    fn from(v: Value) -> Self {
        SelectItem::Expr(Expr::Value(v))
    }
}