rust-ef 1.5.2

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
//! Integration tests for the `linq!` macro CTE syntax sugar (`with` / `from`).
//!
//! Verifies that `linq!(with name as |e: T| ...)` compiles the closure body
//! into a `BoolExpr` and generates a typed CTE whose body
//! `SELECT * FROM <table> WHERE <expr>` uses provider-correct placeholders.
//! Also tests execution against SQLite (CTEs are supported since 3.8.3+).

use rust_ef::db_context::{DbContext, DbContextOptionsBuilder};
use rust_ef::linq;
use rust_ef::prelude::*;
use rust_ef::provider::DbValue;
use rust_ef_sqlite::DbContextOptionsBuilderExt as _;

#[derive(Debug, Clone, EntityType)]
#[table("cte_employees")]
struct CteEmployee {
    #[primary_key]
    #[auto_increment]
    emp_id: i32,
    #[required]
    name: String,
    #[required]
    dept: String,
    #[required]
    salary: i64,
}

fn build_ctx() -> DbContext {
    let mut builder = DbContextOptionsBuilder::new();
    builder.use_sqlite_in_memory();
    let options = builder.build();
    DbContext::from_options(&options).expect("DbContext")
}

async fn seed(ctx: &mut DbContext) {
    ctx.set::<CteEmployee>();
    ctx.ensure_created().await.unwrap();

    let employees = [
        ("Alice", "Engineering", 100_000),
        ("Bob", "Engineering", 90_000),
        ("Carol", "Engineering", 110_000),
        ("Dave", "Sales", 80_000),
        ("Eve", "Sales", 85_000),
        ("Frank", "Sales", 80_000),
    ];

    for (name, dept, salary) in &employees {
        ctx.set::<CteEmployee>().add(CteEmployee {
            emp_id: 0,
            name: (*name).into(),
            dept: (*dept).into(),
            salary: *salary,
        });
    }
    ctx.save_changes().await.unwrap();
}

// ---------------------------------------------------------------------------
// SQL generation tests
// ---------------------------------------------------------------------------

#[test]
fn test_typed_cte_sql_generation() {
    let mut ctx = build_ctx();
    let sql = linq!(
        ctx.set::<CteEmployee>();
        with high_earners as |e: CteEmployee| e.salary > 85_000;
        from high_earners
    )
    .to_sql();

    assert!(
        sql.contains("WITH high_earners AS ("),
        "expected `WITH high_earners AS (...)` in SQL, got: {sql}"
    );
    assert!(
        sql.contains("SELECT * FROM \"cte_employees\" WHERE"),
        "expected typed CTE body `SELECT * FROM \"cte_employees\" WHERE ...`, got: {sql}"
    );
    // SQLite uses ? placeholders via PortablePlaceholderGenerator.
    assert!(
        sql.contains("?"),
        "expected ? placeholder in typed CTE body, got: {sql}"
    );
    assert!(
        sql.contains("FROM high_earners"),
        "expected main query to reference CTE by name, got: {sql}"
    );
}

#[test]
fn test_typed_cte_compound_where() {
    let mut ctx = build_ctx();
    let sql = linq!(
        ctx.set::<CteEmployee>();
        with eng_high as |e: CteEmployee| e.salary > 85_000 && e.dept == "Engineering";
        from eng_high
    )
    .to_sql();

    assert!(
        sql.contains("WITH eng_high AS ("),
        "expected `WITH eng_high AS (...)`, got: {sql}"
    );
    // Compound WHERE should produce AND in the CTE body.
    assert!(
        sql.contains("AND"),
        "expected AND in compound WHERE CTE body, got: {sql}"
    );
    assert!(
        sql.contains("salary") && sql.contains("dept"),
        "expected column names in CTE body, got: {sql}"
    );
}

#[test]
fn test_typed_cte_multiple_ctes() {
    let mut ctx = build_ctx();
    let sql = linq!(
        ctx.set::<CteEmployee>();
        with high_earners as |e: CteEmployee| e.salary > 85_000;
        with eng_earners as |e: CteEmployee| e.dept == "Engineering";
        from high_earners
    )
    .to_sql();

    assert!(
        sql.contains("WITH high_earners AS ("),
        "expected first CTE, got: {sql}"
    );
    assert!(
        sql.contains(", eng_earners AS ("),
        "expected second CTE with comma separator, got: {sql}"
    );
    assert!(
        sql.contains("\"cte_employees\""),
        "expected both CTEs to reference the source table, got: {sql}"
    );
}

