torm 0.3.0

A Tokio-based async ORM for Rust with GORM-like API, supporting SQLite, MySQL and PostgreSQL via native wire protocols
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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
use crate::db::db_types::SqlValue;
use crate::orm::query::{OrderDirection, WhereCondition};
use crate::utils::sql_safety::{validate_identifier, validate_qualified_identifier};
use std::collections::HashMap;

/// 校验并规范化列/表达式标识符(允许 `users.id`、`COUNT(*)`)。
fn safe_column(col: &str) -> String {
    validate_qualified_identifier(col).unwrap_or_else(|e| {
        eprintln!("[torm::sql_safety] rejected unsafe column: {e}");
        String::new()
    })
}

/// 校验并规范化表名标识符。
///
/// 表名可能携带别名(如 `roles r`、`roles AS r`),因此使用宽松校验:
/// 允许字母/数字/`_`/`$`/`.`/空格(用于别名)与聚合括号,但仍拒绝
/// `;`、单引号、注释及危险关键字等注入特征。
fn safe_table(id: &str) -> String {
    validate_qualified_identifier(id).unwrap_or_else(|e| {
        eprintln!("[torm::sql_safety] rejected unsafe table: {e}");
        String::new()
    })
}

/// 校验并规范化别名标识符(单一标识符,严格校验)。
fn safe_identifier(id: &str) -> String {
    validate_identifier(id).unwrap_or_else(|e| {
        eprintln!("[torm::sql_safety] rejected unsafe identifier: {e}");
        String::new()
    })
}

/// 高级查询构建器
pub struct AdvancedQuery {
    table_name: String,
    joins: Vec<JoinClause>,
    group_bys: Vec<String>,
    having: Option<WhereCondition>,
    distinct: bool,
    select_columns: Vec<String>,
    aggregations: Vec<AggregationClause>,
    unions: Vec<UnionClause>,
    where_conditions: Vec<WhereCondition>,
    orders: Vec<OrderClause>,
    pagination: Option<Pagination>,
}

#[derive(Debug, Clone)]
pub struct JoinClause {
    pub join_type: JoinType,
    pub table_name: String,
    pub alias: Option<String>,
    pub on_condition: String,
}

#[derive(Debug, Clone, Copy)]
pub enum JoinType {
    Inner,
    Left,
    Right,
    Full,
}

impl JoinType {
    pub fn as_str(&self) -> &str {
        match self {
            JoinType::Inner => "INNER JOIN",
            JoinType::Left => "LEFT JOIN",
            JoinType::Right => "RIGHT JOIN",
            JoinType::Full => "FULL JOIN",
        }
    }
}

#[derive(Debug, Clone)]
pub struct AggregationClause {
    pub function: AggFunction,
    pub column: String,
    pub alias: Option<String>,
}

#[derive(Debug, Clone, Copy)]
pub enum AggFunction {
    Count,
    Sum,
    Avg,
    Min,
    Max,
}

impl AggFunction {
    pub fn as_str(&self) -> &str {
        match self {
            AggFunction::Count => "COUNT",
            AggFunction::Sum => "SUM",
            AggFunction::Avg => "AVG",
            AggFunction::Min => "MIN",
            AggFunction::Max => "MAX",
        }
    }
}

#[derive(Debug, Clone)]
pub struct UnionClause {
    pub query: String,
    pub all: bool,
}

#[derive(Debug, Clone)]
pub struct OrderClause {
    pub column: String,
    pub direction: OrderDirection,
}

#[derive(Debug, Clone)]
pub struct Pagination {
    pub limit: u64,
    pub offset: u64,
}

impl AdvancedQuery {
    pub fn new(table_name: &str) -> Self {
        Self {
            table_name: safe_table(table_name),
            joins: Vec::new(),
            group_bys: Vec::new(),
            having: None,
            distinct: false,
            select_columns: Vec::new(),
            aggregations: Vec::new(),
            unions: Vec::new(),
            where_conditions: Vec::new(),
            orders: Vec::new(),
            pagination: None,
        }
    }

    /// SELECT 特定列
    pub fn select(mut self, columns: &[&str]) -> Self {
        self.select_columns = columns.iter().map(|s| safe_column(s)).collect();
        self
    }

    /// DISTINCT 查询
    pub fn distinct(mut self) -> Self {
        self.distinct = true;
        self
    }

