3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
12 # Tests for calling sqlite3_snapshot_open() when there is already
13 # a read transaction open on the database.
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
18 ifcapable !snapshot {finish_test; return}
19 set testprefix snapshot_up
21 # This test does not work with the inmemory_journal permutation. The reason
22 # is that each connection opened as part of this permutation executes
23 # "PRAGMA journal_mode=memory", which fails if the database is in wal mode
24 # and there are one or more existing connections.
25 if {[permutation]=="inmemory_journal"} {
33 CREATE TABLE t1(a, b, c);
34 PRAGMA journal_mode = wal;
35 INSERT INTO t1 VALUES(1, 2, 3);
36 INSERT INTO t1 VALUES(4, 5, 6);
37 INSERT INTO t1 VALUES(7, 8, 9);
42 set ::snap1 [sqlite3_snapshot_get db main]
44 execsql { INSERT INTO t1 VALUES(10, 11, 12); }
46 set ::snap2 [sqlite3_snapshot_get db main]
48 execsql { INSERT INTO t1 VALUES(13, 14, 15); }
50 set ::snap3 [sqlite3_snapshot_get db main]
57 } {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15}
60 sqlite3_snapshot_open db main $::snap1
61 execsql { SELECT * FROM t1 }
65 sqlite3_snapshot_open db main $::snap2
66 execsql { SELECT * FROM t1 }
67 } {1 2 3 4 5 6 7 8 9 10 11 12}
71 execsql { PRAGMA wal_checkpoint } db2
76 } {1 2 3 4 5 6 7 8 9 10 11 12}
79 list [catch { sqlite3_snapshot_open db main $::snap1 } msg] $msg
80 } {1 SQLITE_ERROR_SNAPSHOT}
84 } {1 2 3 4 5 6 7 8 9 10 11 12}
87 execsql { COMMIT ; BEGIN }
88 list [catch { sqlite3_snapshot_open db main $::snap1 } msg] $msg
89 } {1 SQLITE_ERROR_SNAPSHOT}
94 PRAGMA wal_checkpoint;
95 DELETE FROM t1 WHERE a = 1;
98 set ::snap4 [sqlite3_snapshot_get db main]
101 DELETE FROM t1 WHERE a = 4;
110 } {7 8 9 10 11 12 13 14 15}
112 sqlite3_snapshot_open db main $::snap4
113 execsql { SELECT * FROM t1 }
114 } {4 5 6 7 8 9 10 11 12 13 14 15}
117 list [catch { sqlite3_snapshot_open db main $::snap3 } msg] $msg
118 } {1 SQLITE_ERROR_SNAPSHOT}
120 execsql { SELECT * FROM t1 }
121 } {4 5 6 7 8 9 10 11 12 13 14 15}
126 do_execsql_test 1.15 {
129 } {7 8 9 10 11 12 13 14 15}
131 list [catch { sqlite3_snapshot_open db main $::snap4 } msg] $msg
132 } {1 SQLITE_ERROR_SNAPSHOT}
133 do_execsql_test 1.17 { COMMIT }
135 sqlite3_snapshot_free $::snap1
136 sqlite3_snapshot_free $::snap2
137 sqlite3_snapshot_free $::snap3
138 sqlite3_snapshot_free $::snap4
140 #-------------------------------------------------------------------------
146 proc xBusy {args} { return 1 }
150 execsql { INSERT INTO t1 VALUES(16, 17, 18) } db2
152 set ::snap1 [sqlite3_snapshot_get db main]
154 execsql { INSERT INTO t1 VALUES(19, 20, 21) } db2
156 set ::snap2 [sqlite3_snapshot_get db main]
161 do_execsql_test -db db2 2.2 {
163 INSERT INTO t1 VALUES(19, 20, 21);
168 sqlite3_snapshot_open db main $::snap1
169 execsql { SELECT * FROM t1 }
170 } {7 8 9 10 11 12 13 14 15 16 17 18}
173 set ::res [list [catch { sqlite3_snapshot_open db main $::snap2 } msg] $msg]
178 execsql {PRAGMA wal_checkpoint = restart} db3
182 sqlite3_snapshot_free $::snap1
183 sqlite3_snapshot_free $::snap2