xref: /sqlite-3.40.0/test/vtab_shared.test (revision d5578433)
1# 2007 April 16
2#
3# The author disclaims copyright to this source code.  In place of
4# a legal notice, here is a blessing:
5#
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.
9#
10#***********************************************************************
11# This file tests interactions between the virtual table and
12# shared-schema functionality.
13#
14# $Id: vtab_shared.test,v 1.3 2009/07/24 17:58:53 danielk1977 Exp $
15
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18
19ifcapable !vtab||!shared_cache {
20  finish_test
21  return
22}
23
24db close
25sqlite3_enable_shared_cache 1
26sqlite3 db  test.db
27sqlite3 db2 test.db
28
29do_test vtab_shared-1.1 {
30  register_echo_module [sqlite3_connection_pointer db]
31  execsql {
32    CREATE TABLE t0(a, b, c);
33    INSERT INTO t0 VALUES(1, 2, 3);
34    CREATE VIRTUAL TABLE t1 USING echo(t0);
35  }
36} {}
37
38do_test vtab_shared-1.2 {
39  execsql { SELECT * FROM t1 } db
40} {1 2 3}
41
42# Fails because the 'echo' module has not been registered with connection db2
43do_test vtab_shared-1.3 {
44  catchsql { SELECT * FROM t1 } db2
45} {1 {no such module: echo}}
46
47do_test vtab_shared-1.4 {
48  execsql { SELECT * FROM t0 } db2
49} {1 2 3}
50
51do_test vtab_shared-1.5 {
52  register_echo_module [sqlite3_connection_pointer db2]
53  execsql { SELECT * FROM t1 } db
54} {1 2 3}
55
56# Works after the module is registered with db2
57do_test vtab_shared-1.6 {
58  execsql { SELECT * FROM t1 } db2
59} {1 2 3}
60
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.
64#
65do_test vtab_shared-1.8.1 {
66  execsql {
67    BEGIN;
68    INSERT INTO t1 VALUES(4, 5, 6);
69    SELECT * FROM t1;
70  }
71} {1 2 3 4 5 6}
72do_test vtab_shared-1.8.2 {
73  catchsql { SELECT * FROM t1 } db2
74} {1 {database table is locked}}
75do_test vtab_shared-1.8.3 {
76  catchsql { SELECT *  FROM t0 } db2
77} {1 {database table is locked: t0}}
78do_test vtab_shared-1.8.4 {
79  execsql { SELECT * FROM t0 } db
80} {1 2 3 4 5 6}
81do_test vtab_shared-1.8.5 {
82  execsql { COMMIT } db
83  execsql { SELECT *  FROM t1 } db2
84} {1 2 3 4 5 6}
85
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.
89#
90foreach {iTest dbSelect dbClose} {
91  1 db  db2
92  2 db  db2
93  3 db2 db
94} {
95  do_test vtab_shared-1.9.$iTest {
96    set res [list]
97    $dbSelect eval { SELECT * FROM t1 } {
98      if {$a == 1} {$dbClose close}
99      lappend res $a $b $c
100    }
101    sqlite3 $dbClose test.db
102    register_echo_module [sqlite3_connection_pointer $dbClose]
103    set res
104  } {1 2 3 4 5 6}
105}
106
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.
109#
110do_test vtab_shared-1.10 {
111  db eval { SELECT * FROM t1 } {
112    set error [catchsql { DROP TABLE t1 } db2]
113    break
114  }
115  set error
116} {1 {database table is locked: sqlite_master}}
117
118do_test vtab_shared-1.11 {
119breakpoint
120  execsql {
121    CREATE VIRTUAL TABLE t2 USING echo(t0);
122    CREATE VIRTUAL TABLE t3 USING echo(t0);
123  }
124  execsql { SELECT * FROM t3 } db2
125} {1 2 3 4 5 6}
126
127ifcapable compound {
128  do_test vtab_shared-1.12.1 {
129    db close
130    execsql {
131      SELECT * FROM t1 UNION ALL
132      SELECT * FROM t2 UNION ALL
133      SELECT * FROM t3
134    } db2
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 {
137    sqlite3 db test.db
138    register_echo_module [sqlite3_connection_pointer db]
139    execsql {
140      SELECT * FROM t1 UNION ALL
141      SELECT * FROM t2 UNION ALL
142      SELECT * FROM t3
143    } db
144  } {1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6}
145}
146
147# Try a rename or two.
148#
149ifcapable altertable {
150  do_test vtab_shared-1.13.1 {
151    execsql { ALTER TABLE t1 RENAME TO t4 }
152    execsql { SELECT * FROM t4 } db
153  } {1 2 3 4 5 6}
154  do_test vtab_shared-1.13.2 {
155    execsql { SELECT * FROM t4 } db2
156  } {1 2 3 4 5 6}
157  do_test vtab_shared-1.13.3 {
158    execsql { ALTER TABLE t2 RENAME TO t5 }
159    execsql { SELECT * FROM t4 } db2
160  } {1 2 3 4 5 6}
161}
162
163# Try an UPDATE/INSERT/DELETE on a shared vtab as the first statement after a
164# schema is loaded.
165do_test vtab_shared_1.14.1 {
166  db2 close
167  sqlite3 db2 test.db
168  register_echo_module [sqlite3_connection_pointer db2]
169  execsql { SELECT * FROM t3 }
170} {1 2 3 4 5 6}
171do_test vtab_shared_1.14.2 {
172  execsql {
173    UPDATE t3 SET c = 'six' WHERE c = 6;
174    SELECT * FROM t3;
175  } db2
176} {1 2 3 4 5 six}
177do_test vtab_shared_1.14.3 {
178  db2 close
179  sqlite3 db2 test.db
180  register_echo_module [sqlite3_connection_pointer db2]
181  execsql { SELECT * FROM t3 }
182} {1 2 3 4 5 six}
183do_test vtab_shared_1.14.4 {
184  execsql {
185    DELETE FROM t3 WHERE c = 'six';
186    SELECT * FROM t3;
187  } db2
188} {1 2 3}
189do_test vtab_shared_1.14.5 {
190  db2 close
191  sqlite3 db2 test.db
192  register_echo_module [sqlite3_connection_pointer db2]
193  execsql { SELECT * FROM t3 }
194} {1 2 3}
195do_test vtab_shared_1.14.6 {
196  execsql {
197    INSERT INTO t3 VALUES(4, 5, 6);
198    SELECT * FROM t3;
199  } db2
200} {1 2 3 4 5 6}
201
202do_test vtab_shared_1.15.1 {
203  db2 close
204  sqlite3 db2 test.db
205  register_echo_module [sqlite3_connection_pointer db2]
206  execsql {
207    UPDATE t3 SET c = 'six' WHERE c = 6;
208    SELECT * FROM t3;
209  } db2
210} {1 2 3 4 5 six}
211do_test vtab_shared_1.15.2 {
212  db2 close
213  sqlite3 db2 test.db
214  register_echo_module [sqlite3_connection_pointer db2]
215  execsql {
216    DELETE FROM t3 WHERE c = 'six';
217    SELECT * FROM t3;
218  } db2
219} {1 2 3}
220do_test vtab_shared_1.15.3 {
221  db2 close
222  sqlite3 db2 test.db
223  register_echo_module [sqlite3_connection_pointer db2]
224  execsql {
225    INSERT INTO t3 VALUES(4, 5, 6);
226    SELECT * FROM t3;
227  }
228} {1 2 3 4 5 6}
229
230db close
231db2 close
232sqlite3_enable_shared_cache 0
233finish_test
234