graphitesql 0.0.5

A pure, safe, no_std Rust re-implementation of SQLite, compatible with the SQLite 3 file format.
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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
//! Track C (multi-schema): the database registry and `ATTACH`/`DETACH`.
//! Built up piece by piece (C1: `PRAGMA database_list`).

#![cfg(feature = "std")]

use graphitesql::{Connection, Value};

#[test]
fn database_list_reports_main() {
    // In-memory main has an empty file path.
    let c = Connection::open_memory().unwrap();
    let r = c.query("PRAGMA database_list").unwrap();
    assert_eq!(
        r.rows,
        vec![vec![
            Value::Integer(0),
            Value::Text("main".into()),
            Value::Text("".into())
        ]]
    );

    // A file-backed main reports its path.
    let mut p = std::env::temp_dir();
    p.push(format!("graphitesql-attach-{}.db", std::process::id()));
    let path = p.to_string_lossy().into_owned();
    let _ = std::fs::remove_file(&path);
    {
        let mut c = Connection::create(&path).unwrap();
        c.execute("CREATE TABLE t(a)").unwrap();
    }
    let c = Connection::open(&path).unwrap();
    let r = c.query("PRAGMA database_list").unwrap();
    assert_eq!(r.rows[0][1], Value::Text("main".into()));
    assert_eq!(r.rows[0][2], Value::Text(path.clone()));
    let _ = std::fs::remove_file(&path);
}

#[test]
fn attach_and_detach_in_memory() {
    let mut c = Connection::open_memory().unwrap();
    c.execute("ATTACH ':memory:' AS aux").unwrap();
    c.execute("ATTACH DATABASE '' AS aux2").unwrap();
    // Attached databases start at seq 2 (seq 1 is reserved for temp).
    let r = c.query("PRAGMA database_list").unwrap();
    assert_eq!(
        r.rows,
        vec![
            vec![
                Value::Integer(0),
                Value::Text("main".into()),
                Value::Text("".into())
            ],
            vec![
                Value::Integer(2),
                Value::Text("aux".into()),
                Value::Text("".into())
            ],
            vec![
                Value::Integer(3),
                Value::Text("aux2".into()),
                Value::Text("".into())
            ],
        ]
    );

    // Duplicate / reserved names are rejected.
    assert!(c.execute("ATTACH ':memory:' AS aux").is_err());
    assert!(c.execute("ATTACH ':memory:' AS main").is_err());
    assert!(c.execute("ATTACH ':memory:' AS temp").is_err());

    // DETACH removes it; main/temp and unknown names are rejected.
    c.execute("DETACH aux").unwrap();
    assert!(c.execute("DETACH main").is_err());
    assert!(c.execute("DETACH nope").is_err());
    let r = c.query("PRAGMA database_list").unwrap();
    assert_eq!(r.rows.len(), 2); // main + aux2
    assert_eq!(r.rows[1][1], Value::Text("aux2".into()));
}

#[test]
fn schema_qualified_read_main() {
    let mut c = Connection::open_memory().unwrap();
    c.execute("CREATE TABLE t(a, b)").unwrap();
    c.execute("INSERT INTO t VALUES(1,'x'),(2,'y')").unwrap();
    // `main.t` resolves to the main database.
    let r = c.query("SELECT a, b FROM main.t ORDER BY a").unwrap();
    assert_eq!(r.rows.len(), 2);
    assert_eq!(r.rows[0][1], Value::Text("x".into()));
    // A table-qualified alias works too.
    assert_eq!(
        c.query("SELECT m.b FROM main.t AS m WHERE m.a = 2")
            .unwrap()
            .rows[0][0],
        Value::Text("y".into())
    );
    // An unknown database qualifier is a clear error (not silent).
    c.execute("ATTACH ':memory:' AS aux").unwrap();
    assert!(c.query("SELECT * FROM zzz.t").is_err());
}

