nautilus-orm-core 1.2.3

Core query AST and type system for Nautilus ORM
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
//! SELECT query AST and builder.

use crate::column::ColumnMarker;
use crate::error::{Error, Result};
use crate::expr::Expr;

/// A select list item that can be either a simple column or a computed expression.
#[derive(Debug, Clone, PartialEq)]
pub enum SelectItem {
    /// A simple column reference.
    Column(ColumnMarker),
    /// A computed expression with an alias.
    Computed {
        /// The expression to compute.
        expr: Expr,
        /// The alias for the computed expression.
        alias: String,
    },
}

impl SelectItem {
    /// Creates a SelectItem from a ColumnMarker.
    pub fn column(marker: ColumnMarker) -> Self {
        SelectItem::Column(marker)
    }

    /// Creates a computed SelectItem with an expression and alias.
    pub fn computed(expr: Expr, alias: impl Into<String>) -> Self {
        SelectItem::Computed {
            expr,
            alias: alias.into(),
        }
    }
}

impl From<ColumnMarker> for SelectItem {
    fn from(marker: ColumnMarker) -> Self {
        SelectItem::Column(marker)
    }
}

/// Sort direction for ORDER BY clauses.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OrderDir {
    /// Ascending.
    Asc,
    /// Descending.
    Desc,
}

/// ORDER BY clause item.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OrderBy {
    /// Column name.
    pub column: String,
    /// Sort direction.
    pub direction: OrderDir,
}

impl OrderBy {
    /// Creates a new ORDER BY clause.
    pub fn new(column: impl Into<String>, direction: OrderDir) -> Self {
        OrderBy {
            column: column.into(),
            direction,
        }
    }

    /// Creates an ascending ORDER BY clause.
    pub fn asc(column: impl Into<String>) -> Self {
        OrderBy::new(column, OrderDir::Asc)
    }

    /// Creates a descending ORDER BY clause.
    pub fn desc(column: impl Into<String>) -> Self {
        OrderBy::new(column, OrderDir::Desc)
    }
}

/// Reserved capacities for the `Vec`s maintained by a [`SelectBuilder`].
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct SelectCapacity {
    /// Expected number of select-list items.
    pub items: usize,
    /// Expected number of JOIN clauses.
    pub joins: usize,
    /// Expected number of column-based `ORDER BY` items.
    pub order_by_columns: usize,
    /// Expected number of expression-based `ORDER BY` items.
    pub order_by_exprs: usize,
    /// Expected number of `GROUP BY` columns.
    pub group_by: usize,
    /// Expected number of `DISTINCT` columns.
    pub distinct: usize,
}

/// One ORDER BY item in the original user-specified sequence.
#[derive(Debug, Clone, PartialEq)]
pub enum OrderByItem {
    /// An ORDER BY over a plain column reference.
    Column(OrderBy),
    /// An ORDER BY over an arbitrary expression, such as an aggregate function.
    Expr(Expr, OrderDir),
}

/// Type of JOIN operation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum JoinType {
    /// INNER JOIN — only matching rows from both tables.
    Inner,
    /// LEFT JOIN — all rows from the left table, matching rows from the right.
    Left,
}

/// A JOIN clause attached to a SELECT query.
#[derive(Debug, Clone, PartialEq)]
pub struct JoinClause {
    /// The type of join (INNER, LEFT).
    pub join_type: JoinType,
    /// The table to join.
    pub table: String,
    /// The ON condition expression.
    pub on: Expr,
    /// Select items (columns or computed expressions) from the joined table.
    pub items: Vec<SelectItem>,
}

impl JoinClause {
    /// Creates a new JOIN clause.
    pub fn new(
        join_type: JoinType,
        table: impl Into<String>,
        on: Expr,
        items: Vec<SelectItem>,
    ) -> Self {
        Self {
            join_type,
            table: table.into(),
            on,
            items,
        }
    }
}

