blob: e737d9bbfc1430616936e086aaeef355b6c045a3 [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#
danielk19773bdca9c2006-01-17 09:35:01 +000012# $Id: shared.test,v 1.17 2006/01/17 09:35:02 danielk1977 Exp $
danielk1977aef0bf62005-12-30 16:28:01 +000013
14set testdir [file dirname $argv0]
15source $testdir/tester.tcl
16db close
17
18ifcapable !shared_cache {
19 finish_test
20 return
21}
danielk1977a96a7102006-01-16 12:46:41 +000022
danielk1977da184232006-01-05 11:34:32 +000023set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
danielk1977aef0bf62005-12-30 16:28:01 +000024
danielk1977a96a7102006-01-16 12:46:41 +000025foreach av [list 0 1] {
26
danielk1977bab45c62006-01-16 15:14:27 +000027# Open the database connection and execute the auto-vacuum pragma
28file delete -force test.db
29sqlite3 db test.db
30
31ifcapable autovacuum {
32 do_test shared-[expr $av+1].1.0 {
33 execsql "pragma auto_vacuum=$::av"
34 execsql {pragma auto_vacuum}
35 } "$av"
36} else {
37 if {$av} {
38 db close
39 break
40 }
danielk1977a96a7102006-01-16 12:46:41 +000041}
42
danielk1977a96a7102006-01-16 12:46:41 +000043incr av
44
danielk1977aef0bf62005-12-30 16:28:01 +000045# Test organization:
46#
47# shared-1.*: Simple test to verify basic sanity of table level locking when
48# two connections share a pager cache.
49# shared-2.*: Test that a read transaction can co-exist with a
50# write-transaction, including a simple test to ensure the
51# external locking protocol is still working.
danielk1977da184232006-01-05 11:34:32 +000052# shared-3.*: Simple test of read-uncommitted mode.
danielk1977de0fe3e2006-01-06 06:33:12 +000053# shared-4.*: Check that the schema is locked and unlocked correctly.
danielk1977aaf22682006-01-06 15:03:48 +000054# shared-5.*: Test that creating/dropping schema items works when databases
55# are attached in different orders to different handles.
danielk1977c00da102006-01-07 13:21:04 +000056# shared-6.*: Locking, UNION ALL queries and sub-queries.
danielk197714db2662006-01-09 16:12:04 +000057# shared-7.*: Autovacuum and shared-cache.
danielk1977de0fe3e2006-01-06 06:33:12 +000058#
danielk1977aef0bf62005-12-30 16:28:01 +000059
danielk1977a96a7102006-01-16 12:46:41 +000060do_test shared-$av.1.1 {
danielk1977aef0bf62005-12-30 16:28:01 +000061 # Open a second database on the file test.db. It should use the same pager
62 # cache and schema as the original connection. Verify that only 1 file is
63 # opened.
64 sqlite3 db2 test.db
danielk1977aef0bf62005-12-30 16:28:01 +000065 set ::sqlite_open_file_count
66} {1}
danielk1977a96a7102006-01-16 12:46:41 +000067do_test shared-$av.1.2 {
danielk1977aef0bf62005-12-30 16:28:01 +000068 # Add a table and a single row of data via the first connection.
69 # Ensure that the second connection can see them.
70 execsql {
71 CREATE TABLE abc(a, b, c);
72 INSERT INTO abc VALUES(1, 2, 3);
73 } db
74 execsql {
75 SELECT * FROM abc;
76 } db2
77} {1 2 3}
danielk1977a96a7102006-01-16 12:46:41 +000078do_test shared-$av.1.3 {
danielk1977aef0bf62005-12-30 16:28:01 +000079 # Have the first connection begin a transaction and obtain a read-lock
80 # on table abc. This should not prevent the second connection from
81 # querying abc.
82 execsql {
83 BEGIN;
84 SELECT * FROM abc;
85 }
86 execsql {
87 SELECT * FROM abc;
88 } db2
89} {1 2 3}
danielk1977a96a7102006-01-16 12:46:41 +000090do_test shared-$av.1.4 {
danielk1977aef0bf62005-12-30 16:28:01 +000091 # Try to insert a row into abc via connection 2. This should fail because
92 # of the read-lock connection 1 is holding on table abc (obtained in the
93 # previous test case).
94 catchsql {
95 INSERT INTO abc VALUES(4, 5, 6);
96 } db2
danielk1977c00da102006-01-07 13:21:04 +000097} {1 {database table is locked: abc}}
danielk1977a96a7102006-01-16 12:46:41 +000098do_test shared-$av.1.5 {
danielk1977da184232006-01-05 11:34:32 +000099 # Using connection 2 (the one without the open transaction), try to create
100 # a new table. This should fail because of the open read transaction
101 # held by connection 1.
102 catchsql {
103 CREATE TABLE def(d, e, f);
104 } db2
danielk1977c00da102006-01-07 13:21:04 +0000105} {1 {database table is locked: sqlite_master}}
danielk1977a96a7102006-01-16 12:46:41 +0000106do_test shared-$av.1.6 {
danielk1977da184232006-01-05 11:34:32 +0000107 # Upgrade connection 1's transaction to a write transaction. Create
108 # a new table - def - and insert a row into it. Because the connection 1
109 # transaction modifies the schema, it should not be possible for
110 # connection 2 to access the database at all until the connection 1
111 # has finished the transaction.
danielk1977aef0bf62005-12-30 16:28:01 +0000112 execsql {
113 CREATE TABLE def(d, e, f);
danielk1977aef0bf62005-12-30 16:28:01 +0000114 INSERT INTO def VALUES('IV', 'V', 'VI');
115 }
116} {}
danielk1977a96a7102006-01-16 12:46:41 +0000117do_test shared-$av.1.7 {
danielk1977aef0bf62005-12-30 16:28:01 +0000118 # Read from the sqlite_master table with connection 1 (inside the
danielk1977da184232006-01-05 11:34:32 +0000119 # transaction). Then test that we can not do this with connection 2. This
120 # is because of the schema-modified lock established by connection 1
121 # in the previous test case.
danielk1977aef0bf62005-12-30 16:28:01 +0000122 execsql {
123 SELECT * FROM sqlite_master;
124 }
125 catchsql {
danielk1977da184232006-01-05 11:34:32 +0000126 SELECT * FROM sqlite_master;
danielk1977aef0bf62005-12-30 16:28:01 +0000127 } db2
danielk1977c87d34d2006-01-06 13:00:28 +0000128} {1 {database schema is locked: main}}
danielk1977a96a7102006-01-16 12:46:41 +0000129do_test shared-$av.1.8 {
danielk1977aef0bf62005-12-30 16:28:01 +0000130 # Commit the connection 1 transaction.
131 execsql {
132 COMMIT;
133 }
134} {}
135
danielk1977a96a7102006-01-16 12:46:41 +0000136do_test shared-$av.2.1 {
danielk1977aef0bf62005-12-30 16:28:01 +0000137 # Open connection db3 to the database. Use a different path to the same
138 # file so that db3 does *not* share the same pager cache as db and db2
139 # (there should be two open file handles).
140 sqlite3 db3 ./test.db
141 set ::sqlite_open_file_count
142} {2}
danielk1977a96a7102006-01-16 12:46:41 +0000143do_test shared-$av.2.2 {
danielk1977aef0bf62005-12-30 16:28:01 +0000144 # Start read transactions on db and db2 (the shared pager cache). Ensure
145 # db3 cannot write to the database.
146 execsql {
147 BEGIN;
148 SELECT * FROM abc;
149 }
150 execsql {
151 BEGIN;
152 SELECT * FROM abc;
153 } db2
154 catchsql {
155 INSERT INTO abc VALUES(1, 2, 3);
156 } db2
danielk1977c00da102006-01-07 13:21:04 +0000157} {1 {database table is locked: abc}}
danielk1977a96a7102006-01-16 12:46:41 +0000158do_test shared-$av.2.3 {
danielk1977aef0bf62005-12-30 16:28:01 +0000159 # Turn db's transaction into a write-transaction. db3 should still be
160 # able to read from table def (but will not see the new row). Connection
161 # db2 should not be able to read def (because of the write-lock).
162
163# Todo: The failed "INSERT INTO abc ..." statement in the above test
164# has started a write-transaction on db2 (should this be so?). This
165# would prevent connection db from starting a write-transaction. So roll the
166# db2 transaction back and replace it with a new read transaction.
167 execsql {
168 ROLLBACK;
169 BEGIN;
170 SELECT * FROM abc;
171 } db2
172
173 execsql {
174 INSERT INTO def VALUES('VII', 'VIII', 'IX');
175 }
176 concat [
177 catchsql { SELECT * FROM def; } db3
178 ] [
179 catchsql { SELECT * FROM def; } db2
180 ]
danielk1977c00da102006-01-07 13:21:04 +0000181} {0 {IV V VI} 1 {database table is locked: def}}
danielk1977a96a7102006-01-16 12:46:41 +0000182do_test shared-$av.2.4 {
danielk1977aef0bf62005-12-30 16:28:01 +0000183 # Commit the open transaction on db. db2 still holds a read-transaction.
184 # This should prevent db3 from writing to the database, but not from
185 # reading.
186 execsql {
187 COMMIT;
188 }
189 concat [
190 catchsql { SELECT * FROM def; } db3
191 ] [
192 catchsql { INSERT INTO def VALUES('X', 'XI', 'XII'); } db3
193 ]
danielk1977da184232006-01-05 11:34:32 +0000194} {0 {IV V VI VII VIII IX} 1 {database is locked}}
danielk1977aef0bf62005-12-30 16:28:01 +0000195
danielk1977da184232006-01-05 11:34:32 +0000196catchsql COMMIT db2
197
danielk1977a96a7102006-01-16 12:46:41 +0000198do_test shared-$av.3.1.1 {
danielk1977da184232006-01-05 11:34:32 +0000199 # This test case starts a linear scan of table 'seq' using a
200 # read-uncommitted connection. In the middle of the scan, rows are added
201 # to the end of the seq table (ahead of the current cursor position).
202 # The uncommitted rows should be included in the results of the scan.
203 execsql "
204 CREATE TABLE seq(i, x);
205 INSERT INTO seq VALUES(1, '[string repeat X 500]');
206 INSERT INTO seq VALUES(2, '[string repeat X 500]');
207 "
208 execsql {SELECT * FROM sqlite_master} db2
209 execsql {PRAGMA read_uncommitted = 1} db2
210
211 set ret [list]
212 db2 eval {SELECT i FROM seq} {
213 if {$i < 4} {
danielk19771576cd92006-01-14 08:02:28 +0000214 set max [execsql {SELECT max(i) FROM seq}]
215 db eval {
danielk19773bdca9c2006-01-17 09:35:01 +0000216 INSERT INTO seq SELECT i + :max, x FROM seq;
danielk1977da184232006-01-05 11:34:32 +0000217 }
218 }
219 lappend ret $i
220 }
221 set ret
222} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16}
danielk1977a96a7102006-01-16 12:46:41 +0000223do_test shared-$av.3.1.2 {
danielk1977da184232006-01-05 11:34:32 +0000224 # Another linear scan through table seq using a read-uncommitted connection.
225 # This time, delete each row as it is read. Should not affect the results of
226 # the scan, but the table should be empty after the scan is concluded
227 # (test 3.1.3 verifies this).
228 set ret [list]
229 db2 eval {SELECT i FROM seq} {
danielk19773bdca9c2006-01-17 09:35:01 +0000230 db eval {DELETE FROM seq WHERE i = :i}
danielk1977da184232006-01-05 11:34:32 +0000231 lappend ret $i
232 }
233 set ret
234} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16}
danielk1977a96a7102006-01-16 12:46:41 +0000235do_test shared-$av.3.1.3 {
danielk1977da184232006-01-05 11:34:32 +0000236 execsql {
237 SELECT * FROM seq;
238 }
239} {}
danielk1977aef0bf62005-12-30 16:28:01 +0000240
241catch {db close}
242catch {db2 close}
243catch {db3 close}
244
danielk1977de0fe3e2006-01-06 06:33:12 +0000245#--------------------------------------------------------------------------
246# Tests shared-4.* test that the schema locking rules are applied
247# correctly. i.e.:
248#
249# 1. All transactions require a read-lock on the schemas of databases they
250# access.
251# 2. Transactions that modify a database schema require a write-lock on that
252# schema.
253# 3. It is not possible to compile a statement while another handle has a
254# write-lock on the schema.
255#
256
257# Open two database handles db and db2. Each has a single attach database
258# (as well as main):
259#
260# db.main -> ./test.db
261# db.test2 -> ./test2.db
262# db2.main -> ./test2.db
263# db2.test -> ./test.db
264#
265file delete -force test.db
266file delete -force test2.db
267file delete -force test2.db-journal
268sqlite3 db test.db
269sqlite3 db2 test2.db
danielk1977a96a7102006-01-16 12:46:41 +0000270do_test shared-$av.4.1.1 {
danielk1977de0fe3e2006-01-06 06:33:12 +0000271 set sqlite_open_file_count
272} {2}
danielk1977a96a7102006-01-16 12:46:41 +0000273do_test shared-$av.4.1.2 {
danielk1977de0fe3e2006-01-06 06:33:12 +0000274 execsql {ATTACH 'test2.db' AS test2}
275 set sqlite_open_file_count
276} {2}
danielk1977a96a7102006-01-16 12:46:41 +0000277do_test shared-$av.4.1.3 {
danielk1977de0fe3e2006-01-06 06:33:12 +0000278 execsql {ATTACH 'test.db' AS test} db2
279 set sqlite_open_file_count
280} {2}
281
danielk1977c87d34d2006-01-06 13:00:28 +0000282# Sanity check: Create a table in ./test.db via handle db, and test that handle
283# db2 can "see" the new table immediately. A handle using a seperate pager
284# cache would have to reload the database schema before this were possible.
285#
danielk1977a96a7102006-01-16 12:46:41 +0000286do_test shared-$av.4.2.1 {
danielk1977de0fe3e2006-01-06 06:33:12 +0000287 execsql {
288 CREATE TABLE abc(a, b, c);
danielk1977c87d34d2006-01-06 13:00:28 +0000289 CREATE TABLE def(d, e, f);
danielk1977de0fe3e2006-01-06 06:33:12 +0000290 INSERT INTO abc VALUES('i', 'ii', 'iii');
danielk1977c87d34d2006-01-06 13:00:28 +0000291 INSERT INTO def VALUES('I', 'II', 'III');
danielk1977de0fe3e2006-01-06 06:33:12 +0000292 }
293} {}
danielk1977a96a7102006-01-16 12:46:41 +0000294do_test shared-$av.4.2.2 {
danielk1977de0fe3e2006-01-06 06:33:12 +0000295 execsql {
296 SELECT * FROM test.abc;
297 } db2
298} {i ii iii}
299
danielk1977c87d34d2006-01-06 13:00:28 +0000300# Open a read-transaction and read from table abc via handle 2. Check that
301# handle 1 can read table abc. Check that handle 1 cannot modify table abc
302# or the database schema. Then check that handle 1 can modify table def.
303#
danielk1977a96a7102006-01-16 12:46:41 +0000304do_test shared-$av.4.3.1 {
danielk1977c87d34d2006-01-06 13:00:28 +0000305 execsql {
306 BEGIN;
307 SELECT * FROM test.abc;
308 } db2
309} {i ii iii}
danielk1977a96a7102006-01-16 12:46:41 +0000310do_test shared-$av.4.3.2 {
danielk1977c87d34d2006-01-06 13:00:28 +0000311 catchsql {
312 INSERT INTO abc VALUES('iv', 'v', 'vi');
313 }
danielk1977c00da102006-01-07 13:21:04 +0000314} {1 {database table is locked: abc}}
danielk1977a96a7102006-01-16 12:46:41 +0000315do_test shared-$av.4.3.3 {
danielk1977c87d34d2006-01-06 13:00:28 +0000316 catchsql {
317 CREATE TABLE ghi(g, h, i);
318 }
danielk1977c00da102006-01-07 13:21:04 +0000319} {1 {database table is locked: sqlite_master}}
danielk1977a96a7102006-01-16 12:46:41 +0000320do_test shared-$av.4.3.3 {
danielk1977c87d34d2006-01-06 13:00:28 +0000321 catchsql {
322 INSERT INTO def VALUES('IV', 'V', 'VI');
323 }
324} {0 {}}
danielk1977a96a7102006-01-16 12:46:41 +0000325do_test shared-$av.4.3.4 {
danielk1977c87d34d2006-01-06 13:00:28 +0000326 # Cleanup: commit the transaction opened by db2.
327 execsql {
328 COMMIT
329 } db2
330} {}
331
332# Open a write-transaction using handle 1 and modify the database schema.
333# Then try to execute a compiled statement to read from the same
334# database via handle 2 (fails to get the lock on sqlite_master). Also
335# try to compile a read of the same database using handle 2 (also fails).
336# Finally, compile a read of the other database using handle 2. This
337# should also fail.
338#
danielk1977ff890792006-01-16 16:24:25 +0000339ifcapable compound {
340 do_test shared-$av.4.4.1.2 {
341 # Sanity check 1: Check that the schema is what we think it is when viewed
342 # via handle 1.
343 execsql {
344 CREATE TABLE test2.ghi(g, h, i);
345 SELECT 'test.db:'||name FROM sqlite_master
346 UNION ALL
347 SELECT 'test2.db:'||name FROM test2.sqlite_master;
348 }
349 } {test.db:abc test.db:def test2.db:ghi}
350 do_test shared-$av.4.4.1.2 {
351 # Sanity check 2: Check that the schema is what we think it is when viewed
352 # via handle 2.
353 execsql {
354 SELECT 'test2.db:'||name FROM sqlite_master
355 UNION ALL
356 SELECT 'test.db:'||name FROM test.sqlite_master;
357 } db2
358 } {test2.db:ghi test.db:abc test.db:def}
359}
danielk1977c87d34d2006-01-06 13:00:28 +0000360
danielk1977a96a7102006-01-16 12:46:41 +0000361do_test shared-$av.4.4.2 {
danielk1977c87d34d2006-01-06 13:00:28 +0000362 set ::DB2 [sqlite3_connection_pointer db2]
363 set sql {SELECT * FROM abc}
364 set ::STMT1 [sqlite3_prepare $::DB2 $sql -1 DUMMY]
365 execsql {
366 BEGIN;
367 CREATE TABLE jkl(j, k, l);
368 }
369 sqlite3_step $::STMT1
370} {SQLITE_ERROR}
danielk1977a96a7102006-01-16 12:46:41 +0000371do_test shared-$av.4.4.3 {
danielk1977c87d34d2006-01-06 13:00:28 +0000372 sqlite3_finalize $::STMT1
373} {SQLITE_LOCKED}
danielk1977a96a7102006-01-16 12:46:41 +0000374do_test shared-$av.4.4.4 {
danielk1977c87d34d2006-01-06 13:00:28 +0000375 set rc [catch {
376 set ::STMT1 [sqlite3_prepare $::DB2 $sql -1 DUMMY]
377 } msg]
378 list $rc $msg
379} {1 {(6) database schema is locked: test}}
danielk1977a96a7102006-01-16 12:46:41 +0000380do_test shared-$av.4.4.5 {
danielk1977c87d34d2006-01-06 13:00:28 +0000381 set rc [catch {
382 set ::STMT1 [sqlite3_prepare $::DB2 "SELECT * FROM ghi" -1 DUMMY]
383 } msg]
384 list $rc $msg
385} {1 {(6) database schema is locked: test}}
386
danielk1977aaf22682006-01-06 15:03:48 +0000387
danielk1977de0fe3e2006-01-06 06:33:12 +0000388catch {db2 close}
389catch {db close}
390
danielk1977aaf22682006-01-06 15:03:48 +0000391#--------------------------------------------------------------------------
392# Tests shared-5.*
393#
394foreach db [list test.db test1.db test2.db test3.db] {
395 file delete -force $db ${db}-journal
396}
danielk1977a96a7102006-01-16 12:46:41 +0000397do_test shared-$av.5.1.1 {
danielk1977aaf22682006-01-06 15:03:48 +0000398 sqlite3 db1 test.db
399 sqlite3 db2 test.db
400 execsql {
401 ATTACH 'test1.db' AS test1;
402 ATTACH 'test2.db' AS test2;
403 ATTACH 'test3.db' AS test3;
404 } db1
405 execsql {
406 ATTACH 'test3.db' AS test3;
407 ATTACH 'test2.db' AS test2;
408 ATTACH 'test1.db' AS test1;
409 } db2
410} {}
danielk1977a96a7102006-01-16 12:46:41 +0000411do_test shared-$av.5.1.2 {
danielk1977aaf22682006-01-06 15:03:48 +0000412 execsql {
413 CREATE TABLE test1.t1(a, b);
414 CREATE INDEX test1.i1 ON t1(a, b);
danielk1977aaf22682006-01-06 15:03:48 +0000415 } db1
danielk19773bdca9c2006-01-17 09:35:01 +0000416} {}
417ifcapable view {
418 do_test shared-$av.5.1.3 {
419 execsql {
420 CREATE VIEW test1.v1 AS SELECT * FROM t1;
421 } db1
422 } {}
423}
424ifcapable trigger {
425 do_test shared-$av.5.1.4 {
426 execsql {
427 CREATE TRIGGER test1.trig1 AFTER INSERT ON t1 BEGIN
428 INSERT INTO t1 VALUES(new.a, new.b);
429 END;
430 } db1
431 } {}
432}
433do_test shared-$av.5.1.5 {
danielk1977aaf22682006-01-06 15:03:48 +0000434 execsql {
435 DROP INDEX i1;
danielk19773bdca9c2006-01-17 09:35:01 +0000436 } db2
437} {}
438ifcapable view {
439 do_test shared-$av.5.1.6 {
440 execsql {
441 DROP VIEW v1;
442 } db2
443 } {}
444}
445ifcapable trigger {
446 do_test shared-$av.5.1.7 {
447 execsql {
448 DROP TRIGGER trig1;
449 } db2
450 } {}
451}
452do_test shared-$av.5.1.8 {
453 execsql {
danielk1977aaf22682006-01-06 15:03:48 +0000454 DROP TABLE t1;
455 } db2
456} {}
danielk1977ff890792006-01-16 16:24:25 +0000457ifcapable compound {
danielk19773bdca9c2006-01-17 09:35:01 +0000458 do_test shared-$av.5.1.9 {
danielk1977ff890792006-01-16 16:24:25 +0000459 execsql {
460 SELECT * FROM sqlite_master UNION ALL SELECT * FROM test1.sqlite_master
461 } db1
462 } {}
463}
danielk1977aaf22682006-01-06 15:03:48 +0000464
danielk1977c00da102006-01-07 13:21:04 +0000465#--------------------------------------------------------------------------
466# Tests shared-6.* test that a query obtains all the read-locks it needs
467# before starting execution of the query. This means that there is no chance
468# some rows of data will be returned before a lock fails and SQLITE_LOCK
469# is returned.
470#
danielk1977a96a7102006-01-16 12:46:41 +0000471do_test shared-$av.6.1.1 {
danielk1977c00da102006-01-07 13:21:04 +0000472 execsql {
473 CREATE TABLE t1(a, b);
474 CREATE TABLE t2(a, b);
475 INSERT INTO t1 VALUES(1, 2);
476 INSERT INTO t2 VALUES(3, 4);
477 } db1
danielk1977ff890792006-01-16 16:24:25 +0000478} {}
479ifcapable compound {
480 do_test shared-$av.6.1.2 {
481 execsql {
482 SELECT * FROM t1 UNION ALL SELECT * FROM t2;
483 } db2
484 } {1 2 3 4}
485}
486do_test shared-$av.6.1.3 {
danielk1977c00da102006-01-07 13:21:04 +0000487 # Establish a write lock on table t2 via connection db2. Then make a
488 # UNION all query using connection db1 that first accesses t1, followed
489 # by t2. If the locks are grabbed at the start of the statement (as
490 # they should be), no rows are returned. If (as was previously the case)
491 # they are grabbed as the tables are accessed, the t1 rows will be
492 # returned before the query fails.
493 #
494 execsql {
495 BEGIN;
496 INSERT INTO t2 VALUES(5, 6);
497 } db2
498 set ret [list]
499 catch {
500 db1 eval {SELECT * FROM t1 UNION ALL SELECT * FROM t2} {
501 lappend ret $a $b
502 }
503 }
504 set ret
505} {}
danielk1977ff890792006-01-16 16:24:25 +0000506do_test shared-$av.6.1.4 {
danielk1977c00da102006-01-07 13:21:04 +0000507 execsql {
508 COMMIT;
509 BEGIN;
510 INSERT INTO t1 VALUES(7, 8);
511 } db2
512 set ret [list]
513 catch {
514 db1 eval {
515 SELECT (CASE WHEN a>4 THEN (SELECT a FROM t1) ELSE 0 END) AS d FROM t2;
516 } {
517 lappend ret $d
518 }
519 }
520 set ret
521} {}
522
danielk1977aaf22682006-01-06 15:03:48 +0000523catch {db1 close}
524catch {db2 close}
danielk1977e501b892006-01-09 06:29:47 +0000525foreach f [list test.db test2.db] {
526 file delete -force $f ${f}-journal
527}
528
529#--------------------------------------------------------------------------
530# Tests shared-7.* test auto-vacuum does not invalidate cursors from
531# other shared-cache users when it reorganizes the database on
532# COMMIT.
533#
danielk1977a96a7102006-01-16 12:46:41 +0000534do_test shared-$av.7.1 {
danielk197714db2662006-01-09 16:12:04 +0000535 # This test case sets up a test database in auto-vacuum mode consisting
536 # of two tables, t1 and t2. Both have a single index. Table t1 is
537 # populated first (so consists of pages toward the start of the db file),
538 # t2 second (pages toward the end of the file).
danielk1977e501b892006-01-09 06:29:47 +0000539 sqlite3 db test.db
540 sqlite3 db2 test.db
541 execsql {
danielk1977e501b892006-01-09 06:29:47 +0000542 BEGIN;
543 CREATE TABLE t1(a PRIMARY KEY, b);
544 CREATE TABLE t2(a PRIMARY KEY, b);
545 }
drh7a91dd82006-01-11 01:08:34 +0000546 set ::contents {}
danielk1977e501b892006-01-09 06:29:47 +0000547 for {set i 0} {$i < 100} {incr i} {
548 set a [string repeat "$i " 20]
549 set b [string repeat "$i " 20]
550 db eval {
danielk19773bdca9c2006-01-17 09:35:01 +0000551 INSERT INTO t1 VALUES(:a, :b);
danielk1977e501b892006-01-09 06:29:47 +0000552 }
553 lappend ::contents [list [expr $i+1] $a $b]
554 }
555 execsql {
556 INSERT INTO t2 SELECT * FROM t1;
557 COMMIT;
558 }
danielk1977bab45c62006-01-16 15:14:27 +0000559} {}
danielk1977a96a7102006-01-16 12:46:41 +0000560do_test shared-$av.7.2 {
danielk197714db2662006-01-09 16:12:04 +0000561 # This test case deletes the contents of table t1 (the one at the start of
562 # the file) while many cursors are open on table t2 and it's index. All of
563 # the non-root pages will be moved from the end to the start of the file
564 # when the DELETE is committed - this test verifies that moving the pages
565 # does not disturb the open cursors.
566 #
567
danielk1977e501b892006-01-09 06:29:47 +0000568 proc lockrow {db tbl oids body} {
569 set ret [list]
570 db eval "SELECT oid AS i, a, b FROM $tbl ORDER BY a" {
571 if {$i==[lindex $oids 0]} {
572 set noids [lrange $oids 1 end]
573 if {[llength $noids]==0} {
574 set subret [eval $body]
575 } else {
576 set subret [lockrow $db $tbl $noids $body]
577 }
578 }
579 lappend ret [list $i $a $b]
580 }
581 return [linsert $subret 0 $ret]
582 }
583 proc locktblrows {db tbl body} {
584 set oids [db eval "SELECT oid FROM $tbl"]
585 lockrow $db $tbl $oids $body
586 }
587
588 set scans [locktblrows db t2 {
589 execsql {
590 DELETE FROM t1;
591 } db2
592 }]
593 set error 0
danielk197714db2662006-01-09 16:12:04 +0000594
595 # Test that each SELECT query returned the expected contents of t2.
danielk1977e501b892006-01-09 06:29:47 +0000596 foreach s $scans {
597 if {[lsort -integer -index 0 $s]!=$::contents} {
598 set error 1
599 }
600 }
601 set error
602} {0}
603
604catch {db close}
605catch {db2 close}
drh7a91dd82006-01-11 01:08:34 +0000606unset -nocomplain contents
danielk1977aaf22682006-01-06 15:03:48 +0000607
danielk197714db2662006-01-09 16:12:04 +0000608#--------------------------------------------------------------------------
609# The following tests try to trick the shared-cache code into assuming
610# the wrong encoding for a database.
611#
612file delete -force test.db test.db-journal
danielk19773bdca9c2006-01-17 09:35:01 +0000613ifcapable utf16 {
614 do_test shared-$av.8.1.1 {
615 sqlite3 db test.db
616 execsql {
617 PRAGMA encoding = 'UTF-16';
618 SELECT * FROM sqlite_master;
619 }
620 } {}
621 do_test shared-$av.8.1.2 {
622 string range [execsql {PRAGMA encoding;}] 0 end-2
623 } {UTF-16}
624 do_test shared-$av.8.1.3 {
625 sqlite3 db2 test.db
626 execsql {
627 PRAGMA encoding = 'UTF-8';
628 CREATE TABLE abc(a, b, c);
629 } db2
630 } {}
631 do_test shared-$av.8.1.4 {
632 execsql {
633 SELECT * FROM sqlite_master;
634 }
635 } "table abc abc [expr $AUTOVACUUM?3:2] {CREATE TABLE abc(a, b, c)}"
636 do_test shared-$av.8.1.5 {
637 db2 close
638 execsql {
639 PRAGMA encoding;
640 }
641 } {UTF-8}
642 file delete -force test2.db test2.db-journal
643 do_test shared-$av.8.2.1 {
644 execsql {
645 ATTACH 'test2.db' AS aux;
646 SELECT * FROM aux.sqlite_master;
647 }
648 } {}
649 do_test shared-$av.8.2.2 {
650 sqlite3 db2 test2.db
651 execsql {
652 PRAGMA encoding = 'UTF-16';
653 CREATE TABLE def(d, e, f);
654 } db2
655 string range [execsql {PRAGMA encoding;} db2] 0 end-2
656 } {UTF-16}
657 do_test shared-$av.8.2.3 {
658 catchsql {
659 SELECT * FROM aux.sqlite_master;
660 }
661 } {1 {attached databases must use the same text encoding as main database}}
662}
danielk197714db2662006-01-09 16:12:04 +0000663
664catch {db close}
665catch {db2 close}
danielk1977eecfb3e2006-01-10 12:31:39 +0000666file delete -force test.db test2.db
667
danielk1977eecfb3e2006-01-10 12:31:39 +0000668#---------------------------------------------------------------------------
669# The following tests - shared-9.* - test interactions between TEMP triggers
670# and shared-schemas.
671#
672ifcapable trigger&&tempdb {
673
danielk1977a96a7102006-01-16 12:46:41 +0000674do_test shared-$av.9.1 {
danielk1977eecfb3e2006-01-10 12:31:39 +0000675 sqlite3 db test.db
676 sqlite3 db2 test.db
677 execsql {
678 CREATE TABLE abc(a, b, c);
679 CREATE TABLE abc_mirror(a, b, c);
680 CREATE TEMP TRIGGER BEFORE INSERT ON abc BEGIN
681 INSERT INTO abc_mirror(a, b, c) VALUES(new.a, new.b, new.c);
682 END;
683 INSERT INTO abc VALUES(1, 2, 3);
684 SELECT * FROM abc_mirror;
685 }
686} {1 2 3}
danielk1977a96a7102006-01-16 12:46:41 +0000687do_test shared-$av.9.2 {
danielk1977eecfb3e2006-01-10 12:31:39 +0000688 execsql {
689 INSERT INTO abc VALUES(4, 5, 6);
690 SELECT * FROM abc_mirror;
691 } db2
692} {1 2 3}
danielk1977a96a7102006-01-16 12:46:41 +0000693do_test shared-$av.9.3 {
danielk1977eecfb3e2006-01-10 12:31:39 +0000694 db close
695 db2 close
696} {}
697
698} ; # End shared-9.*
danielk197714db2662006-01-09 16:12:04 +0000699
danielk1977b597f742006-01-15 11:39:18 +0000700#---------------------------------------------------------------------------
701# The following tests - shared-10.* - test that the library behaves
702# correctly when a connection to a shared-cache is closed.
703#
danielk1977a96a7102006-01-16 12:46:41 +0000704do_test shared-$av.10.1 {
danielk1977b597f742006-01-15 11:39:18 +0000705 # Create a small sample database with two connections to it (db and db2).
706 file delete -force test.db
707 sqlite3 db test.db
708 sqlite3 db2 test.db
709 execsql {
710 CREATE TABLE ab(a PRIMARY KEY, b);
711 CREATE TABLE de(d PRIMARY KEY, e);
712 INSERT INTO ab VALUES('Chiang Mai', 100000);
713 INSERT INTO ab VALUES('Bangkok', 8000000);
714 INSERT INTO de VALUES('Ubon', 120000);
715 INSERT INTO de VALUES('Khon Kaen', 200000);
716 }
717} {}
danielk1977a96a7102006-01-16 12:46:41 +0000718do_test shared-$av.10.2 {
danielk1977b597f742006-01-15 11:39:18 +0000719 # Open a read-transaction with the first connection, a write-transaction
720 # with the second.
721 execsql {
722 BEGIN;
723 SELECT * FROM ab;
724 }
725 execsql {
726 BEGIN;
727 INSERT INTO de VALUES('Pataya', 30000);
728 } db2
729} {}
danielk1977a96a7102006-01-16 12:46:41 +0000730do_test shared-$av.10.3 {
danielk1977b597f742006-01-15 11:39:18 +0000731 # An external connection should be able to read the database, but not
732 # prepare a write operation.
733 sqlite3 db3 ./test.db
734 execsql {
735 SELECT * FROM ab;
736 } db3
737 catchsql {
738 BEGIN;
739 INSERT INTO de VALUES('Pataya', 30000);
740 } db3
741} {1 {database is locked}}
danielk1977a96a7102006-01-16 12:46:41 +0000742do_test shared-$av.10.4 {
danielk1977b597f742006-01-15 11:39:18 +0000743 # Close the connection with the write-transaction open
744 db2 close
745} {}
danielk1977a96a7102006-01-16 12:46:41 +0000746do_test shared-$av.10.5 {
danielk1977b597f742006-01-15 11:39:18 +0000747 # Test that the db2 transaction has been automatically rolled back.
748 # If it has not the ('Pataya', 30000) entry will still be in the table.
749 execsql {
750 SELECT * FROM de;
751 }
752} {Ubon 120000 {Khon Kaen} 200000}
danielk1977a96a7102006-01-16 12:46:41 +0000753do_test shared-$av.10.5 {
danielk1977b597f742006-01-15 11:39:18 +0000754 # Closing db2 should have dropped the shared-cache back to a read-lock.
755 # So db3 should be able to prepare a write...
756 catchsql {INSERT INTO de VALUES('Pataya', 30000);} db3
757} {0 {}}
danielk1977a96a7102006-01-16 12:46:41 +0000758do_test shared-$av.10.6 {
danielk1977b597f742006-01-15 11:39:18 +0000759 # ... but not commit it.
760 catchsql {COMMIT} db3
761} {1 {database is locked}}
danielk1977a96a7102006-01-16 12:46:41 +0000762do_test shared-$av.10.7 {
danielk1977b597f742006-01-15 11:39:18 +0000763 # Commit the (read-only) db transaction. Check via db3 to make sure the
764 # contents of table "de" are still as they should be.
765 execsql {
766 COMMIT;
767 }
768 execsql {
769 SELECT * FROM de;
770 } db3
771} {Ubon 120000 {Khon Kaen} 200000 Pataya 30000}
danielk1977a96a7102006-01-16 12:46:41 +0000772do_test shared-$av.10.9 {
danielk1977b597f742006-01-15 11:39:18 +0000773 # Commit the external transaction.
774 catchsql {COMMIT} db3
775} {0 {}}
danielk1977a96a7102006-01-16 12:46:41 +0000776integrity_check shared-$av.10.10
777do_test shared-$av.10.11 {
danielk1977b597f742006-01-15 11:39:18 +0000778 db close
779 db3 close
780} {}
781
danielk1977a96a7102006-01-16 12:46:41 +0000782}
783
danielk1977aef0bf62005-12-30 16:28:01 +0000784sqlite3_enable_shared_cache $::enable_shared_cache
danielk1977a96a7102006-01-16 12:46:41 +0000785finish_test