sqlmodel-query 0.2.2

Type-safe SQL query builder for SQLModel Rust
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
//! Set operations for combining query results.
//!
//! Provides UNION, UNION ALL, INTERSECT, INTERSECT ALL, EXCEPT, and EXCEPT ALL
//! operations for combining multiple SELECT queries.
//!
//! # Example
//!
//! ```ignore
//! use sqlmodel_query::{select, union, union_all, SetOperation};
//!
//! // UNION - removes duplicates
//! let admins = select!(User).filter(Expr::col("role").eq("admin"));
//! let managers = select!(User).filter(Expr::col("role").eq("manager"));
//! let query = admins.union(managers);
//!
//! // UNION ALL - keeps duplicates
//! let query = union_all([query1, query2, query3]);
//!
//! // With ORDER BY on final result
//! let query = select!(User)
//!     .filter(Expr::col("active").eq(true))
//!     .union(select!(User).filter(Expr::col("premium").eq(true)))
//!     .order_by(Expr::col("name").asc());
//! ```

use crate::clause::OrderBy;
use crate::expr::Dialect;
use sqlmodel_core::Value;

/// Type of set operation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SetOpType {
    /// UNION - combines results, removes duplicates
    Union,
    /// UNION ALL - combines results, keeps duplicates
    UnionAll,
    /// INTERSECT - returns common rows, removes duplicates
    Intersect,
    /// INTERSECT ALL - returns common rows, keeps duplicates
    IntersectAll,
    /// EXCEPT - returns rows in first query not in second, removes duplicates
    Except,
    /// EXCEPT ALL - returns rows in first query not in second, keeps duplicates
    ExceptAll,
}

impl SetOpType {
    /// Get the SQL keyword for this set operation.
    pub const fn as_sql(&self) -> &'static str {
        match self {
            SetOpType::Union => "UNION",
            SetOpType::UnionAll => "UNION ALL",
            SetOpType::Intersect => "INTERSECT",
            SetOpType::IntersectAll => "INTERSECT ALL",
            SetOpType::Except => "EXCEPT",
            SetOpType::ExceptAll => "EXCEPT ALL",
        }
    }
}

/// A set operation combining multiple queries.
#[derive(Debug, Clone)]
pub struct SetOperation {
    /// The queries to combine (in order)
    queries: Vec<(String, Vec<Value>)>,
    /// The type of set operation between consecutive queries
    op_types: Vec<SetOpType>,
    /// Optional ORDER BY on the final result
    order_by: Vec<OrderBy>,
    /// Optional LIMIT on the final result
    limit: Option<u64>,
    /// Optional OFFSET on the final result
    offset: Option<u64>,
}

impl SetOperation {
    /// Create a new set operation from a single query.
    pub fn new(query_sql: impl Into<String>, params: Vec<Value>) -> Self {
        Self {
            queries: vec![(query_sql.into(), params)],
            op_types: Vec::new(),
            order_by: Vec::new(),
            limit: None,
            offset: None,
        }
    }

    /// Add a UNION operation with another query.
    pub fn union(self, query_sql: impl Into<String>, params: Vec<Value>) -> Self {
        self.add_op(SetOpType::Union, query_sql, params)
    }

    /// Add a UNION ALL operation with another query.
    pub fn union_all(self, query_sql: impl Into<String>, params: Vec<Value>) -> Self {
        self.add_op(SetOpType::UnionAll, query_sql, params)
    }

    /// Add an INTERSECT operation with another query.
    pub fn intersect(self, query_sql: impl Into<String>, params: Vec<Value>) -> Self {
        self.add_op(SetOpType::Intersect, query_sql, params)
    }

    /// Add an INTERSECT ALL operation with another query.
    pub fn intersect_all(self, query_sql: impl Into<String>, params: Vec<Value>) -> Self {
        self.add_op(SetOpType::IntersectAll, query_sql, params)
    }

    /// Add an EXCEPT operation with another query.
    pub fn except(self, query_sql: impl Into<String>, params: Vec<Value>) -> Self {
        self.add_op(SetOpType::Except, query_sql, params)
    }

