iridium_core 0.1.4

SQL Server-compatible Rust engine core for Iridium SQL
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
use iridium_core::{parse_batch, parse_sql, types::Value, Engine};

fn exec(engine: &mut Engine, sql: &str) {
    let stmt = parse_sql(sql).expect("parse failed");
    engine.execute(stmt).expect("execute failed");
}

fn exec_batch(engine: &mut Engine, sql: &str) {
    let stmts = parse_batch(sql).expect("parse batch failed");
    engine.execute_batch(stmts).expect("execute batch failed");
}

fn query(engine: &mut Engine, sql: &str) -> iridium_core::QueryResult {
    let stmt = parse_sql(sql).expect("parse failed");
    engine
        .execute(stmt)
        .expect("execute failed")
        .expect("expected result")
}

fn query_batch(engine: &mut Engine, sql: &str) -> iridium_core::QueryResult {
    let stmts = parse_batch(sql).expect("parse batch failed");
    let mut result = None;
    for stmt in stmts {
        result = engine.execute(stmt).expect("execute failed");
    }
    result.expect("expected result")
}

// ─── PRINT ─────────────────────────────────────────────────────────────

#[test]
fn test_print() {
    let e = Engine::new();
    let stmts = parse_batch("PRINT 'Hello World'; PRINT 123 + 456").unwrap();
    e.execute_batch(stmts).unwrap();
    assert_eq!(e.print_output(), vec!["Hello World".to_string(), "579".to_string()]);
}

// ─── Scalar UDF ────────────────────────────────────────────────────────

