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 #***********************************************************************
11 # This file tests interactions between the virtual table and
12 # shared-schema functionality.
14 # $Id: vtab_shared.test,v 1.3 2009/07/24 17:58:53 danielk1977 Exp $
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
19 ifcapable !vtab||!shared_cache {
25 sqlite3_enable_shared_cache 1
29 do_test vtab_shared-1.1 {
30 register_echo_module [sqlite3_connection_pointer db]
32 CREATE TABLE t0(a, b, c);
33 INSERT INTO t0 VALUES(1, 2, 3);
34 CREATE VIRTUAL TABLE t1 USING echo(t0);
38 do_test vtab_shared-1.2 {
39 execsql { SELECT * FROM t1 } db
42 # Fails because the 'echo' module has not been registered with connection db2
43 do_test vtab_shared-1.3 {
44 catchsql { SELECT * FROM t1 } db2
45 } {1 {no such module: echo}}
47 do_test vtab_shared-1.4 {
48 execsql { SELECT * FROM t0 } db2
51 do_test vtab_shared-1.5 {
52 register_echo_module [sqlite3_connection_pointer db2]
53 execsql { SELECT * FROM t1 } db
56 # Works after the module is registered with db2
57 do_test vtab_shared-1.6 {
58 execsql { SELECT * FROM t1 } db2
61 # Set a write-lock on table t0 using connection [db]. Then try to read from
62 # virtual table t1 using [db2]. That this returns an SQLITE_LOCKED error
63 # shows that the correct sqlite3_vtab is being used.
65 do_test vtab_shared-1.8.1 {
68 INSERT INTO t1 VALUES(4, 5, 6);
72 do_test vtab_shared-1.8.2 {
73 catchsql { SELECT * FROM t1 } db2
74 } {1 {database table is locked}}
75 do_test vtab_shared-1.8.3 {
76 catchsql { SELECT * FROM t0 } db2
77 } {1 {database table is locked: t0}}
78 do_test vtab_shared-1.8.4 {
79 execsql { SELECT * FROM t0 } db
81 do_test vtab_shared-1.8.5 {
83 execsql { SELECT * FROM t1 } db2
86 # While a SELECT is active on virtual table t1 via connection [db], close
87 # [db2]. This causes the schema to be reset internally. Verify that this
88 # does not cause a problem.
90 foreach {iTest dbSelect dbClose} {
95 do_test vtab_shared-1.9.$iTest {
97 $dbSelect eval { SELECT * FROM t1 } {
98 if {$a == 1} {$dbClose close}
101 sqlite3 $dbClose test.db
102 register_echo_module [sqlite3_connection_pointer $dbClose]
107 # Ensure that it is not possible for one connection to DROP a virtual
108 # table while a second connection is reading from the database.
110 do_test vtab_shared-1.10 {
111 db eval { SELECT * FROM t1 } {
112 set error [catchsql { DROP TABLE t1 } db2]
116 } {1 {database table is locked: sqlite_master}}
118 do_test vtab_shared-1.11 {
121 CREATE VIRTUAL TABLE t2 USING echo(t0);
122 CREATE VIRTUAL TABLE t3 USING echo(t0);
124 execsql { SELECT * FROM t3 } db2
127 do_test vtab_shared-1.12.1 {
130 SELECT * FROM t1 UNION ALL
131 SELECT * FROM t2 UNION ALL
134 } {1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6}
135 do_test vtab_shared-1.12.2 {
137 register_echo_module [sqlite3_connection_pointer db]
139 SELECT * FROM t1 UNION ALL
140 SELECT * FROM t2 UNION ALL
143 } {1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6}
145 # Try a rename or two.
147 ifcapable altertable {
148 do_test vtab_shared-1.13.1 {
149 execsql { ALTER TABLE t1 RENAME TO t4 }
150 execsql { SELECT * FROM t4 } db
152 do_test vtab_shared-1.13.2 {
153 execsql { SELECT * FROM t4 } db2
155 do_test vtab_shared-1.13.3 {
156 execsql { ALTER TABLE t2 RENAME TO t5 }
157 execsql { SELECT * FROM t4 } db2
161 # Try an UPDATE/INSERT/DELETE on a shared vtab as the first statement after a
163 do_test vtab_shared_1.14.1 {
166 register_echo_module [sqlite3_connection_pointer db2]
167 execsql { SELECT * FROM t3 }
169 do_test vtab_shared_1.14.2 {
171 UPDATE t3 SET c = 'six' WHERE c = 6;
175 do_test vtab_shared_1.14.3 {
178 register_echo_module [sqlite3_connection_pointer db2]
179 execsql { SELECT * FROM t3 }
181 do_test vtab_shared_1.14.4 {
183 DELETE FROM t3 WHERE c = 'six';
187 do_test vtab_shared_1.14.5 {
190 register_echo_module [sqlite3_connection_pointer db2]
191 execsql { SELECT * FROM t3 }
193 do_test vtab_shared_1.14.6 {
195 INSERT INTO t3 VALUES(4, 5, 6);
200 do_test vtab_shared_1.15.1 {
203 register_echo_module [sqlite3_connection_pointer db2]
205 UPDATE t3 SET c = 'six' WHERE c = 6;
209 do_test vtab_shared_1.15.2 {
212 register_echo_module [sqlite3_connection_pointer db2]
214 DELETE FROM t3 WHERE c = 'six';
218 do_test vtab_shared_1.15.3 {
221 register_echo_module [sqlite3_connection_pointer db2]
223 INSERT INTO t3 VALUES(4, 5, 6);
230 sqlite3_enable_shared_cache 0