#[test]
fn cross_database_join() {
    let mut c = Connection::open_memory().unwrap();
    c.execute("CREATE TABLE u(id INTEGER PRIMARY KEY, name TEXT)")
        .unwrap();
    c.execute("INSERT INTO u VALUES(1,'alice'),(2,'bob'),(3,'carol')")
        .unwrap();
    c.execute("ATTACH ':memory:' AS aux").unwrap();
    c.execute("CREATE TABLE aux.o(oid INTEGER PRIMARY KEY, uid INT, amt INT)")
        .unwrap();
    c.execute("INSERT INTO aux.o VALUES(10,1,100),(11,1,200),(12,2,50)")
        .unwrap();

    // INNER join across databases, with 3-part column names (`aux.o.amt`).
    let r = c
        .query("SELECT u.name, aux.o.amt FROM u JOIN aux.o ON u.id = aux.o.uid ORDER BY aux.o.oid")
        .unwrap();
    assert_eq!(
        r.rows,
        vec![
            vec![Value::Text("alice".into()), Value::Integer(100)],
            vec![Value::Text("alice".into()), Value::Integer(200)],
            vec![Value::Text("bob".into()), Value::Integer(50)],
        ]
    );

    // LEFT join: carol (id 3) has no orders, so a NULL-extended row.
    let r = c
        .query(
            "SELECT u.name, o.amt FROM u LEFT JOIN aux.o AS o ON u.id = o.uid ORDER BY u.id, o.amt",
        )
        .unwrap();
    assert_eq!(
        r.rows.last().unwrap(),
        &vec![Value::Text("carol".into()), Value::Null]
    );

    // Both sources qualified, with aliases.
    assert_eq!(
        c.query("SELECT count(*) FROM main.u AS u JOIN aux.o AS o ON u.id=o.uid")
            .unwrap()
            .rows[0][0],
        Value::Integer(3)
    );

    // A temp table joins against a main table for unqualified names.
    c.execute("CREATE TEMP TABLE labels(uid INT, tag TEXT)")
        .unwrap();
    c.execute("INSERT INTO labels VALUES(1,'vip'),(2,'std')")
        .unwrap();
    let r = c
        .query("SELECT u.name, labels.tag FROM u JOIN labels ON u.id = labels.uid ORDER BY u.id")
        .unwrap();
    assert_eq!(
        r.rows,
        vec![
            vec![Value::Text("alice".into()), Value::Text("vip".into())],
            vec![Value::Text("bob".into()), Value::Text("std".into())],
        ]
    );
}

#[test]
fn cross_database_without_rowid_read() {
    let mut c = Connection::open_memory().unwrap();
    c.execute("ATTACH ':memory:' AS aux").unwrap();
    c.execute("CREATE TABLE aux.k(a TEXT, b INT, PRIMARY KEY(a)) WITHOUT ROWID")
        .unwrap();
    c.execute("INSERT INTO aux.k VALUES('x',1),('a',2),('m',3)")
        .unwrap();
    // Sole-source read walks the clustered index in PK order.
    let r = c.query("SELECT a, b FROM aux.k ORDER BY a").unwrap();
    assert_eq!(
        r.rows,
        vec![
            vec![Value::Text("a".into()), Value::Integer(2)],
            vec![Value::Text("m".into()), Value::Integer(3)],
            vec![Value::Text("x".into()), Value::Integer(1)],
        ]
    );
    // A WITHOUT ROWID attached table as a join source.
    c.execute("CREATE TABLE main.t(a TEXT, n INT)").unwrap();
    c.execute("INSERT INTO main.t VALUES('a',10),('x',20)")
        .unwrap();
    let r = c
        .query("SELECT t.a, aux.k.b, t.n FROM t JOIN aux.k ON t.a=aux.k.a ORDER BY t.a")
        .unwrap();
    assert_eq!(
        r.rows,
        vec![
            vec![
                Value::Text("a".into()),
                Value::Integer(2),
                Value::Integer(10)
            ],
            vec![
                Value::Text("x".into()),
                Value::Integer(1),
                Value::Integer(20)
            ],
        ]
    );
}

#[test]
fn cross_database_create_index() {
    let mut c = Connection::open_memory().unwrap();
    c.execute("ATTACH ':memory:' AS aux").unwrap();
    c.execute("CREATE TABLE aux.t(a INT, b TEXT)").unwrap();
    c.execute("INSERT INTO aux.t VALUES(1,'x'),(2,'y'),(1,'z')")
        .unwrap();
    c.execute("CREATE INDEX aux.idx_a ON t(a)").unwrap();
    c.execute("CREATE UNIQUE INDEX aux.idx_b ON t(b)").unwrap();

    // The index serves an equality seek and its UNIQUE constraint is enforced.
    let r = c.query("SELECT b FROM aux.t WHERE a=1 ORDER BY b").unwrap();
    assert_eq!(
        r.rows,
        vec![vec![Value::Text("x".into())], vec![Value::Text("z".into())]]
    );
    assert!(c.execute("INSERT INTO aux.t VALUES(9,'x')").is_err());

    // The indexes live in aux's catalog, none leak into main.
    let names: Vec<_> = c
        .query("SELECT name FROM aux.sqlite_master WHERE type='index' ORDER BY name")
        .unwrap()
        .rows
        .into_iter()
        .map(|r| r[0].clone())
        .collect();
    assert_eq!(
        names,
        vec![Value::Text("idx_a".into()), Value::Text("idx_b".into())]
    );
    assert_eq!(
        c.query("SELECT count(*) FROM main.sqlite_master WHERE type='index'")
            .unwrap()
            .rows[0][0],
        Value::Integer(0)
    );
}

