rust-ef 1.5.3

Rust Entity Framework - An EFCore-inspired ORM for 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
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
//! Regression tests for v1.1 HAVING placeholder and LIMIT/OFFSET pagination
//! dialect fixes.
//!
//! These tests verify that:
//! - `HavingExpr::to_sql` uses the provider-specific placeholder syntax
//!   (`?` for SQLite/MySQL, `$N` for PostgreSQL) instead of hardcoding `?`.
//! - The PostgreSQL `$N` index continues contiguously from the WHERE clause
//!   into the HAVING clause (e.g. WHERE `$1` → HAVING `$2`).
//! - `QueryState::to_sql_with` delegates LIMIT/OFFSET generation to
//!   `gen.pagination()` so each dialect emits its correct clause order:
//!     - SQLite/MySQL: `LIMIT x OFFSET y`
//!     - PostgreSQL: `OFFSET y LIMIT x`
//!     - MySQL offset-only: `LIMIT 18446744073709551615 OFFSET y`
//!
//! See `迭代计划_v1.1_plus_plan.md` tasks 3.4.1, 3.4.2, 3.4.3.

use rust_ef::provider::{DbValue, ISqlGenerator};
use rust_ef::query::{AggKind, BoolExpr, CompareOp, FilterCondition, HavingExpr, QueryState};

// ---------------------------------------------------------------------------
// Mock generators mimicking each provider's dialect behavior
// ---------------------------------------------------------------------------

/// Mimics PostgreSQL: `$N` placeholders, `OFFSET x LIMIT y` pagination order.
struct PgLikeGenerator;

impl ISqlGenerator for PgLikeGenerator {
    fn select(&self, _: &str, _: &[&str]) -> String {
        String::new()
    }
    fn insert(&self, _: &str, _: &[&str], _: bool) -> String {
        String::new()
    }
    fn update(&self, _: &str, _: &[&str], _: &str) -> String {
        String::new()
    }
    fn delete(&self, _: &str, _: &str) -> String {
        String::new()
    }
    fn create_table(&self, _: &str, _: &[(String, String)]) -> String {
        String::new()
    }
    fn drop_table(&self, _: &str) -> String {
        String::new()
    }
    fn pagination(&self, skip: Option<usize>, take: Option<usize>) -> String {
        match (skip, take) {
            (Some(s), Some(t)) => format!("OFFSET {} LIMIT {}", s, t),
            (Some(s), None) => format!("OFFSET {}", s),
            (None, Some(t)) => format!("LIMIT {}", t),
            (None, None) => String::new(),
        }
    }
    fn parameter_placeholder(&self, index: usize) -> String {
        format!("${}", index)
    }
    fn quote_identifier(&self, identifier: &str) -> String {
        format!("\"{}\"", identifier)
    }
    fn auto_increment_syntax(&self) -> &'static str {
        "SERIAL"
    }
}

/// Mimics MySQL: `?` placeholders, `LIMIT x OFFSET y` order, special
/// offset-only case using `LIMIT 18446744073709551615 OFFSET y`.
struct MySqlLikeGenerator;

impl ISqlGenerator for MySqlLikeGenerator {
    fn select(&self, _: &str, _: &[&str]) -> String {
        String::new()
    }
    fn insert(&self, _: &str, _: &[&str], _: bool) -> String {
        String::new()
    }
    fn update(&self, _: &str, _: &[&str], _: &str) -> String {
        String::new()
    }
    fn delete(&self, _: &str, _: &str) -> String {
        String::new()
    }
    fn create_table(&self, _: &str, _: &[(String, String)]) -> String {
        String::new()
    }
    fn drop_table(&self, _: &str) -> String {
        String::new()
    }
    fn pagination(&self, skip: Option<usize>, take: Option<usize>) -> String {
        match (skip, take) {
            (Some(s), Some(t)) => format!("LIMIT {} OFFSET {}", t, s),
            (None, Some(t)) => format!("LIMIT {}", t),
            (Some(s), None) => format!("LIMIT 18446744073709551615 OFFSET {}", s),
            (None, None) => String::new(),
        }
    }
    fn parameter_placeholder(&self, _: usize) -> String {
        "?".to_string()
    }
    fn quote_identifier(&self, identifier: &str) -> String {
        format!("`{}`", identifier)
    }
    fn auto_increment_syntax(&self) -> &'static str {
        "AUTO_INCREMENT"
    }
}

/// Mimics SQLite: `?` placeholders, `LIMIT x OFFSET y` order, no offset-only.
struct SqliteLikeGenerator;