    /// Add an EXCEPT ALL operation with another query.
    pub fn except_all(self, query_sql: impl Into<String>, params: Vec<Value>) -> Self {
        self.add_op(SetOpType::ExceptAll, query_sql, params)
    }

    fn add_op(mut self, op: SetOpType, query_sql: impl Into<String>, params: Vec<Value>) -> Self {
        self.op_types.push(op);
        self.queries.push((query_sql.into(), params));
        self
    }

    /// Add ORDER BY to the final result.
    pub fn order_by(mut self, order: OrderBy) -> Self {
        self.order_by.push(order);
        self
    }

    /// Add multiple ORDER BY clauses.
    pub fn order_by_many(mut self, orders: Vec<OrderBy>) -> Self {
        self.order_by.extend(orders);
        self
    }

    /// Set LIMIT on the final result.
    pub fn limit(mut self, limit: u64) -> Self {
        self.limit = Some(limit);
        self
    }

    /// Set OFFSET on the final result.
    pub fn offset(mut self, offset: u64) -> Self {
        self.offset = Some(offset);
        self
    }

    /// Build the SQL query with default dialect (PostgreSQL).
    pub fn build(&self) -> (String, Vec<Value>) {
        self.build_with_dialect(Dialect::Postgres)
    }

    /// Build the SQL query with a specific dialect.
    pub fn build_with_dialect(&self, dialect: Dialect) -> (String, Vec<Value>) {
        let mut sql = String::new();
        let mut params = Vec::new();

        // Build each query with set operations between them
        for (i, (query_sql, query_params)) in self.queries.iter().enumerate() {
            if i > 0 {
                // Add the set operation before this query
                let op = &self.op_types[i - 1];
                sql.push(' ');
                sql.push_str(op.as_sql());
                sql.push(' ');
            }

            // Wrap each query in parentheses for clarity
            sql.push('(');
            sql.push_str(query_sql);
            sql.push(')');

            params.extend(query_params.clone());
        }

        // ORDER BY on final result
        if !self.order_by.is_empty() {
            sql.push_str(" ORDER BY ");
            let order_strs: Vec<String> = self
                .order_by
                .iter()
                .map(|o| {
                    let expr_sql = o.expr.build_with_dialect(dialect, &mut params, 0);
                    let dir = match o.direction {
                        crate::clause::OrderDirection::Asc => "ASC",
                        crate::clause::OrderDirection::Desc => "DESC",
                    };
                    let nulls = match o.nulls {
                        Some(crate::clause::NullsOrder::First) => " NULLS FIRST",
                        Some(crate::clause::NullsOrder::Last) => " NULLS LAST",
                        None => "",
                    };
                    format!("{expr_sql} {dir}{nulls}")
                })
                .collect();
            sql.push_str(&order_strs.join(", "));
        }

        // LIMIT
        if let Some(limit) = self.limit {
            sql.push_str(" LIMIT ");
            sql.push_str(&limit.to_string());
        }

        // OFFSET
        if let Some(offset) = self.offset {
            sql.push_str(" OFFSET ");
            sql.push_str(&offset.to_string());
        }

        (sql, params)
    }
}

/// Create a UNION of multiple queries.
///
/// Returns `None` if the iterator is empty.
///
/// # Example
///
/// ```ignore
/// let query = union([
///     ("SELECT * FROM users WHERE role = 'admin'", vec![]),
///     ("SELECT * FROM users WHERE role = 'manager'", vec![]),
/// ]).expect("at least one query required");
/// ```
pub fn union<I, S>(queries: I) -> Option<SetOperation>
where
    I: IntoIterator<Item = (S, Vec<Value>)>,
    S: Into<String>,
{
    combine_queries(SetOpType::Union, queries)
}

/// Create a UNION ALL of multiple queries.
///
/// Returns `None` if the iterator is empty.
///
/// # Example
///
/// ```ignore
/// let query = union_all([
///     ("SELECT id FROM table1", vec![]),
///     ("SELECT id FROM table2", vec![]),
///     ("SELECT id FROM table3", vec![]),
/// ]).expect("at least one query required");
/// ```
pub fn union_all<I, S>(queries: I) -> Option<SetOperation>
where
    I: IntoIterator<Item = (S, Vec<Value>)>,
    S: Into<String>,
{
    combine_queries(SetOpType::UnionAll, queries)
}

