blob: a0cd0a66969aae055f48505f470f9ab1a838f3e3 [file] [log] [blame]
danielk1977aef0bf62005-12-30 16:28:01 +00001# 2005 December 30
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#***********************************************************************
danielk1977aef0bf62005-12-30 16:28:01 +000011#
danielk1977404ca072009-03-16 13:19:36 +000012# $Id: shared.test,v 1.36 2009/03/16 13:19:36 danielk1977 Exp $
danielk1977aef0bf62005-12-30 16:28:01 +000013
14set testdir [file dirname $argv0]
15source $testdir/tester.tcl
16db close
17
danielk19775a8f9372007-10-09 08:29:32 +000018# These tests cannot be run without the ATTACH command.
19#
20ifcapable !shared_cache||!attach {
danielk1977aef0bf62005-12-30 16:28:01 +000021 finish_test
22 return
23}
danielk1977a96a7102006-01-16 12:46:41 +000024
danielk1977da184232006-01-05 11:34:32 +000025set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
danielk1977aef0bf62005-12-30 16:28:01 +000026
danielk1977a96a7102006-01-16 12:46:41 +000027foreach av [list 0 1] {
28
danielk1977bab45c62006-01-16 15:14:27 +000029# Open the database connection and execute the auto-vacuum pragma
mistachkinfda06be2011-08-02 00:57:34 +000030forcedelete test.db
danielk1977bab45c62006-01-16 15:14:27 +000031sqlite3 db test.db
32
33ifcapable autovacuum {
34 do_test shared-[expr $av+1].1.0 {
35 execsql "pragma auto_vacuum=$::av"
36 execsql {pragma auto_vacuum}
37 } "$av"
38} else {
39 if {$av} {
40 db close
41 break
42 }
danielk1977a96a7102006-01-16 12:46:41 +000043}
44
aswiftaebf4132008-11-21 00:10:35 +000045# if we're using proxy locks, we use 2 filedescriptors for a db
46# that is open but NOT yet locked, after a lock is taken we'll have 3,
47# normally sqlite uses 1 (proxy locking adds the conch and the local lock)
48set using_proxy 0
49foreach {name value} [array get env SQLITE_FORCE_PROXY_LOCKING] {
50 set using_proxy $value
51}
52set extrafds_prelock 0
53set extrafds_postlock 0
54if {$using_proxy>0} {
55 set extrafds_prelock 1
56 set extrafds_postlock 2
57}
58
danielk1977191c3e72006-01-19 07:18:14 +000059# $av is currently 0 if this loop iteration is to test with auto-vacuum turned
60# off, and 1 if it is turned on. Increment it so that (1 -> no auto-vacuum)
61# and (2 -> auto-vacuum). The sole reason for this is so that it looks nicer
62# when we use this variable as part of test-case names.
63#
danielk1977a96a7102006-01-16 12:46:41 +000064incr av
65
danielk1977aef0bf62005-12-30 16:28:01 +000066# Test organization:
67#
68# shared-1.*: Simple test to verify basic sanity of table level locking when
69# two connections share a pager cache.
70# shared-2.*: Test that a read transaction can co-exist with a
71# write-transaction, including a simple test to ensure the
72# external locking protocol is still working.
danielk1977da184232006-01-05 11:34:32 +000073# shared-3.*: Simple test of read-uncommitted mode.
danielk1977de0fe3e2006-01-06 06:33:12 +000074# shared-4.*: Check that the schema is locked and unlocked correctly.
danielk1977aaf22682006-01-06 15:03:48 +000075# shared-5.*: Test that creating/dropping schema items works when databases
76# are attached in different orders to different handles.
danielk1977c00da102006-01-07 13:21:04 +000077# shared-6.*: Locking, UNION ALL queries and sub-queries.
danielk197714db2662006-01-09 16:12:04 +000078# shared-7.*: Autovacuum and shared-cache.
danielk1977ed429312006-01-19 08:43:31 +000079# shared-8.*: Tests related to the text encoding of shared-cache databases.
80# shared-9.*: TEMP triggers and shared-cache databases.
81# shared-10.*: Tests of sqlite3_close().
danielk19774b202ae2006-01-23 05:50:58 +000082# shared-11.*: Test transaction locking.
danielk1977de0fe3e2006-01-06 06:33:12 +000083#
danielk1977aef0bf62005-12-30 16:28:01 +000084
danielk1977a96a7102006-01-16 12:46:41 +000085do_test shared-$av.1.1 {
danielk1977aef0bf62005-12-30 16:28:01 +000086 # Open a second database on the file test.db. It should use the same pager
87 # cache and schema as the original connection. Verify that only 1 file is
88 # opened.
89 sqlite3 db2 test.db
danielk1977aef0bf62005-12-30 16:28:01 +000090 set ::sqlite_open_file_count
aswiftaebf4132008-11-21 00:10:35 +000091 expr $sqlite_open_file_count-$extrafds_postlock
danielk1977aef0bf62005-12-30 16:28:01 +000092} {1}
danielk1977a96a7102006-01-16 12:46:41 +000093do_test shared-$av.1.2 {
danielk1977aef0bf62005-12-30 16:28:01 +000094 # Add a table and a single row of data via the first connection.
95 # Ensure that the second connection can see them.
96 execsql {
97 CREATE TABLE abc(a, b, c);
98 INSERT INTO abc VALUES(1, 2, 3);
99 } db
100 execsql {
101 SELECT * FROM abc;
102 } db2
103} {1 2 3}
danielk1977a96a7102006-01-16 12:46:41 +0000104do_test shared-$av.1.3 {
danielk1977aef0bf62005-12-30 16:28:01 +0000105 # Have the first connection begin a transaction and obtain a read-lock
106 # on table abc. This should not prevent the second connection from
107 # querying abc.
108 execsql {
109 BEGIN;
110 SELECT * FROM abc;
111 }
112 execsql {
113 SELECT * FROM abc;
114 } db2
115} {1 2 3}
danielk1977a96a7102006-01-16 12:46:41 +0000116do_test shared-$av.1.4 {
danielk1977aef0bf62005-12-30 16:28:01 +0000117 # Try to insert a row into abc via connection 2. This should fail because
118 # of the read-lock connection 1 is holding on table abc (obtained in the
119 # previous test case).
120 catchsql {
121 INSERT INTO abc VALUES(4, 5, 6);
122 } db2
danielk1977c00da102006-01-07 13:21:04 +0000123} {1 {database table is locked: abc}}
danielk1977a96a7102006-01-16 12:46:41 +0000124do_test shared-$av.1.5 {
danielk1977da184232006-01-05 11:34:32 +0000125 # Using connection 2 (the one without the open transaction), try to create
126 # a new table. This should fail because of the open read transaction
127 # held by connection 1.
128 catchsql {
129 CREATE TABLE def(d, e, f);
130 } db2
danielk1977c00da102006-01-07 13:21:04 +0000131} {1 {database table is locked: sqlite_master}}
danielk1977a96a7102006-01-16 12:46:41 +0000132do_test shared-$av.1.6 {
danielk1977da184232006-01-05 11:34:32 +0000133 # Upgrade connection 1's transaction to a write transaction. Create
134 # a new table - def - and insert a row into it. Because the connection 1
135 # transaction modifies the schema, it should not be possible for
136 # connection 2 to access the database at all until the connection 1
137 # has finished the transaction.
danielk1977aef0bf62005-12-30 16:28:01 +0000138 execsql {
139 CREATE TABLE def(d, e, f);
danielk1977aef0bf62005-12-30 16:28:01 +0000140 INSERT INTO def VALUES('IV', 'V', 'VI');
141 }
142} {}
danielk1977a96a7102006-01-16 12:46:41 +0000143do_test shared-$av.1.7 {
danielk1977aef0bf62005-12-30 16:28:01 +0000144 # Read from the sqlite_master table with connection 1 (inside the
danielk1977da184232006-01-05 11:34:32 +0000145 # transaction). Then test that we can not do this with connection 2. This
146 # is because of the schema-modified lock established by connection 1
147 # in the previous test case.
danielk1977aef0bf62005-12-30 16:28:01 +0000148 execsql {
149 SELECT * FROM sqlite_master;
150 }
151 catchsql {
danielk1977da184232006-01-05 11:34:32 +0000152 SELECT * FROM sqlite_master;
danielk1977aef0bf62005-12-30 16:28:01 +0000153 } db2
danielk1977c87d34d2006-01-06 13:00:28 +0000154} {1 {database schema is locked: main}}
danielk1977a96a7102006-01-16 12:46:41 +0000155do_test shared-$av.1.8 {
danielk1977aef0bf62005-12-30 16:28:01 +0000156 # Commit the connection 1 transaction.
157 execsql {
158 COMMIT;
159 }
160} {}
161
danielk1977a96a7102006-01-16 12:46:41 +0000162do_test shared-$av.2.1 {
drh7f42dcd2020-11-16 18:45:21 +0000163 # Open connection db3 to the database.
drhc693e9e2006-01-23 21:38:03 +0000164 if {$::tcl_platform(platform)=="unix"} {
drh7f42dcd2020-11-16 18:45:21 +0000165 sqlite3 db3 "file:test.db?cache=private" -uri 1
drhc693e9e2006-01-23 21:38:03 +0000166 } else {
167 sqlite3 db3 TEST.DB
168 }
danielk1977aef0bf62005-12-30 16:28:01 +0000169 set ::sqlite_open_file_count
aswiftaebf4132008-11-21 00:10:35 +0000170 expr $sqlite_open_file_count-($extrafds_prelock+$extrafds_postlock)
danielk1977aef0bf62005-12-30 16:28:01 +0000171} {2}
danielk1977a96a7102006-01-16 12:46:41 +0000172do_test shared-$av.2.2 {
danielk1977aef0bf62005-12-30 16:28:01 +0000173 # Start read transactions on db and db2 (the shared pager cache). Ensure
174 # db3 cannot write to the database.
175 execsql {
176 BEGIN;
177 SELECT * FROM abc;
178 }
179 execsql {
180 BEGIN;
181 SELECT * FROM abc;
182 } db2
183 catchsql {
184 INSERT INTO abc VALUES(1, 2, 3);
185 } db2
danielk1977c00da102006-01-07 13:21:04 +0000186} {1 {database table is locked: abc}}
danielk1977a96a7102006-01-16 12:46:41 +0000187do_test shared-$av.2.3 {
danielk1977aef0bf62005-12-30 16:28:01 +0000188 # Turn db's transaction into a write-transaction. db3 should still be
189 # able to read from table def (but will not see the new row). Connection
190 # db2 should not be able to read def (because of the write-lock).
191
192# Todo: The failed "INSERT INTO abc ..." statement in the above test
193# has started a write-transaction on db2 (should this be so?). This
194# would prevent connection db from starting a write-transaction. So roll the
195# db2 transaction back and replace it with a new read transaction.
196 execsql {
197 ROLLBACK;
198 BEGIN;
199 SELECT * FROM abc;
200 } db2
201
202 execsql {
203 INSERT INTO def VALUES('VII', 'VIII', 'IX');
204 }
205 concat [
206 catchsql { SELECT * FROM def; } db3
207 ] [
208 catchsql { SELECT * FROM def; } db2
209 ]
danielk1977c00da102006-01-07 13:21:04 +0000210} {0 {IV V VI} 1 {database table is locked: def}}
danielk1977a96a7102006-01-16 12:46:41 +0000211do_test shared-$av.2.4 {
danielk1977aef0bf62005-12-30 16:28:01 +0000212 # Commit the open transaction on db. db2 still holds a read-transaction.
213 # This should prevent db3 from writing to the database, but not from
214 # reading.
215 execsql {
216 COMMIT;
217 }
218 concat [
219 catchsql { SELECT * FROM def; } db3
220 ] [
221 catchsql { INSERT INTO def VALUES('X', 'XI', 'XII'); } db3
222 ]
danielk1977da184232006-01-05 11:34:32 +0000223} {0 {IV V VI VII VIII IX} 1 {database is locked}}
danielk1977aef0bf62005-12-30 16:28:01 +0000224
danielk1977da184232006-01-05 11:34:32 +0000225catchsql COMMIT db2
226
danielk1977a96a7102006-01-16 12:46:41 +0000227do_test shared-$av.3.1.1 {
danielk1977da184232006-01-05 11:34:32 +0000228 # This test case starts a linear scan of table 'seq' using a
229 # read-uncommitted connection. In the middle of the scan, rows are added
230 # to the end of the seq table (ahead of the current cursor position).
231 # The uncommitted rows should be included in the results of the scan.
232 execsql "
danielk1977191c3e72006-01-19 07:18:14 +0000233 CREATE TABLE seq(i PRIMARY KEY, x);
danielk1977da184232006-01-05 11:34:32 +0000234 INSERT INTO seq VALUES(1, '[string repeat X 500]');
235 INSERT INTO seq VALUES(2, '[string repeat X 500]');
236 "
237 execsql {SELECT * FROM sqlite_master} db2
238 execsql {PRAGMA read_uncommitted = 1} db2
239
240 set ret [list]
danielk1977191c3e72006-01-19 07:18:14 +0000241 db2 eval {SELECT i FROM seq ORDER BY i} {
danielk1977da184232006-01-05 11:34:32 +0000242 if {$i < 4} {
danielk19771576cd92006-01-14 08:02:28 +0000243 set max [execsql {SELECT max(i) FROM seq}]
244 db eval {
danielk19773bdca9c2006-01-17 09:35:01 +0000245 INSERT INTO seq SELECT i + :max, x FROM seq;
danielk1977da184232006-01-05 11:34:32 +0000246 }
247 }
248 lappend ret $i
249 }
250 set ret
251} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16}
danielk1977a96a7102006-01-16 12:46:41 +0000252do_test shared-$av.3.1.2 {
danielk1977da184232006-01-05 11:34:32 +0000253 # Another linear scan through table seq using a read-uncommitted connection.
254 # This time, delete each row as it is read. Should not affect the results of
255 # the scan, but the table should be empty after the scan is concluded
256 # (test 3.1.3 verifies this).
257 set ret [list]
258 db2 eval {SELECT i FROM seq} {
danielk19773bdca9c2006-01-17 09:35:01 +0000259 db eval {DELETE FROM seq WHERE i = :i}
danielk1977da184232006-01-05 11:34:32 +0000260 lappend ret $i
261 }
262 set ret
263} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16}
danielk1977a96a7102006-01-16 12:46:41 +0000264do_test shared-$av.3.1.3 {
danielk1977da184232006-01-05 11:34:32 +0000265 execsql {
266 SELECT * FROM seq;
267 }
268} {}
danielk1977aef0bf62005-12-30 16:28:01 +0000269
270catch {db close}
271catch {db2 close}
272catch {db3 close}
273
danielk1977de0fe3e2006-01-06 06:33:12 +0000274#--------------------------------------------------------------------------
275# Tests shared-4.* test that the schema locking rules are applied
276# correctly. i.e.:
277#
278# 1. All transactions require a read-lock on the schemas of databases they
279# access.
280# 2. Transactions that modify a database schema require a write-lock on that
281# schema.
282# 3. It is not possible to compile a statement while another handle has a
283# write-lock on the schema.
284#
285
286# Open two database handles db and db2. Each has a single attach database
287# (as well as main):
288#
289# db.main -> ./test.db
290# db.test2 -> ./test2.db
291# db2.main -> ./test2.db
292# db2.test -> ./test.db
293#
mistachkinfda06be2011-08-02 00:57:34 +0000294forcedelete test.db
295forcedelete test2.db
296forcedelete test2.db-journal
danielk1977de0fe3e2006-01-06 06:33:12 +0000297sqlite3 db test.db
298sqlite3 db2 test2.db
danielk1977a96a7102006-01-16 12:46:41 +0000299do_test shared-$av.4.1.1 {
danielk1977de0fe3e2006-01-06 06:33:12 +0000300 set sqlite_open_file_count
aswiftaebf4132008-11-21 00:10:35 +0000301 expr $sqlite_open_file_count-($extrafds_prelock*2)
danielk1977de0fe3e2006-01-06 06:33:12 +0000302} {2}
danielk1977a96a7102006-01-16 12:46:41 +0000303do_test shared-$av.4.1.2 {
danielk1977de0fe3e2006-01-06 06:33:12 +0000304 execsql {ATTACH 'test2.db' AS test2}
305 set sqlite_open_file_count
aswiftaebf4132008-11-21 00:10:35 +0000306 expr $sqlite_open_file_count-($extrafds_postlock*2)
danielk1977de0fe3e2006-01-06 06:33:12 +0000307} {2}
danielk1977a96a7102006-01-16 12:46:41 +0000308do_test shared-$av.4.1.3 {
danielk1977de0fe3e2006-01-06 06:33:12 +0000309 execsql {ATTACH 'test.db' AS test} db2
310 set sqlite_open_file_count
aswiftaebf4132008-11-21 00:10:35 +0000311 expr $sqlite_open_file_count-($extrafds_postlock*2)
danielk1977de0fe3e2006-01-06 06:33:12 +0000312} {2}
313
danielk1977c87d34d2006-01-06 13:00:28 +0000314# Sanity check: Create a table in ./test.db via handle db, and test that handle
315# db2 can "see" the new table immediately. A handle using a seperate pager
316# cache would have to reload the database schema before this were possible.
317#
danielk1977a96a7102006-01-16 12:46:41 +0000318do_test shared-$av.4.2.1 {
danielk1977de0fe3e2006-01-06 06:33:12 +0000319 execsql {
320 CREATE TABLE abc(a, b, c);
danielk1977c87d34d2006-01-06 13:00:28 +0000321 CREATE TABLE def(d, e, f);
danielk1977de0fe3e2006-01-06 06:33:12 +0000322 INSERT INTO abc VALUES('i', 'ii', 'iii');
danielk1977c87d34d2006-01-06 13:00:28 +0000323 INSERT INTO def VALUES('I', 'II', 'III');
danielk1977de0fe3e2006-01-06 06:33:12 +0000324 }
325} {}
danielk1977a96a7102006-01-16 12:46:41 +0000326do_test shared-$av.4.2.2 {
danielk1977de0fe3e2006-01-06 06:33:12 +0000327 execsql {
328 SELECT * FROM test.abc;
329 } db2
330} {i ii iii}
331
danielk1977c87d34d2006-01-06 13:00:28 +0000332# Open a read-transaction and read from table abc via handle 2. Check that
333# handle 1 can read table abc. Check that handle 1 cannot modify table abc
334# or the database schema. Then check that handle 1 can modify table def.
335#
danielk1977a96a7102006-01-16 12:46:41 +0000336do_test shared-$av.4.3.1 {
danielk1977c87d34d2006-01-06 13:00:28 +0000337 execsql {
338 BEGIN;
339 SELECT * FROM test.abc;
340 } db2
341} {i ii iii}
danielk1977a96a7102006-01-16 12:46:41 +0000342do_test shared-$av.4.3.2 {
danielk1977c87d34d2006-01-06 13:00:28 +0000343 catchsql {
344 INSERT INTO abc VALUES('iv', 'v', 'vi');
345 }
danielk1977c00da102006-01-07 13:21:04 +0000346} {1 {database table is locked: abc}}
danielk1977a96a7102006-01-16 12:46:41 +0000347do_test shared-$av.4.3.3 {
danielk1977c87d34d2006-01-06 13:00:28 +0000348 catchsql {
349 CREATE TABLE ghi(g, h, i);
350 }
danielk1977c00da102006-01-07 13:21:04 +0000351} {1 {database table is locked: sqlite_master}}
danielk1977a96a7102006-01-16 12:46:41 +0000352do_test shared-$av.4.3.3 {
danielk1977c87d34d2006-01-06 13:00:28 +0000353 catchsql {
354 INSERT INTO def VALUES('IV', 'V', 'VI');
355 }
356} {0 {}}
danielk1977a96a7102006-01-16 12:46:41 +0000357do_test shared-$av.4.3.4 {
danielk1977c87d34d2006-01-06 13:00:28 +0000358 # Cleanup: commit the transaction opened by db2.
359 execsql {
360 COMMIT
361 } db2
362} {}
363
364# Open a write-transaction using handle 1 and modify the database schema.
365# Then try to execute a compiled statement to read from the same
366# database via handle 2 (fails to get the lock on sqlite_master). Also
367# try to compile a read of the same database using handle 2 (also fails).
368# Finally, compile a read of the other database using handle 2. This
369# should also fail.
370#
danielk1977ff890792006-01-16 16:24:25 +0000371ifcapable compound {
372 do_test shared-$av.4.4.1.2 {
373 # Sanity check 1: Check that the schema is what we think it is when viewed
374 # via handle 1.
375 execsql {
376 CREATE TABLE test2.ghi(g, h, i);
377 SELECT 'test.db:'||name FROM sqlite_master
378 UNION ALL
379 SELECT 'test2.db:'||name FROM test2.sqlite_master;
380 }
381 } {test.db:abc test.db:def test2.db:ghi}
382 do_test shared-$av.4.4.1.2 {
383 # Sanity check 2: Check that the schema is what we think it is when viewed
384 # via handle 2.
385 execsql {
386 SELECT 'test2.db:'||name FROM sqlite_master
387 UNION ALL
388 SELECT 'test.db:'||name FROM test.sqlite_master;
389 } db2
390 } {test2.db:ghi test.db:abc test.db:def}
391}
danielk1977c87d34d2006-01-06 13:00:28 +0000392
danielk1977a96a7102006-01-16 12:46:41 +0000393do_test shared-$av.4.4.2 {
danielk1977c87d34d2006-01-06 13:00:28 +0000394 set ::DB2 [sqlite3_connection_pointer db2]
395 set sql {SELECT * FROM abc}
396 set ::STMT1 [sqlite3_prepare $::DB2 $sql -1 DUMMY]
397 execsql {
398 BEGIN;
399 CREATE TABLE jkl(j, k, l);
400 }
401 sqlite3_step $::STMT1
402} {SQLITE_ERROR}
danielk1977a96a7102006-01-16 12:46:41 +0000403do_test shared-$av.4.4.3 {
danielk1977c87d34d2006-01-06 13:00:28 +0000404 sqlite3_finalize $::STMT1
405} {SQLITE_LOCKED}
danielk1977a96a7102006-01-16 12:46:41 +0000406do_test shared-$av.4.4.4 {
danielk1977c87d34d2006-01-06 13:00:28 +0000407 set rc [catch {
408 set ::STMT1 [sqlite3_prepare $::DB2 $sql -1 DUMMY]
409 } msg]
410 list $rc $msg
411} {1 {(6) database schema is locked: test}}
danielk1977a96a7102006-01-16 12:46:41 +0000412do_test shared-$av.4.4.5 {
danielk1977c87d34d2006-01-06 13:00:28 +0000413 set rc [catch {
414 set ::STMT1 [sqlite3_prepare $::DB2 "SELECT * FROM ghi" -1 DUMMY]
415 } msg]
416 list $rc $msg
417} {1 {(6) database schema is locked: test}}
418
danielk1977aaf22682006-01-06 15:03:48 +0000419
danielk1977de0fe3e2006-01-06 06:33:12 +0000420catch {db2 close}
421catch {db close}
422
danielk1977aaf22682006-01-06 15:03:48 +0000423#--------------------------------------------------------------------------
424# Tests shared-5.*
425#
426foreach db [list test.db test1.db test2.db test3.db] {
mistachkinfda06be2011-08-02 00:57:34 +0000427 forcedelete $db ${db}-journal
danielk1977aaf22682006-01-06 15:03:48 +0000428}
danielk1977a96a7102006-01-16 12:46:41 +0000429do_test shared-$av.5.1.1 {
danielk1977aaf22682006-01-06 15:03:48 +0000430 sqlite3 db1 test.db
431 sqlite3 db2 test.db
432 execsql {
433 ATTACH 'test1.db' AS test1;
434 ATTACH 'test2.db' AS test2;
435 ATTACH 'test3.db' AS test3;
436 } db1
437 execsql {
438 ATTACH 'test3.db' AS test3;
439 ATTACH 'test2.db' AS test2;
440 ATTACH 'test1.db' AS test1;
441 } db2
442} {}
danielk1977a96a7102006-01-16 12:46:41 +0000443do_test shared-$av.5.1.2 {
danielk1977aaf22682006-01-06 15:03:48 +0000444 execsql {
445 CREATE TABLE test1.t1(a, b);
446 CREATE INDEX test1.i1 ON t1(a, b);
danielk1977aaf22682006-01-06 15:03:48 +0000447 } db1
danielk19773bdca9c2006-01-17 09:35:01 +0000448} {}
449ifcapable view {
450 do_test shared-$av.5.1.3 {
451 execsql {
452 CREATE VIEW test1.v1 AS SELECT * FROM t1;
453 } db1
454 } {}
455}
456ifcapable trigger {
457 do_test shared-$av.5.1.4 {
458 execsql {
459 CREATE TRIGGER test1.trig1 AFTER INSERT ON t1 BEGIN
460 INSERT INTO t1 VALUES(new.a, new.b);
461 END;
462 } db1
463 } {}
464}
465do_test shared-$av.5.1.5 {
danielk1977aaf22682006-01-06 15:03:48 +0000466 execsql {
467 DROP INDEX i1;
danielk19773bdca9c2006-01-17 09:35:01 +0000468 } db2
469} {}
470ifcapable view {
471 do_test shared-$av.5.1.6 {
472 execsql {
473 DROP VIEW v1;
474 } db2
475 } {}
476}
477ifcapable trigger {
478 do_test shared-$av.5.1.7 {
479 execsql {
480 DROP TRIGGER trig1;
481 } db2
482 } {}
483}
484do_test shared-$av.5.1.8 {
485 execsql {
danielk1977aaf22682006-01-06 15:03:48 +0000486 DROP TABLE t1;
487 } db2
488} {}
danielk1977ff890792006-01-16 16:24:25 +0000489ifcapable compound {
danielk19773bdca9c2006-01-17 09:35:01 +0000490 do_test shared-$av.5.1.9 {
danielk1977ff890792006-01-16 16:24:25 +0000491 execsql {
492 SELECT * FROM sqlite_master UNION ALL SELECT * FROM test1.sqlite_master
493 } db1
494 } {}
495}
danielk1977aaf22682006-01-06 15:03:48 +0000496
danielk1977c00da102006-01-07 13:21:04 +0000497#--------------------------------------------------------------------------
498# Tests shared-6.* test that a query obtains all the read-locks it needs
499# before starting execution of the query. This means that there is no chance
500# some rows of data will be returned before a lock fails and SQLITE_LOCK
501# is returned.
502#
danielk1977a96a7102006-01-16 12:46:41 +0000503do_test shared-$av.6.1.1 {
danielk1977c00da102006-01-07 13:21:04 +0000504 execsql {
505 CREATE TABLE t1(a, b);
506 CREATE TABLE t2(a, b);
507 INSERT INTO t1 VALUES(1, 2);
508 INSERT INTO t2 VALUES(3, 4);
509 } db1
danielk1977ff890792006-01-16 16:24:25 +0000510} {}
511ifcapable compound {
512 do_test shared-$av.6.1.2 {
513 execsql {
514 SELECT * FROM t1 UNION ALL SELECT * FROM t2;
515 } db2
516 } {1 2 3 4}
517}
518do_test shared-$av.6.1.3 {
danielk1977c00da102006-01-07 13:21:04 +0000519 # Establish a write lock on table t2 via connection db2. Then make a
520 # UNION all query using connection db1 that first accesses t1, followed
521 # by t2. If the locks are grabbed at the start of the statement (as
522 # they should be), no rows are returned. If (as was previously the case)
523 # they are grabbed as the tables are accessed, the t1 rows will be
524 # returned before the query fails.
525 #
526 execsql {
527 BEGIN;
528 INSERT INTO t2 VALUES(5, 6);
529 } db2
530 set ret [list]
531 catch {
532 db1 eval {SELECT * FROM t1 UNION ALL SELECT * FROM t2} {
533 lappend ret $a $b
534 }
535 }
536 set ret
537} {}
danielk1977ff890792006-01-16 16:24:25 +0000538do_test shared-$av.6.1.4 {
danielk1977c00da102006-01-07 13:21:04 +0000539 execsql {
540 COMMIT;
541 BEGIN;
542 INSERT INTO t1 VALUES(7, 8);
543 } db2
544 set ret [list]
545 catch {
546 db1 eval {
547 SELECT (CASE WHEN a>4 THEN (SELECT a FROM t1) ELSE 0 END) AS d FROM t2;
548 } {
549 lappend ret $d
550 }
551 }
552 set ret
553} {}
554
danielk1977aaf22682006-01-06 15:03:48 +0000555catch {db1 close}
556catch {db2 close}
danielk1977e501b892006-01-09 06:29:47 +0000557foreach f [list test.db test2.db] {
mistachkinfda06be2011-08-02 00:57:34 +0000558 forcedelete $f ${f}-journal
danielk1977e501b892006-01-09 06:29:47 +0000559}
560
561#--------------------------------------------------------------------------
562# Tests shared-7.* test auto-vacuum does not invalidate cursors from
563# other shared-cache users when it reorganizes the database on
564# COMMIT.
565#
danielk1977a96a7102006-01-16 12:46:41 +0000566do_test shared-$av.7.1 {
danielk197714db2662006-01-09 16:12:04 +0000567 # This test case sets up a test database in auto-vacuum mode consisting
568 # of two tables, t1 and t2. Both have a single index. Table t1 is
569 # populated first (so consists of pages toward the start of the db file),
570 # t2 second (pages toward the end of the file).
danielk1977e501b892006-01-09 06:29:47 +0000571 sqlite3 db test.db
572 sqlite3 db2 test.db
573 execsql {
danielk1977e501b892006-01-09 06:29:47 +0000574 BEGIN;
575 CREATE TABLE t1(a PRIMARY KEY, b);
576 CREATE TABLE t2(a PRIMARY KEY, b);
577 }
drh7a91dd82006-01-11 01:08:34 +0000578 set ::contents {}
danielk1977e501b892006-01-09 06:29:47 +0000579 for {set i 0} {$i < 100} {incr i} {
580 set a [string repeat "$i " 20]
581 set b [string repeat "$i " 20]
582 db eval {
danielk19773bdca9c2006-01-17 09:35:01 +0000583 INSERT INTO t1 VALUES(:a, :b);
danielk1977e501b892006-01-09 06:29:47 +0000584 }
585 lappend ::contents [list [expr $i+1] $a $b]
586 }
587 execsql {
588 INSERT INTO t2 SELECT * FROM t1;
589 COMMIT;
590 }
danielk1977bab45c62006-01-16 15:14:27 +0000591} {}
danielk1977a96a7102006-01-16 12:46:41 +0000592do_test shared-$av.7.2 {
danielk197714db2662006-01-09 16:12:04 +0000593 # This test case deletes the contents of table t1 (the one at the start of
drh85b623f2007-12-13 21:54:09 +0000594 # the file) while many cursors are open on table t2 and its index. All of
danielk197714db2662006-01-09 16:12:04 +0000595 # the non-root pages will be moved from the end to the start of the file
596 # when the DELETE is committed - this test verifies that moving the pages
597 # does not disturb the open cursors.
598 #
599
danielk1977e501b892006-01-09 06:29:47 +0000600 proc lockrow {db tbl oids body} {
601 set ret [list]
602 db eval "SELECT oid AS i, a, b FROM $tbl ORDER BY a" {
603 if {$i==[lindex $oids 0]} {
604 set noids [lrange $oids 1 end]
605 if {[llength $noids]==0} {
606 set subret [eval $body]
607 } else {
608 set subret [lockrow $db $tbl $noids $body]
609 }
610 }
611 lappend ret [list $i $a $b]
612 }
613 return [linsert $subret 0 $ret]
614 }
615 proc locktblrows {db tbl body} {
616 set oids [db eval "SELECT oid FROM $tbl"]
617 lockrow $db $tbl $oids $body
618 }
619
620 set scans [locktblrows db t2 {
621 execsql {
622 DELETE FROM t1;
623 } db2
624 }]
625 set error 0
danielk197714db2662006-01-09 16:12:04 +0000626
627 # Test that each SELECT query returned the expected contents of t2.
danielk1977e501b892006-01-09 06:29:47 +0000628 foreach s $scans {
629 if {[lsort -integer -index 0 $s]!=$::contents} {
630 set error 1
631 }
632 }
633 set error
634} {0}
635
636catch {db close}
637catch {db2 close}
drh7a91dd82006-01-11 01:08:34 +0000638unset -nocomplain contents
danielk1977aaf22682006-01-06 15:03:48 +0000639
danielk197714db2662006-01-09 16:12:04 +0000640#--------------------------------------------------------------------------
641# The following tests try to trick the shared-cache code into assuming
642# the wrong encoding for a database.
643#
mistachkinfda06be2011-08-02 00:57:34 +0000644forcedelete test.db test.db-journal
danielk19773bdca9c2006-01-17 09:35:01 +0000645ifcapable utf16 {
646 do_test shared-$av.8.1.1 {
647 sqlite3 db test.db
648 execsql {
649 PRAGMA encoding = 'UTF-16';
650 SELECT * FROM sqlite_master;
651 }
652 } {}
653 do_test shared-$av.8.1.2 {
654 string range [execsql {PRAGMA encoding;}] 0 end-2
655 } {UTF-16}
danielk1977f51bf482008-04-28 16:19:35 +0000656
danielk19773bdca9c2006-01-17 09:35:01 +0000657 do_test shared-$av.8.1.3 {
658 sqlite3 db2 test.db
659 execsql {
660 PRAGMA encoding = 'UTF-8';
661 CREATE TABLE abc(a, b, c);
662 } db2
663 } {}
664 do_test shared-$av.8.1.4 {
665 execsql {
666 SELECT * FROM sqlite_master;
667 }
668 } "table abc abc [expr $AUTOVACUUM?3:2] {CREATE TABLE abc(a, b, c)}"
669 do_test shared-$av.8.1.5 {
670 db2 close
671 execsql {
672 PRAGMA encoding;
673 }
674 } {UTF-8}
danielk1977f51bf482008-04-28 16:19:35 +0000675
mistachkinfda06be2011-08-02 00:57:34 +0000676 forcedelete test2.db test2.db-journal
danielk19773bdca9c2006-01-17 09:35:01 +0000677 do_test shared-$av.8.2.1 {
678 execsql {
679 ATTACH 'test2.db' AS aux;
680 SELECT * FROM aux.sqlite_master;
681 }
682 } {}
683 do_test shared-$av.8.2.2 {
684 sqlite3 db2 test2.db
685 execsql {
686 PRAGMA encoding = 'UTF-16';
687 CREATE TABLE def(d, e, f);
688 } db2
689 string range [execsql {PRAGMA encoding;} db2] 0 end-2
690 } {UTF-16}
danielk1977d42f8fd2007-08-03 07:33:08 +0000691
danielk1977f51bf482008-04-28 16:19:35 +0000692 catch {db close}
693 catch {db2 close}
mistachkinfda06be2011-08-02 00:57:34 +0000694 forcedelete test.db test2.db
danielk1977f51bf482008-04-28 16:19:35 +0000695
696 do_test shared-$av.8.3.2 {
697 sqlite3 db test.db
698 execsql { CREATE TABLE def(d, e, f) }
699 execsql { PRAGMA encoding }
700 } {UTF-8}
701 do_test shared-$av.8.3.3 {
702 set zDb16 "[encoding convertto unicode test.db]\x00\x00"
703 set db16 [sqlite3_open16 $zDb16 {}]
704
705 set stmt [sqlite3_prepare $db16 "SELECT sql FROM sqlite_master" -1 DUMMY]
706 sqlite3_step $stmt
707 set sql [sqlite3_column_text $stmt 0]
708 sqlite3_finalize $stmt
709 set sql
710 } {CREATE TABLE def(d, e, f)}
711 do_test shared-$av.8.3.4 {
712 set stmt [sqlite3_prepare $db16 "PRAGMA encoding" -1 DUMMY]
713 sqlite3_step $stmt
714 set enc [sqlite3_column_text $stmt 0]
715 sqlite3_finalize $stmt
716 set enc
717 } {UTF-8}
718
719 sqlite3_close $db16
720
danielk1977d42f8fd2007-08-03 07:33:08 +0000721# Bug #2547 is causing this to fail.
722if 0 {
danielk19773bdca9c2006-01-17 09:35:01 +0000723 do_test shared-$av.8.2.3 {
724 catchsql {
725 SELECT * FROM aux.sqlite_master;
726 }
727 } {1 {attached databases must use the same text encoding as main database}}
728}
danielk1977d42f8fd2007-08-03 07:33:08 +0000729}
danielk197714db2662006-01-09 16:12:04 +0000730
731catch {db close}
732catch {db2 close}
mistachkinfda06be2011-08-02 00:57:34 +0000733forcedelete test.db test2.db
danielk1977eecfb3e2006-01-10 12:31:39 +0000734
danielk1977eecfb3e2006-01-10 12:31:39 +0000735#---------------------------------------------------------------------------
736# The following tests - shared-9.* - test interactions between TEMP triggers
737# and shared-schemas.
738#
739ifcapable trigger&&tempdb {
740
danielk1977a96a7102006-01-16 12:46:41 +0000741do_test shared-$av.9.1 {
danielk1977eecfb3e2006-01-10 12:31:39 +0000742 sqlite3 db test.db
743 sqlite3 db2 test.db
744 execsql {
745 CREATE TABLE abc(a, b, c);
746 CREATE TABLE abc_mirror(a, b, c);
747 CREATE TEMP TRIGGER BEFORE INSERT ON abc BEGIN
748 INSERT INTO abc_mirror(a, b, c) VALUES(new.a, new.b, new.c);
749 END;
750 INSERT INTO abc VALUES(1, 2, 3);
751 SELECT * FROM abc_mirror;
752 }
753} {1 2 3}
danielk1977a96a7102006-01-16 12:46:41 +0000754do_test shared-$av.9.2 {
danielk1977eecfb3e2006-01-10 12:31:39 +0000755 execsql {
756 INSERT INTO abc VALUES(4, 5, 6);
757 SELECT * FROM abc_mirror;
758 } db2
759} {1 2 3}
danielk1977a96a7102006-01-16 12:46:41 +0000760do_test shared-$av.9.3 {
danielk1977eecfb3e2006-01-10 12:31:39 +0000761 db close
762 db2 close
763} {}
764
765} ; # End shared-9.*
danielk197714db2662006-01-09 16:12:04 +0000766
danielk1977b597f742006-01-15 11:39:18 +0000767#---------------------------------------------------------------------------
768# The following tests - shared-10.* - test that the library behaves
769# correctly when a connection to a shared-cache is closed.
770#
danielk1977a96a7102006-01-16 12:46:41 +0000771do_test shared-$av.10.1 {
danielk1977b597f742006-01-15 11:39:18 +0000772 # Create a small sample database with two connections to it (db and db2).
mistachkinfda06be2011-08-02 00:57:34 +0000773 forcedelete test.db
danielk1977b597f742006-01-15 11:39:18 +0000774 sqlite3 db test.db
775 sqlite3 db2 test.db
776 execsql {
777 CREATE TABLE ab(a PRIMARY KEY, b);
778 CREATE TABLE de(d PRIMARY KEY, e);
779 INSERT INTO ab VALUES('Chiang Mai', 100000);
780 INSERT INTO ab VALUES('Bangkok', 8000000);
781 INSERT INTO de VALUES('Ubon', 120000);
782 INSERT INTO de VALUES('Khon Kaen', 200000);
783 }
784} {}
danielk1977a96a7102006-01-16 12:46:41 +0000785do_test shared-$av.10.2 {
danielk1977b597f742006-01-15 11:39:18 +0000786 # Open a read-transaction with the first connection, a write-transaction
787 # with the second.
788 execsql {
789 BEGIN;
790 SELECT * FROM ab;
791 }
792 execsql {
793 BEGIN;
794 INSERT INTO de VALUES('Pataya', 30000);
795 } db2
796} {}
danielk1977a96a7102006-01-16 12:46:41 +0000797do_test shared-$av.10.3 {
danielk1977b597f742006-01-15 11:39:18 +0000798 # An external connection should be able to read the database, but not
799 # prepare a write operation.
drhc693e9e2006-01-23 21:38:03 +0000800 if {$::tcl_platform(platform)=="unix"} {
drh7f42dcd2020-11-16 18:45:21 +0000801 sqlite3 db3 "file:test.db?cache=private" -uri 1
drhc693e9e2006-01-23 21:38:03 +0000802 } else {
803 sqlite3 db3 TEST.DB
804 }
danielk1977b597f742006-01-15 11:39:18 +0000805 execsql {
806 SELECT * FROM ab;
807 } db3
808 catchsql {
809 BEGIN;
810 INSERT INTO de VALUES('Pataya', 30000);
811 } db3
812} {1 {database is locked}}
danielk1977a96a7102006-01-16 12:46:41 +0000813do_test shared-$av.10.4 {
danielk1977b597f742006-01-15 11:39:18 +0000814 # Close the connection with the write-transaction open
815 db2 close
816} {}
danielk1977a96a7102006-01-16 12:46:41 +0000817do_test shared-$av.10.5 {
danielk1977b597f742006-01-15 11:39:18 +0000818 # Test that the db2 transaction has been automatically rolled back.
819 # If it has not the ('Pataya', 30000) entry will still be in the table.
820 execsql {
821 SELECT * FROM de;
822 }
823} {Ubon 120000 {Khon Kaen} 200000}
danielk1977a96a7102006-01-16 12:46:41 +0000824do_test shared-$av.10.5 {
danielk1977b597f742006-01-15 11:39:18 +0000825 # Closing db2 should have dropped the shared-cache back to a read-lock.
826 # So db3 should be able to prepare a write...
827 catchsql {INSERT INTO de VALUES('Pataya', 30000);} db3
828} {0 {}}
danielk1977a96a7102006-01-16 12:46:41 +0000829do_test shared-$av.10.6 {
danielk1977b597f742006-01-15 11:39:18 +0000830 # ... but not commit it.
831 catchsql {COMMIT} db3
832} {1 {database is locked}}
danielk1977a96a7102006-01-16 12:46:41 +0000833do_test shared-$av.10.7 {
danielk1977b597f742006-01-15 11:39:18 +0000834 # Commit the (read-only) db transaction. Check via db3 to make sure the
835 # contents of table "de" are still as they should be.
836 execsql {
837 COMMIT;
838 }
839 execsql {
840 SELECT * FROM de;
841 } db3
842} {Ubon 120000 {Khon Kaen} 200000 Pataya 30000}
danielk1977a96a7102006-01-16 12:46:41 +0000843do_test shared-$av.10.9 {
danielk1977b597f742006-01-15 11:39:18 +0000844 # Commit the external transaction.
845 catchsql {COMMIT} db3
846} {0 {}}
danielk1977a96a7102006-01-16 12:46:41 +0000847integrity_check shared-$av.10.10
848do_test shared-$av.10.11 {
danielk1977b597f742006-01-15 11:39:18 +0000849 db close
850 db3 close
851} {}
852
danielk19774b202ae2006-01-23 05:50:58 +0000853do_test shared-$av.11.1 {
mistachkinfda06be2011-08-02 00:57:34 +0000854 forcedelete test.db
danielk19774b202ae2006-01-23 05:50:58 +0000855 sqlite3 db test.db
856 sqlite3 db2 test.db
857 execsql {
858 CREATE TABLE abc(a, b, c);
859 CREATE TABLE abc2(a, b, c);
860 BEGIN;
861 INSERT INTO abc VALUES(1, 2, 3);
862 }
863} {}
864do_test shared-$av.11.2 {
865 catchsql {BEGIN;} db2
866 catchsql {SELECT * FROM abc;} db2
867} {1 {database table is locked: abc}}
868do_test shared-$av.11.3 {
869 catchsql {BEGIN} db2
870} {1 {cannot start a transaction within a transaction}}
871do_test shared-$av.11.4 {
872 catchsql {SELECT * FROM abc2;} db2
873} {0 {}}
874do_test shared-$av.11.5 {
875 catchsql {INSERT INTO abc2 VALUES(1, 2, 3);} db2
danielk1977404ca072009-03-16 13:19:36 +0000876} {1 {database table is locked}}
danielk19774b202ae2006-01-23 05:50:58 +0000877do_test shared-$av.11.6 {
878 catchsql {SELECT * FROM abc2}
879} {0 {}}
880do_test shared-$av.11.6 {
881 execsql {
882 ROLLBACK;
883 PRAGMA read_uncommitted = 1;
884 } db2
885} {}
886do_test shared-$av.11.7 {
887 execsql {
888 INSERT INTO abc2 VALUES(4, 5, 6);
889 INSERT INTO abc2 VALUES(7, 8, 9);
890 }
891} {}
892do_test shared-$av.11.8 {
893 set res [list]
danielk19774b202ae2006-01-23 05:50:58 +0000894 db2 eval {
895 SELECT abc.a as I, abc2.a as II FROM abc, abc2;
896 } {
897 execsql {
898 DELETE FROM abc WHERE 1;
899 }
900 lappend res $I $II
901 }
902 set res
903} {1 4 {} 7}
drh16a9b832007-05-05 18:39:25 +0000904if {[llength [info command sqlite3_shared_cache_report]]==1} {
mistachkin4a41f342012-03-06 03:00:49 +0000905 ifcapable curdir {
906 do_test shared-$av.11.9 {
907 string tolower [sqlite3_shared_cache_report]
908 } [string tolower [list [file nativename [file normalize test.db]] 2]]
909 }
drh7c4ac0c2007-04-05 11:25:58 +0000910}
danielk19774b202ae2006-01-23 05:50:58 +0000911
912do_test shared-$av.11.11 {
913 db close
914 db2 close
915} {}
916
danielk1977843e65f2007-09-01 16:16:15 +0000917# This tests that if it is impossible to free any pages, SQLite will
918# exceed the limit set by PRAGMA cache_size.
mistachkinfda06be2011-08-02 00:57:34 +0000919forcedelete test.db test.db-journal
danielk19774152e672007-09-12 17:01:45 +0000920sqlite3 db test.db
921ifcapable pager_pragmas {
922 do_test shared-$av.12.1 {
923 execsql {
924 PRAGMA cache_size = 10;
925 PRAGMA cache_size;
926 }
927 } {10}
928}
danielk1977843e65f2007-09-01 16:16:15 +0000929do_test shared-$av.12.2 {
930 set ::db_handles [list]
931 for {set i 1} {$i < 15} {incr i} {
932 lappend ::db_handles db$i
933 sqlite3 db$i test.db
934 execsql "CREATE TABLE db${i}(a, b, c)" db$i
935 execsql "INSERT INTO db${i} VALUES(1, 2, 3)"
936 }
937} {}
938proc nested_select {handles} {
939 [lindex $handles 0] eval "SELECT * FROM [lindex $handles 0]" {
940 lappend ::res $a $b $c
941 if {[llength $handles]>1} {
942 nested_select [lrange $handles 1 end]
943 }
944 }
945}
946do_test shared-$av.12.3 {
947 set ::res [list]
948 nested_select $::db_handles
949 set ::res
950} [string range [string repeat "1 2 3 " [llength $::db_handles]] 0 end-1]
951
952do_test shared-$av.12.X {
953 db close
954 foreach h $::db_handles {
955 $h close
956 }
957} {}
958
danielk1977983e2302008-07-08 07:35:51 +0000959# Internally, locks are acquired on shared B-Tree structures in the order
960# that the structures appear in the virtual memory address space. This
961# test case attempts to cause the order of the structures in memory
962# to be different from the order in which they are attached to a given
963# database handle. This covers an extra line or two.
964#
965do_test shared-$av.13.1 {
mistachkinfda06be2011-08-02 00:57:34 +0000966 forcedelete test2.db test3.db test4.db test5.db
danielk1977983e2302008-07-08 07:35:51 +0000967 sqlite3 db :memory:
968 execsql {
969 ATTACH 'test2.db' AS aux2;
970 ATTACH 'test3.db' AS aux3;
971 ATTACH 'test4.db' AS aux4;
972 ATTACH 'test5.db' AS aux5;
973 DETACH aux2;
974 DETACH aux3;
975 DETACH aux4;
976 ATTACH 'test2.db' AS aux2;
977 ATTACH 'test3.db' AS aux3;
978 ATTACH 'test4.db' AS aux4;
979 }
980} {}
981do_test shared-$av.13.2 {
982 execsql {
983 CREATE TABLE t1(a, b, c);
984 CREATE TABLE aux2.t2(a, b, c);
985 CREATE TABLE aux3.t3(a, b, c);
986 CREATE TABLE aux4.t4(a, b, c);
987 CREATE TABLE aux5.t5(a, b, c);
988 SELECT count(*) FROM
989 aux2.sqlite_master,
990 aux3.sqlite_master,
991 aux4.sqlite_master,
992 aux5.sqlite_master
993 }
994} {1}
995do_test shared-$av.13.3 {
996 db close
997} {}
998
999# Test that nothing horrible happens if a connection to a shared B-Tree
1000# structure is closed while some other connection has an open cursor.
1001#
1002do_test shared-$av.14.1 {
1003 sqlite3 db test.db
1004 sqlite3 db2 test.db
1005 execsql {SELECT name FROM sqlite_master}
1006} {db1 db2 db3 db4 db5 db6 db7 db8 db9 db10 db11 db12 db13 db14}
1007do_test shared-$av.14.2 {
1008 set res [list]
1009 db eval {SELECT name FROM sqlite_master} {
1010 if {$name eq "db7"} {
1011 db2 close
1012 }
1013 lappend res $name
1014 }
1015 set res
1016} {db1 db2 db3 db4 db5 db6 db7 db8 db9 db10 db11 db12 db13 db14}
1017do_test shared-$av.14.3 {
1018 db close
1019} {}
1020
dan2949e4e2010-07-23 15:55:31 +00001021# Populate a database schema using connection [db]. Then drop it using
1022# [db2]. This is to try to find any points where shared-schema elements
1023# are allocated using the lookaside buffer of [db].
1024#
1025# Mutexes are enabled for this test as that activates a couple of useful
1026# assert() statements in the C code.
1027#
1028do_test shared-$av-15.1 {
mistachkinfda06be2011-08-02 00:57:34 +00001029 forcedelete test.db
dan2949e4e2010-07-23 15:55:31 +00001030 sqlite3 db test.db -fullmutex 1
1031 sqlite3 db2 test.db -fullmutex 1
1032 execsql {
1033 CREATE TABLE t1(a, b, c);
1034 CREATE INDEX i1 ON t1(a, b);
1035 CREATE VIEW v1 AS SELECT * FROM t1;
1036 CREATE VIEW v2 AS SELECT * FROM t1, v1
1037 WHERE t1.c=v1.c GROUP BY t1.a ORDER BY v1.b;
1038 CREATE TRIGGER tr1 AFTER INSERT ON t1
1039 WHEN new.a!=1
1040 BEGIN
1041 DELETE FROM t1 WHERE a=5;
1042 INSERT INTO t1 VALUES(1, 2, 3);
1043 UPDATE t1 SET c=c+1;
1044 END;
1045
1046 INSERT INTO t1 VALUES(5, 6, 7);
1047 INSERT INTO t1 VALUES(8, 9, 10);
1048 INSERT INTO t1 VALUES(11, 12, 13);
1049 ANALYZE;
1050 SELECT * FROM t1;
1051 }
1052} {1 2 6 8 9 12 1 2 5 11 12 14 1 2 4}
1053do_test shared-$av-15.2 {
1054 execsql { DROP TABLE t1 } db2
1055} {}
1056db close
1057db2 close
1058
drh4ab9d252012-05-26 20:08:49 +00001059# Shared cache on a :memory: database. This only works for URI filenames.
drhafc8b7f2012-05-26 18:06:38 +00001060#
1061do_test shared-$av-16.1 {
drh4ab9d252012-05-26 20:08:49 +00001062 sqlite3 db1 file::memory: -uri 1
1063 sqlite3 db2 file::memory: -uri 1
drhafc8b7f2012-05-26 18:06:38 +00001064 db1 eval {
1065 CREATE TABLE t1(x); INSERT INTO t1 VALUES(1),(2),(3);
1066 }
1067 db2 eval {
1068 SELECT x FROM t1 ORDER BY x;
1069 }
1070} {1 2 3}
1071do_test shared-$av-16.2 {
1072 db2 eval {
1073 INSERT INTO t1 VALUES(99);
1074 DELETE FROM t1 WHERE x=2;
1075 }
1076 db1 eval {
1077 SELECT x FROM t1 ORDER BY x;
1078 }
1079} {1 3 99}
1080
drh4ab9d252012-05-26 20:08:49 +00001081# Verify that there is no cache sharing ordinary (non-URI) filenames are
1082# used.
1083#
1084do_test shared-$av-16.3 {
1085 db1 close
1086 db2 close
1087 sqlite3 db1 :memory:
1088 sqlite3 db2 :memory:
1089 db1 eval {
1090 CREATE TABLE t1(x); INSERT INTO t1 VALUES(4),(5),(6);
1091 }
1092 catchsql {
1093 SELECT * FROM t1;
1094 } db2
1095} {1 {no such table: t1}}
drh9c67b2a2012-05-28 13:58:00 +00001096
1097# Shared cache on named memory databases.
1098#
1099do_test shared-$av-16.4 {
1100 db1 close
1101 db2 close
1102 forcedelete test.db test.db-wal test.db-journal
1103 sqlite3 db1 file:test.db?mode=memory -uri 1
1104 sqlite3 db2 file:test.db?mode=memory -uri 1
1105 db1 eval {
1106 CREATE TABLE t1(x); INSERT INTO t1 VALUES(1),(2),(3);
1107 }
1108 db2 eval {
1109 SELECT x FROM t1 ORDER BY x;
1110 }
1111} {1 2 3}
1112do_test shared-$av-16.5 {
1113 db2 eval {
1114 INSERT INTO t1 VALUES(99);
1115 DELETE FROM t1 WHERE x=2;
1116 }
1117 db1 eval {
1118 SELECT x FROM t1 ORDER BY x;
1119 }
1120} {1 3 99}
1121do_test shared-$av-16.6 {
1122 file exists test.db
1123} {0} ;# Verify that the database is in-memory
1124
1125# Shared cache on named memory databases with different names.
1126#
1127do_test shared-$av-16.7 {
1128 db1 close
1129 db2 close
1130 forcedelete test1.db test2.db
1131 sqlite3 db1 file:test1.db?mode=memory -uri 1
1132 sqlite3 db2 file:test2.db?mode=memory -uri 1
1133 db1 eval {
1134 CREATE TABLE t1(x); INSERT INTO t1 VALUES(1),(2),(3);
1135 }
1136 catchsql {
1137 SELECT x FROM t1 ORDER BY x;
1138 } db2
1139} {1 {no such table: t1}}
1140do_test shared-$av-16.8 {
1141 file exists test1.db
1142} {0} ;# Verify that the database is in-memory
1143
dan0b8dcfa2012-06-07 17:16:04 +00001144# Shared cache on named memory databases attached to readonly connections.
1145#
drhbafad062018-10-12 15:01:56 +00001146if {![sqlite3 -has-codec]} {
1147 do_test shared-$av-16.8.1 {
1148 db1 close
1149 db2 close
1150
1151 sqlite3 db test1.db
1152 db eval {
1153 CREATE TABLE yy(a, b);
1154 INSERT INTO yy VALUES(77, 88);
1155 }
1156 db close
1157
1158 sqlite3 db1 test1.db -uri 1 -readonly 1
1159 sqlite3 db2 test2.db -uri 1
1160
1161 db1 eval {
1162 ATTACH 'file:mem?mode=memory&cache=shared' AS shared;
1163 CREATE TABLE shared.xx(a, b);
1164 INSERT INTO xx VALUES(55, 66);
1165 }
1166 db2 eval {
1167 ATTACH 'file:mem?mode=memory&cache=shared' AS shared;
1168 SELECT * FROM xx;
1169 }
1170 } {55 66}
1171
1172 do_test shared-$av-16.8.2 { db1 eval { SELECT * FROM yy } } {77 88}
1173 do_test shared-$av-16.8.3 {
1174 list [catch {db1 eval { INSERT INTO yy VALUES(1, 2) }} msg] $msg
1175 } {1 {attempt to write a readonly database}}
1176
dan0b8dcfa2012-06-07 17:16:04 +00001177 db1 close
1178 db2 close
drhbafad062018-10-12 15:01:56 +00001179}
drh4ab9d252012-05-26 20:08:49 +00001180
drhafc8b7f2012-05-26 18:06:38 +00001181} ;# end of autovacuum on/off loop
danielk1977a96a7102006-01-16 12:46:41 +00001182
danielk1977aef0bf62005-12-30 16:28:01 +00001183sqlite3_enable_shared_cache $::enable_shared_cache
danielk1977a96a7102006-01-16 12:46:41 +00001184finish_test