#[test]
fn cross_database_create_index_file_cross_engine() {
    let sqlite = std::process::Command::new("sqlite3")
        .arg("--version")
        .output()
        .is_ok();
    if !sqlite {
        return;
    }
    let mut p = std::env::temp_dir();
    p.push(format!("graphitesql-attach-idx-{}.db", std::process::id()));
    let path = p.to_string_lossy().into_owned();
    let _ = std::fs::remove_file(&path);
    {
        let mut c = Connection::open_memory().unwrap();
        c.execute(&format!("ATTACH '{path}' AS d")).unwrap();
        c.execute("CREATE TABLE d.t(a INT, b TEXT)").unwrap();
        c.execute("INSERT INTO d.t VALUES(1,'x'),(2,'y')").unwrap();
        c.execute("CREATE INDEX d.idx_a ON t(a)").unwrap();
    }
    // sqlite3 reads the file: integrity ok (index pages valid) and the index is
    // stored bare-named so sqlite can use it.
    let out = std::process::Command::new("sqlite3")
        .arg(&path)
        .arg("PRAGMA integrity_check; SELECT b FROM t WHERE a=2;")
        .output()
        .unwrap();
    let s = String::from_utf8_lossy(&out.stdout);
    assert!(s.contains("ok"), "integrity: {s}");
    assert!(s.contains('y'), "seek: {s}");
    let _ = std::fs::remove_file(&path);
    let _ = std::fs::remove_file(format!("{path}-journal"));
}

#[test]
fn cross_database_transaction() {
    let mut c = Connection::open_memory().unwrap();
    c.execute("CREATE TABLE m(a)").unwrap();
    c.execute("ATTACH ':memory:' AS aux").unwrap();
    c.execute("CREATE TABLE aux.t(a)").unwrap();

    // A transaction spanning both databases commits atomically.
    c.execute("BEGIN").unwrap();
    c.execute("INSERT INTO m VALUES(1)").unwrap();
    c.execute("INSERT INTO aux.t VALUES(10)").unwrap();
    c.execute("COMMIT").unwrap();
    assert_eq!(
        c.query("SELECT a FROM m").unwrap().rows[0][0],
        Value::Integer(1)
    );
    assert_eq!(
        c.query("SELECT a FROM aux.t").unwrap().rows[0][0],
        Value::Integer(10)
    );

    // ROLLBACK discards the attached database's changes too (not just main's).
    c.execute("BEGIN").unwrap();
    c.execute("INSERT INTO m VALUES(2)").unwrap();
    c.execute("INSERT INTO aux.t VALUES(20)").unwrap();
    c.execute("ROLLBACK").unwrap();
    assert_eq!(
        c.query("SELECT count(*) FROM m").unwrap().rows[0][0],
        Value::Integer(1)
    );
    assert_eq!(
        c.query("SELECT count(*) FROM aux.t").unwrap().rows[0][0],
        Value::Integer(1) // only the committed 10 remains
    );

    // DDL in the attached database is rolled back as well.
    c.execute("BEGIN").unwrap();
    c.execute("CREATE TABLE aux.tmp2(x)").unwrap();
    c.execute("ROLLBACK").unwrap();
    let names: Vec<_> = c
        .query("SELECT name FROM aux.sqlite_master WHERE type='table' ORDER BY name")
        .unwrap()
        .rows
        .into_iter()
        .map(|r| r[0].clone())
        .collect();
    assert_eq!(names, vec![Value::Text("t".into())]);
}