/// SELECT query AST node.
#[derive(Debug, Clone, PartialEq)]
pub struct Select {
    /// Table name.
    pub table: String,
    /// Select items (columns or computed expressions).
    pub items: Vec<SelectItem>,
    /// JOIN clauses.
    pub joins: Vec<JoinClause>,
    /// WHERE clause.
    pub filter: Option<Expr>,
    /// ORDER BY clauses.
    pub order_by: Vec<OrderBy>,
    /// Row count to return (maps to SQL LIMIT).
    ///
    /// Positive values limit forward; negative values signal backward pagination
    /// (callers must negate and reverse `ORDER BY` in application code).
    /// The absolute value is used when building the SQL `LIMIT` clause.
    pub take: Option<i32>,
    /// Row offset to skip (maps to SQL OFFSET).
    pub skip: Option<u32>,
    /// GROUP BY columns.
    pub group_by: Vec<ColumnMarker>,
    /// Columns to deduplicate on.
    ///
    /// Non-empty activates deduplication:
    /// - Postgres: `SELECT DISTINCT ON (cols)`
    /// - SQLite / MySQL: plain `SELECT DISTINCT`
    pub distinct: Vec<String>,
    /// HAVING clause (post-GROUP BY filter).
    pub having: Option<Expr>,
    /// ORDER BY items preserved in their original mixed column/expression order.
    pub order_by_items: Vec<OrderByItem>,
    /// ORDER BY expression items (for aggregate functions, e.g. `COUNT(*) DESC`).
    pub order_by_exprs: Vec<(Expr, OrderDir)>,
}

impl Select {
    /// Creates a new SELECT query builder for the given table.
    pub fn from_table(table: impl Into<String>) -> SelectBuilder {
        SelectBuilder {
            table: table.into(),
            items: Vec::new(),
            joins: Vec::new(),
            filter: None,
            order_by: Vec::new(),
            take: None,
            skip: None,
            group_by: Vec::new(),
            distinct: Vec::new(),
            having: None,
            order_by_items: Vec::new(),
            order_by_exprs: Vec::new(),
        }
    }
}

/// Builder for SELECT queries.
#[derive(Debug, Clone)]
pub struct SelectBuilder {
    table: String,
    items: Vec<SelectItem>,
    joins: Vec<JoinClause>,
    filter: Option<Expr>,
    order_by: Vec<OrderBy>,
    take: Option<i32>,
    skip: Option<u32>,
    group_by: Vec<ColumnMarker>,
    distinct: Vec<String>,
    having: Option<Expr>,
    order_by_items: Vec<OrderByItem>,
    order_by_exprs: Vec<(Expr, OrderDir)>,
}

impl SelectBuilder {
    /// Reserve capacity for the builder's internal vectors.
    #[must_use]
    pub fn with_capacity(mut self, capacity: SelectCapacity) -> Self {
        self.items.reserve(capacity.items);
        self.joins.reserve(capacity.joins);
        self.order_by.reserve(capacity.order_by_columns);
        self.group_by.reserve(capacity.group_by);
        self.distinct.reserve(capacity.distinct);
        self.order_by_items
            .reserve(capacity.order_by_columns + capacity.order_by_exprs);
        self.order_by_exprs.reserve(capacity.order_by_exprs);
        self
    }

    /// Sets the select items.
    #[must_use]
    pub fn items(mut self, items: Vec<SelectItem>) -> Self {
        self.items = items;
        self
    }

    /// Adds a select item.
    #[must_use]
    pub fn item(mut self, item: SelectItem) -> Self {
        self.items.push(item);
        self
    }

    /// Adds a computed expression with an alias.
    #[must_use]
    pub fn computed(mut self, expr: Expr, alias: impl Into<String>) -> Self {
        self.items.push(SelectItem::computed(expr, alias));
        self
    }

    /// Adds a WHERE clause filter.
    #[must_use]
    pub fn filter(mut self, expr: Expr) -> Self {
        self.filter = Some(expr);
        self
    }

    /// Adds an ORDER BY clause.
    #[must_use]
    pub fn order_by(mut self, column: impl Into<String>, direction: OrderDir) -> Self {
        let order = OrderBy::new(column, direction);
        self.order_by.push(order.clone());
        self.order_by_items.push(OrderByItem::Column(order));
        self
    }

    /// Adds an ORDER BY ASC clause.
    #[must_use]
    pub fn order_by_asc(mut self, column: impl Into<String>) -> Self {
        let order = OrderBy::asc(column);
        self.order_by.push(order.clone());
        self.order_by_items.push(OrderByItem::Column(order));
        self
    }

    /// Adds an ORDER BY DESC clause.
    #[must_use]
    pub fn order_by_desc(mut self, column: impl Into<String>) -> Self {
        let order = OrderBy::desc(column);
        self.order_by.push(order.clone());
        self.order_by_items.push(OrderByItem::Column(order));
        self
    }