impl ISqlGenerator for SqliteLikeGenerator {
    fn select(&self, _: &str, _: &[&str]) -> String {
        String::new()
    }
    fn insert(&self, _: &str, _: &[&str], _: bool) -> String {
        String::new()
    }
    fn update(&self, _: &str, _: &[&str], _: &str) -> String {
        String::new()
    }
    fn delete(&self, _: &str, _: &str) -> String {
        String::new()
    }
    fn create_table(&self, _: &str, _: &[(String, String)]) -> String {
        String::new()
    }
    fn drop_table(&self, _: &str) -> String {
        String::new()
    }
    fn pagination(&self, skip: Option<usize>, take: Option<usize>) -> String {
        match (skip, take) {
            (Some(s), Some(t)) => format!("LIMIT {} OFFSET {}", t, s),
            (None, Some(t)) => format!("LIMIT {}", t),
            _ => String::new(),
        }
    }
    fn parameter_placeholder(&self, _: usize) -> String {
        "?".to_string()
    }
    fn quote_identifier(&self, identifier: &str) -> String {
        format!("\"{}\"", identifier)
    }
    fn auto_increment_syntax(&self) -> &'static str {
        "AUTOINCREMENT"
    }
}

// ---------------------------------------------------------------------------
// HAVING placeholder dialect tests
// ---------------------------------------------------------------------------

#[test]
fn test_pg_having_uses_dollar_n_placeholder() {
    // HAVING COUNT(blog_id) > 1 — should emit `$1` on PostgreSQL, not `?`.
    let mut state = QueryState::new("blogs");
    state.group_bys = vec!["category".to_string()];
    state.havings.push(HavingExpr::Compare {
        agg: AggKind::Count,
        col: "blog_id".to_string(),
        op: CompareOp::Gt,
        value: DbValue::I32(1),
    });
    state.parameters.push(DbValue::I32(1));

    let sql = state.to_sql_with(&PgLikeGenerator);
    assert!(
        sql.contains("HAVING COUNT(blog_id) > $1"),
        "PG HAVING should use $1 placeholder, got: {sql}"
    );
    assert!(
        !sql.contains("?"),
        "PG HAVING must not contain `?` placeholder, got: {sql}"
    );
}

#[test]
fn test_sqlite_having_uses_question_mark_placeholder() {
    let mut state = QueryState::new("blogs");
    state.group_bys = vec!["category".to_string()];
    state.havings.push(HavingExpr::Compare {
        agg: AggKind::Count,
        col: "blog_id".to_string(),
        op: CompareOp::Gt,
        value: DbValue::I32(1),
    });
    state.parameters.push(DbValue::I32(1));

    let sql = state.to_sql_with(&SqliteLikeGenerator);
    assert!(
        sql.contains("HAVING COUNT(blog_id) > ?"),
        "SQLite HAVING should use ? placeholder, got: {sql}"
    );
}

#[test]
fn test_mysql_having_uses_question_mark_placeholder() {
    let mut state = QueryState::new("blogs");
    state.group_bys = vec!["category".to_string()];
    state.havings.push(HavingExpr::Compare {
        agg: AggKind::Count,
        col: "blog_id".to_string(),
        op: CompareOp::Gt,
        value: DbValue::I32(1),
    });
    state.parameters.push(DbValue::I32(1));

    let sql = state.to_sql_with(&MySqlLikeGenerator);
    assert!(
        sql.contains("HAVING COUNT(blog_id) > ?"),
        "MySQL HAVING should use ? placeholder, got: {sql}"
    );
}

// ---------------------------------------------------------------------------
// PG $N index continuation tests (the core of the bug)
// ---------------------------------------------------------------------------

#[test]
fn test_pg_having_index_continues_from_where() {
    // WHERE category = $1 GROUP BY ... HAVING COUNT(blog_id) > $2
    // The HAVING placeholder must be $2 (continuing from WHERE's $1), not $1.
    let mut state = QueryState::new("blogs");
    state.parameters.push(DbValue::String("tech".to_string()));
    state.parameters.push(DbValue::I32(1));
    state.where_expr = Some(BoolExpr::Filter(FilterCondition::new("category", "=", 1)));
    state.group_bys = vec!["category".to_string()];
    state.havings.push(HavingExpr::Compare {
        agg: AggKind::Count,
        col: "blog_id".to_string(),
        op: CompareOp::Gt,
        value: DbValue::I32(1),
    });

    let sql = state.to_sql_with(&PgLikeGenerator);
    assert!(
        sql.contains("WHERE category = $1"),
        "WHERE should use $1, got: {sql}"
    );
    assert!(
        sql.contains("HAVING COUNT(blog_id) > $2"),
        "HAVING should continue at $2 after WHERE $1, got: {sql}"
    );
}