/// Create an INTERSECT of multiple queries.
///
/// Returns `None` if the iterator is empty.
pub fn intersect<I, S>(queries: I) -> Option<SetOperation>
where
    I: IntoIterator<Item = (S, Vec<Value>)>,
    S: Into<String>,
{
    combine_queries(SetOpType::Intersect, queries)
}

/// Create an INTERSECT ALL of multiple queries.
///
/// Returns `None` if the iterator is empty.
pub fn intersect_all<I, S>(queries: I) -> Option<SetOperation>
where
    I: IntoIterator<Item = (S, Vec<Value>)>,
    S: Into<String>,
{
    combine_queries(SetOpType::IntersectAll, queries)
}

/// Create an EXCEPT of multiple queries.
///
/// Returns `None` if the iterator is empty.
pub fn except<I, S>(queries: I) -> Option<SetOperation>
where
    I: IntoIterator<Item = (S, Vec<Value>)>,
    S: Into<String>,
{
    combine_queries(SetOpType::Except, queries)
}

/// Create an EXCEPT ALL of multiple queries.
///
/// Returns `None` if the iterator is empty.
pub fn except_all<I, S>(queries: I) -> Option<SetOperation>
where
    I: IntoIterator<Item = (S, Vec<Value>)>,
    S: Into<String>,
{
    combine_queries(SetOpType::ExceptAll, queries)
}

fn combine_queries<I, S>(op: SetOpType, queries: I) -> Option<SetOperation>
where
    I: IntoIterator<Item = (S, Vec<Value>)>,
    S: Into<String>,
{
    let mut iter = queries.into_iter();

    // Get the first query, return None if empty
    let (first_sql, first_params) = iter.next()?;

    let mut result = SetOperation::new(first_sql, first_params);

    // Add remaining queries with the set operation
    for (sql, params) in iter {
        result = result.add_op(op, sql, params);
    }

    Some(result)
}