    /// Sets the row count (maps to SQL LIMIT).
    ///
    /// Pass a positive value for forward pagination. Negative values signal
    /// backward pagination to callers; the dialect renders the absolute value.
    #[must_use]
    pub fn take(mut self, n: i32) -> Self {
        self.take = Some(n);
        self
    }

    /// Sets the row offset (maps to SQL OFFSET).
    #[must_use]
    pub fn skip(mut self, n: u32) -> Self {
        self.skip = Some(n);
        self
    }

    /// Adds a JOIN clause.
    #[must_use]
    pub fn join(mut self, clause: JoinClause) -> Self {
        self.joins.push(clause);
        self
    }

    /// Adds an INNER JOIN clause.
    #[must_use]
    pub fn inner_join(self, table: impl Into<String>, on: Expr, items: Vec<SelectItem>) -> Self {
        self.join(JoinClause::new(JoinType::Inner, table, on, items))
    }

    /// Adds a LEFT JOIN clause.
    #[must_use]
    pub fn left_join(self, table: impl Into<String>, on: Expr, items: Vec<SelectItem>) -> Self {
        self.join(JoinClause::new(JoinType::Left, table, on, items))
    }

    /// Adds a GROUP BY clause.
    #[must_use]
    pub fn group_by_column(mut self, column: ColumnMarker) -> Self {
        self.group_by.push(column);
        self
    }

    /// Adds multiple columns to the GROUP BY clause.
    #[must_use]
    pub fn group_by(mut self, columns: Vec<ColumnMarker>) -> Self {
        self.group_by.extend(columns);
        self
    }

    /// Sets the HAVING clause (post-GROUP BY filter).
    #[must_use]
    pub fn having(mut self, expr: Expr) -> Self {
        self.having = Some(expr);
        self
    }

    /// Adds an ORDER BY clause using an arbitrary expression (e.g. an aggregate function).
    #[must_use]
    pub fn order_by_expr(mut self, expr: Expr, direction: OrderDir) -> Self {
        self.order_by_exprs.push((expr.clone(), direction));
        self.order_by_items.push(OrderByItem::Expr(expr, direction));
        self
    }

    /// Sets the columns to deduplicate on (SELECT DISTINCT / DISTINCT ON).
    ///
    /// - **Postgres**: emits `SELECT DISTINCT ON (col, ...)` and requires those
    ///   columns to appear first in `ORDER BY` (callers should prepend them).
    /// - **SQLite / MySQL**: emits plain `SELECT DISTINCT`.
    #[must_use]
    pub fn distinct(mut self, columns: Vec<String>) -> Self {
        self.distinct = columns;
        self
    }