#[test]
fn cross_database_transaction_file_durability() {
    let mut p = std::env::temp_dir();
    p.push(format!("graphitesql-attach-tx-{}.db", std::process::id()));
    let path = p.to_string_lossy().into_owned();
    let _ = std::fs::remove_file(&path);

    // A committed cross-database transaction lands durably in the attached file.
    {
        let mut c = Connection::open_memory().unwrap();
        c.execute(&format!("ATTACH '{path}' AS d")).unwrap();
        c.execute("CREATE TABLE d.t(a)").unwrap();
        c.execute("BEGIN").unwrap();
        c.execute("INSERT INTO d.t VALUES(1),(2)").unwrap();
        c.execute("COMMIT").unwrap();
        // A subsequent rolled-back transaction leaves no trace.
        c.execute("BEGIN").unwrap();
        c.execute("INSERT INTO d.t VALUES(3)").unwrap();
        c.execute("ROLLBACK").unwrap();
    }
    // Reopen the file: exactly the committed rows are present.
    {
        let mut c = Connection::open_memory().unwrap();
        c.execute(&format!("ATTACH '{path}' AS d")).unwrap();
        assert_eq!(
            c.query("SELECT count(*) FROM d.t").unwrap().rows[0][0],
            Value::Integer(2)
        );
    }
    let _ = std::fs::remove_file(&path);
    let _ = std::fs::remove_file(format!("{path}-journal"));
}

#[test]
fn cross_database_savepoint() {
    let mut c = Connection::open_memory().unwrap();
    c.execute("ATTACH ':memory:' AS aux").unwrap();
    c.execute("CREATE TABLE aux.t(a)").unwrap();
    c.execute("INSERT INTO aux.t VALUES(1)").unwrap();

    // ROLLBACK TO reverts the attached database to the savepoint.
    c.execute("BEGIN").unwrap();
    c.execute("INSERT INTO aux.t VALUES(2)").unwrap();
    c.execute("SAVEPOINT sp").unwrap();
    c.execute("INSERT INTO aux.t VALUES(3)").unwrap();
    c.execute("ROLLBACK TO sp").unwrap();
    c.execute("COMMIT").unwrap();
    assert_eq!(
        c.query("SELECT a FROM aux.t ORDER BY a").unwrap().rows,
        vec![vec![Value::Integer(1)], vec![Value::Integer(2)]]
    );

    // RELEASE keeps the staged changes in the attached database.
    c.execute("BEGIN").unwrap();
    c.execute("SAVEPOINT s2").unwrap();
    c.execute("INSERT INTO aux.t VALUES(9)").unwrap();
    c.execute("RELEASE s2").unwrap();
    c.execute("COMMIT").unwrap();
    assert_eq!(
        c.query("SELECT count(*) FROM aux.t").unwrap().rows[0][0],
        Value::Integer(3)
    );

    // DDL in the attached database is reverted by ROLLBACK TO as well.
    c.execute("BEGIN").unwrap();
    c.execute("SAVEPOINT s3").unwrap();
    c.execute("CREATE TABLE aux.z(x)").unwrap();
    c.execute("ROLLBACK TO s3").unwrap();
    c.execute("COMMIT").unwrap();
    let names: Vec<_> = c
        .query("SELECT name FROM aux.sqlite_master WHERE type='table' ORDER BY name")
        .unwrap()
        .rows
        .into_iter()
        .map(|r| r[0].clone())
        .collect();
    assert_eq!(names, vec![Value::Text("t".into())]);
}