    /// INNER JOIN
    pub fn inner_join(mut self, table_name: &str, on_condition: &str) -> Self {
        self.joins.push(JoinClause {
            join_type: JoinType::Inner,
            table_name: safe_table(table_name),
            alias: None,
            on_condition: on_condition.to_string(),
        });
        self
    }

    /// LEFT JOIN
    pub fn left_join(mut self, table_name: &str, on_condition: &str) -> Self {
        self.joins.push(JoinClause {
            join_type: JoinType::Left,
            table_name: safe_table(table_name),
            alias: None,
            on_condition: on_condition.to_string(),
        });
        self
    }

    /// RIGHT JOIN
    pub fn right_join(mut self, table_name: &str, on_condition: &str) -> Self {
        self.joins.push(JoinClause {
            join_type: JoinType::Right,
            table_name: safe_table(table_name),
            alias: None,
            on_condition: on_condition.to_string(),
        });
        self
    }

    /// FULL JOIN
    pub fn full_join(mut self, table_name: &str, on_condition: &str) -> Self {
        self.joins.push(JoinClause {
            join_type: JoinType::Full,
            table_name: safe_table(table_name),
            alias: None,
            on_condition: on_condition.to_string(),
        });
        self
    }

    /// JOIN 带别名
    pub fn join_alias(mut self, join_type: JoinType, table_name: &str, alias: &str, on_condition: &str) -> Self {
        self.joins.push(JoinClause {
            join_type,
            table_name: safe_table(table_name),
            alias: Some(safe_identifier(alias)),
            on_condition: on_condition.to_string(),
        });
        self
    }

    /// GROUP BY
    pub fn group_by(mut self, columns: &[&str]) -> Self {
        self.group_bys = columns.iter().map(|s| safe_column(s)).collect();
        self
    }

    /// HAVING
    pub fn having(mut self, condition: WhereCondition) -> Self {
        self.having = Some(condition);
        self
    }

    /// 聚合函数
    pub fn count(mut self, column: &str) -> Self {
        self.aggregations.push(AggregationClause {
            function: AggFunction::Count,
            column: safe_column(column),
            alias: None,
        });
        self
    }

    pub fn sum(mut self, column: &str) -> Self {
        self.aggregations.push(AggregationClause {
            function: AggFunction::Sum,
            column: safe_column(column),
            alias: None,
        });
        self
    }

    pub fn avg(mut self, column: &str) -> Self {
        self.aggregations.push(AggregationClause {
            function: AggFunction::Avg,
            column: safe_column(column),
            alias: None,
        });
        self
    }

    pub fn min(mut self, column: &str) -> Self {
        self.aggregations.push(AggregationClause {
            function: AggFunction::Min,
            column: safe_column(column),
            alias: None,
        });
        self
    }

    pub fn max(mut self, column: &str) -> Self {
        self.aggregations.push(AggregationClause {
            function: AggFunction::Max,
            column: safe_column(column),
            alias: None,
        });
        self
    }

    /// UNION
    pub fn union(mut self, query: &str) -> Self {
        self.unions.push(UnionClause {
            query: query.to_string(),
            all: false,
        });
        self
    }

    /// UNION ALL
    pub fn union_all(mut self, query: &str) -> Self {
        self.unions.push(UnionClause {
            query: query.to_string(),
            all: true,
        });
        self
    }

    /// WHERE 条件
    pub fn where_eq(mut self, column: &str, value: impl Into<SqlValue>) -> Self {
        self.where_conditions.push(WhereCondition::Eq(column.to_string(), value.into()));
        self
    }

    pub fn where_gt(mut self, column: &str, value: impl Into<SqlValue>) -> Self {
        self.where_conditions.push(WhereCondition::Gt(column.to_string(), value.into()));
        self
    }

    pub fn where_lt(mut self, column: &str, value: impl Into<SqlValue>) -> Self {
        self.where_conditions.push(WhereCondition::Lt(column.to_string(), value.into()));
        self
    }

    pub fn where_ne(mut self, column: &str, value: impl Into<SqlValue>) -> Self {
        self.where_conditions.push(WhereCondition::Ne(column.to_string(), value.into()));
        self
    }

    pub fn where_like(mut self, column: &str, pattern: impl Into<SqlValue>) -> Self {
        self.where_conditions.push(WhereCondition::Like(column.to_string(), pattern.into()));
        self
    }

    pub fn where_in(mut self, column: &str, values: Vec<SqlValue>) -> Self {
        self.where_conditions.push(WhereCondition::In(
            column.to_string(),
            values,
        ));
        self
    }