#[test]
fn test_multi_statement_udf() {
    let mut e = Engine::new();
    exec_batch(&mut e, "
        CREATE FUNCTION dbo.Calculate(@a INT, @b INT)
        RETURNS INT
        AS
        BEGIN
            DECLARE @res INT;
            SET @res = @a * 2 + @b;
            RETURN @res;
        END
    ");
    let r = query_batch(&mut e, "SELECT dbo.Calculate(10, 5) AS val");
    assert_eq!(r.rows[0][0], Value::Int(25));
}

// ─── Cursors ───────────────────────────────────────────────────────────

#[test]
fn test_cursor_basic() {
    let mut e = Engine::new();
    exec_batch(&mut e, "
        CREATE TABLE Items (Id INT, Name VARCHAR(50));
        INSERT INTO Items VALUES (1, 'A'), (2, 'B'), (3, 'C');
    ");

    let r = query_batch(&mut e, "
        DECLARE @id INT;
        DECLARE @name VARCHAR(50);
        DECLARE @concat VARCHAR(200) = '';

        DECLARE cur CURSOR FOR SELECT Id, Name FROM Items ORDER BY Id;
        OPEN cur;

        FETCH NEXT FROM cur INTO @id, @name;
        WHILE @@FETCH_STATUS = 0
        BEGIN
            SET @concat = @concat + @name;
            FETCH NEXT FROM cur INTO @id, @name;
        END

        CLOSE cur;
        DEALLOCATE cur;
        SELECT @concat;
    ");
    assert_eq!(r.rows[0][0], Value::VarChar("ABC".to_string()));
}

// ─── Triggers ──────────────────────────────────────────────────────────

#[test]
fn test_trigger_after_insert() {
    let mut e = Engine::new();
    exec_batch(&mut e, "
        CREATE TABLE Logs (Msg VARCHAR(100));
        CREATE TABLE Data (Val INT);

        CREATE TRIGGER tr_Data_Insert ON Data AFTER INSERT AS
        BEGIN
            INSERT INTO Logs SELECT 'Inserted ' + CAST(Val AS VARCHAR) FROM inserted;
        END
    ");

    exec_batch(&mut e, "INSERT INTO Data VALUES (10), (20)");

    let r = query_batch(&mut e, "SELECT Msg FROM Logs ORDER BY Msg");
    assert_eq!(r.rows.len(), 2);
    assert_eq!(r.rows[0][0], Value::VarChar("Inserted 10".to_string()));
    assert_eq!(r.rows[1][0], Value::VarChar("Inserted 20".to_string()));
}

#[test]
fn test_trigger_after_update() {
    let mut e = Engine::new();
    exec_batch(&mut e, "
        CREATE TABLE Audit (OldVal INT, NewVal INT);
        CREATE TABLE Data (Id INT, Val INT);
        INSERT INTO Data VALUES (1, 100);

        CREATE TRIGGER tr_Data_Update ON Data AFTER UPDATE AS
        BEGIN
            INSERT INTO Audit (OldVal, NewVal)
            SELECT d.Val, i.Val FROM deleted d JOIN inserted i ON d.Id = i.Id;
        END
    ");

    exec_batch(&mut e, "UPDATE Data SET Val = 200 WHERE Id = 1");

    let r = query_batch(&mut e, "SELECT OldVal, NewVal FROM Audit");
    assert_eq!(r.rows[0][0], Value::Int(100));
    assert_eq!(r.rows[0][1], Value::Int(200));
}

#[test]
fn test_trigger_after_delete() {
    let mut e = Engine::new();
    exec_batch(&mut e, "
        CREATE TABLE Trash (Val INT);
        CREATE TABLE Data (Val INT);
        INSERT INTO Data VALUES (1), (2), (3);

        CREATE TRIGGER tr_Data_Delete ON Data AFTER DELETE AS
        BEGIN
            INSERT INTO Trash SELECT Val FROM deleted;
        END
    ");

    exec_batch(&mut e, "DELETE FROM Data WHERE Val > 1");

    let r = query_batch(&mut e, "SELECT Val FROM Trash ORDER BY Val");
    assert_eq!(r.rows.len(), 2);
    assert_eq!(r.rows[0][0], Value::Int(2));
    assert_eq!(r.rows[1][0], Value::Int(3));
}

// ─── Existing tests from phase4_programmability ────────────────────────

#[test]
fn test_declare_and_set() {
    let mut e = Engine::new();
    exec_batch(&mut e, "DECLARE @x INT; SET @x = 42");
    let r = query_batch(&mut e, "DECLARE @x INT; SET @x = 42; SELECT @x AS val");
    assert_eq!(r.rows[0][0], Value::Int(42));
}

#[test]
fn test_declare_with_default() {
    let mut e = Engine::new();
    let r = query_batch(&mut e, "DECLARE @x INT = 10; SELECT @x AS val");
    assert_eq!(r.rows[0][0], Value::Int(10));
}

#[test]
fn test_set_arithmetic() {
    let mut e = Engine::new();
    let r = query_batch(
        &mut e,
        "DECLARE @x INT = 10; SET @x = @x * 2 + 5; SELECT @x AS val",
    );
    assert_eq!(r.rows[0][0], Value::Int(25));
}

#[test]
fn test_declare_string() {
    let mut e = Engine::new();
    let r = query_batch(
        &mut e,
        "DECLARE @name VARCHAR(50) = 'Alice'; SELECT @name AS val",
    );
    assert_eq!(r.rows[0][0], Value::VarChar("Alice".to_string()));
}

#[test]
fn test_multiple_variables() {
    let mut e = Engine::new();
    let r = query_batch(
        &mut e,
        "DECLARE @a INT = 3; DECLARE @b INT = 4; SELECT @a + @b AS val",
    );
    assert_eq!(r.rows[0][0], Value::BigInt(7));
}

#[test]
fn test_if_true() {
    let mut e = Engine::new();
    let r = query_batch(
        &mut e,
        "DECLARE @x INT = 10; IF @x > 5 BEGIN SELECT 'big' AS val END",
    );
    assert_eq!(r.rows[0][0], Value::VarChar("big".to_string()));
}

#[test]
fn test_while_loop() {
    let mut e = Engine::new();
    exec(&mut e, "CREATE TABLE t (v INT)");
    let r = query_batch(
        &mut e,
        "
        DECLARE @i INT = 1;
        WHILE @i <= 5
        BEGIN
            INSERT INTO t (v) VALUES (@i);
            SET @i = @i + 1;
        END;
        SELECT COUNT(*) AS cnt FROM t
    ",
    );
    assert_eq!(r.rows[0][0], Value::BigInt(5));
}

#[test]
fn test_if_else_chain() {
    let mut e = Engine::new();
    let r = query_batch(&mut e, "DECLARE @result VARCHAR(10) = 'none'; IF 1 = 1 BEGIN SET @result = 'yes' END ELSE BEGIN SET @result = 'no' END; SELECT @result AS val");
    assert_eq!(r.rows[0][0], Value::VarChar("yes".to_string()));
}

#[test]
fn test_while_sum() {
    let mut e = Engine::new();
    let r = query_batch(
        &mut e,
        "
        DECLARE @sum INT = 0;
        DECLARE @i INT = 1;
        WHILE @i <= 10
        BEGIN
            SET @sum = @sum + @i;
            SET @i = @i + 1;
        END;
        SELECT @sum AS total
    ",
    );
    assert_eq!(r.rows[0][0], Value::Int(55));
}

#[test]
fn test_semicolon_separated() {
    let mut e = Engine::new();
    exec_batch(&mut e, "CREATE TABLE t (id INT); INSERT INTO t VALUES (1); INSERT INTO t VALUES (2); INSERT INTO t VALUES (3)");
    let r = query(&mut e, "SELECT COUNT(*) AS cnt FROM t");
    assert_eq!(r.rows[0][0], Value::BigInt(3));
}

#[test]
fn test_batch_with_select() {
    let mut e = Engine::new();
    let r = query_batch(
        &mut e,
        "CREATE TABLE t (name VARCHAR(50)); INSERT INTO t VALUES ('hello'); SELECT name FROM t",
    );
    assert_eq!(r.rows[0][0], Value::VarChar("hello".to_string()));
}

#[test]
fn test_variable_in_where() {
    let mut e = Engine::new();
    exec(&mut e, "CREATE TABLE t (v INT)");
    exec(&mut e, "INSERT INTO t VALUES (1)");
    exec(&mut e, "INSERT INTO t VALUES (2)");
    exec(&mut e, "INSERT INTO t VALUES (3)");
    let r = query_batch(
        &mut e,
        "DECLARE @threshold INT = 2; SELECT v FROM t WHERE v > @threshold",
    );
    assert_eq!(r.rows.len(), 1);
    assert_eq!(r.rows[0][0], Value::Int(3));
}

#[test]
fn test_variable_concat() {
    let mut e = Engine::new();
    let r = query_batch(&mut e, "DECLARE @first VARCHAR(50) = 'Hello'; DECLARE @second VARCHAR(50) = ' World'; SELECT @first + @second AS greeting");
    assert_eq!(r.rows[0][0], Value::VarChar("Hello World".to_string()));
}

#[test]
fn test_while_with_break() {
    let mut e = Engine::new();
    exec(&mut e, "CREATE TABLE t (v INT)");
    exec_batch(
        &mut e,
        "
        DECLARE @i INT = 1;
        WHILE @i <= 100
        BEGIN
            IF @i > 5
            BEGIN
                BREAK;
            END
            INSERT INTO t VALUES (@i);
            SET @i = @i + 1;
        END
    ",
    );
    let r = query(&mut e, "SELECT COUNT(*) AS cnt FROM t");
    assert_eq!(r.rows[0][0], Value::BigInt(5));
}

#[test]
fn test_while_with_continue() {
    let mut e = Engine::new();
    exec(&mut e, "CREATE TABLE t (v INT)");
    exec_batch(
        &mut e,
        "
        DECLARE @i INT = 1;
        WHILE @i <= 5
        BEGIN
            SET @i = @i + 1;
            IF @i = 3
            BEGIN
                CONTINUE;
            END
            INSERT INTO t VALUES (@i);
        END
    ",
    );
    let r = query(&mut e, "SELECT COUNT(*) AS cnt FROM t");
    assert_eq!(r.rows[0][0], Value::BigInt(4));
}

#[test]
fn test_nested_while_break_inner() {
    let mut e = Engine::new();
    exec(&mut e, "CREATE TABLE t (v INT)");
    exec_batch(
        &mut e,
        "
        DECLARE @i INT = 1;
        WHILE @i <= 3
        BEGIN
            DECLARE @j INT = 1;
            WHILE @j <= 10
            BEGIN
                IF @j > 2
                BEGIN
                    BREAK;
                END
                INSERT INTO t VALUES (@i * 100 + @j);
                SET @j = @j + 1;
            END;
            SET @i = @i + 1;
        END
    ",
    );
    let r = query(&mut e, "SELECT COUNT(*) AS cnt FROM t");
    assert_eq!(r.rows[0][0], Value::BigInt(6));
}

#[test]
fn test_return_value() {
    let mut e = Engine::new();
    exec(&mut e, "CREATE TABLE t (v INT)");
    exec_batch(
        &mut e,
        "
        DECLARE @i INT = 1;
        WHILE @i <= 5
        BEGIN
            IF @i = 3
            BEGIN
                RETURN;
            END
            INSERT INTO t VALUES (@i);
            SET @i = @i + 1;
        END
    ",
    );
    let cnt = query(&mut e, "SELECT COUNT(*) AS cnt FROM t");
    assert_eq!(cnt.rows[0][0], Value::BigInt(2));
}

#[test]
fn test_return_with_value() {
    let mut e = Engine::new();
    exec(&mut e, "CREATE TABLE t (v INT)");
    exec_batch(
        &mut e,
        "
        DECLARE @val INT = 42;
        IF @val > 0
        BEGIN
            RETURN @val;
        END
        INSERT INTO t VALUES (@val);
    ",
    );
    let cnt = query(&mut e, "SELECT COUNT(*) AS cnt FROM t");
    assert_eq!(cnt.rows[0][0], Value::BigInt(0));
}

#[test]
fn test_return_early() {
    let mut e = Engine::new();
    exec(&mut e, "CREATE TABLE t (v INT)");
    exec_batch(
        &mut e,
        "
        DECLARE @x INT = 100;
        RETURN;
        SET @x = 200;
        INSERT INTO t VALUES (@x);
    ",
    );
    let cnt = query(&mut e, "SELECT COUNT(*) AS cnt FROM t");
    assert_eq!(cnt.rows[0][0], Value::BigInt(0));
}

#[test]
fn test_continue_skip_insert() {
    let mut e = Engine::new();
    exec(&mut e, "CREATE TABLE t (v INT)");
    exec_batch(
        &mut e,
        "
        DECLARE @i INT = 1;
        WHILE @i <= 4
        BEGIN
            SET @i = @i + 1;
            CONTINUE;
            INSERT INTO t VALUES (@i);
        END
    ",
    );
    let cnt = query(&mut e, "SELECT COUNT(*) AS cnt FROM t");
    assert_eq!(cnt.rows[0][0], Value::BigInt(0));
}