#[test]
fn cross_database_view() {
    let mut c = Connection::open_memory().unwrap();
    c.execute("ATTACH ':memory:' AS aux").unwrap();
    c.execute("CREATE TABLE aux.t(a INT, b TEXT)").unwrap();
    c.execute("INSERT INTO aux.t VALUES(1,'x'),(2,'y'),(3,'z')")
        .unwrap();
    c.execute("CREATE TABLE aux.u(a INT, n INT)").unwrap();
    c.execute("INSERT INTO aux.u VALUES(2,20),(3,30)").unwrap();

    // A view whose body resolves its (unqualified) tables in the attached db.
    c.execute("CREATE VIEW aux.v AS SELECT a, b FROM t WHERE a > 1")
        .unwrap();
    assert_eq!(
        c.query("SELECT * FROM aux.v ORDER BY a").unwrap().rows,
        vec![
            vec![Value::Integer(2), Value::Text("y".into())],
            vec![Value::Integer(3), Value::Text("z".into())],
        ]
    );
    // Stored bare-named.
    assert_eq!(
        c.query("SELECT sql FROM aux.sqlite_master WHERE name='v'")
            .unwrap()
            .rows[0][0],
        Value::Text("CREATE VIEW v AS SELECT a, b FROM t WHERE a > 1".into())
    );

    // A view body containing a join (both tables in the attached db).
    c.execute("CREATE VIEW aux.j AS SELECT t.b, u.n FROM t JOIN u ON t.a=u.a")
        .unwrap();
    assert_eq!(
        c.query("SELECT * FROM aux.j ORDER BY n").unwrap().rows,
        vec![
            vec![Value::Text("y".into()), Value::Integer(20)],
            vec![Value::Text("z".into()), Value::Integer(30)],
        ]
    );

    // A subquery in the view body also resolves in the attached db.
    c.execute("CREATE VIEW aux.s AS SELECT b FROM t WHERE a IN (SELECT a FROM u)")
        .unwrap();
    assert_eq!(
        c.query("SELECT * FROM aux.s ORDER BY b").unwrap().rows,
        vec![vec![Value::Text("y".into())], vec![Value::Text("z".into())]]
    );

    // The cross-db view used as a join source from a main table.
    c.execute("CREATE TABLE main.m(a INT, tag TEXT)").unwrap();
    c.execute("INSERT INTO main.m VALUES(2,'two'),(3,'three')")
        .unwrap();
    assert_eq!(
        c.query("SELECT m.tag, aux.v.b FROM m JOIN aux.v ON m.a=aux.v.a ORDER BY m.a")
            .unwrap()
            .rows,
        vec![
            vec![Value::Text("two".into()), Value::Text("y".into())],
            vec![Value::Text("three".into()), Value::Text("z".into())],
        ]
    );

    // A plain/TEMP view (unqualified) still lives in main and reads fine.
    c.execute("CREATE TEMP VIEW tv AS SELECT 42").unwrap();
    assert_eq!(
        c.query("SELECT * FROM tv").unwrap().rows[0][0],
        Value::Integer(42)
    );
}

#[test]
fn cross_database_view_file_cross_engine() {
    let sqlite = std::process::Command::new("sqlite3")
        .arg("--version")
        .output()
        .is_ok();
    if !sqlite {
        return;
    }
    let mut p = std::env::temp_dir();
    p.push(format!("graphitesql-attach-view-{}.db", std::process::id()));
    let path = p.to_string_lossy().into_owned();
    let _ = std::fs::remove_file(&path);
    {
        let mut c = Connection::open_memory().unwrap();
        c.execute(&format!("ATTACH '{path}' AS d")).unwrap();
        c.execute("CREATE TABLE d.t(a INT, b TEXT)").unwrap();
        c.execute("INSERT INTO d.t VALUES(1,'x'),(2,'y')").unwrap();
        c.execute("CREATE VIEW d.v AS SELECT b FROM t WHERE a=2")
            .unwrap();
    }
    // graphite re-reads the view from the file...
    {
        let mut c = Connection::open_memory().unwrap();
        c.execute(&format!("ATTACH '{path}' AS d")).unwrap();
        assert_eq!(
            c.query("SELECT * FROM d.v").unwrap().rows[0][0],
            Value::Text("y".into())
        );
    }
    // ...and so does sqlite3 (bare-named view SQL, integrity ok).
    let out = std::process::Command::new("sqlite3")
        .arg(&path)
        .arg("PRAGMA integrity_check; SELECT b FROM v;")
        .output()
        .unwrap();
    let s = String::from_utf8_lossy(&out.stdout);
    assert!(s.contains("ok"), "integrity: {s}");
    assert!(s.contains('y'), "view under sqlite3: {s}");
    let _ = std::fs::remove_file(&path);
    let _ = std::fs::remove_file(format!("{path}-journal"));
}

#[test]
fn cross_database_create_trigger() {
    let mut c = Connection::open_memory().unwrap();
    c.execute("ATTACH ':memory:' AS aux").unwrap();
    c.execute("CREATE TABLE aux.t(a INT, b TEXT)").unwrap();
    c.execute("CREATE TABLE aux.log(msg TEXT)").unwrap();
    // A trigger on an attached table, with `NEW.col` in its body.
    c.execute(
        "CREATE TRIGGER aux.tr AFTER INSERT ON t BEGIN INSERT INTO log(msg) VALUES(NEW.b); END",
    )
    .unwrap();
    c.execute("INSERT INTO aux.t VALUES(3,'z')").unwrap();
    // It fires against the attached database.
    assert_eq!(
        c.query("SELECT msg FROM aux.log").unwrap().rows[0][0],
        Value::Text("z".into())
    );
    // Stored bare-named (the `aux.` qualifier stripped, but `NEW.b` untouched).
    assert_eq!(
        c.query("SELECT sql FROM aux.sqlite_master WHERE name='tr'")
            .unwrap()
            .rows[0][0],
        Value::Text(
            "CREATE TRIGGER tr AFTER INSERT ON t BEGIN INSERT INTO log(msg) VALUES(NEW.b); END"
                .into()
        )
    );
    // Nothing leaked into main.
    assert_eq!(
        c.query("SELECT count(*) FROM main.sqlite_master")
            .unwrap()
            .rows[0][0],
        Value::Integer(0)
    );
}

