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"} {
31 CREATE TABLE t1(a, b, c);
32 PRAGMA journal_mode = wal;
33 INSERT INTO t1 VALUES(1, 2, 3);
34 INSERT INTO t1 VALUES(4, 5, 6);
35 INSERT INTO t1 VALUES(7, 8, 9);
40 set ::snap1 [sqlite3_snapshot_get db main]
42 execsql { INSERT INTO t1 VALUES(10, 11, 12); }
44 set ::snap2 [sqlite3_snapshot_get db main]
46 execsql { INSERT INTO t1 VALUES(13, 14, 15); }
48 set ::snap3 [sqlite3_snapshot_get db main]
55 } {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15}
58 sqlite3_snapshot_open db main $::snap1
59 execsql { SELECT * FROM t1 }
63 sqlite3_snapshot_open db main $::snap2
64 execsql { SELECT * FROM t1 }
65 } {1 2 3 4 5 6 7 8 9 10 11 12}
69 execsql { PRAGMA wal_checkpoint } db2
74 } {1 2 3 4 5 6 7 8 9 10 11 12}
77 list [catch { sqlite3_snapshot_open db main $::snap1 } msg] $msg
78 } {1 SQLITE_ERROR_SNAPSHOT}
82 } {1 2 3 4 5 6 7 8 9 10 11 12}
85 execsql { COMMIT ; BEGIN }
86 list [catch { sqlite3_snapshot_open db main $::snap1 } msg] $msg
87 } {1 SQLITE_ERROR_SNAPSHOT}
92 PRAGMA wal_checkpoint;
93 DELETE FROM t1 WHERE a = 1;
96 set ::snap4 [sqlite3_snapshot_get db main]
99 DELETE FROM t1 WHERE a = 4;
108 } {7 8 9 10 11 12 13 14 15}
110 sqlite3_snapshot_open db main $::snap4
111 execsql { SELECT * FROM t1 }
112 } {4 5 6 7 8 9 10 11 12 13 14 15}
115 list [catch { sqlite3_snapshot_open db main $::snap3 } msg] $msg
116 } {1 SQLITE_ERROR_SNAPSHOT}
118 execsql { SELECT * FROM t1 }
119 } {4 5 6 7 8 9 10 11 12 13 14 15}
124 do_execsql_test 1.15 {
127 } {7 8 9 10 11 12 13 14 15}
129 list [catch { sqlite3_snapshot_open db main $::snap4 } msg] $msg
130 } {1 SQLITE_ERROR_SNAPSHOT}
131 do_execsql_test 1.17 { COMMIT }
133 sqlite3_snapshot_free $::snap1
134 sqlite3_snapshot_free $::snap2
135 sqlite3_snapshot_free $::snap3
136 sqlite3_snapshot_free $::snap4
138 #-------------------------------------------------------------------------
144 proc xBusy {args} { return 1 }
148 execsql { INSERT INTO t1 VALUES(16, 17, 18) } db2
150 set ::snap1 [sqlite3_snapshot_get db main]
152 execsql { INSERT INTO t1 VALUES(19, 20, 21) } db2
154 set ::snap2 [sqlite3_snapshot_get db main]
159 do_execsql_test -db db2 2.2 {
161 INSERT INTO t1 VALUES(19, 20, 21);
166 sqlite3_snapshot_open db main $::snap1
167 execsql { SELECT * FROM t1 }
168 } {7 8 9 10 11 12 13 14 15 16 17 18}
171 set ::res [list [catch { sqlite3_snapshot_open db main $::snap2 } msg] $msg]
176 execsql {PRAGMA wal_checkpoint = restart} db3
180 sqlite3_snapshot_free $::snap1
181 sqlite3_snapshot_free $::snap2