    pub fn where_between(mut self, column: &str, min: impl Into<SqlValue>, max: impl Into<SqlValue>) -> Self {
        self.where_conditions.push(WhereCondition::Between(
            column.to_string(),
            min.into(),
            max.into(),
        ));
        self
    }

    pub fn where_null(mut self, column: &str) -> Self {
        self.where_conditions.push(WhereCondition::IsNull(column.to_string()));
        self
    }

    pub fn where_raw(mut self, condition: &str) -> Self {
        self.where_conditions.push(WhereCondition::Raw(condition.to_string()));
        self
    }

    /// ORDER BY
    pub fn order_by(mut self, column: &str, direction: OrderDirection) -> Self {
        self.orders.push(OrderClause {
            column: safe_column(column),
            direction,
        });
        self
    }

    pub fn order_by_asc(self, column: &str) -> Self {
        self.order_by(column, OrderDirection::Asc)
    }

    pub fn order_by_desc(self, column: &str) -> Self {
        self.order_by(column, OrderDirection::Desc)
    }

    /// LIMIT 和 OFFSET
    pub fn limit(mut self, limit: u64) -> Self {
        self.pagination = Some(Pagination {
            limit,
            offset: self.pagination.as_ref().map_or(0, |p| p.offset),
        });
        self
    }

    pub fn offset(mut self, offset: u64) -> Self {
        self.pagination = Some(Pagination {
            limit: self.pagination.as_ref().map_or(u64::MAX, |p| p.limit),
            offset,
        });
        self
    }

    pub fn paginate(mut self, page: u64, per_page: u64) -> Self {
        self.pagination = Some(Pagination {
            limit: per_page,
            offset: (page - 1) * per_page,
        });
        self
    }

    /// 构建 SELECT 查询
    pub fn build_select(&self) -> (String, Vec<SqlValue>) {
        let mut query = String::new();
        let mut bindings: Vec<SqlValue> = Vec::new();

        // SELECT clause
        query.push_str("SELECT ");
        
        if self.distinct {
            query.push_str("DISTINCT ");
        }

        if !self.select_columns.is_empty() {
            query.push_str(&self.select_columns.join(", "));
        } else if !self.aggregations.is_empty() {
            let agg_parts: Vec<String> = self.aggregations.iter()
                .map(|agg| {
                    let alias = agg.alias.as_ref()
                        .map(|a| format!(" AS {}", a))
                        .unwrap_or_default();
                    format!("{}({}){}", agg.function.as_str(), agg.column, alias)
                })
                .collect();
            query.push_str(&agg_parts.join(", "));
        } else {
            query.push_str("*");
        }

        // FROM clause
        query.push_str(&format!(" FROM {}", self.table_name));

        // JOINs
        for join in &self.joins {
            query.push_str(&format!(" {} {}", join.join_type.as_str(), join.table_name));
            if let Some(alias) = &join.alias {
                query.push_str(&format!(" AS {}", alias));
            }
            query.push_str(&format!(" ON {}", join.on_condition));
        }

        // WHERE clause
        if !self.where_conditions.is_empty() {
            query.push_str(" WHERE ");
            let mut where_clauses = Vec::new();
            for condition in &self.where_conditions {
                let (clause, mut b) = condition.to_sql();
                where_clauses.push(clause);
                bindings.append(&mut b);
            }
            query.push_str(&where_clauses.join(" AND "));
        }

        // GROUP BY
        if !self.group_bys.is_empty() {
            query.push_str(&format!(" GROUP BY {}", self.group_bys.join(", ")));
        }

        // HAVING
        if let Some(condition) = &self.having {
            let (clause, mut b) = condition.to_sql();
            query.push_str(&format!(" HAVING {}", clause));
            bindings.append(&mut b);
        }

        // ORDER BY
        if !self.orders.is_empty() {
            let order_parts: Vec<String> = self.orders.iter()
                .map(|o| format!("{} {}", o.column, o.direction.as_str()))
                .collect();
            query.push_str(&format!(" ORDER BY {}", order_parts.join(", ")));
        }

        // LIMIT and OFFSET
        if let Some(pag) = &self.pagination {
            query.push_str(&format!(" LIMIT {}", pag.limit));
            if pag.offset > 0 {
                query.push_str(&format!(" OFFSET {}", pag.offset));
            }
        }

        // UNIONs
        for union_clause in &self.unions {
            let union_type = if union_clause.all { "UNION ALL" } else { "UNION" };
            query.push_str(&format!(" {} ({})", union_type, union_clause.query));
        }

        (query, bindings)
    }