// ==================== Tests ====================

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

    #[test]
    fn test_union_basic() {
        let query = SetOperation::new("SELECT * FROM users WHERE role = 'admin'", vec![])
            .union("SELECT * FROM users WHERE role = 'manager'", vec![]);

        let (sql, params) = query.build();
        assert_eq!(
            sql,
            "(SELECT * FROM users WHERE role = 'admin') UNION (SELECT * FROM users WHERE role = 'manager')"
        );
        assert!(params.is_empty());
    }

    #[test]
    fn test_union_all_basic() {
        let query = SetOperation::new("SELECT id FROM table1", vec![])
            .union_all("SELECT id FROM table2", vec![]);

        let (sql, _) = query.build();
        assert_eq!(
            sql,
            "(SELECT id FROM table1) UNION ALL (SELECT id FROM table2)"
        );
    }

    #[test]
    fn test_union_with_params() {
        let query = SetOperation::new(
            "SELECT * FROM users WHERE role = $1",
            vec![Value::Text("admin".to_string())],
        )
        .union(
            "SELECT * FROM users WHERE role = $2",
            vec![Value::Text("manager".to_string())],
        );

        let (sql, params) = query.build();
        assert_eq!(params.len(), 2);
        assert_eq!(params[0], Value::Text("admin".to_string()));
        assert_eq!(params[1], Value::Text("manager".to_string()));
        assert!(sql.contains("$1"));
        assert!(sql.contains("$2"));
    }

    #[test]
    fn test_union_function() {
        let query = union([
            ("SELECT * FROM admins", vec![]),
            ("SELECT * FROM managers", vec![]),
            ("SELECT * FROM employees", vec![]),
        ])
        .expect("non-empty iterator");

        let (sql, _) = query.build();
        assert!(sql.contains("UNION"));
        assert!(!sql.contains("UNION ALL"));
        assert!(sql.contains("admins"));
        assert!(sql.contains("managers"));
        assert!(sql.contains("employees"));
    }

    #[test]
    fn test_union_all_function() {
        let query = union_all([
            ("SELECT 1", vec![]),
            ("SELECT 2", vec![]),
            ("SELECT 3", vec![]),
        ])
        .expect("non-empty iterator");

        let (sql, _) = query.build();
        // Should have two UNION ALL operations
        assert_eq!(sql.matches("UNION ALL").count(), 2);
    }

    #[test]
    fn test_union_empty_returns_none() {
        let empty: Vec<(&str, Vec<Value>)> = vec![];
        assert!(union(empty).is_none());
    }

    #[test]
    fn test_union_with_order_by() {
        let query = SetOperation::new("SELECT name FROM users WHERE active = true", vec![])
            .union("SELECT name FROM users WHERE premium = true", vec![])
            .order_by(Expr::col("name").asc());

        let (sql, _) = query.build();
        assert!(sql.ends_with("ORDER BY \"name\" ASC"));
    }

    #[test]
    fn test_union_with_limit_offset() {
        let query = SetOperation::new("SELECT * FROM t1", vec![])
            .union("SELECT * FROM t2", vec![])
            .limit(10)
            .offset(5);

        let (sql, _) = query.build();
        assert!(sql.ends_with("LIMIT 10 OFFSET 5"));
    }

    #[test]
    fn test_intersect() {
        let query = SetOperation::new("SELECT id FROM users WHERE active = true", vec![])
            .intersect("SELECT id FROM users WHERE premium = true", vec![]);

        let (sql, _) = query.build();
        assert!(sql.contains("INTERSECT"));
        assert!(!sql.contains("INTERSECT ALL"));
    }

    #[test]
    fn test_intersect_all() {
        let query = intersect_all([("SELECT id FROM t1", vec![]), ("SELECT id FROM t2", vec![])])
            .expect("non-empty iterator");

        let (sql, _) = query.build();
        assert!(sql.contains("INTERSECT ALL"));
    }

    #[test]
    fn test_except() {
        let query = SetOperation::new("SELECT id FROM all_users", vec![])
            .except("SELECT id FROM banned_users", vec![]);

        let (sql, _) = query.build();
        assert!(sql.contains("EXCEPT"));
        assert!(!sql.contains("EXCEPT ALL"));
    }

    #[test]
    fn test_except_all() {
        let query = except_all([("SELECT id FROM t1", vec![]), ("SELECT id FROM t2", vec![])])
            .expect("non-empty iterator");

        let (sql, _) = query.build();
        assert!(sql.contains("EXCEPT ALL"));
    }

    #[test]
    fn test_chained_operations() {
        let query = SetOperation::new("SELECT id FROM t1", vec![])
            .union("SELECT id FROM t2", vec![])
            .union_all("SELECT id FROM t3", vec![]);

        let (sql, _) = query.build();
        // First should be UNION, second should be UNION ALL
        let union_pos = sql.find("UNION").unwrap();
        let union_all_pos = sql.find("UNION ALL").unwrap();
        assert!(union_pos < union_all_pos);
    }

    #[test]
    fn test_complex_query() {
        let query = SetOperation::new(
            "SELECT name, email FROM users WHERE role = $1",
            vec![Value::Text("admin".to_string())],
        )
        .union_all(
            "SELECT name, email FROM users WHERE department = $2",
            vec![Value::Text("engineering".to_string())],
        )
        .order_by(Expr::col("name").asc())
        .order_by(Expr::col("email").desc())
        .limit(100)
        .offset(0);

        let (sql, params) = query.build();

        assert!(sql.contains("UNION ALL"));
        assert!(sql.contains("ORDER BY"));
        assert!(sql.contains("LIMIT 100"));
        assert!(sql.contains("OFFSET 0"));
        assert_eq!(params.len(), 2);
    }

    #[test]
    fn test_set_op_type_sql() {
        assert_eq!(SetOpType::Union.as_sql(), "UNION");
        assert_eq!(SetOpType::UnionAll.as_sql(), "UNION ALL");
        assert_eq!(SetOpType::Intersect.as_sql(), "INTERSECT");
        assert_eq!(SetOpType::IntersectAll.as_sql(), "INTERSECT ALL");
        assert_eq!(SetOpType::Except.as_sql(), "EXCEPT");
        assert_eq!(SetOpType::ExceptAll.as_sql(), "EXCEPT ALL");
    }
}