#[test]
fn test_typed_cte_parameter_ordering() {
    let mut ctx = build_ctx();
    // Two typed CTEs each with one parameter, plus a WHERE on the main query.
    // The all_params() should return: [cte1_param, cte2_param, main_param].
    let query = linq!(
        ctx.set::<CteEmployee>(),
        |e: CteEmployee| e.emp_id > 0;
        with high_earners as |e: CteEmployee| e.salary > 85_000;
        from high_earners
    );

    let params = query.state().all_params();
    assert_eq!(
        params.len(),
        2,
        "expected 2 params (CTE + main WHERE), got {}",
        params.len()
    );
    // CTE param comes first. `85_000` literal infers as i32.
    assert!(
        matches!(params[0], DbValue::I32(85_000)),
        "expected CTE param (85000) first, got {:?}",
        params[0]
    );
    // Main query param second. `0` literal infers as i32.
    assert!(
        matches!(params[1], DbValue::I32(0)),
        "expected main query param (0) second, got {:?}",
        params[1]
    );
}

// ---------------------------------------------------------------------------
// Execution tests
// ---------------------------------------------------------------------------

#[tokio::test]
async fn test_typed_cte_execution() {
    let mut ctx = build_ctx();
    seed(&mut ctx).await;

    let employees = linq!(
        ctx.set::<CteEmployee>();
        with high_earners as |e: CteEmployee| e.salary > 85_000;
        from high_earners
    )
    .to_list()
    .await
    .unwrap();

    // salary > 85000: Alice(100k), Bob(90k), Carol(110k), Eve(85k excluded — strictly >).
    assert_eq!(employees.len(), 3, "typed CTE should return 3 high earners");
}

#[tokio::test]
async fn test_typed_cte_compound_execution() {
    let mut ctx = build_ctx();
    seed(&mut ctx).await;

    let employees = linq!(
        ctx.set::<CteEmployee>();
        with eng_high as |e: CteEmployee| e.salary > 85_000 && e.dept == "Engineering";
        from eng_high
    )
    .to_list()
    .await
    .unwrap();

    // Engineering + salary > 85000: Alice(100k), Bob(90k), Carol(110k) → 3 rows.
    assert_eq!(
        employees.len(),
        3,
        "compound CTE should return 3 Engineering high earners"
    );
}

#[tokio::test]
async fn test_typed_cte_with_main_where() {
    let mut ctx = build_ctx();
    seed(&mut ctx).await;

    // CTE filters to high earners, main query further filters by dept.
    let employees = linq!(
        ctx.set::<CteEmployee>(),
        |e: CteEmployee| e.dept == "Engineering";
        with high_earners as |e: CteEmployee| e.salary > 80_000;
        from high_earners
    )
    .to_list()
    .await
    .unwrap();

    // CTE: salary > 80000 → Alice, Bob, Carol, Eve (4 rows, excludes Dave & Frank at 80k).
    // Main WHERE: dept == "Engineering" → Alice, Bob, Carol (3 rows).
    assert_eq!(
        employees.len(),
        3,
        "CTE + main WHERE should return 3 Engineering high earners"
    );
}

#[tokio::test]
async fn test_typed_cte_with_order_by() {
    let mut ctx = build_ctx();
    seed(&mut ctx).await;

    let employees = linq!(
        ctx.set::<CteEmployee>();
        with high_earners as |e: CteEmployee| e.salary > 85_000;
        from high_earners;
        order_by e.salary desc
    )
    .to_list()
    .await
    .unwrap();

    assert_eq!(employees.len(), 3);
    // Ordered by salary desc: Carol(110k), Alice(100k), Bob(90k).
    assert_eq!(employees[0].name, "Carol");
    assert_eq!(employees[1].name, "Alice");
    assert_eq!(employees[2].name, "Bob");
}

#[tokio::test]
async fn test_typed_cte_with_or_condition() {
    let mut ctx = build_ctx();
    seed(&mut ctx).await;

    // CTE with OR: salary > 100000 OR dept == "Sales".
    let employees = linq!(
        ctx.set::<CteEmployee>();
        with special as |e: CteEmployee| e.salary > 100_000 || e.dept == "Sales";
        from special
    )
    .to_list()
    .await
    .unwrap();

    // salary > 100000: Carol(110k) → 1
    // dept == "Sales": Dave, Eve, Frank → 3
    // Total: 4 (no overlap).
    assert_eq!(
        employees.len(),
        4,
        "OR condition CTE should return 4 employees"
    );
}

// ---------------------------------------------------------------------------
// PostgreSQL dialect tests
// ---------------------------------------------------------------------------
//
// Regression coverage for a v1.1 bug where multiple typed CTEs reset
// `cte_idx` to 1 inside the `.map()` closure, producing duplicate `$1`
// placeholders across CTEs on PostgreSQL. The fix uses a `running_idx` that
// accumulates across CTEs so `$N` stays contiguous with `all_params()` order.
//
// These tests use a `PgLikeGenerator` mock so they run without a live
// PostgreSQL instance.

use rust_ef::provider::ISqlGenerator;

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, _: Option<usize>, _: Option<usize>) -> String {
        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"
    }
}