#[test]
fn cross_database_create_trigger_file_cross_engine() {
    let sqlite = std::process::Command::new("sqlite3")
        .arg("--version")
        .output()
        .is_ok();
    if !sqlite {
        return;
    }
    let mut p = std::env::temp_dir();
    p.push(format!("graphitesql-attach-trig-{}.db", std::process::id()));
    let path = p.to_string_lossy().into_owned();
    let _ = std::fs::remove_file(&path);
    {
        let mut c = Connection::open_memory().unwrap();
        c.execute(&format!("ATTACH '{path}' AS d")).unwrap();
        c.execute("CREATE TABLE d.t(a INT, b TEXT)").unwrap();
        c.execute("CREATE TABLE d.log(msg TEXT)").unwrap();
        c.execute(
            "CREATE TRIGGER d.tr AFTER INSERT ON t BEGIN INSERT INTO log(msg) VALUES(NEW.b); END",
        )
        .unwrap();
    }
    // sqlite3 reads the file (bare-named trigger SQL), and the trigger it parsed
    // fires on its own insert.
    let out = std::process::Command::new("sqlite3")
        .arg(&path)
        .arg("PRAGMA integrity_check; INSERT INTO t VALUES(1,'hi'); SELECT msg FROM log;")
        .output()
        .unwrap();
    let s = String::from_utf8_lossy(&out.stdout);
    assert!(s.contains("ok"), "integrity: {s}");
    assert!(s.contains("hi"), "trigger fire under sqlite3: {s}");
    let _ = std::fs::remove_file(&path);
    let _ = std::fs::remove_file(format!("{path}-journal"));
}

#[test]
fn cross_database_alter() {
    let mut c = Connection::open_memory().unwrap();
    c.execute("ATTACH ':memory:' AS aux").unwrap();
    c.execute("CREATE TABLE aux.t(a INT, b TEXT)").unwrap();
    c.execute("INSERT INTO aux.t VALUES(1,'x')").unwrap();

    // ADD / RENAME COLUMN / RENAME TABLE all target the attached database.
    c.execute("ALTER TABLE aux.t ADD COLUMN c INT DEFAULT 7")
        .unwrap();
    c.execute("ALTER TABLE aux.t RENAME COLUMN b TO bb")
        .unwrap();
    c.execute("ALTER TABLE aux.t RENAME TO t2").unwrap();
    let r = c.query("SELECT a, bb, c FROM aux.t2").unwrap();
    assert_eq!(
        r.rows,
        vec![vec![
            Value::Integer(1),
            Value::Text("x".into()),
            Value::Integer(7)
        ]]
    );

    // A same-named main table is untouched by ALTERs against aux.
    c.execute("CREATE TABLE main.t(z)").unwrap();
    c.execute("ALTER TABLE main.t ADD COLUMN w").unwrap();
    assert_eq!(
        c.query("SELECT count(*) FROM aux.sqlite_master WHERE name='t'")
            .unwrap()
            .rows[0][0],
        Value::Integer(0)
    );
}

#[test]
fn cross_database_alter_file_cross_engine() {
    let sqlite = std::process::Command::new("sqlite3")
        .arg("--version")
        .output()
        .is_ok();
    if !sqlite {
        return;
    }
    let mut p = std::env::temp_dir();
    p.push(format!(
        "graphitesql-attach-alter-{}.db",
        std::process::id()
    ));
    let path = p.to_string_lossy().into_owned();
    let _ = std::fs::remove_file(&path);

    {
        let mut c = Connection::open_memory().unwrap();
        c.execute(&format!("ATTACH '{path}' AS d")).unwrap();
        c.execute("CREATE TABLE d.t(a INT, b TEXT)").unwrap();
        c.execute("INSERT INTO d.t VALUES(1,'x')").unwrap();
        c.execute("ALTER TABLE d.t ADD COLUMN c INT DEFAULT 9")
            .unwrap();
        c.execute("ALTER TABLE d.t RENAME COLUMN b TO bb").unwrap();
    }
    // sqlite3 reads the ALTERed file: integrity ok and the new schema is visible.
    let out = std::process::Command::new("sqlite3")
        .arg(&path)
        .arg("PRAGMA integrity_check; SELECT a||'|'||bb||'|'||c FROM t;")
        .output()
        .unwrap();
    let s = String::from_utf8_lossy(&out.stdout);
    assert!(s.contains("ok"), "integrity: {s}");
    assert!(s.contains("1|x|9"), "row: {s}");
    let _ = std::fs::remove_file(&path);
    let _ = std::fs::remove_file(format!("{path}-journal"));
}

