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
18 set testprefix vtab_shared
20 ifcapable !vtab||!shared_cache {
26 sqlite3_enable_shared_cache 1
30 do_test vtab_shared-1.1 {
31 register_echo_module [sqlite3_connection_pointer db]
33 CREATE TABLE t0(a, b, c);
34 INSERT INTO t0 VALUES(1, 2, 3);
35 CREATE VIRTUAL TABLE t1 USING echo(t0);
39 do_test vtab_shared-1.2 {
40 execsql { SELECT * FROM t1 } db
43 # Fails because the 'echo' module has not been registered with connection db2
44 do_test vtab_shared-1.3 {
45 catchsql { SELECT * FROM t1 } db2
46 } {1 {no such module: echo}}
48 do_test vtab_shared-1.4 {
49 execsql { SELECT * FROM t0 } db2
52 do_test vtab_shared-1.5 {
53 register_echo_module [sqlite3_connection_pointer db2]
54 execsql { SELECT * FROM t1 } db
57 # Works after the module is registered with db2
58 do_test vtab_shared-1.6 {
59 execsql { SELECT * FROM t1 } db2
62 # Set a write-lock on table t0 using connection [db]. Then try to read from
63 # virtual table t1 using [db2]. That this returns an SQLITE_LOCKED error
64 # shows that the correct sqlite3_vtab is being used.
66 do_test vtab_shared-1.8.1 {
69 INSERT INTO t1 VALUES(4, 5, 6);
73 do_test vtab_shared-1.8.2 {
74 catchsql { SELECT * FROM t1 } db2
75 } {1 {database table is locked}}
76 do_test vtab_shared-1.8.3 {
77 catchsql { SELECT * FROM t0 } db2
78 } {1 {database table is locked: t0}}
79 do_test vtab_shared-1.8.4 {
80 execsql { SELECT * FROM t0 } db
82 do_test vtab_shared-1.8.5 {
84 execsql { SELECT * FROM t1 } db2
87 # While a SELECT is active on virtual table t1 via connection [db], close
88 # [db2]. This causes the schema to be reset internally. Verify that this
89 # does not cause a problem.
91 foreach {iTest dbSelect dbClose} {
96 do_test vtab_shared-1.9.$iTest {
98 $dbSelect eval { SELECT * FROM t1 } {
99 if {$a == 1} {$dbClose close}
102 sqlite3 $dbClose test.db
103 register_echo_module [sqlite3_connection_pointer $dbClose]
108 # Ensure that it is not possible for one connection to DROP a virtual
109 # table while a second connection is reading from the database.
111 do_test vtab_shared-1.10 {
112 db eval { SELECT * FROM t1 } {
113 set error [catchsql { DROP TABLE t1 } db2]
117 } {1 {database table is locked: sqlite_master}}
119 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
128 do_test vtab_shared-1.12.1 {
131 SELECT * FROM t1 UNION ALL
132 SELECT * FROM t2 UNION ALL
135 } {1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6}
136 do_test vtab_shared-1.12.2 {
138 register_echo_module [sqlite3_connection_pointer db]
140 SELECT * FROM t1 UNION ALL
141 SELECT * FROM t2 UNION ALL
144 } {1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6}
147 # Try a rename or two.
149 ifcapable altertable {
150 do_test vtab_shared-1.13.1 {
151 execsql { ALTER TABLE t1 RENAME TO t4 }
152 execsql { SELECT * FROM t4 } db
154 do_test vtab_shared-1.13.2 {
155 execsql { SELECT * FROM t4 } db2
157 do_test vtab_shared-1.13.3 {
158 execsql { ALTER TABLE t2 RENAME TO t5 }
159 execsql { SELECT * FROM t4 } db2
163 # Try an UPDATE/INSERT/DELETE on a shared vtab as the first statement after a
165 do_test vtab_shared_1.14.1 {
168 register_echo_module [sqlite3_connection_pointer db2]
169 execsql { SELECT * FROM t3 }
171 do_test vtab_shared_1.14.2 {
173 UPDATE t3 SET c = 'six' WHERE c = 6;
177 do_test vtab_shared_1.14.3 {
180 register_echo_module [sqlite3_connection_pointer db2]
181 execsql { SELECT * FROM t3 }
183 do_test vtab_shared_1.14.4 {
185 DELETE FROM t3 WHERE c = 'six';
189 do_test vtab_shared_1.14.5 {
192 register_echo_module [sqlite3_connection_pointer db2]
193 execsql { SELECT * FROM t3 }
195 do_test vtab_shared_1.14.6 {
197 INSERT INTO t3 VALUES(4, 5, 6);
202 do_test vtab_shared_1.15.1 {
205 register_echo_module [sqlite3_connection_pointer db2]
207 UPDATE t3 SET c = 'six' WHERE c = 6;
211 do_test vtab_shared_1.15.2 {
214 register_echo_module [sqlite3_connection_pointer db2]
216 DELETE FROM t3 WHERE c = 'six';
220 do_test vtab_shared_1.15.3 {
223 register_echo_module [sqlite3_connection_pointer db2]
225 INSERT INTO t3 VALUES(4, 5, 6);
233 #---------------------------------------------------------------
234 # Test calling sqlite3_close() with vtabs on the disconnect list.
242 # Create a virtual table using [db].
244 CREATE VIRTUAL TABLE rt USING rtree(id, x1, x2);
245 INSERT INTO rt VALUES(1, 2 ,3);
249 # Drop the virtual table using [db2]. The sqlite3_vtab object belonging
250 # to [db] is moved to the sqlite3.pDisconnect list.
251 execsql { DROP TABLE rt } db2
253 # Immediately close [db]. At one point this would fail due to the
254 # unfinalized statements held by the un-xDisconnect()ed sqlite3_vtab.
261 # Same test as above, except using fts3 instead of rtree.
267 CREATE VIRTUAL TABLE ft USING fts3;
268 INSERT INTO ft VALUES('hello world');
271 execsql { DROP TABLE ft } db2
277 sqlite3_enable_shared_cache 0