blob: f05e9651a2b3cebd754c0806787420689a9b751f [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#
danielk1977f51bf482008-04-28 16:19:35 +000012# $Id: shared.test,v 1.31 2008/04/28 16:19:35 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
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}
danielk1977f51bf482008-04-28 16:19:35 +0000639
danielk19773bdca9c2006-01-17 09:35:01 +0000640 do_test shared-$av.8.1.3 {
641 sqlite3 db2 test.db
642 execsql {
643 PRAGMA encoding = 'UTF-8';
644 CREATE TABLE abc(a, b, c);
645 } db2
646 } {}
647 do_test shared-$av.8.1.4 {
648 execsql {
649 SELECT * FROM sqlite_master;
650 }
651 } "table abc abc [expr $AUTOVACUUM?3:2] {CREATE TABLE abc(a, b, c)}"
652 do_test shared-$av.8.1.5 {
653 db2 close
654 execsql {
655 PRAGMA encoding;
656 }
657 } {UTF-8}
danielk1977f51bf482008-04-28 16:19:35 +0000658
danielk19773bdca9c2006-01-17 09:35:01 +0000659 file delete -force test2.db test2.db-journal
660 do_test shared-$av.8.2.1 {
661 execsql {
662 ATTACH 'test2.db' AS aux;
663 SELECT * FROM aux.sqlite_master;
664 }
665 } {}
666 do_test shared-$av.8.2.2 {
667 sqlite3 db2 test2.db
668 execsql {
669 PRAGMA encoding = 'UTF-16';
670 CREATE TABLE def(d, e, f);
671 } db2
672 string range [execsql {PRAGMA encoding;} db2] 0 end-2
673 } {UTF-16}
danielk1977d42f8fd2007-08-03 07:33:08 +0000674
danielk1977f51bf482008-04-28 16:19:35 +0000675 catch {db close}
676 catch {db2 close}
677 file delete -force test.db test2.db
678
679 do_test shared-$av.8.3.2 {
680 sqlite3 db test.db
681 execsql { CREATE TABLE def(d, e, f) }
682 execsql { PRAGMA encoding }
683 } {UTF-8}
684 do_test shared-$av.8.3.3 {
685 set zDb16 "[encoding convertto unicode test.db]\x00\x00"
686 set db16 [sqlite3_open16 $zDb16 {}]
687
688 set stmt [sqlite3_prepare $db16 "SELECT sql FROM sqlite_master" -1 DUMMY]
689 sqlite3_step $stmt
690 set sql [sqlite3_column_text $stmt 0]
691 sqlite3_finalize $stmt
692 set sql
693 } {CREATE TABLE def(d, e, f)}
694 do_test shared-$av.8.3.4 {
695 set stmt [sqlite3_prepare $db16 "PRAGMA encoding" -1 DUMMY]
696 sqlite3_step $stmt
697 set enc [sqlite3_column_text $stmt 0]
698 sqlite3_finalize $stmt
699 set enc
700 } {UTF-8}
701
702 sqlite3_close $db16
703
danielk1977d42f8fd2007-08-03 07:33:08 +0000704# Bug #2547 is causing this to fail.
705if 0 {
danielk19773bdca9c2006-01-17 09:35:01 +0000706 do_test shared-$av.8.2.3 {
707 catchsql {
708 SELECT * FROM aux.sqlite_master;
709 }
710 } {1 {attached databases must use the same text encoding as main database}}
711}
danielk1977d42f8fd2007-08-03 07:33:08 +0000712}
danielk197714db2662006-01-09 16:12:04 +0000713
714catch {db close}
715catch {db2 close}
danielk1977eecfb3e2006-01-10 12:31:39 +0000716file delete -force test.db test2.db
717
danielk1977eecfb3e2006-01-10 12:31:39 +0000718#---------------------------------------------------------------------------
719# The following tests - shared-9.* - test interactions between TEMP triggers
720# and shared-schemas.
721#
722ifcapable trigger&&tempdb {
723
danielk1977a96a7102006-01-16 12:46:41 +0000724do_test shared-$av.9.1 {
danielk1977eecfb3e2006-01-10 12:31:39 +0000725 sqlite3 db test.db
726 sqlite3 db2 test.db
727 execsql {
728 CREATE TABLE abc(a, b, c);
729 CREATE TABLE abc_mirror(a, b, c);
730 CREATE TEMP TRIGGER BEFORE INSERT ON abc BEGIN
731 INSERT INTO abc_mirror(a, b, c) VALUES(new.a, new.b, new.c);
732 END;
733 INSERT INTO abc VALUES(1, 2, 3);
734 SELECT * FROM abc_mirror;
735 }
736} {1 2 3}
danielk1977a96a7102006-01-16 12:46:41 +0000737do_test shared-$av.9.2 {
danielk1977eecfb3e2006-01-10 12:31:39 +0000738 execsql {
739 INSERT INTO abc VALUES(4, 5, 6);
740 SELECT * FROM abc_mirror;
741 } db2
742} {1 2 3}
danielk1977a96a7102006-01-16 12:46:41 +0000743do_test shared-$av.9.3 {
danielk1977eecfb3e2006-01-10 12:31:39 +0000744 db close
745 db2 close
746} {}
747
748} ; # End shared-9.*
danielk197714db2662006-01-09 16:12:04 +0000749
danielk1977b597f742006-01-15 11:39:18 +0000750#---------------------------------------------------------------------------
751# The following tests - shared-10.* - test that the library behaves
752# correctly when a connection to a shared-cache is closed.
753#
danielk1977a96a7102006-01-16 12:46:41 +0000754do_test shared-$av.10.1 {
danielk1977b597f742006-01-15 11:39:18 +0000755 # Create a small sample database with two connections to it (db and db2).
756 file delete -force test.db
757 sqlite3 db test.db
758 sqlite3 db2 test.db
759 execsql {
760 CREATE TABLE ab(a PRIMARY KEY, b);
761 CREATE TABLE de(d PRIMARY KEY, e);
762 INSERT INTO ab VALUES('Chiang Mai', 100000);
763 INSERT INTO ab VALUES('Bangkok', 8000000);
764 INSERT INTO de VALUES('Ubon', 120000);
765 INSERT INTO de VALUES('Khon Kaen', 200000);
766 }
767} {}
danielk1977a96a7102006-01-16 12:46:41 +0000768do_test shared-$av.10.2 {
danielk1977b597f742006-01-15 11:39:18 +0000769 # Open a read-transaction with the first connection, a write-transaction
770 # with the second.
771 execsql {
772 BEGIN;
773 SELECT * FROM ab;
774 }
775 execsql {
776 BEGIN;
777 INSERT INTO de VALUES('Pataya', 30000);
778 } db2
779} {}
danielk1977a96a7102006-01-16 12:46:41 +0000780do_test shared-$av.10.3 {
danielk1977b597f742006-01-15 11:39:18 +0000781 # An external connection should be able to read the database, but not
782 # prepare a write operation.
drhc693e9e2006-01-23 21:38:03 +0000783 if {$::tcl_platform(platform)=="unix"} {
784 sqlite3 db3 ./test.db
785 } else {
786 sqlite3 db3 TEST.DB
787 }
danielk1977b597f742006-01-15 11:39:18 +0000788 execsql {
789 SELECT * FROM ab;
790 } db3
791 catchsql {
792 BEGIN;
793 INSERT INTO de VALUES('Pataya', 30000);
794 } db3
795} {1 {database is locked}}
danielk1977a96a7102006-01-16 12:46:41 +0000796do_test shared-$av.10.4 {
danielk1977b597f742006-01-15 11:39:18 +0000797 # Close the connection with the write-transaction open
798 db2 close
799} {}
danielk1977a96a7102006-01-16 12:46:41 +0000800do_test shared-$av.10.5 {
danielk1977b597f742006-01-15 11:39:18 +0000801 # Test that the db2 transaction has been automatically rolled back.
802 # If it has not the ('Pataya', 30000) entry will still be in the table.
803 execsql {
804 SELECT * FROM de;
805 }
806} {Ubon 120000 {Khon Kaen} 200000}
danielk1977a96a7102006-01-16 12:46:41 +0000807do_test shared-$av.10.5 {
danielk1977b597f742006-01-15 11:39:18 +0000808 # Closing db2 should have dropped the shared-cache back to a read-lock.
809 # So db3 should be able to prepare a write...
810 catchsql {INSERT INTO de VALUES('Pataya', 30000);} db3
811} {0 {}}
danielk1977a96a7102006-01-16 12:46:41 +0000812do_test shared-$av.10.6 {
danielk1977b597f742006-01-15 11:39:18 +0000813 # ... but not commit it.
814 catchsql {COMMIT} db3
815} {1 {database is locked}}
danielk1977a96a7102006-01-16 12:46:41 +0000816do_test shared-$av.10.7 {
danielk1977b597f742006-01-15 11:39:18 +0000817 # Commit the (read-only) db transaction. Check via db3 to make sure the
818 # contents of table "de" are still as they should be.
819 execsql {
820 COMMIT;
821 }
822 execsql {
823 SELECT * FROM de;
824 } db3
825} {Ubon 120000 {Khon Kaen} 200000 Pataya 30000}
danielk1977a96a7102006-01-16 12:46:41 +0000826do_test shared-$av.10.9 {
danielk1977b597f742006-01-15 11:39:18 +0000827 # Commit the external transaction.
828 catchsql {COMMIT} db3
829} {0 {}}
danielk1977a96a7102006-01-16 12:46:41 +0000830integrity_check shared-$av.10.10
831do_test shared-$av.10.11 {
danielk1977b597f742006-01-15 11:39:18 +0000832 db close
833 db3 close
834} {}
835
danielk19774b202ae2006-01-23 05:50:58 +0000836do_test shared-$av.11.1 {
837 file delete -force test.db
838 sqlite3 db test.db
839 sqlite3 db2 test.db
840 execsql {
841 CREATE TABLE abc(a, b, c);
842 CREATE TABLE abc2(a, b, c);
843 BEGIN;
844 INSERT INTO abc VALUES(1, 2, 3);
845 }
846} {}
847do_test shared-$av.11.2 {
848 catchsql {BEGIN;} db2
849 catchsql {SELECT * FROM abc;} db2
850} {1 {database table is locked: abc}}
851do_test shared-$av.11.3 {
852 catchsql {BEGIN} db2
853} {1 {cannot start a transaction within a transaction}}
854do_test shared-$av.11.4 {
855 catchsql {SELECT * FROM abc2;} db2
856} {0 {}}
857do_test shared-$av.11.5 {
858 catchsql {INSERT INTO abc2 VALUES(1, 2, 3);} db2
859} {1 {database is locked}}
860do_test shared-$av.11.6 {
861 catchsql {SELECT * FROM abc2}
862} {0 {}}
863do_test shared-$av.11.6 {
864 execsql {
865 ROLLBACK;
866 PRAGMA read_uncommitted = 1;
867 } db2
868} {}
869do_test shared-$av.11.7 {
870 execsql {
871 INSERT INTO abc2 VALUES(4, 5, 6);
872 INSERT INTO abc2 VALUES(7, 8, 9);
873 }
874} {}
875do_test shared-$av.11.8 {
876 set res [list]
877 breakpoint
878 db2 eval {
879 SELECT abc.a as I, abc2.a as II FROM abc, abc2;
880 } {
881 execsql {
882 DELETE FROM abc WHERE 1;
883 }
884 lappend res $I $II
885 }
886 set res
887} {1 4 {} 7}
drh16a9b832007-05-05 18:39:25 +0000888if {[llength [info command sqlite3_shared_cache_report]]==1} {
drh7c4ac0c2007-04-05 11:25:58 +0000889 do_test shared-$av.11.9 {
danielk1977044d3052008-02-08 18:25:48 +0000890 string tolower [sqlite3_shared_cache_report]
891 } [string tolower [list [file normalize test.db] 2]]
drh7c4ac0c2007-04-05 11:25:58 +0000892}
danielk19774b202ae2006-01-23 05:50:58 +0000893
894do_test shared-$av.11.11 {
895 db close
896 db2 close
897} {}
898
danielk1977843e65f2007-09-01 16:16:15 +0000899# This tests that if it is impossible to free any pages, SQLite will
900# exceed the limit set by PRAGMA cache_size.
danielk19774152e672007-09-12 17:01:45 +0000901file delete -force test.db test.db-journal
902sqlite3 db test.db
903ifcapable pager_pragmas {
904 do_test shared-$av.12.1 {
905 execsql {
906 PRAGMA cache_size = 10;
907 PRAGMA cache_size;
908 }
909 } {10}
910}
danielk1977843e65f2007-09-01 16:16:15 +0000911do_test shared-$av.12.2 {
912 set ::db_handles [list]
913 for {set i 1} {$i < 15} {incr i} {
914 lappend ::db_handles db$i
915 sqlite3 db$i test.db
916 execsql "CREATE TABLE db${i}(a, b, c)" db$i
917 execsql "INSERT INTO db${i} VALUES(1, 2, 3)"
918 }
919} {}
920proc nested_select {handles} {
921 [lindex $handles 0] eval "SELECT * FROM [lindex $handles 0]" {
922 lappend ::res $a $b $c
923 if {[llength $handles]>1} {
924 nested_select [lrange $handles 1 end]
925 }
926 }
927}
928do_test shared-$av.12.3 {
929 set ::res [list]
930 nested_select $::db_handles
931 set ::res
932} [string range [string repeat "1 2 3 " [llength $::db_handles]] 0 end-1]
933
934do_test shared-$av.12.X {
935 db close
936 foreach h $::db_handles {
937 $h close
938 }
939} {}
940
danielk1977a96a7102006-01-16 12:46:41 +0000941}
942
danielk1977aef0bf62005-12-30 16:28:01 +0000943sqlite3_enable_shared_cache $::enable_shared_cache
danielk1977a96a7102006-01-16 12:46:41 +0000944finish_test