blob: 3ba435da182999fba8293c63d826a038b9f04b70 [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#
drh85b623f2007-12-13 21:54:09 +000012# $Id: shared.test,v 1.29 2007/12/13 21:54:11 drh 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
30file delete -force test.db
31sqlite3 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
danielk1977191c3e72006-01-19 07:18:14 +000045# $av is currently 0 if this loop iteration is to test with auto-vacuum turned
46# off, and 1 if it is turned on. Increment it so that (1 -> no auto-vacuum)
47# and (2 -> auto-vacuum). The sole reason for this is so that it looks nicer
48# when we use this variable as part of test-case names.
49#
danielk1977a96a7102006-01-16 12:46:41 +000050incr av
51
danielk1977aef0bf62005-12-30 16:28:01 +000052# Test organization:
53#
54# shared-1.*: Simple test to verify basic sanity of table level locking when
55# two connections share a pager cache.
56# shared-2.*: Test that a read transaction can co-exist with a
57# write-transaction, including a simple test to ensure the
58# external locking protocol is still working.
danielk1977da184232006-01-05 11:34:32 +000059# shared-3.*: Simple test of read-uncommitted mode.
danielk1977de0fe3e2006-01-06 06:33:12 +000060# shared-4.*: Check that the schema is locked and unlocked correctly.
danielk1977aaf22682006-01-06 15:03:48 +000061# shared-5.*: Test that creating/dropping schema items works when databases
62# are attached in different orders to different handles.
danielk1977c00da102006-01-07 13:21:04 +000063# shared-6.*: Locking, UNION ALL queries and sub-queries.
danielk197714db2662006-01-09 16:12:04 +000064# shared-7.*: Autovacuum and shared-cache.
danielk1977ed429312006-01-19 08:43:31 +000065# shared-8.*: Tests related to the text encoding of shared-cache databases.
66# shared-9.*: TEMP triggers and shared-cache databases.
67# shared-10.*: Tests of sqlite3_close().
danielk19774b202ae2006-01-23 05:50:58 +000068# shared-11.*: Test transaction locking.
danielk1977de0fe3e2006-01-06 06:33:12 +000069#
danielk1977aef0bf62005-12-30 16:28:01 +000070
danielk1977a96a7102006-01-16 12:46:41 +000071do_test shared-$av.1.1 {
danielk1977aef0bf62005-12-30 16:28:01 +000072 # Open a second database on the file test.db. It should use the same pager
73 # cache and schema as the original connection. Verify that only 1 file is
74 # opened.
75 sqlite3 db2 test.db
danielk1977aef0bf62005-12-30 16:28:01 +000076 set ::sqlite_open_file_count
77} {1}
danielk1977a96a7102006-01-16 12:46:41 +000078do_test shared-$av.1.2 {
danielk1977aef0bf62005-12-30 16:28:01 +000079 # Add a table and a single row of data via the first connection.
80 # Ensure that the second connection can see them.
81 execsql {
82 CREATE TABLE abc(a, b, c);
83 INSERT INTO abc VALUES(1, 2, 3);
84 } db
85 execsql {
86 SELECT * FROM abc;
87 } db2
88} {1 2 3}
danielk1977a96a7102006-01-16 12:46:41 +000089do_test shared-$av.1.3 {
danielk1977aef0bf62005-12-30 16:28:01 +000090 # Have the first connection begin a transaction and obtain a read-lock
91 # on table abc. This should not prevent the second connection from
92 # querying abc.
93 execsql {
94 BEGIN;
95 SELECT * FROM abc;
96 }
97 execsql {
98 SELECT * FROM abc;
99 } db2
100} {1 2 3}
danielk1977a96a7102006-01-16 12:46:41 +0000101do_test shared-$av.1.4 {
danielk1977aef0bf62005-12-30 16:28:01 +0000102 # Try to insert a row into abc via connection 2. This should fail because
103 # of the read-lock connection 1 is holding on table abc (obtained in the
104 # previous test case).
105 catchsql {
106 INSERT INTO abc VALUES(4, 5, 6);
107 } db2
danielk1977c00da102006-01-07 13:21:04 +0000108} {1 {database table is locked: abc}}
danielk1977a96a7102006-01-16 12:46:41 +0000109do_test shared-$av.1.5 {
danielk1977da184232006-01-05 11:34:32 +0000110 # Using connection 2 (the one without the open transaction), try to create
111 # a new table. This should fail because of the open read transaction
112 # held by connection 1.
113 catchsql {
114 CREATE TABLE def(d, e, f);
115 } db2
danielk1977c00da102006-01-07 13:21:04 +0000116} {1 {database table is locked: sqlite_master}}
danielk1977a96a7102006-01-16 12:46:41 +0000117do_test shared-$av.1.6 {
danielk1977da184232006-01-05 11:34:32 +0000118 # Upgrade connection 1's transaction to a write transaction. Create
119 # a new table - def - and insert a row into it. Because the connection 1
120 # transaction modifies the schema, it should not be possible for
121 # connection 2 to access the database at all until the connection 1
122 # has finished the transaction.
danielk1977aef0bf62005-12-30 16:28:01 +0000123 execsql {
124 CREATE TABLE def(d, e, f);
danielk1977aef0bf62005-12-30 16:28:01 +0000125 INSERT INTO def VALUES('IV', 'V', 'VI');
126 }
127} {}
danielk1977a96a7102006-01-16 12:46:41 +0000128do_test shared-$av.1.7 {
danielk1977aef0bf62005-12-30 16:28:01 +0000129 # Read from the sqlite_master table with connection 1 (inside the
danielk1977da184232006-01-05 11:34:32 +0000130 # transaction). Then test that we can not do this with connection 2. This
131 # is because of the schema-modified lock established by connection 1
132 # in the previous test case.
danielk1977aef0bf62005-12-30 16:28:01 +0000133 execsql {
134 SELECT * FROM sqlite_master;
135 }
136 catchsql {
danielk1977da184232006-01-05 11:34:32 +0000137 SELECT * FROM sqlite_master;
danielk1977aef0bf62005-12-30 16:28:01 +0000138 } db2
danielk1977c87d34d2006-01-06 13:00:28 +0000139} {1 {database schema is locked: main}}
danielk1977a96a7102006-01-16 12:46:41 +0000140do_test shared-$av.1.8 {
danielk1977aef0bf62005-12-30 16:28:01 +0000141 # Commit the connection 1 transaction.
142 execsql {
143 COMMIT;
144 }
145} {}
146
danielk1977a96a7102006-01-16 12:46:41 +0000147do_test shared-$av.2.1 {
danielk1977aef0bf62005-12-30 16:28:01 +0000148 # Open connection db3 to the database. Use a different path to the same
149 # file so that db3 does *not* share the same pager cache as db and db2
150 # (there should be two open file handles).
drhc693e9e2006-01-23 21:38:03 +0000151 if {$::tcl_platform(platform)=="unix"} {
152 sqlite3 db3 ./test.db
153 } else {
154 sqlite3 db3 TEST.DB
155 }
danielk1977aef0bf62005-12-30 16:28:01 +0000156 set ::sqlite_open_file_count
157} {2}
danielk1977a96a7102006-01-16 12:46:41 +0000158do_test shared-$av.2.2 {
danielk1977aef0bf62005-12-30 16:28:01 +0000159 # Start read transactions on db and db2 (the shared pager cache). Ensure
160 # db3 cannot write to the database.
161 execsql {
162 BEGIN;
163 SELECT * FROM abc;
164 }
165 execsql {
166 BEGIN;
167 SELECT * FROM abc;
168 } db2
169 catchsql {
170 INSERT INTO abc VALUES(1, 2, 3);
171 } db2
danielk1977c00da102006-01-07 13:21:04 +0000172} {1 {database table is locked: abc}}
danielk1977a96a7102006-01-16 12:46:41 +0000173do_test shared-$av.2.3 {
danielk1977aef0bf62005-12-30 16:28:01 +0000174 # Turn db's transaction into a write-transaction. db3 should still be
175 # able to read from table def (but will not see the new row). Connection
176 # db2 should not be able to read def (because of the write-lock).
177
178# Todo: The failed "INSERT INTO abc ..." statement in the above test
179# has started a write-transaction on db2 (should this be so?). This
180# would prevent connection db from starting a write-transaction. So roll the
181# db2 transaction back and replace it with a new read transaction.
182 execsql {
183 ROLLBACK;
184 BEGIN;
185 SELECT * FROM abc;
186 } db2
187
188 execsql {
189 INSERT INTO def VALUES('VII', 'VIII', 'IX');
190 }
191 concat [
192 catchsql { SELECT * FROM def; } db3
193 ] [
194 catchsql { SELECT * FROM def; } db2
195 ]
danielk1977c00da102006-01-07 13:21:04 +0000196} {0 {IV V VI} 1 {database table is locked: def}}
danielk1977a96a7102006-01-16 12:46:41 +0000197do_test shared-$av.2.4 {
danielk1977aef0bf62005-12-30 16:28:01 +0000198 # Commit the open transaction on db. db2 still holds a read-transaction.
199 # This should prevent db3 from writing to the database, but not from
200 # reading.
201 execsql {
202 COMMIT;
203 }
204 concat [
205 catchsql { SELECT * FROM def; } db3
206 ] [
207 catchsql { INSERT INTO def VALUES('X', 'XI', 'XII'); } db3
208 ]
danielk1977da184232006-01-05 11:34:32 +0000209} {0 {IV V VI VII VIII IX} 1 {database is locked}}
danielk1977aef0bf62005-12-30 16:28:01 +0000210
danielk1977da184232006-01-05 11:34:32 +0000211catchsql COMMIT db2
212
danielk1977a96a7102006-01-16 12:46:41 +0000213do_test shared-$av.3.1.1 {
danielk1977da184232006-01-05 11:34:32 +0000214 # This test case starts a linear scan of table 'seq' using a
215 # read-uncommitted connection. In the middle of the scan, rows are added
216 # to the end of the seq table (ahead of the current cursor position).
217 # The uncommitted rows should be included in the results of the scan.
218 execsql "
danielk1977191c3e72006-01-19 07:18:14 +0000219 CREATE TABLE seq(i PRIMARY KEY, x);
danielk1977da184232006-01-05 11:34:32 +0000220 INSERT INTO seq VALUES(1, '[string repeat X 500]');
221 INSERT INTO seq VALUES(2, '[string repeat X 500]');
222 "
223 execsql {SELECT * FROM sqlite_master} db2
224 execsql {PRAGMA read_uncommitted = 1} db2
225
226 set ret [list]
danielk1977191c3e72006-01-19 07:18:14 +0000227 db2 eval {SELECT i FROM seq ORDER BY i} {
danielk1977da184232006-01-05 11:34:32 +0000228 if {$i < 4} {
danielk19771576cd92006-01-14 08:02:28 +0000229 set max [execsql {SELECT max(i) FROM seq}]
230 db eval {
danielk19773bdca9c2006-01-17 09:35:01 +0000231 INSERT INTO seq SELECT i + :max, x FROM seq;
danielk1977da184232006-01-05 11:34:32 +0000232 }
233 }
234 lappend ret $i
235 }
236 set ret
237} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16}
danielk1977a96a7102006-01-16 12:46:41 +0000238do_test shared-$av.3.1.2 {
danielk1977da184232006-01-05 11:34:32 +0000239 # Another linear scan through table seq using a read-uncommitted connection.
240 # This time, delete each row as it is read. Should not affect the results of
241 # the scan, but the table should be empty after the scan is concluded
242 # (test 3.1.3 verifies this).
243 set ret [list]
244 db2 eval {SELECT i FROM seq} {
danielk19773bdca9c2006-01-17 09:35:01 +0000245 db eval {DELETE FROM seq WHERE i = :i}
danielk1977da184232006-01-05 11:34:32 +0000246 lappend ret $i
247 }
248 set ret
249} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16}
danielk1977a96a7102006-01-16 12:46:41 +0000250do_test shared-$av.3.1.3 {
danielk1977da184232006-01-05 11:34:32 +0000251 execsql {
252 SELECT * FROM seq;
253 }
254} {}
danielk1977aef0bf62005-12-30 16:28:01 +0000255
256catch {db close}
257catch {db2 close}
258catch {db3 close}
259
danielk1977de0fe3e2006-01-06 06:33:12 +0000260#--------------------------------------------------------------------------
261# Tests shared-4.* test that the schema locking rules are applied
262# correctly. i.e.:
263#
264# 1. All transactions require a read-lock on the schemas of databases they
265# access.
266# 2. Transactions that modify a database schema require a write-lock on that
267# schema.
268# 3. It is not possible to compile a statement while another handle has a
269# write-lock on the schema.
270#
271
272# Open two database handles db and db2. Each has a single attach database
273# (as well as main):
274#
275# db.main -> ./test.db
276# db.test2 -> ./test2.db
277# db2.main -> ./test2.db
278# db2.test -> ./test.db
279#
280file delete -force test.db
281file delete -force test2.db
282file delete -force test2.db-journal
283sqlite3 db test.db
284sqlite3 db2 test2.db
danielk1977a96a7102006-01-16 12:46:41 +0000285do_test shared-$av.4.1.1 {
danielk1977de0fe3e2006-01-06 06:33:12 +0000286 set sqlite_open_file_count
287} {2}
danielk1977a96a7102006-01-16 12:46:41 +0000288do_test shared-$av.4.1.2 {
danielk1977de0fe3e2006-01-06 06:33:12 +0000289 execsql {ATTACH 'test2.db' AS test2}
290 set sqlite_open_file_count
291} {2}
danielk1977a96a7102006-01-16 12:46:41 +0000292do_test shared-$av.4.1.3 {
danielk1977de0fe3e2006-01-06 06:33:12 +0000293 execsql {ATTACH 'test.db' AS test} db2
294 set sqlite_open_file_count
295} {2}
296
danielk1977c87d34d2006-01-06 13:00:28 +0000297# Sanity check: Create a table in ./test.db via handle db, and test that handle
298# db2 can "see" the new table immediately. A handle using a seperate pager
299# cache would have to reload the database schema before this were possible.
300#
danielk1977a96a7102006-01-16 12:46:41 +0000301do_test shared-$av.4.2.1 {
danielk1977de0fe3e2006-01-06 06:33:12 +0000302 execsql {
303 CREATE TABLE abc(a, b, c);
danielk1977c87d34d2006-01-06 13:00:28 +0000304 CREATE TABLE def(d, e, f);
danielk1977de0fe3e2006-01-06 06:33:12 +0000305 INSERT INTO abc VALUES('i', 'ii', 'iii');
danielk1977c87d34d2006-01-06 13:00:28 +0000306 INSERT INTO def VALUES('I', 'II', 'III');
danielk1977de0fe3e2006-01-06 06:33:12 +0000307 }
308} {}
danielk1977a96a7102006-01-16 12:46:41 +0000309do_test shared-$av.4.2.2 {
danielk1977de0fe3e2006-01-06 06:33:12 +0000310 execsql {
311 SELECT * FROM test.abc;
312 } db2
313} {i ii iii}
314
danielk1977c87d34d2006-01-06 13:00:28 +0000315# Open a read-transaction and read from table abc via handle 2. Check that
316# handle 1 can read table abc. Check that handle 1 cannot modify table abc
317# or the database schema. Then check that handle 1 can modify table def.
318#
danielk1977a96a7102006-01-16 12:46:41 +0000319do_test shared-$av.4.3.1 {
danielk1977c87d34d2006-01-06 13:00:28 +0000320 execsql {
321 BEGIN;
322 SELECT * FROM test.abc;
323 } db2
324} {i ii iii}
danielk1977a96a7102006-01-16 12:46:41 +0000325do_test shared-$av.4.3.2 {
danielk1977c87d34d2006-01-06 13:00:28 +0000326 catchsql {
327 INSERT INTO abc VALUES('iv', 'v', 'vi');
328 }
danielk1977c00da102006-01-07 13:21:04 +0000329} {1 {database table is locked: abc}}
danielk1977a96a7102006-01-16 12:46:41 +0000330do_test shared-$av.4.3.3 {
danielk1977c87d34d2006-01-06 13:00:28 +0000331 catchsql {
332 CREATE TABLE ghi(g, h, i);
333 }
danielk1977c00da102006-01-07 13:21:04 +0000334} {1 {database table is locked: sqlite_master}}
danielk1977a96a7102006-01-16 12:46:41 +0000335do_test shared-$av.4.3.3 {
danielk1977c87d34d2006-01-06 13:00:28 +0000336 catchsql {
337 INSERT INTO def VALUES('IV', 'V', 'VI');
338 }
339} {0 {}}
danielk1977a96a7102006-01-16 12:46:41 +0000340do_test shared-$av.4.3.4 {
danielk1977c87d34d2006-01-06 13:00:28 +0000341 # Cleanup: commit the transaction opened by db2.
342 execsql {
343 COMMIT
344 } db2
345} {}
346
347# Open a write-transaction using handle 1 and modify the database schema.
348# Then try to execute a compiled statement to read from the same
349# database via handle 2 (fails to get the lock on sqlite_master). Also
350# try to compile a read of the same database using handle 2 (also fails).
351# Finally, compile a read of the other database using handle 2. This
352# should also fail.
353#
danielk1977ff890792006-01-16 16:24:25 +0000354ifcapable compound {
355 do_test shared-$av.4.4.1.2 {
356 # Sanity check 1: Check that the schema is what we think it is when viewed
357 # via handle 1.
358 execsql {
359 CREATE TABLE test2.ghi(g, h, i);
360 SELECT 'test.db:'||name FROM sqlite_master
361 UNION ALL
362 SELECT 'test2.db:'||name FROM test2.sqlite_master;
363 }
364 } {test.db:abc test.db:def test2.db:ghi}
365 do_test shared-$av.4.4.1.2 {
366 # Sanity check 2: Check that the schema is what we think it is when viewed
367 # via handle 2.
368 execsql {
369 SELECT 'test2.db:'||name FROM sqlite_master
370 UNION ALL
371 SELECT 'test.db:'||name FROM test.sqlite_master;
372 } db2
373 } {test2.db:ghi test.db:abc test.db:def}
374}
danielk1977c87d34d2006-01-06 13:00:28 +0000375
danielk1977a96a7102006-01-16 12:46:41 +0000376do_test shared-$av.4.4.2 {
danielk1977c87d34d2006-01-06 13:00:28 +0000377 set ::DB2 [sqlite3_connection_pointer db2]
378 set sql {SELECT * FROM abc}
379 set ::STMT1 [sqlite3_prepare $::DB2 $sql -1 DUMMY]
380 execsql {
381 BEGIN;
382 CREATE TABLE jkl(j, k, l);
383 }
384 sqlite3_step $::STMT1
385} {SQLITE_ERROR}
danielk1977a96a7102006-01-16 12:46:41 +0000386do_test shared-$av.4.4.3 {
danielk1977c87d34d2006-01-06 13:00:28 +0000387 sqlite3_finalize $::STMT1
388} {SQLITE_LOCKED}
danielk1977a96a7102006-01-16 12:46:41 +0000389do_test shared-$av.4.4.4 {
danielk1977c87d34d2006-01-06 13:00:28 +0000390 set rc [catch {
391 set ::STMT1 [sqlite3_prepare $::DB2 $sql -1 DUMMY]
392 } msg]
393 list $rc $msg
394} {1 {(6) database schema is locked: test}}
danielk1977a96a7102006-01-16 12:46:41 +0000395do_test shared-$av.4.4.5 {
danielk1977c87d34d2006-01-06 13:00:28 +0000396 set rc [catch {
397 set ::STMT1 [sqlite3_prepare $::DB2 "SELECT * FROM ghi" -1 DUMMY]
398 } msg]
399 list $rc $msg
400} {1 {(6) database schema is locked: test}}
401
danielk1977aaf22682006-01-06 15:03:48 +0000402
danielk1977de0fe3e2006-01-06 06:33:12 +0000403catch {db2 close}
404catch {db close}
405
danielk1977aaf22682006-01-06 15:03:48 +0000406#--------------------------------------------------------------------------
407# Tests shared-5.*
408#
409foreach db [list test.db test1.db test2.db test3.db] {
410 file delete -force $db ${db}-journal
411}
danielk1977a96a7102006-01-16 12:46:41 +0000412do_test shared-$av.5.1.1 {
danielk1977aaf22682006-01-06 15:03:48 +0000413 sqlite3 db1 test.db
414 sqlite3 db2 test.db
415 execsql {
416 ATTACH 'test1.db' AS test1;
417 ATTACH 'test2.db' AS test2;
418 ATTACH 'test3.db' AS test3;
419 } db1
420 execsql {
421 ATTACH 'test3.db' AS test3;
422 ATTACH 'test2.db' AS test2;
423 ATTACH 'test1.db' AS test1;
424 } db2
425} {}
danielk1977a96a7102006-01-16 12:46:41 +0000426do_test shared-$av.5.1.2 {
danielk1977aaf22682006-01-06 15:03:48 +0000427 execsql {
428 CREATE TABLE test1.t1(a, b);
429 CREATE INDEX test1.i1 ON t1(a, b);
danielk1977aaf22682006-01-06 15:03:48 +0000430 } db1
danielk19773bdca9c2006-01-17 09:35:01 +0000431} {}
432ifcapable view {
433 do_test shared-$av.5.1.3 {
434 execsql {
435 CREATE VIEW test1.v1 AS SELECT * FROM t1;
436 } db1
437 } {}
438}
439ifcapable trigger {
440 do_test shared-$av.5.1.4 {
441 execsql {
442 CREATE TRIGGER test1.trig1 AFTER INSERT ON t1 BEGIN
443 INSERT INTO t1 VALUES(new.a, new.b);
444 END;
445 } db1
446 } {}
447}
448do_test shared-$av.5.1.5 {
danielk1977aaf22682006-01-06 15:03:48 +0000449 execsql {
450 DROP INDEX i1;
danielk19773bdca9c2006-01-17 09:35:01 +0000451 } db2
452} {}
453ifcapable view {
454 do_test shared-$av.5.1.6 {
455 execsql {
456 DROP VIEW v1;
457 } db2
458 } {}
459}
460ifcapable trigger {
461 do_test shared-$av.5.1.7 {
462 execsql {
463 DROP TRIGGER trig1;
464 } db2
465 } {}
466}
467do_test shared-$av.5.1.8 {
468 execsql {
danielk1977aaf22682006-01-06 15:03:48 +0000469 DROP TABLE t1;
470 } db2
471} {}
danielk1977ff890792006-01-16 16:24:25 +0000472ifcapable compound {
danielk19773bdca9c2006-01-17 09:35:01 +0000473 do_test shared-$av.5.1.9 {
danielk1977ff890792006-01-16 16:24:25 +0000474 execsql {
475 SELECT * FROM sqlite_master UNION ALL SELECT * FROM test1.sqlite_master
476 } db1
477 } {}
478}
danielk1977aaf22682006-01-06 15:03:48 +0000479
danielk1977c00da102006-01-07 13:21:04 +0000480#--------------------------------------------------------------------------
481# Tests shared-6.* test that a query obtains all the read-locks it needs
482# before starting execution of the query. This means that there is no chance
483# some rows of data will be returned before a lock fails and SQLITE_LOCK
484# is returned.
485#
danielk1977a96a7102006-01-16 12:46:41 +0000486do_test shared-$av.6.1.1 {
danielk1977c00da102006-01-07 13:21:04 +0000487 execsql {
488 CREATE TABLE t1(a, b);
489 CREATE TABLE t2(a, b);
490 INSERT INTO t1 VALUES(1, 2);
491 INSERT INTO t2 VALUES(3, 4);
492 } db1
danielk1977ff890792006-01-16 16:24:25 +0000493} {}
494ifcapable compound {
495 do_test shared-$av.6.1.2 {
496 execsql {
497 SELECT * FROM t1 UNION ALL SELECT * FROM t2;
498 } db2
499 } {1 2 3 4}
500}
501do_test shared-$av.6.1.3 {
danielk1977c00da102006-01-07 13:21:04 +0000502 # Establish a write lock on table t2 via connection db2. Then make a
503 # UNION all query using connection db1 that first accesses t1, followed
504 # by t2. If the locks are grabbed at the start of the statement (as
505 # they should be), no rows are returned. If (as was previously the case)
506 # they are grabbed as the tables are accessed, the t1 rows will be
507 # returned before the query fails.
508 #
509 execsql {
510 BEGIN;
511 INSERT INTO t2 VALUES(5, 6);
512 } db2
513 set ret [list]
514 catch {
515 db1 eval {SELECT * FROM t1 UNION ALL SELECT * FROM t2} {
516 lappend ret $a $b
517 }
518 }
519 set ret
520} {}
danielk1977ff890792006-01-16 16:24:25 +0000521do_test shared-$av.6.1.4 {
danielk1977c00da102006-01-07 13:21:04 +0000522 execsql {
523 COMMIT;
524 BEGIN;
525 INSERT INTO t1 VALUES(7, 8);
526 } db2
527 set ret [list]
528 catch {
529 db1 eval {
530 SELECT (CASE WHEN a>4 THEN (SELECT a FROM t1) ELSE 0 END) AS d FROM t2;
531 } {
532 lappend ret $d
533 }
534 }
535 set ret
536} {}
537
danielk1977aaf22682006-01-06 15:03:48 +0000538catch {db1 close}
539catch {db2 close}
danielk1977e501b892006-01-09 06:29:47 +0000540foreach f [list test.db test2.db] {
541 file delete -force $f ${f}-journal
542}
543
544#--------------------------------------------------------------------------
545# Tests shared-7.* test auto-vacuum does not invalidate cursors from
546# other shared-cache users when it reorganizes the database on
547# COMMIT.
548#
danielk1977a96a7102006-01-16 12:46:41 +0000549do_test shared-$av.7.1 {
danielk197714db2662006-01-09 16:12:04 +0000550 # This test case sets up a test database in auto-vacuum mode consisting
551 # of two tables, t1 and t2. Both have a single index. Table t1 is
552 # populated first (so consists of pages toward the start of the db file),
553 # t2 second (pages toward the end of the file).
danielk1977e501b892006-01-09 06:29:47 +0000554 sqlite3 db test.db
555 sqlite3 db2 test.db
556 execsql {
danielk1977e501b892006-01-09 06:29:47 +0000557 BEGIN;
558 CREATE TABLE t1(a PRIMARY KEY, b);
559 CREATE TABLE t2(a PRIMARY KEY, b);
560 }
drh7a91dd82006-01-11 01:08:34 +0000561 set ::contents {}
danielk1977e501b892006-01-09 06:29:47 +0000562 for {set i 0} {$i < 100} {incr i} {
563 set a [string repeat "$i " 20]
564 set b [string repeat "$i " 20]
565 db eval {
danielk19773bdca9c2006-01-17 09:35:01 +0000566 INSERT INTO t1 VALUES(:a, :b);
danielk1977e501b892006-01-09 06:29:47 +0000567 }
568 lappend ::contents [list [expr $i+1] $a $b]
569 }
570 execsql {
571 INSERT INTO t2 SELECT * FROM t1;
572 COMMIT;
573 }
danielk1977bab45c62006-01-16 15:14:27 +0000574} {}
danielk1977a96a7102006-01-16 12:46:41 +0000575do_test shared-$av.7.2 {
danielk197714db2662006-01-09 16:12:04 +0000576 # This test case deletes the contents of table t1 (the one at the start of
drh85b623f2007-12-13 21:54:09 +0000577 # the file) while many cursors are open on table t2 and its index. All of
danielk197714db2662006-01-09 16:12:04 +0000578 # the non-root pages will be moved from the end to the start of the file
579 # when the DELETE is committed - this test verifies that moving the pages
580 # does not disturb the open cursors.
581 #
582
danielk1977e501b892006-01-09 06:29:47 +0000583 proc lockrow {db tbl oids body} {
584 set ret [list]
585 db eval "SELECT oid AS i, a, b FROM $tbl ORDER BY a" {
586 if {$i==[lindex $oids 0]} {
587 set noids [lrange $oids 1 end]
588 if {[llength $noids]==0} {
589 set subret [eval $body]
590 } else {
591 set subret [lockrow $db $tbl $noids $body]
592 }
593 }
594 lappend ret [list $i $a $b]
595 }
596 return [linsert $subret 0 $ret]
597 }
598 proc locktblrows {db tbl body} {
599 set oids [db eval "SELECT oid FROM $tbl"]
600 lockrow $db $tbl $oids $body
601 }
602
603 set scans [locktblrows db t2 {
604 execsql {
605 DELETE FROM t1;
606 } db2
607 }]
608 set error 0
danielk197714db2662006-01-09 16:12:04 +0000609
610 # Test that each SELECT query returned the expected contents of t2.
danielk1977e501b892006-01-09 06:29:47 +0000611 foreach s $scans {
612 if {[lsort -integer -index 0 $s]!=$::contents} {
613 set error 1
614 }
615 }
616 set error
617} {0}
618
619catch {db close}
620catch {db2 close}
drh7a91dd82006-01-11 01:08:34 +0000621unset -nocomplain contents
danielk1977aaf22682006-01-06 15:03:48 +0000622
danielk197714db2662006-01-09 16:12:04 +0000623#--------------------------------------------------------------------------
624# The following tests try to trick the shared-cache code into assuming
625# the wrong encoding for a database.
626#
627file delete -force test.db test.db-journal
danielk19773bdca9c2006-01-17 09:35:01 +0000628ifcapable utf16 {
629 do_test shared-$av.8.1.1 {
630 sqlite3 db test.db
631 execsql {
632 PRAGMA encoding = 'UTF-16';
633 SELECT * FROM sqlite_master;
634 }
635 } {}
636 do_test shared-$av.8.1.2 {
637 string range [execsql {PRAGMA encoding;}] 0 end-2
638 } {UTF-16}
639 do_test shared-$av.8.1.3 {
640 sqlite3 db2 test.db
641 execsql {
642 PRAGMA encoding = 'UTF-8';
643 CREATE TABLE abc(a, b, c);
644 } db2
645 } {}
646 do_test shared-$av.8.1.4 {
647 execsql {
648 SELECT * FROM sqlite_master;
649 }
650 } "table abc abc [expr $AUTOVACUUM?3:2] {CREATE TABLE abc(a, b, c)}"
651 do_test shared-$av.8.1.5 {
652 db2 close
653 execsql {
654 PRAGMA encoding;
655 }
656 } {UTF-8}
657 file delete -force test2.db test2.db-journal
658 do_test shared-$av.8.2.1 {
659 execsql {
660 ATTACH 'test2.db' AS aux;
661 SELECT * FROM aux.sqlite_master;
662 }
663 } {}
664 do_test shared-$av.8.2.2 {
665 sqlite3 db2 test2.db
666 execsql {
667 PRAGMA encoding = 'UTF-16';
668 CREATE TABLE def(d, e, f);
669 } db2
670 string range [execsql {PRAGMA encoding;} db2] 0 end-2
671 } {UTF-16}
danielk1977d42f8fd2007-08-03 07:33:08 +0000672
673# Bug #2547 is causing this to fail.
674if 0 {
danielk19773bdca9c2006-01-17 09:35:01 +0000675 do_test shared-$av.8.2.3 {
676 catchsql {
677 SELECT * FROM aux.sqlite_master;
678 }
679 } {1 {attached databases must use the same text encoding as main database}}
680}
danielk1977d42f8fd2007-08-03 07:33:08 +0000681}
danielk197714db2662006-01-09 16:12:04 +0000682
683catch {db close}
684catch {db2 close}
danielk1977eecfb3e2006-01-10 12:31:39 +0000685file delete -force test.db test2.db
686
danielk1977eecfb3e2006-01-10 12:31:39 +0000687#---------------------------------------------------------------------------
688# The following tests - shared-9.* - test interactions between TEMP triggers
689# and shared-schemas.
690#
691ifcapable trigger&&tempdb {
692
danielk1977a96a7102006-01-16 12:46:41 +0000693do_test shared-$av.9.1 {
danielk1977eecfb3e2006-01-10 12:31:39 +0000694 sqlite3 db test.db
695 sqlite3 db2 test.db
696 execsql {
697 CREATE TABLE abc(a, b, c);
698 CREATE TABLE abc_mirror(a, b, c);
699 CREATE TEMP TRIGGER BEFORE INSERT ON abc BEGIN
700 INSERT INTO abc_mirror(a, b, c) VALUES(new.a, new.b, new.c);
701 END;
702 INSERT INTO abc VALUES(1, 2, 3);
703 SELECT * FROM abc_mirror;
704 }
705} {1 2 3}
danielk1977a96a7102006-01-16 12:46:41 +0000706do_test shared-$av.9.2 {
danielk1977eecfb3e2006-01-10 12:31:39 +0000707 execsql {
708 INSERT INTO abc VALUES(4, 5, 6);
709 SELECT * FROM abc_mirror;
710 } db2
711} {1 2 3}
danielk1977a96a7102006-01-16 12:46:41 +0000712do_test shared-$av.9.3 {
danielk1977eecfb3e2006-01-10 12:31:39 +0000713 db close
714 db2 close
715} {}
716
717} ; # End shared-9.*
danielk197714db2662006-01-09 16:12:04 +0000718
danielk1977b597f742006-01-15 11:39:18 +0000719#---------------------------------------------------------------------------
720# The following tests - shared-10.* - test that the library behaves
721# correctly when a connection to a shared-cache is closed.
722#
danielk1977a96a7102006-01-16 12:46:41 +0000723do_test shared-$av.10.1 {
danielk1977b597f742006-01-15 11:39:18 +0000724 # Create a small sample database with two connections to it (db and db2).
725 file delete -force test.db
726 sqlite3 db test.db
727 sqlite3 db2 test.db
728 execsql {
729 CREATE TABLE ab(a PRIMARY KEY, b);
730 CREATE TABLE de(d PRIMARY KEY, e);
731 INSERT INTO ab VALUES('Chiang Mai', 100000);
732 INSERT INTO ab VALUES('Bangkok', 8000000);
733 INSERT INTO de VALUES('Ubon', 120000);
734 INSERT INTO de VALUES('Khon Kaen', 200000);
735 }
736} {}
danielk1977a96a7102006-01-16 12:46:41 +0000737do_test shared-$av.10.2 {
danielk1977b597f742006-01-15 11:39:18 +0000738 # Open a read-transaction with the first connection, a write-transaction
739 # with the second.
740 execsql {
741 BEGIN;
742 SELECT * FROM ab;
743 }
744 execsql {
745 BEGIN;
746 INSERT INTO de VALUES('Pataya', 30000);
747 } db2
748} {}
danielk1977a96a7102006-01-16 12:46:41 +0000749do_test shared-$av.10.3 {
danielk1977b597f742006-01-15 11:39:18 +0000750 # An external connection should be able to read the database, but not
751 # prepare a write operation.
drhc693e9e2006-01-23 21:38:03 +0000752 if {$::tcl_platform(platform)=="unix"} {
753 sqlite3 db3 ./test.db
754 } else {
755 sqlite3 db3 TEST.DB
756 }
danielk1977b597f742006-01-15 11:39:18 +0000757 execsql {
758 SELECT * FROM ab;
759 } db3
760 catchsql {
761 BEGIN;
762 INSERT INTO de VALUES('Pataya', 30000);
763 } db3
764} {1 {database is locked}}
danielk1977a96a7102006-01-16 12:46:41 +0000765do_test shared-$av.10.4 {
danielk1977b597f742006-01-15 11:39:18 +0000766 # Close the connection with the write-transaction open
767 db2 close
768} {}
danielk1977a96a7102006-01-16 12:46:41 +0000769do_test shared-$av.10.5 {
danielk1977b597f742006-01-15 11:39:18 +0000770 # Test that the db2 transaction has been automatically rolled back.
771 # If it has not the ('Pataya', 30000) entry will still be in the table.
772 execsql {
773 SELECT * FROM de;
774 }
775} {Ubon 120000 {Khon Kaen} 200000}
danielk1977a96a7102006-01-16 12:46:41 +0000776do_test shared-$av.10.5 {
danielk1977b597f742006-01-15 11:39:18 +0000777 # Closing db2 should have dropped the shared-cache back to a read-lock.
778 # So db3 should be able to prepare a write...
779 catchsql {INSERT INTO de VALUES('Pataya', 30000);} db3
780} {0 {}}
danielk1977a96a7102006-01-16 12:46:41 +0000781do_test shared-$av.10.6 {
danielk1977b597f742006-01-15 11:39:18 +0000782 # ... but not commit it.
783 catchsql {COMMIT} db3
784} {1 {database is locked}}
danielk1977a96a7102006-01-16 12:46:41 +0000785do_test shared-$av.10.7 {
danielk1977b597f742006-01-15 11:39:18 +0000786 # Commit the (read-only) db transaction. Check via db3 to make sure the
787 # contents of table "de" are still as they should be.
788 execsql {
789 COMMIT;
790 }
791 execsql {
792 SELECT * FROM de;
793 } db3
794} {Ubon 120000 {Khon Kaen} 200000 Pataya 30000}
danielk1977a96a7102006-01-16 12:46:41 +0000795do_test shared-$av.10.9 {
danielk1977b597f742006-01-15 11:39:18 +0000796 # Commit the external transaction.
797 catchsql {COMMIT} db3
798} {0 {}}
danielk1977a96a7102006-01-16 12:46:41 +0000799integrity_check shared-$av.10.10
800do_test shared-$av.10.11 {
danielk1977b597f742006-01-15 11:39:18 +0000801 db close
802 db3 close
803} {}
804
danielk19774b202ae2006-01-23 05:50:58 +0000805do_test shared-$av.11.1 {
806 file delete -force test.db
807 sqlite3 db test.db
808 sqlite3 db2 test.db
809 execsql {
810 CREATE TABLE abc(a, b, c);
811 CREATE TABLE abc2(a, b, c);
812 BEGIN;
813 INSERT INTO abc VALUES(1, 2, 3);
814 }
815} {}
816do_test shared-$av.11.2 {
817 catchsql {BEGIN;} db2
818 catchsql {SELECT * FROM abc;} db2
819} {1 {database table is locked: abc}}
820do_test shared-$av.11.3 {
821 catchsql {BEGIN} db2
822} {1 {cannot start a transaction within a transaction}}
823do_test shared-$av.11.4 {
824 catchsql {SELECT * FROM abc2;} db2
825} {0 {}}
826do_test shared-$av.11.5 {
827 catchsql {INSERT INTO abc2 VALUES(1, 2, 3);} db2
828} {1 {database is locked}}
829do_test shared-$av.11.6 {
830 catchsql {SELECT * FROM abc2}
831} {0 {}}
832do_test shared-$av.11.6 {
833 execsql {
834 ROLLBACK;
835 PRAGMA read_uncommitted = 1;
836 } db2
837} {}
838do_test shared-$av.11.7 {
839 execsql {
840 INSERT INTO abc2 VALUES(4, 5, 6);
841 INSERT INTO abc2 VALUES(7, 8, 9);
842 }
843} {}
844do_test shared-$av.11.8 {
845 set res [list]
846 breakpoint
847 db2 eval {
848 SELECT abc.a as I, abc2.a as II FROM abc, abc2;
849 } {
850 execsql {
851 DELETE FROM abc WHERE 1;
852 }
853 lappend res $I $II
854 }
855 set res
856} {1 4 {} 7}
drh16a9b832007-05-05 18:39:25 +0000857if {[llength [info command sqlite3_shared_cache_report]]==1} {
drh7c4ac0c2007-04-05 11:25:58 +0000858 do_test shared-$av.11.9 {
drh16a9b832007-05-05 18:39:25 +0000859 sqlite3_shared_cache_report
drh7c4ac0c2007-04-05 11:25:58 +0000860 } [list [file normalize test.db] 2]
861}
danielk19774b202ae2006-01-23 05:50:58 +0000862
863do_test shared-$av.11.11 {
864 db close
865 db2 close
866} {}
867
danielk1977843e65f2007-09-01 16:16:15 +0000868# This tests that if it is impossible to free any pages, SQLite will
869# exceed the limit set by PRAGMA cache_size.
danielk19774152e672007-09-12 17:01:45 +0000870file delete -force test.db test.db-journal
871sqlite3 db test.db
872ifcapable pager_pragmas {
873 do_test shared-$av.12.1 {
874 execsql {
875 PRAGMA cache_size = 10;
876 PRAGMA cache_size;
877 }
878 } {10}
879}
danielk1977843e65f2007-09-01 16:16:15 +0000880do_test shared-$av.12.2 {
881 set ::db_handles [list]
882 for {set i 1} {$i < 15} {incr i} {
883 lappend ::db_handles db$i
884 sqlite3 db$i test.db
885 execsql "CREATE TABLE db${i}(a, b, c)" db$i
886 execsql "INSERT INTO db${i} VALUES(1, 2, 3)"
887 }
888} {}
889proc nested_select {handles} {
890 [lindex $handles 0] eval "SELECT * FROM [lindex $handles 0]" {
891 lappend ::res $a $b $c
892 if {[llength $handles]>1} {
893 nested_select [lrange $handles 1 end]
894 }
895 }
896}
897do_test shared-$av.12.3 {
898 set ::res [list]
899 nested_select $::db_handles
900 set ::res
901} [string range [string repeat "1 2 3 " [llength $::db_handles]] 0 end-1]
902
903do_test shared-$av.12.X {
904 db close
905 foreach h $::db_handles {
906 $h close
907 }
908} {}
909
danielk1977a96a7102006-01-16 12:46:41 +0000910}
911
danielk1977aef0bf62005-12-30 16:28:01 +0000912sqlite3_enable_shared_cache $::enable_shared_cache
danielk1977a96a7102006-01-16 12:46:41 +0000913finish_test