#[test]
fn test_pg_having_index_continues_through_and_combination() {
    // HAVING COUNT(blog_id) > $1 AND SUM(views) > $2
    // (No WHERE clause, so HAVING starts at $1.)
    let mut state = QueryState::new("blogs");
    state.group_bys = vec!["category".to_string()];
    let expr = HavingExpr::And(
        Box::new(HavingExpr::Compare {
            agg: AggKind::Count,
            col: "blog_id".to_string(),
            op: CompareOp::Gt,
            value: DbValue::I32(1),
        }),
        Box::new(HavingExpr::Compare {
            agg: AggKind::Sum,
            col: "views".to_string(),
            op: CompareOp::Gt,
            value: DbValue::I32(100),
        }),
    );
    state.parameters.extend(expr.collect_params());
    state.havings.push(expr);

    let sql = state.to_sql_with(&PgLikeGenerator);
    assert!(
        sql.contains("HAVING (COUNT(blog_id) > $1 AND SUM(views) > $2)"),
        "HAVING AND should use $1 and $2, got: {sql}"
    );
}

#[test]
fn test_pg_having_index_continues_through_or_with_where() {
    // WHERE category = $1 HAVING (COUNT(blog_id) > $2 OR SUM(views) > $3)
    let mut state = QueryState::new("blogs");
    state.parameters.push(DbValue::String("tech".to_string()));
    state.where_expr = Some(BoolExpr::Filter(FilterCondition::new("category", "=", 1)));
    state.group_bys = vec!["category".to_string()];
    let having = HavingExpr::Or(
        Box::new(HavingExpr::Compare {
            agg: AggKind::Count,
            col: "blog_id".to_string(),
            op: CompareOp::Gt,
            value: DbValue::I32(5),
        }),
        Box::new(HavingExpr::Compare {
            agg: AggKind::Sum,
            col: "views".to_string(),
            op: CompareOp::Gt,
            value: DbValue::I32(100),
        }),
    );
    state.parameters.extend(having.collect_params());
    state.havings.push(having);

    let sql = state.to_sql_with(&PgLikeGenerator);
    assert!(
        sql.contains("WHERE category = $1"),
        "WHERE should use $1, got: {sql}"
    );
    assert!(
        sql.contains("HAVING (COUNT(blog_id) > $2 OR SUM(views) > $3)"),
        "HAVING OR should continue at $2 and $3, got: {sql}"
    );
}

#[test]
fn test_pg_having_not_preserves_index() {
    // HAVING NOT (COUNT(blog_id) > $1)
    let mut state = QueryState::new("blogs");
    state.group_bys = vec!["category".to_string()];
    let having = HavingExpr::Not(Box::new(HavingExpr::Compare {
        agg: AggKind::Count,
        col: "blog_id".to_string(),
        op: CompareOp::Gt,
        value: DbValue::I32(1),
    }));
    state.parameters.extend(having.collect_params());
    state.havings.push(having);

    let sql = state.to_sql_with(&PgLikeGenerator);
    assert!(
        sql.contains("HAVING NOT (COUNT(blog_id) > $1)"),
        "HAVING NOT should use $1, got: {sql}"
    );
}

#[test]
fn test_pg_having_compare_agg_emits_no_placeholder() {
    // HAVING SUM(views) > COUNT(blog_id) — no bound params, no placeholders.
    let mut state = QueryState::new("blogs");
    state.group_bys = vec!["category".to_string()];
    state.havings.push(HavingExpr::CompareAgg {
        left_agg: AggKind::Sum,
        left_col: "views".to_string(),
        op: CompareOp::Gt,
        right_agg: AggKind::Count,
        right_col: "blog_id".to_string(),
    });

    let sql = state.to_sql_with(&PgLikeGenerator);
    assert!(
        sql.contains("HAVING SUM(views) > COUNT(blog_id)"),
        "HAVING CompareAgg should emit no placeholder, got: {sql}"
    );
    assert!(
        !sql.contains('$'),
        "CompareAgg must not emit any $N placeholder, got: {sql}"
    );
}