    /// Builds the final SELECT query.
    pub fn build(self) -> Result<Select> {
        if self.table.is_empty() {
            return Err(Error::MissingField("table".to_string()));
        }

        Ok(Select {
            table: self.table,
            items: self.items,
            joins: self.joins,
            filter: self.filter,
            order_by: self.order_by,
            take: self.take,
            skip: self.skip,
            group_by: self.group_by,
            distinct: self.distinct,
            having: self.having,
            order_by_items: self.order_by_items,
            order_by_exprs: self.order_by_exprs,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::expr::Expr;

    #[test]
    fn test_order_by() {
        let asc = OrderBy::asc("id");
        assert_eq!(asc.column, "id");
        assert_eq!(asc.direction, OrderDir::Asc);

        let desc = OrderBy::desc("created_at");
        assert_eq!(desc.column, "created_at");
        assert_eq!(desc.direction, OrderDir::Desc);
    }

    #[test]
    fn test_simple_select() {
        let query = Select::from_table("users").build().unwrap();

        assert_eq!(query.table, "users");
        assert!(query.items.is_empty());
        assert!(query.joins.is_empty());
        assert!(query.filter.is_none());
        assert!(query.order_by.is_empty());
        assert!(query.take.is_none());
        assert!(query.skip.is_none());
    }

    #[test]
    fn test_select_with_columns() {
        let query = Select::from_table("users")
            .item(SelectItem::from(ColumnMarker::new("users", "id")))
            .item(SelectItem::from(ColumnMarker::new("users", "email")))
            .build()
            .unwrap();

        assert_eq!(query.items.len(), 2);
        if let SelectItem::Column(col) = &query.items[0] {
            assert_eq!(col.table, "users");
            assert_eq!(col.name, "id");
        }
        if let SelectItem::Column(col) = &query.items[1] {
            assert_eq!(col.table, "users");
            assert_eq!(col.name, "email");
        }
    }

    #[test]
    fn test_select_with_filter() {
        let filter = Expr::column("age").ge(Expr::param(18i64));
        let query = Select::from_table("users")
            .filter(filter.clone())
            .build()
            .unwrap();

        assert_eq!(query.filter, Some(filter));
    }

    #[test]
    fn test_select_with_order_by() {
        let query = Select::from_table("users")
            .order_by_desc("created_at")
            .order_by_asc("email")
            .build()
            .unwrap();

        assert_eq!(query.order_by.len(), 2);
        assert_eq!(query.order_by[0].column, "created_at");
        assert_eq!(query.order_by[0].direction, OrderDir::Desc);
        assert_eq!(query.order_by[1].column, "email");
        assert_eq!(query.order_by[1].direction, OrderDir::Asc);
    }

    #[test]
    fn test_select_with_take_and_skip() {
        let query = Select::from_table("users")
            .take(10)
            .skip(20)
            .build()
            .unwrap();

        assert_eq!(query.take, Some(10));
        assert_eq!(query.skip, Some(20));
    }

    #[test]
    fn test_complex_select() {
        let filter = Expr::column("age")
            .ge(Expr::param(18i64))
            .and(Expr::column("email").like(Expr::param("%@gmail.com")));

        let query = Select::from_table("users")
            .items(vec![
                SelectItem::from(ColumnMarker::new("users", "id")),
                SelectItem::from(ColumnMarker::new("users", "email")),
                SelectItem::from(ColumnMarker::new("users", "age")),
            ])
            .filter(filter)
            .order_by_desc("id")
            .take(10)
            .build()
            .unwrap();

        assert_eq!(query.table, "users");
        assert_eq!(query.items.len(), 3);
        assert!(query.filter.is_some());
        assert_eq!(query.order_by.len(), 1);
        assert_eq!(query.take, Some(10));
    }

    #[test]
    fn test_select_with_inner_join() {
        let on = Expr::column("users__id").eq(Expr::column("posts__user_id"));
        let query = Select::from_table("users")
            .item(SelectItem::from(ColumnMarker::new("users", "id")))
            .inner_join(
                "posts",
                on.clone(),
                vec![
                    SelectItem::from(ColumnMarker::new("posts", "id")),
                    SelectItem::from(ColumnMarker::new("posts", "title")),
                ],
            )
            .build()
            .unwrap();

        assert_eq!(query.joins.len(), 1);
        assert_eq!(query.joins[0].join_type, JoinType::Inner);
        assert_eq!(query.joins[0].table, "posts");
        assert_eq!(query.joins[0].on, on);
        assert_eq!(query.joins[0].items.len(), 2);
    }

    #[test]
    fn test_select_with_left_join() {
        let on = Expr::column("users__id").eq(Expr::column("posts__user_id"));
        let query = Select::from_table("users")
            .item(SelectItem::from(ColumnMarker::new("users", "id")))
            .left_join(
                "posts",
                on,
                vec![SelectItem::from(ColumnMarker::new("posts", "title"))],
            )
            .build()
            .unwrap();

        assert_eq!(query.joins.len(), 1);
        assert_eq!(query.joins[0].join_type, JoinType::Left);
        assert_eq!(query.joins[0].table, "posts");
        assert_eq!(query.joins[0].items.len(), 1);
    }

    #[test]
    fn test_select_with_multiple_joins() {
        let query = Select::from_table("users")
            .inner_join(
                "posts",
                Expr::column("users__id").eq(Expr::column("posts__user_id")),
                vec![SelectItem::from(ColumnMarker::new("posts", "title"))],
            )
            .left_join(
                "comments",
                Expr::column("posts__id").eq(Expr::column("comments__post_id")),
                vec![SelectItem::from(ColumnMarker::new("comments", "body"))],
            )
            .build()
            .unwrap();

        assert_eq!(query.joins.len(), 2);
        assert_eq!(query.joins[0].join_type, JoinType::Inner);
        assert_eq!(query.joins[0].table, "posts");
        assert_eq!(query.joins[1].join_type, JoinType::Left);
        assert_eq!(query.joins[1].table, "comments");
    }
}