blob: 270ba1965a3dbc53710454e47bd87aae2ff1ac7c [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#
danielk1977ff890792006-01-16 16:24:25 +000012# $Id: shared.test,v 1.16 2006/01/16 16:24:25 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 {
216 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} {
230 db eval {DELETE FROM seq WHERE i = $i}
231 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);
415 CREATE VIEW test1.v1 AS SELECT * FROM t1;
416 CREATE TRIGGER test1.trig1 AFTER INSERT ON t1 BEGIN
417 INSERT INTO t1 VALUES(new.a, new.b);
418 END;
419 } db1
420 execsql {
421 DROP INDEX i1;
422 DROP VIEW v1;
423 DROP TRIGGER trig1;
424 DROP TABLE t1;
425 } db2
426} {}
danielk1977ff890792006-01-16 16:24:25 +0000427ifcapable compound {
428 do_test shared-$av.5.1.2 {
429 execsql {
430 SELECT * FROM sqlite_master UNION ALL SELECT * FROM test1.sqlite_master
431 } db1
432 } {}
433}
danielk1977aaf22682006-01-06 15:03:48 +0000434
danielk1977c00da102006-01-07 13:21:04 +0000435#--------------------------------------------------------------------------
436# Tests shared-6.* test that a query obtains all the read-locks it needs
437# before starting execution of the query. This means that there is no chance
438# some rows of data will be returned before a lock fails and SQLITE_LOCK
439# is returned.
440#
danielk1977a96a7102006-01-16 12:46:41 +0000441do_test shared-$av.6.1.1 {
danielk1977c00da102006-01-07 13:21:04 +0000442 execsql {
443 CREATE TABLE t1(a, b);
444 CREATE TABLE t2(a, b);
445 INSERT INTO t1 VALUES(1, 2);
446 INSERT INTO t2 VALUES(3, 4);
447 } db1
danielk1977ff890792006-01-16 16:24:25 +0000448} {}
449ifcapable compound {
450 do_test shared-$av.6.1.2 {
451 execsql {
452 SELECT * FROM t1 UNION ALL SELECT * FROM t2;
453 } db2
454 } {1 2 3 4}
455}
456do_test shared-$av.6.1.3 {
danielk1977c00da102006-01-07 13:21:04 +0000457 # Establish a write lock on table t2 via connection db2. Then make a
458 # UNION all query using connection db1 that first accesses t1, followed
459 # by t2. If the locks are grabbed at the start of the statement (as
460 # they should be), no rows are returned. If (as was previously the case)
461 # they are grabbed as the tables are accessed, the t1 rows will be
462 # returned before the query fails.
463 #
464 execsql {
465 BEGIN;
466 INSERT INTO t2 VALUES(5, 6);
467 } db2
468 set ret [list]
469 catch {
470 db1 eval {SELECT * FROM t1 UNION ALL SELECT * FROM t2} {
471 lappend ret $a $b
472 }
473 }
474 set ret
475} {}
danielk1977ff890792006-01-16 16:24:25 +0000476do_test shared-$av.6.1.4 {
danielk1977c00da102006-01-07 13:21:04 +0000477 execsql {
478 COMMIT;
479 BEGIN;
480 INSERT INTO t1 VALUES(7, 8);
481 } db2
482 set ret [list]
483 catch {
484 db1 eval {
485 SELECT (CASE WHEN a>4 THEN (SELECT a FROM t1) ELSE 0 END) AS d FROM t2;
486 } {
487 lappend ret $d
488 }
489 }
490 set ret
491} {}
492
danielk1977aaf22682006-01-06 15:03:48 +0000493catch {db1 close}
494catch {db2 close}
danielk1977e501b892006-01-09 06:29:47 +0000495foreach f [list test.db test2.db] {
496 file delete -force $f ${f}-journal
497}
498
499#--------------------------------------------------------------------------
500# Tests shared-7.* test auto-vacuum does not invalidate cursors from
501# other shared-cache users when it reorganizes the database on
502# COMMIT.
503#
danielk1977a96a7102006-01-16 12:46:41 +0000504do_test shared-$av.7.1 {
danielk197714db2662006-01-09 16:12:04 +0000505 # This test case sets up a test database in auto-vacuum mode consisting
506 # of two tables, t1 and t2. Both have a single index. Table t1 is
507 # populated first (so consists of pages toward the start of the db file),
508 # t2 second (pages toward the end of the file).
danielk1977e501b892006-01-09 06:29:47 +0000509 sqlite3 db test.db
510 sqlite3 db2 test.db
511 execsql {
danielk1977e501b892006-01-09 06:29:47 +0000512 BEGIN;
513 CREATE TABLE t1(a PRIMARY KEY, b);
514 CREATE TABLE t2(a PRIMARY KEY, b);
515 }
drh7a91dd82006-01-11 01:08:34 +0000516 set ::contents {}
danielk1977e501b892006-01-09 06:29:47 +0000517 for {set i 0} {$i < 100} {incr i} {
518 set a [string repeat "$i " 20]
519 set b [string repeat "$i " 20]
520 db eval {
521 INSERT INTO t1 VALUES($a, $b);
522 }
523 lappend ::contents [list [expr $i+1] $a $b]
524 }
525 execsql {
526 INSERT INTO t2 SELECT * FROM t1;
527 COMMIT;
528 }
danielk1977bab45c62006-01-16 15:14:27 +0000529} {}
danielk1977a96a7102006-01-16 12:46:41 +0000530do_test shared-$av.7.2 {
danielk197714db2662006-01-09 16:12:04 +0000531 # This test case deletes the contents of table t1 (the one at the start of
532 # the file) while many cursors are open on table t2 and it's index. All of
533 # the non-root pages will be moved from the end to the start of the file
534 # when the DELETE is committed - this test verifies that moving the pages
535 # does not disturb the open cursors.
536 #
537
danielk1977e501b892006-01-09 06:29:47 +0000538 proc lockrow {db tbl oids body} {
539 set ret [list]
540 db eval "SELECT oid AS i, a, b FROM $tbl ORDER BY a" {
541 if {$i==[lindex $oids 0]} {
542 set noids [lrange $oids 1 end]
543 if {[llength $noids]==0} {
544 set subret [eval $body]
545 } else {
546 set subret [lockrow $db $tbl $noids $body]
547 }
548 }
549 lappend ret [list $i $a $b]
550 }
551 return [linsert $subret 0 $ret]
552 }
553 proc locktblrows {db tbl body} {
554 set oids [db eval "SELECT oid FROM $tbl"]
555 lockrow $db $tbl $oids $body
556 }
557
558 set scans [locktblrows db t2 {
559 execsql {
560 DELETE FROM t1;
561 } db2
562 }]
563 set error 0
danielk197714db2662006-01-09 16:12:04 +0000564
565 # Test that each SELECT query returned the expected contents of t2.
danielk1977e501b892006-01-09 06:29:47 +0000566 foreach s $scans {
567 if {[lsort -integer -index 0 $s]!=$::contents} {
568 set error 1
569 }
570 }
571 set error
572} {0}
573
574catch {db close}
575catch {db2 close}
drh7a91dd82006-01-11 01:08:34 +0000576unset -nocomplain contents
danielk1977aaf22682006-01-06 15:03:48 +0000577
danielk197714db2662006-01-09 16:12:04 +0000578#--------------------------------------------------------------------------
579# The following tests try to trick the shared-cache code into assuming
580# the wrong encoding for a database.
581#
582file delete -force test.db test.db-journal
danielk1977a96a7102006-01-16 12:46:41 +0000583do_test shared-$av.8.1.1 {
danielk197714db2662006-01-09 16:12:04 +0000584 sqlite3 db test.db
585 execsql {
586 PRAGMA encoding = 'UTF-16';
587 SELECT * FROM sqlite_master;
588 }
589} {}
danielk1977a96a7102006-01-16 12:46:41 +0000590do_test shared-$av.8.1.2 {
danielk197714db2662006-01-09 16:12:04 +0000591 string range [execsql {PRAGMA encoding;}] 0 end-2
592} {UTF-16}
danielk1977a96a7102006-01-16 12:46:41 +0000593do_test shared-$av.8.1.3 {
danielk197714db2662006-01-09 16:12:04 +0000594 sqlite3 db2 test.db
595 execsql {
596 PRAGMA encoding = 'UTF-8';
597 CREATE TABLE abc(a, b, c);
598 } db2
599} {}
danielk1977a96a7102006-01-16 12:46:41 +0000600do_test shared-$av.8.1.4 {
danielk197714db2662006-01-09 16:12:04 +0000601 execsql {
602 SELECT * FROM sqlite_master;
603 }
604} "table abc abc [expr $AUTOVACUUM?3:2] {CREATE TABLE abc(a, b, c)}"
danielk1977a96a7102006-01-16 12:46:41 +0000605do_test shared-$av.8.1.5 {
danielk197714db2662006-01-09 16:12:04 +0000606 db2 close
607 execsql {
608 PRAGMA encoding;
609 }
610} {UTF-8}
611file delete -force test2.db test2.db-journal
danielk1977a96a7102006-01-16 12:46:41 +0000612do_test shared-$av.8.2.1 {
danielk197714db2662006-01-09 16:12:04 +0000613 execsql {
614 ATTACH 'test2.db' AS aux;
615 SELECT * FROM aux.sqlite_master;
616 }
617} {}
danielk1977a96a7102006-01-16 12:46:41 +0000618do_test shared-$av.8.2.2 {
danielk197714db2662006-01-09 16:12:04 +0000619 sqlite3 db2 test2.db
620 execsql {
621 PRAGMA encoding = 'UTF-16';
622 CREATE TABLE def(d, e, f);
623 } db2
624 string range [execsql {PRAGMA encoding;} db2] 0 end-2
625} {UTF-16}
danielk1977a96a7102006-01-16 12:46:41 +0000626do_test shared-$av.8.2.3 {
danielk197714db2662006-01-09 16:12:04 +0000627 catchsql {
628 SELECT * FROM aux.sqlite_master;
629 }
630} {1 {attached databases must use the same text encoding as main database}}
631
632catch {db close}
633catch {db2 close}
danielk1977eecfb3e2006-01-10 12:31:39 +0000634file delete -force test.db test2.db
635
danielk1977eecfb3e2006-01-10 12:31:39 +0000636#---------------------------------------------------------------------------
637# The following tests - shared-9.* - test interactions between TEMP triggers
638# and shared-schemas.
639#
640ifcapable trigger&&tempdb {
641
danielk1977a96a7102006-01-16 12:46:41 +0000642do_test shared-$av.9.1 {
danielk1977eecfb3e2006-01-10 12:31:39 +0000643 sqlite3 db test.db
644 sqlite3 db2 test.db
645 execsql {
646 CREATE TABLE abc(a, b, c);
647 CREATE TABLE abc_mirror(a, b, c);
648 CREATE TEMP TRIGGER BEFORE INSERT ON abc BEGIN
649 INSERT INTO abc_mirror(a, b, c) VALUES(new.a, new.b, new.c);
650 END;
651 INSERT INTO abc VALUES(1, 2, 3);
652 SELECT * FROM abc_mirror;
653 }
654} {1 2 3}
danielk1977a96a7102006-01-16 12:46:41 +0000655do_test shared-$av.9.2 {
danielk1977eecfb3e2006-01-10 12:31:39 +0000656 execsql {
657 INSERT INTO abc VALUES(4, 5, 6);
658 SELECT * FROM abc_mirror;
659 } db2
660} {1 2 3}
danielk1977a96a7102006-01-16 12:46:41 +0000661do_test shared-$av.9.3 {
danielk1977eecfb3e2006-01-10 12:31:39 +0000662 db close
663 db2 close
664} {}
665
666} ; # End shared-9.*
danielk197714db2662006-01-09 16:12:04 +0000667
danielk1977b597f742006-01-15 11:39:18 +0000668#---------------------------------------------------------------------------
669# The following tests - shared-10.* - test that the library behaves
670# correctly when a connection to a shared-cache is closed.
671#
danielk1977a96a7102006-01-16 12:46:41 +0000672do_test shared-$av.10.1 {
danielk1977b597f742006-01-15 11:39:18 +0000673 # Create a small sample database with two connections to it (db and db2).
674 file delete -force test.db
675 sqlite3 db test.db
676 sqlite3 db2 test.db
677 execsql {
678 CREATE TABLE ab(a PRIMARY KEY, b);
679 CREATE TABLE de(d PRIMARY KEY, e);
680 INSERT INTO ab VALUES('Chiang Mai', 100000);
681 INSERT INTO ab VALUES('Bangkok', 8000000);
682 INSERT INTO de VALUES('Ubon', 120000);
683 INSERT INTO de VALUES('Khon Kaen', 200000);
684 }
685} {}
danielk1977a96a7102006-01-16 12:46:41 +0000686do_test shared-$av.10.2 {
danielk1977b597f742006-01-15 11:39:18 +0000687 # Open a read-transaction with the first connection, a write-transaction
688 # with the second.
689 execsql {
690 BEGIN;
691 SELECT * FROM ab;
692 }
693 execsql {
694 BEGIN;
695 INSERT INTO de VALUES('Pataya', 30000);
696 } db2
697} {}
danielk1977a96a7102006-01-16 12:46:41 +0000698do_test shared-$av.10.3 {
danielk1977b597f742006-01-15 11:39:18 +0000699 # An external connection should be able to read the database, but not
700 # prepare a write operation.
701 sqlite3 db3 ./test.db
702 execsql {
703 SELECT * FROM ab;
704 } db3
705 catchsql {
706 BEGIN;
707 INSERT INTO de VALUES('Pataya', 30000);
708 } db3
709} {1 {database is locked}}
danielk1977a96a7102006-01-16 12:46:41 +0000710do_test shared-$av.10.4 {
danielk1977b597f742006-01-15 11:39:18 +0000711 # Close the connection with the write-transaction open
712 db2 close
713} {}
danielk1977a96a7102006-01-16 12:46:41 +0000714do_test shared-$av.10.5 {
danielk1977b597f742006-01-15 11:39:18 +0000715 # Test that the db2 transaction has been automatically rolled back.
716 # If it has not the ('Pataya', 30000) entry will still be in the table.
717 execsql {
718 SELECT * FROM de;
719 }
720} {Ubon 120000 {Khon Kaen} 200000}
danielk1977a96a7102006-01-16 12:46:41 +0000721do_test shared-$av.10.5 {
danielk1977b597f742006-01-15 11:39:18 +0000722 # Closing db2 should have dropped the shared-cache back to a read-lock.
723 # So db3 should be able to prepare a write...
724 catchsql {INSERT INTO de VALUES('Pataya', 30000);} db3
725} {0 {}}
danielk1977a96a7102006-01-16 12:46:41 +0000726do_test shared-$av.10.6 {
danielk1977b597f742006-01-15 11:39:18 +0000727 # ... but not commit it.
728 catchsql {COMMIT} db3
729} {1 {database is locked}}
danielk1977a96a7102006-01-16 12:46:41 +0000730do_test shared-$av.10.7 {
danielk1977b597f742006-01-15 11:39:18 +0000731 # Commit the (read-only) db transaction. Check via db3 to make sure the
732 # contents of table "de" are still as they should be.
733 execsql {
734 COMMIT;
735 }
736 execsql {
737 SELECT * FROM de;
738 } db3
739} {Ubon 120000 {Khon Kaen} 200000 Pataya 30000}
danielk1977a96a7102006-01-16 12:46:41 +0000740do_test shared-$av.10.9 {
danielk1977b597f742006-01-15 11:39:18 +0000741 # Commit the external transaction.
742 catchsql {COMMIT} db3
743} {0 {}}
danielk1977a96a7102006-01-16 12:46:41 +0000744integrity_check shared-$av.10.10
745do_test shared-$av.10.11 {
danielk1977b597f742006-01-15 11:39:18 +0000746 db close
747 db3 close
748} {}
749
danielk1977a96a7102006-01-16 12:46:41 +0000750}
751
danielk1977aef0bf62005-12-30 16:28:01 +0000752sqlite3_enable_shared_cache $::enable_shared_cache
danielk1977a96a7102006-01-16 12:46:41 +0000753finish_test