#[test]
fn test_pg_having_multiple_clauses_join_with_and() {
    // Two separate having() calls → joined by AND, indices continue.
    let mut state = QueryState::new("blogs");
    state.group_bys = vec!["category".to_string()];
    state.havings.push(HavingExpr::Compare {
        agg: AggKind::Count,
        col: "blog_id".to_string(),
        op: CompareOp::Gt,
        value: DbValue::I32(1),
    });
    state.havings.push(HavingExpr::Compare {
        agg: AggKind::Sum,
        col: "views".to_string(),
        op: CompareOp::Gt,
        value: DbValue::I32(100),
    });
    state.parameters.push(DbValue::I32(1));
    state.parameters.push(DbValue::I32(100));

    let sql = state.to_sql_with(&PgLikeGenerator);
    assert!(
        sql.contains("HAVING COUNT(blog_id) > $1 AND SUM(views) > $2"),
        "Multiple HAVING clauses should join with AND and continue indices, got: {sql}"
    );
}

// ---------------------------------------------------------------------------
// LIMIT / OFFSET pagination dialect tests (task 3.4.2)
// ---------------------------------------------------------------------------

#[test]
fn test_pg_limit_offset_clause_order() {
    // PostgreSQL emits `OFFSET y LIMIT x` (not `LIMIT x OFFSET y`).
    let mut state = QueryState::new("blogs");
    state.offset = Some(10);
    state.limit = Some(5);

    let sql = state.to_sql_with(&PgLikeGenerator);
    assert!(
        sql.contains("OFFSET 10 LIMIT 5"),
        "PG pagination should be `OFFSET 10 LIMIT 5`, got: {sql}"
    );
    assert!(
        !sql.contains("LIMIT 5 OFFSET 10"),
        "PG must NOT use `LIMIT x OFFSET y` order, got: {sql}"
    );
}

#[test]
fn test_pg_offset_only() {
    // PG offset-only: `OFFSET y` with no LIMIT.
    let mut state = QueryState::new("blogs");
    state.offset = Some(20);

    let sql = state.to_sql_with(&PgLikeGenerator);
    assert!(
        sql.contains("OFFSET 20") && !sql.contains("LIMIT"),
        "PG offset-only should be `OFFSET 20` with no LIMIT, got: {sql}"
    );
}

#[test]
fn test_pg_limit_only() {
    let mut state = QueryState::new("blogs");
    state.limit = Some(5);

    let sql = state.to_sql_with(&PgLikeGenerator);
    assert!(
        sql.contains("LIMIT 5") && !sql.contains("OFFSET"),
        "PG limit-only should be `LIMIT 5`, got: {sql}"
    );
}

#[test]
fn test_sqlite_limit_offset_clause_order() {
    // SQLite emits `LIMIT x OFFSET y`.
    let mut state = QueryState::new("blogs");
    state.offset = Some(10);
    state.limit = Some(5);

    let sql = state.to_sql_with(&SqliteLikeGenerator);
    assert!(
        sql.contains("LIMIT 5 OFFSET 10"),
        "SQLite pagination should be `LIMIT 5 OFFSET 10`, got: {sql}"
    );
}

#[test]
fn test_sqlite_limit_only() {
    let mut state = QueryState::new("blogs");
    state.limit = Some(5);

    let sql = state.to_sql_with(&SqliteLikeGenerator);
    assert!(
        sql.contains("LIMIT 5"),
        "SQLite limit-only should be `LIMIT 5`, got: {sql}"
    );
}

#[test]
fn test_mysql_limit_offset_clause_order() {
    // MySQL emits `LIMIT x OFFSET y`.
    let mut state = QueryState::new("blogs");
    state.offset = Some(10);
    state.limit = Some(5);

    let sql = state.to_sql_with(&MySqlLikeGenerator);
    assert!(
        sql.contains("LIMIT 5 OFFSET 10"),
        "MySQL pagination should be `LIMIT 5 OFFSET 10`, got: {sql}"
    );
}

#[test]
fn test_mysql_offset_only_uses_large_limit() {
    // MySQL offset-only special case: `LIMIT 18446744073709551615 OFFSET y`.
    let mut state = QueryState::new("blogs");
    state.offset = Some(20);

    let sql = state.to_sql_with(&MySqlLikeGenerator);
    assert!(
        sql.contains("LIMIT 18446744073709551615 OFFSET 20"),
        "MySQL offset-only should use large LIMIT, got: {sql}"
    );
}

#[test]
fn test_no_pagination_emits_nothing() {
    let state = QueryState::new("blogs");
    // No limit, no offset → no pagination clause at all.
    let sql_pg = state.to_sql_with(&PgLikeGenerator);
    let sql_sqlite = state.to_sql_with(&SqliteLikeGenerator);
    let sql_mysql = state.to_sql_with(&MySqlLikeGenerator);
    assert!(
        !sql_pg.contains("LIMIT") && !sql_pg.contains("OFFSET"),
        "No pagination should emit nothing (PG), got: {sql_pg}"
    );
    assert!(
        !sql_sqlite.contains("LIMIT") && !sql_sqlite.contains("OFFSET"),
        "No pagination should emit nothing (SQLite), got: {sql_sqlite}"
    );
    assert!(
        !sql_mysql.contains("LIMIT") && !sql_mysql.contains("OFFSET"),
        "No pagination should emit nothing (MySQL), got: {sql_mysql}"
    );
}