#[test]
fn cross_database_create_read_write() {
    let mut c = Connection::open_memory().unwrap();
    c.execute("CREATE TABLE t(a)").unwrap();
    c.execute("INSERT INTO t VALUES(99)").unwrap();
    c.execute("ATTACH ':memory:' AS aux").unwrap();

    // CREATE / INSERT into the attached database, then read it back.
    c.execute("CREATE TABLE aux.t(id INTEGER PRIMARY KEY, name TEXT)")
        .unwrap();
    assert_eq!(
        c.execute("INSERT INTO aux.t VALUES(1,'alice'),(2,'bob')")
            .unwrap(),
        2
    );
    let r = c.query("SELECT id, name FROM aux.t ORDER BY id").unwrap();
    assert_eq!(r.rows[0][1], Value::Text("alice".into()));
    assert_eq!(r.rows[1][1], Value::Text("bob".into()));

    // The two databases are isolated: main.t still has its own single row, and
    // each catalog lists only its own table.
    assert_eq!(
        c.query("SELECT a FROM t").unwrap().rows[0][0],
        Value::Integer(99)
    );
    assert_eq!(
        c.query("SELECT count(*) FROM main.t").unwrap().rows[0][0],
        Value::Integer(1)
    );
    assert_eq!(
        c.query("SELECT count(*) FROM aux.sqlite_master")
            .unwrap()
            .rows[0][0],
        Value::Integer(1)
    );

    // UPDATE / DELETE / DROP against the attached database.
    c.execute("UPDATE aux.t SET name='ALICE' WHERE id=1")
        .unwrap();
    c.execute("DELETE FROM aux.t WHERE id=2").unwrap();
    let r = c.query("SELECT id, name FROM aux.t").unwrap();
    assert_eq!(
        r.rows,
        vec![vec![Value::Integer(1), Value::Text("ALICE".into())]]
    );
    c.execute("DROP TABLE aux.t").unwrap();
    assert_eq!(
        c.query("SELECT count(*) FROM aux.sqlite_master")
            .unwrap()
            .rows[0][0],
        Value::Integer(0)
    );
    // main.t is untouched by the DROP in aux.
    assert_eq!(
        c.query("SELECT count(*) FROM main.sqlite_master")
            .unwrap()
            .rows[0][0],
        Value::Integer(1)
    );
}

#[test]
fn temp_tables() {
    let mut c = Connection::open_memory().unwrap();
    c.execute("CREATE TABLE t(a)").unwrap();
    c.execute("INSERT INTO t VALUES(99)").unwrap();

    // CREATE TEMP TABLE goes to the temp database, not main's catalog.
    c.execute("CREATE TEMP TABLE tmp(id INTEGER PRIMARY KEY, v TEXT)")
        .unwrap();
    assert_eq!(
        c.query("SELECT count(*) FROM sqlite_master").unwrap().rows[0][0],
        Value::Integer(1) // just `t`
    );
    // database_list shows temp at seq 1.
    let r = c.query("PRAGMA database_list").unwrap();
    assert_eq!(
        r.rows[1],
        vec![
            Value::Integer(1),
            Value::Text("temp".into()),
            Value::Text("".into())
        ]
    );
    // sqlite_temp_master lists temp objects.
    assert_eq!(
        c.query("SELECT name FROM sqlite_temp_master").unwrap().rows[0][0],
        Value::Text("tmp".into())
    );

    // Unqualified DML/reads resolve to the temp table.
    c.execute("INSERT INTO tmp VALUES(1,'a'),(2,'b')").unwrap();
    assert_eq!(
        c.query("SELECT count(*) FROM tmp").unwrap().rows[0][0],
        Value::Integer(2)
    );
    c.execute("UPDATE tmp SET v='Z' WHERE id=1").unwrap();
    c.execute("DELETE FROM tmp WHERE id=2").unwrap();
    let r = c.query("SELECT id, v FROM tmp").unwrap();
    assert_eq!(
        r.rows,
        vec![vec![Value::Integer(1), Value::Text("Z".into())]]
    );

    // A temp table shadows a same-named main table for unqualified names.
    c.execute("CREATE TEMP TABLE t(x)").unwrap();
    c.execute("INSERT INTO t VALUES(1)").unwrap();
    c.execute("INSERT INTO t VALUES(2)").unwrap();
    assert_eq!(
        c.query("SELECT count(*) FROM t").unwrap().rows[0][0],
        Value::Integer(2)
    );
    assert_eq!(
        c.query("SELECT count(*) FROM main.t").unwrap().rows[0][0],
        Value::Integer(1)
    );

    // DROP of the temp table leaves the main table intact.
    c.execute("DROP TABLE t").unwrap();
    assert_eq!(
        c.query("SELECT a FROM t").unwrap().rows[0][0],
        Value::Integer(99)
    );
}