    /// 构建 COUNT 查询
    pub fn build_count(&self) -> (String, Vec<SqlValue>) {
        let mut query = format!("SELECT COUNT(*) FROM {}", self.table_name);
        let mut bindings: Vec<SqlValue> = Vec::new();

        // JOINs
        for join in &self.joins {
            query.push_str(&format!(" {} {}", join.join_type.as_str(), join.table_name));
            query.push_str(&format!(" ON {}", join.on_condition));
        }

        // WHERE clause
        if !self.where_conditions.is_empty() {
            query.push_str(" WHERE ");
            let mut where_clauses = Vec::new();
            for condition in &self.where_conditions {
                let (clause, mut b) = condition.to_sql();
                where_clauses.push(clause);
                bindings.append(&mut b);
            }
            query.push_str(&where_clauses.join(" AND "));
        }

        // GROUP BY
        if !self.group_bys.is_empty() {
            query.push_str(&format!(" GROUP BY {}", self.group_bys.join(", ")));
        }

        (query, bindings)
    }

    /// 构建 UPDATE 查询
    pub fn build_update(&self, updates: &HashMap<String, SqlValue>) -> (String, Vec<SqlValue>) {
        let mut query = format!("UPDATE {} SET ", self.table_name);
        let mut bindings: Vec<SqlValue> = Vec::new();

        let mut set_clauses = Vec::new();
        for (column, value) in updates {
            set_clauses.push(format!("{} = ?", safe_column(column)));
            bindings.push(value.clone());
        }
        query.push_str(&set_clauses.join(", "));

        // WHERE clause
        if !self.where_conditions.is_empty() {
            query.push_str(" WHERE ");
            let mut where_clauses = Vec::new();
            for condition in &self.where_conditions {
                let (clause, mut b) = condition.to_sql();
                where_clauses.push(clause);
                bindings.append(&mut b);
            }
            query.push_str(&where_clauses.join(" AND "));
        }

        (query, bindings)
    }

    /// 构建 DELETE 查询
    pub fn build_delete(&self) -> (String, Vec<SqlValue>) {
        let mut query = format!("DELETE FROM {}", self.table_name);
        let mut bindings: Vec<SqlValue> = Vec::new();

        // WHERE clause
        if !self.where_conditions.is_empty() {
            query.push_str(" WHERE ");
            let mut where_clauses = Vec::new();
            for condition in &self.where_conditions {
                let (clause, mut b) = condition.to_sql();
                where_clauses.push(clause);
                bindings.append(&mut b);
            }
            query.push_str(&where_clauses.join(" AND "));
        }

        (query, bindings)
    }
}