// ---------------------------------------------------------------------------
// Combined WHERE + HAVING + LIMIT/OFFSET end-to-end SQL tests
// ---------------------------------------------------------------------------

#[test]
fn test_pg_full_query_where_having_pagination() {
    // Full query: WHERE category = $1 GROUP BY ... HAVING COUNT(id) > $2
    //             OFFSET 10 LIMIT 5
    let mut state = QueryState::new("blogs");
    state.parameters.push(DbValue::String("tech".to_string()));
    state.parameters.push(DbValue::I32(1));
    state.where_expr = Some(BoolExpr::Filter(FilterCondition::new("category", "=", 1)));
    state.group_bys = vec!["category".to_string()];
    state.havings.push(HavingExpr::Compare {
        agg: AggKind::Count,
        col: "id".to_string(),
        op: CompareOp::Gt,
        value: DbValue::I32(1),
    });
    state.offset = Some(10);
    state.limit = Some(5);

    let sql = state.to_sql_with(&PgLikeGenerator);
    assert!(sql.contains("WHERE category = $1"), "WHERE: {sql}");
    assert!(sql.contains("GROUP BY category"), "GROUP BY: {sql}");
    assert!(
        sql.contains("HAVING COUNT(id) > $2"),
        "HAVING should continue at $2: {sql}"
    );
    assert!(
        sql.contains("OFFSET 10 LIMIT 5"),
        "PG pagination should be OFFSET then LIMIT: {sql}"
    );
}

#[test]
fn test_mysql_full_query_where_having_pagination() {
    // MySQL: WHERE category = ? GROUP BY ... HAVING COUNT(id) > ?
    //        LIMIT 5 OFFSET 10
    let mut state = QueryState::new("blogs");
    state.parameters.push(DbValue::String("tech".to_string()));
    state.parameters.push(DbValue::I32(1));
    state.where_expr = Some(BoolExpr::Filter(FilterCondition::new("category", "=", 1)));
    state.group_bys = vec!["category".to_string()];
    state.havings.push(HavingExpr::Compare {
        agg: AggKind::Count,
        col: "id".to_string(),
        op: CompareOp::Gt,
        value: DbValue::I32(1),
    });
    state.offset = Some(10);
    state.limit = Some(5);

    let sql = state.to_sql_with(&MySqlLikeGenerator);
    assert!(sql.contains("WHERE category = ?"), "WHERE: {sql}");
    assert!(sql.contains("HAVING COUNT(id) > ?"), "HAVING: {sql}");
    assert!(
        sql.contains("LIMIT 5 OFFSET 10"),
        "MySQL pagination should be LIMIT then OFFSET: {sql}"
    );
}

#[test]
fn test_collect_params_matches_placeholder_order() {
    // Verify that collect_params() returns values in the same left-to-right
    // order that to_sql() emits placeholders.
    let expr = HavingExpr::And(
        Box::new(HavingExpr::Compare {
            agg: AggKind::Count,
            col: "a".to_string(),
            op: CompareOp::Gt,
            value: DbValue::I32(1),
        }),
        Box::new(HavingExpr::Or(
            Box::new(HavingExpr::Compare {
                agg: AggKind::Sum,
                col: "b".to_string(),
                op: CompareOp::Gt,
                value: DbValue::I32(2),
            }),
            Box::new(HavingExpr::Not(Box::new(HavingExpr::Compare {
                agg: AggKind::Avg,
                col: "c".to_string(),
                op: CompareOp::Lt,
                value: DbValue::I32(3),
            }))),
        )),
    );

    let params = expr.collect_params();
    assert_eq!(
        params,
        vec![DbValue::I32(1), DbValue::I32(2), DbValue::I32(3),],
        "collect_params must return values in left-to-right traversal order"
    );

    // The PG SQL should reference $1, $2, $3 in the same order.
    let mut idx = 1usize;
    let sql = expr.to_sql(&PgLikeGenerator, &mut idx);
    assert!(
        sql.contains("$1") && sql.contains("$2") && sql.contains("$3"),
        "to_sql should emit $1, $2, $3 in order, got: {sql}"
    );
    assert_eq!(idx, 4, "param_idx should advance past all 3 params");
}