#[test]
fn test_pg_single_typed_cte_uses_dollar_n() {
    // Single typed CTE on PostgreSQL: body should use `$1`, not `?`.
    let mut ctx = build_ctx();
    let query = linq!(
        ctx.set::<CteEmployee>();
        with high_earners as |e: CteEmployee| e.salary > 85_000;
        from high_earners
    );

    let sql = query.state().to_sql_with(&PgLikeGenerator);
    assert!(
        sql.contains("WHERE salary > $1"),
        "PG typed CTE body should use $1 placeholder, got: {sql}"
    );
    assert!(
        !sql.contains('?'),
        "PG typed CTE body must not contain `?` placeholder, got: {sql}"
    );
}

#[test]
fn test_pg_multiple_typed_ctes_contiguous_placeholders() {
    // Regression: two typed CTEs each with one parameter. Before the fix,
    // both emitted `$1` because `cte_idx` reset per CTE. After the fix,
    // placeholders should be `$1` (first CTE) and `$2` (second CTE).
    let mut ctx = build_ctx();
    let query = linq!(
        ctx.set::<CteEmployee>();
        with high_earners as |e: CteEmployee| e.salary > 85_000;
        with eng_earners as |e: CteEmployee| e.dept == "Engineering";
        from high_earners
    );

    let sql = query.state().to_sql_with(&PgLikeGenerator);

    // First CTE body: salary > $1
    assert!(
        sql.contains("salary > $1"),
        "first CTE should use $1, got: {sql}"
    );
    // Second CTE body: dept = $2 (NOT $1 — that was the bug)
    assert!(
        sql.contains("dept = $2"),
        "second CTE should use $2 (regression: was $1 before fix), got: {sql}"
    );
    // The string `$1, $1` or `$1) AND ... $1` would indicate the collision.
    // Verify `$2` appears and the second CTE doesn't reuse `$1`.
    let second_cte_start = sql
        .find("eng_earners AS (")
        .unwrap_or_else(|| panic!("expected `eng_earners AS (` in SQL, got: {sql}"));
    let second_cte_body = &sql[second_cte_start..];
    assert!(
        !second_cte_body.contains("= $1"),
        "second CTE must not reuse $1 (regression), got: {sql}"
    );
    assert!(
        second_cte_body.contains("= $2"),
        "second CTE should use $2, got: {sql}"
    );
}

#[test]
fn test_pg_multi_cte_with_main_where_contiguous() {
    // Three param slots across the query: 2 CTE params + 1 main WHERE param.
    // Order in `all_params()`: [cte1_param, cte2_param, main_param].
    // Placeholders must be: CTE1 → $1, CTE2 → $2, main WHERE → $3.
    let mut ctx = build_ctx();
    let query = linq!(
        ctx.set::<CteEmployee>(),
        |e: CteEmployee| e.emp_id > 0;
        with high_earners as |e: CteEmployee| e.salary > 85_000;
        with eng_earners as |e: CteEmployee| e.dept == "Engineering";
        from high_earners
    );

    let sql = query.state().to_sql_with(&PgLikeGenerator);

    // Verify placeholder continuity: $1 (CTE1), $2 (CTE2), $3 (main WHERE).
    assert!(
        sql.contains("salary > $1"),
        "CTE1 should use $1, got: {sql}"
    );
    assert!(sql.contains("dept = $2"), "CTE2 should use $2, got: {sql}");
    assert!(
        sql.contains("emp_id > $3"),
        "main WHERE should use $3 (continuity from CTEs), got: {sql}"
    );

    // Verify all_params() ordering matches the physical SQL order.
    let params = query.state().all_params();
    assert_eq!(
        params.len(),
        3,
        "expected 3 params (2 CTE + 1 main), got {}",
        params.len()
    );
    assert!(
        matches!(params[0], DbValue::I32(85_000)),
        "param[0] should be CTE1's 85000, got {:?}",
        params[0]
    );
    assert!(
        matches!(params[1], DbValue::String(_)),
        "param[1] should be CTE2's dept string, got {:?}",
        params[1]
    );
    assert!(
        matches!(params[2], DbValue::I32(0)),
        "param[2] should be main WHERE's 0, got {:?}",
        params[2]
    );
}

#[test]
fn test_pg_compound_where_cte_placeholder_count() {
    // A single typed CTE with a compound WHERE (2 params) on PostgreSQL.
    // Body should be `WHERE (salary > $1) AND (dept = $2)`.
    let mut ctx = build_ctx();
    let query = linq!(
        ctx.set::<CteEmployee>();
        with eng_high as |e: CteEmployee| e.salary > 85_000 && e.dept == "Engineering";
        from eng_high
    );

    let sql = query.state().to_sql_with(&PgLikeGenerator);
    assert!(
        sql.contains("salary > $1"),
        "compound CTE first param should be $1, got: {sql}"
    );
    assert!(
        sql.contains("dept = $2"),
        "compound CTE second param should be $2, got: {sql}"
    );
    assert!(
        !sql.contains("$3"),
        "compound CTE with 2 params must not emit $3, got: {sql}"
    );
}