#[test]
fn temp_tables_do_not_persist_to_a_file() {
    let mut p = std::env::temp_dir();
    p.push(format!("graphitesql-attach-temp-{}.db", std::process::id()));
    let path = p.to_string_lossy().into_owned();
    let _ = std::fs::remove_file(&path);
    {
        let mut c = Connection::create(&path).unwrap();
        c.execute("CREATE TABLE persist(a)").unwrap();
        c.execute("CREATE TEMP TABLE ephemeral(b)").unwrap();
        c.execute("INSERT INTO ephemeral VALUES(1)").unwrap();
    }
    // Reopening the file shows only the persistent table.
    let c = Connection::open(&path).unwrap();
    let names: Vec<_> = c
        .query("SELECT name FROM sqlite_master")
        .unwrap()
        .rows
        .into_iter()
        .map(|r| r[0].clone())
        .collect();
    assert_eq!(names, vec![Value::Text("persist".into())]);
    let _ = std::fs::remove_file(&path);
}

#[test]
fn attach_file_database_cross_engine() {
    let sqlite = std::process::Command::new("sqlite3")
        .arg("--version")
        .output()
        .is_ok();

    let mut p = std::env::temp_dir();
    p.push(format!("graphitesql-attach-file-{}.db", std::process::id()));
    let path = p.to_string_lossy().into_owned();
    let _ = std::fs::remove_file(&path);

    // Create + populate a brand-new file via ATTACH, then check sqlite3 reads it.
    {
        let mut c = Connection::open_memory().unwrap();
        c.execute(&format!("ATTACH '{path}' AS d")).unwrap();
        c.execute("CREATE TABLE d.t(id INTEGER PRIMARY KEY, v TEXT)")
            .unwrap();
        c.execute("INSERT INTO d.t VALUES(1,'hello'),(2,'world')")
            .unwrap();
        // database_list reports the file path for the attachment.
        let r = c.query("PRAGMA database_list").unwrap();
        assert_eq!(r.rows[1][1], Value::Text("d".into()));
        assert_eq!(r.rows[1][2], Value::Text(path.clone()));
    }
    if sqlite {
        let out = std::process::Command::new("sqlite3")
            .arg(&path)
            .arg("PRAGMA integrity_check; SELECT id||':'||v FROM t ORDER BY id;")
            .output()
            .unwrap();
        let s = String::from_utf8_lossy(&out.stdout);
        assert!(s.contains("ok"), "integrity: {s}");
        assert!(s.contains("1:hello") && s.contains("2:world"), "rows: {s}");
    }

    // Re-attach the existing file and read it back; a further write round-trips.
    {
        let mut c = Connection::open_memory().unwrap();
        c.execute(&format!("ATTACH '{path}' AS d")).unwrap();
        assert_eq!(
            c.query("SELECT v FROM d.t WHERE id=1").unwrap().rows[0][0],
            Value::Text("hello".into())
        );
        c.execute("INSERT INTO d.t VALUES(3,'again')").unwrap();
    }
    if sqlite {
        let out = std::process::Command::new("sqlite3")
            .arg(&path)
            .arg("SELECT count(*) FROM t;")
            .output()
            .unwrap();
        assert_eq!(String::from_utf8_lossy(&out.stdout).trim(), "3");
    }
    let _ = std::fs::remove_file(&path);
    let _ = std::fs::remove_file(format!("{path}-journal"));
}