impl Default for AdvancedQuery {
    fn default() -> Self {
        Self::new("")
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::orm::query::WhereCondition;

    #[test]
    fn test_inner_join() {
        let query = AdvancedQuery::new("users")
            .select(&["users.id", "users.name", "posts.title"])
            .inner_join("posts", "users.id = posts.user_id")
            .where_eq("users.status", "active");

        let (sql, _) = query.build_select();
        assert!(sql.contains("SELECT users.id, users.name, posts.title"));
        assert!(sql.contains("INNER JOIN posts"));
        assert!(sql.contains("ON users.id = posts.user_id"));
        assert!(sql.contains("WHERE users.status = ?"));
    }

    #[test]
    fn test_left_join() {
        let query = AdvancedQuery::new("users")
            .left_join("posts", "users.id = posts.user_id")
            .where_null("posts.id");

        let (sql, _) = query.build_select();
        assert!(sql.contains("LEFT JOIN posts"));
        assert!(sql.contains("ON users.id = posts.user_id"));
        assert!(sql.contains("posts.id IS NULL"));
    }

    #[test]
    fn test_group_by_and_having() {
        let query = AdvancedQuery::new("orders")
            .select(&["user_id", "COUNT(*) as order_count", "SUM(total) as total_amount"])
            .group_by(&["user_id"])
            .having(WhereCondition::Gt("COUNT(*)".to_string(), SqlValue::String("5".to_string())));

        let (sql, _) = query.build_select();
        assert!(sql.contains("SELECT user_id, COUNT(*) as order_count, SUM(total) as total_amount"));
        assert!(sql.contains("GROUP BY user_id"));
        assert!(sql.contains("HAVING COUNT(*) > ?"));
    }

    #[test]
    fn test_aggregation_functions() {
        let query = AdvancedQuery::new("products")
            .count("*")
            .sum("price")
            .avg("rating")
            .min("price")
            .max("price");

        let (sql, _) = query.build_select();
        assert!(sql.contains("SELECT"));
        assert!(sql.contains("COUNT(*)"));
        assert!(sql.contains("SUM(price)"));
        assert!(sql.contains("AVG(rating)"));
        assert!(sql.contains("MIN(price)"));
        assert!(sql.contains("MAX(price)"));
    }

    #[test]
    fn test_distinct_query() {
        let query = AdvancedQuery::new("orders")
            .distinct()
            .select(&["user_id"])
            .where_eq("status", "completed");

        let (sql, _) = query.build_select();
        assert!(sql.contains("SELECT DISTINCT user_id"));
    }

    #[test]
    fn test_union() {
        let query = AdvancedQuery::new("users")
            .union("SELECT id, name FROM admins")
            .where_eq("status", "active");

        let (sql, _) = query.build_select();
        assert!(sql.contains("UNION"));
        assert!(sql.contains("SELECT id, name FROM admins"));
    }

    #[test]
    fn test_union_all() {
        let query = AdvancedQuery::new("users")
            .union_all("SELECT id, name FROM admins");

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

    #[test]
    fn test_complex_join_with_multiple_conditions() {
        let query = AdvancedQuery::new("users")
            .select(&["users.id", "users.name", "orders.order_id", "orders.total"])
            .inner_join("orders", "users.id = orders.user_id")
            .inner_join("order_items", "orders.order_id = order_items.order_id")
            .where_gt("orders.total", "100")
            .where_eq("orders.status", "completed")
            .order_by_desc("orders.created_at");

        let (sql, _) = query.build_select();
        assert!(sql.contains("INNER JOIN orders"));
        assert!(sql.contains("INNER JOIN order_items"));
        assert!(sql.contains("orders.total > ?"));
        assert!(sql.contains("orders.status = ?"));
        assert!(sql.contains("ORDER BY orders.created_at DESC"));
    }

    #[test]
    fn test_join_with_alias() {
        let query = AdvancedQuery::new("users")
            .join_alias(JoinType::Left, "user_profiles", "profiles", "users.id = profiles.user_id")
            .select(&["users.id", "profiles.bio"]);

        let (sql, _) = query.build_select();
        assert!(sql.contains("LEFT JOIN user_profiles AS profiles"));
        assert!(sql.contains("ON users.id = profiles.user_id"));
    }

    #[test]
    fn test_group_by_multiple_columns() {
        let query = AdvancedQuery::new("sales")
            .select(&["category", "region", "SUM(amount) as total_sales"])
            .group_by(&["category", "region"])
            .order_by_desc("total_sales");

        let (sql, _) = query.build_select();
        assert!(sql.contains("GROUP BY category, region"));
        assert!(sql.contains("ORDER BY total_sales DESC"));
    }

    #[test]
    fn test_count_with_joins() {
        let query = AdvancedQuery::new("users")
            .inner_join("posts", "users.id = posts.user_id")
            .where_eq("posts.status", "published");

        let (sql, _) = query.build_count();
        assert!(sql.contains("SELECT COUNT(*) FROM users"));
        assert!(sql.contains("INNER JOIN posts"));
        assert!(sql.contains("posts.status = ?"));
    }

    #[test]
    fn test_update_with_joins() {
        let query = AdvancedQuery::new("users")
            .inner_join("orders", "users.id = orders.user_id")
            .where_eq("orders.status", "pending");

        let mut updates = HashMap::new();
        updates.insert("users.status".to_string(), SqlValue::String("inactive".to_string()));

        let (sql, _) = query.build_update(&updates);
        assert!(sql.contains("UPDATE users SET"));
        assert!(sql.contains("users.status = ?"));
        assert!(sql.contains("WHERE"));
    }

    #[test]
    fn test_delete_with_multiple_conditions() {
        let query = AdvancedQuery::new("users")
            .where_lt("created_at", "2023-01-01")
            .where_eq("status", "inactive")
            .where_null("last_login");

        let (sql, _) = query.build_delete();
        assert!(sql.contains("DELETE FROM users"));
        assert!(sql.contains("WHERE"));
        assert!(sql.contains("created_at < ?"));
        assert!(sql.contains("status = ?"));
        assert!(sql.contains("last_login IS NULL"));
    }
}