blob: 169932c40779a8c751909b96ddec4bb166bbac95 [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#
danielk1977a96a7102006-01-16 12:46:41 +000012# $Id: shared.test,v 1.14 2006/01/16 12:46:41 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
27ifcapable !autovacuum {
28 if {$av} break
29}
30
31# Open the database connection and execute the auto-vacuum pragma
32do_test shared-$av.1.0 {
33 file delete -force test.db
34 sqlite3 db test.db
35 execsql "pragma auto_vacuum=$::av"
36 execsql {pragma auto_vacuum}
37} "$av"
38incr av
39
danielk1977aef0bf62005-12-30 16:28:01 +000040# Test organization:
41#
42# shared-1.*: Simple test to verify basic sanity of table level locking when
43# two connections share a pager cache.
44# shared-2.*: Test that a read transaction can co-exist with a
45# write-transaction, including a simple test to ensure the
46# external locking protocol is still working.
danielk1977da184232006-01-05 11:34:32 +000047# shared-3.*: Simple test of read-uncommitted mode.
danielk1977de0fe3e2006-01-06 06:33:12 +000048# shared-4.*: Check that the schema is locked and unlocked correctly.
danielk1977aaf22682006-01-06 15:03:48 +000049# shared-5.*: Test that creating/dropping schema items works when databases
50# are attached in different orders to different handles.
danielk1977c00da102006-01-07 13:21:04 +000051# shared-6.*: Locking, UNION ALL queries and sub-queries.
danielk197714db2662006-01-09 16:12:04 +000052# shared-7.*: Autovacuum and shared-cache.
danielk1977de0fe3e2006-01-06 06:33:12 +000053#
danielk1977aef0bf62005-12-30 16:28:01 +000054
danielk1977a96a7102006-01-16 12:46:41 +000055do_test shared-$av.1.1 {
danielk1977aef0bf62005-12-30 16:28:01 +000056 # Open a second database on the file test.db. It should use the same pager
57 # cache and schema as the original connection. Verify that only 1 file is
58 # opened.
59 sqlite3 db2 test.db
danielk1977aef0bf62005-12-30 16:28:01 +000060 set ::sqlite_open_file_count
61} {1}
danielk1977a96a7102006-01-16 12:46:41 +000062do_test shared-$av.1.2 {
danielk1977aef0bf62005-12-30 16:28:01 +000063 # Add a table and a single row of data via the first connection.
64 # Ensure that the second connection can see them.
65 execsql {
66 CREATE TABLE abc(a, b, c);
67 INSERT INTO abc VALUES(1, 2, 3);
68 } db
69 execsql {
70 SELECT * FROM abc;
71 } db2
72} {1 2 3}
danielk1977a96a7102006-01-16 12:46:41 +000073do_test shared-$av.1.3 {
danielk1977aef0bf62005-12-30 16:28:01 +000074 # Have the first connection begin a transaction and obtain a read-lock
75 # on table abc. This should not prevent the second connection from
76 # querying abc.
77 execsql {
78 BEGIN;
79 SELECT * FROM abc;
80 }
81 execsql {
82 SELECT * FROM abc;
83 } db2
84} {1 2 3}
danielk1977a96a7102006-01-16 12:46:41 +000085do_test shared-$av.1.4 {
danielk1977aef0bf62005-12-30 16:28:01 +000086 # Try to insert a row into abc via connection 2. This should fail because
87 # of the read-lock connection 1 is holding on table abc (obtained in the
88 # previous test case).
89 catchsql {
90 INSERT INTO abc VALUES(4, 5, 6);
91 } db2
danielk1977c00da102006-01-07 13:21:04 +000092} {1 {database table is locked: abc}}
danielk1977a96a7102006-01-16 12:46:41 +000093do_test shared-$av.1.5 {
danielk1977da184232006-01-05 11:34:32 +000094 # Using connection 2 (the one without the open transaction), try to create
95 # a new table. This should fail because of the open read transaction
96 # held by connection 1.
97 catchsql {
98 CREATE TABLE def(d, e, f);
99 } db2
danielk1977c00da102006-01-07 13:21:04 +0000100} {1 {database table is locked: sqlite_master}}
danielk1977a96a7102006-01-16 12:46:41 +0000101do_test shared-$av.1.6 {
danielk1977da184232006-01-05 11:34:32 +0000102 # Upgrade connection 1's transaction to a write transaction. Create
103 # a new table - def - and insert a row into it. Because the connection 1
104 # transaction modifies the schema, it should not be possible for
105 # connection 2 to access the database at all until the connection 1
106 # has finished the transaction.
danielk1977aef0bf62005-12-30 16:28:01 +0000107 execsql {
108 CREATE TABLE def(d, e, f);
danielk1977aef0bf62005-12-30 16:28:01 +0000109 INSERT INTO def VALUES('IV', 'V', 'VI');
110 }
111} {}
danielk1977a96a7102006-01-16 12:46:41 +0000112do_test shared-$av.1.7 {
danielk1977aef0bf62005-12-30 16:28:01 +0000113 # Read from the sqlite_master table with connection 1 (inside the
danielk1977da184232006-01-05 11:34:32 +0000114 # transaction). Then test that we can not do this with connection 2. This
115 # is because of the schema-modified lock established by connection 1
116 # in the previous test case.
danielk1977aef0bf62005-12-30 16:28:01 +0000117 execsql {
118 SELECT * FROM sqlite_master;
119 }
120 catchsql {
danielk1977da184232006-01-05 11:34:32 +0000121 SELECT * FROM sqlite_master;
danielk1977aef0bf62005-12-30 16:28:01 +0000122 } db2
danielk1977c87d34d2006-01-06 13:00:28 +0000123} {1 {database schema is locked: main}}
danielk1977a96a7102006-01-16 12:46:41 +0000124do_test shared-$av.1.8 {
danielk1977aef0bf62005-12-30 16:28:01 +0000125 # Commit the connection 1 transaction.
126 execsql {
127 COMMIT;
128 }
129} {}
130
danielk1977a96a7102006-01-16 12:46:41 +0000131do_test shared-$av.2.1 {
danielk1977aef0bf62005-12-30 16:28:01 +0000132 # Open connection db3 to the database. Use a different path to the same
133 # file so that db3 does *not* share the same pager cache as db and db2
134 # (there should be two open file handles).
135 sqlite3 db3 ./test.db
136 set ::sqlite_open_file_count
137} {2}
danielk1977a96a7102006-01-16 12:46:41 +0000138do_test shared-$av.2.2 {
danielk1977aef0bf62005-12-30 16:28:01 +0000139 # Start read transactions on db and db2 (the shared pager cache). Ensure
140 # db3 cannot write to the database.
141 execsql {
142 BEGIN;
143 SELECT * FROM abc;
144 }
145 execsql {
146 BEGIN;
147 SELECT * FROM abc;
148 } db2
149 catchsql {
150 INSERT INTO abc VALUES(1, 2, 3);
151 } db2
danielk1977c00da102006-01-07 13:21:04 +0000152} {1 {database table is locked: abc}}
danielk1977a96a7102006-01-16 12:46:41 +0000153do_test shared-$av.2.3 {
danielk1977aef0bf62005-12-30 16:28:01 +0000154 # Turn db's transaction into a write-transaction. db3 should still be
155 # able to read from table def (but will not see the new row). Connection
156 # db2 should not be able to read def (because of the write-lock).
157
158# Todo: The failed "INSERT INTO abc ..." statement in the above test
159# has started a write-transaction on db2 (should this be so?). This
160# would prevent connection db from starting a write-transaction. So roll the
161# db2 transaction back and replace it with a new read transaction.
162 execsql {
163 ROLLBACK;
164 BEGIN;
165 SELECT * FROM abc;
166 } db2
167
168 execsql {
169 INSERT INTO def VALUES('VII', 'VIII', 'IX');
170 }
171 concat [
172 catchsql { SELECT * FROM def; } db3
173 ] [
174 catchsql { SELECT * FROM def; } db2
175 ]
danielk1977c00da102006-01-07 13:21:04 +0000176} {0 {IV V VI} 1 {database table is locked: def}}
danielk1977a96a7102006-01-16 12:46:41 +0000177do_test shared-$av.2.4 {
danielk1977aef0bf62005-12-30 16:28:01 +0000178 # Commit the open transaction on db. db2 still holds a read-transaction.
179 # This should prevent db3 from writing to the database, but not from
180 # reading.
181 execsql {
182 COMMIT;
183 }
184 concat [
185 catchsql { SELECT * FROM def; } db3
186 ] [
187 catchsql { INSERT INTO def VALUES('X', 'XI', 'XII'); } db3
188 ]
danielk1977da184232006-01-05 11:34:32 +0000189} {0 {IV V VI VII VIII IX} 1 {database is locked}}
danielk1977aef0bf62005-12-30 16:28:01 +0000190
danielk1977da184232006-01-05 11:34:32 +0000191catchsql COMMIT db2
192
danielk1977a96a7102006-01-16 12:46:41 +0000193do_test shared-$av.3.1.1 {
danielk1977da184232006-01-05 11:34:32 +0000194 # This test case starts a linear scan of table 'seq' using a
195 # read-uncommitted connection. In the middle of the scan, rows are added
196 # to the end of the seq table (ahead of the current cursor position).
197 # The uncommitted rows should be included in the results of the scan.
198 execsql "
199 CREATE TABLE seq(i, x);
200 INSERT INTO seq VALUES(1, '[string repeat X 500]');
201 INSERT INTO seq VALUES(2, '[string repeat X 500]');
202 "
203 execsql {SELECT * FROM sqlite_master} db2
204 execsql {PRAGMA read_uncommitted = 1} db2
205
206 set ret [list]
207 db2 eval {SELECT i FROM seq} {
208 if {$i < 4} {
danielk19771576cd92006-01-14 08:02:28 +0000209 set max [execsql {SELECT max(i) FROM seq}]
210 db eval {
211 INSERT INTO seq SELECT i + $max, x FROM seq;
danielk1977da184232006-01-05 11:34:32 +0000212 }
213 }
214 lappend ret $i
215 }
216 set ret
217} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16}
danielk1977a96a7102006-01-16 12:46:41 +0000218do_test shared-$av.3.1.2 {
danielk1977da184232006-01-05 11:34:32 +0000219 # Another linear scan through table seq using a read-uncommitted connection.
220 # This time, delete each row as it is read. Should not affect the results of
221 # the scan, but the table should be empty after the scan is concluded
222 # (test 3.1.3 verifies this).
223 set ret [list]
224 db2 eval {SELECT i FROM seq} {
225 db eval {DELETE FROM seq WHERE i = $i}
226 lappend ret $i
227 }
228 set ret
229} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16}
danielk1977a96a7102006-01-16 12:46:41 +0000230do_test shared-$av.3.1.3 {
danielk1977da184232006-01-05 11:34:32 +0000231 execsql {
232 SELECT * FROM seq;
233 }
234} {}
danielk1977aef0bf62005-12-30 16:28:01 +0000235
236catch {db close}
237catch {db2 close}
238catch {db3 close}
239
danielk1977de0fe3e2006-01-06 06:33:12 +0000240#--------------------------------------------------------------------------
241# Tests shared-4.* test that the schema locking rules are applied
242# correctly. i.e.:
243#
244# 1. All transactions require a read-lock on the schemas of databases they
245# access.
246# 2. Transactions that modify a database schema require a write-lock on that
247# schema.
248# 3. It is not possible to compile a statement while another handle has a
249# write-lock on the schema.
250#
251
252# Open two database handles db and db2. Each has a single attach database
253# (as well as main):
254#
255# db.main -> ./test.db
256# db.test2 -> ./test2.db
257# db2.main -> ./test2.db
258# db2.test -> ./test.db
259#
260file delete -force test.db
261file delete -force test2.db
262file delete -force test2.db-journal
263sqlite3 db test.db
264sqlite3 db2 test2.db
danielk1977a96a7102006-01-16 12:46:41 +0000265do_test shared-$av.4.1.1 {
danielk1977de0fe3e2006-01-06 06:33:12 +0000266 set sqlite_open_file_count
267} {2}
danielk1977a96a7102006-01-16 12:46:41 +0000268do_test shared-$av.4.1.2 {
danielk1977de0fe3e2006-01-06 06:33:12 +0000269 execsql {ATTACH 'test2.db' AS test2}
270 set sqlite_open_file_count
271} {2}
danielk1977a96a7102006-01-16 12:46:41 +0000272do_test shared-$av.4.1.3 {
danielk1977de0fe3e2006-01-06 06:33:12 +0000273 execsql {ATTACH 'test.db' AS test} db2
274 set sqlite_open_file_count
275} {2}
276
danielk1977c87d34d2006-01-06 13:00:28 +0000277# Sanity check: Create a table in ./test.db via handle db, and test that handle
278# db2 can "see" the new table immediately. A handle using a seperate pager
279# cache would have to reload the database schema before this were possible.
280#
danielk1977a96a7102006-01-16 12:46:41 +0000281do_test shared-$av.4.2.1 {
danielk1977de0fe3e2006-01-06 06:33:12 +0000282 execsql {
283 CREATE TABLE abc(a, b, c);
danielk1977c87d34d2006-01-06 13:00:28 +0000284 CREATE TABLE def(d, e, f);
danielk1977de0fe3e2006-01-06 06:33:12 +0000285 INSERT INTO abc VALUES('i', 'ii', 'iii');
danielk1977c87d34d2006-01-06 13:00:28 +0000286 INSERT INTO def VALUES('I', 'II', 'III');
danielk1977de0fe3e2006-01-06 06:33:12 +0000287 }
288} {}
danielk1977a96a7102006-01-16 12:46:41 +0000289do_test shared-$av.4.2.2 {
danielk1977de0fe3e2006-01-06 06:33:12 +0000290 execsql {
291 SELECT * FROM test.abc;
292 } db2
293} {i ii iii}
294
danielk1977c87d34d2006-01-06 13:00:28 +0000295# Open a read-transaction and read from table abc via handle 2. Check that
296# handle 1 can read table abc. Check that handle 1 cannot modify table abc
297# or the database schema. Then check that handle 1 can modify table def.
298#
danielk1977a96a7102006-01-16 12:46:41 +0000299do_test shared-$av.4.3.1 {
danielk1977c87d34d2006-01-06 13:00:28 +0000300 execsql {
301 BEGIN;
302 SELECT * FROM test.abc;
303 } db2
304} {i ii iii}
danielk1977a96a7102006-01-16 12:46:41 +0000305do_test shared-$av.4.3.2 {
danielk1977c87d34d2006-01-06 13:00:28 +0000306 catchsql {
307 INSERT INTO abc VALUES('iv', 'v', 'vi');
308 }
danielk1977c00da102006-01-07 13:21:04 +0000309} {1 {database table is locked: abc}}
danielk1977a96a7102006-01-16 12:46:41 +0000310do_test shared-$av.4.3.3 {
danielk1977c87d34d2006-01-06 13:00:28 +0000311 catchsql {
312 CREATE TABLE ghi(g, h, i);
313 }
danielk1977c00da102006-01-07 13:21:04 +0000314} {1 {database table is locked: sqlite_master}}
danielk1977a96a7102006-01-16 12:46:41 +0000315do_test shared-$av.4.3.3 {
danielk1977c87d34d2006-01-06 13:00:28 +0000316 catchsql {
317 INSERT INTO def VALUES('IV', 'V', 'VI');
318 }
319} {0 {}}
danielk1977a96a7102006-01-16 12:46:41 +0000320do_test shared-$av.4.3.4 {
danielk1977c87d34d2006-01-06 13:00:28 +0000321 # Cleanup: commit the transaction opened by db2.
322 execsql {
323 COMMIT
324 } db2
325} {}
326
327# Open a write-transaction using handle 1 and modify the database schema.
328# Then try to execute a compiled statement to read from the same
329# database via handle 2 (fails to get the lock on sqlite_master). Also
330# try to compile a read of the same database using handle 2 (also fails).
331# Finally, compile a read of the other database using handle 2. This
332# should also fail.
333#
danielk1977a96a7102006-01-16 12:46:41 +0000334do_test shared-$av.4.4.1.2 {
danielk1977c87d34d2006-01-06 13:00:28 +0000335 # Sanity check 1: Check that the schema is what we think it is when viewed
336 # via handle 1.
337 execsql {
338 CREATE TABLE test2.ghi(g, h, i);
339 SELECT 'test.db:'||name FROM sqlite_master
340 UNION ALL
341 SELECT 'test2.db:'||name FROM test2.sqlite_master;
342 }
343} {test.db:abc test.db:def test2.db:ghi}
danielk1977a96a7102006-01-16 12:46:41 +0000344do_test shared-$av.4.4.1.2 {
danielk1977c87d34d2006-01-06 13:00:28 +0000345 # Sanity check 2: Check that the schema is what we think it is when viewed
346 # via handle 2.
347 execsql {
348 SELECT 'test2.db:'||name FROM sqlite_master
349 UNION ALL
350 SELECT 'test.db:'||name FROM test.sqlite_master;
351 } db2
352} {test2.db:ghi test.db:abc test.db:def}
353
danielk1977a96a7102006-01-16 12:46:41 +0000354do_test shared-$av.4.4.2 {
danielk1977c87d34d2006-01-06 13:00:28 +0000355 set ::DB2 [sqlite3_connection_pointer db2]
356 set sql {SELECT * FROM abc}
357 set ::STMT1 [sqlite3_prepare $::DB2 $sql -1 DUMMY]
358 execsql {
359 BEGIN;
360 CREATE TABLE jkl(j, k, l);
361 }
362 sqlite3_step $::STMT1
363} {SQLITE_ERROR}
danielk1977a96a7102006-01-16 12:46:41 +0000364do_test shared-$av.4.4.3 {
danielk1977c87d34d2006-01-06 13:00:28 +0000365 sqlite3_finalize $::STMT1
366} {SQLITE_LOCKED}
danielk1977a96a7102006-01-16 12:46:41 +0000367do_test shared-$av.4.4.4 {
danielk1977c87d34d2006-01-06 13:00:28 +0000368 set rc [catch {
369 set ::STMT1 [sqlite3_prepare $::DB2 $sql -1 DUMMY]
370 } msg]
371 list $rc $msg
372} {1 {(6) database schema is locked: test}}
danielk1977a96a7102006-01-16 12:46:41 +0000373do_test shared-$av.4.4.5 {
danielk1977c87d34d2006-01-06 13:00:28 +0000374 set rc [catch {
375 set ::STMT1 [sqlite3_prepare $::DB2 "SELECT * FROM ghi" -1 DUMMY]
376 } msg]
377 list $rc $msg
378} {1 {(6) database schema is locked: test}}
379
danielk1977aaf22682006-01-06 15:03:48 +0000380
danielk1977de0fe3e2006-01-06 06:33:12 +0000381catch {db2 close}
382catch {db close}
383
danielk1977aaf22682006-01-06 15:03:48 +0000384#--------------------------------------------------------------------------
385# Tests shared-5.*
386#
387foreach db [list test.db test1.db test2.db test3.db] {
388 file delete -force $db ${db}-journal
389}
danielk1977a96a7102006-01-16 12:46:41 +0000390do_test shared-$av.5.1.1 {
danielk1977aaf22682006-01-06 15:03:48 +0000391 sqlite3 db1 test.db
392 sqlite3 db2 test.db
393 execsql {
394 ATTACH 'test1.db' AS test1;
395 ATTACH 'test2.db' AS test2;
396 ATTACH 'test3.db' AS test3;
397 } db1
398 execsql {
399 ATTACH 'test3.db' AS test3;
400 ATTACH 'test2.db' AS test2;
401 ATTACH 'test1.db' AS test1;
402 } db2
403} {}
danielk1977a96a7102006-01-16 12:46:41 +0000404do_test shared-$av.5.1.2 {
danielk1977aaf22682006-01-06 15:03:48 +0000405 execsql {
406 CREATE TABLE test1.t1(a, b);
407 CREATE INDEX test1.i1 ON t1(a, b);
408 CREATE VIEW test1.v1 AS SELECT * FROM t1;
409 CREATE TRIGGER test1.trig1 AFTER INSERT ON t1 BEGIN
410 INSERT INTO t1 VALUES(new.a, new.b);
411 END;
412 } db1
413 execsql {
414 DROP INDEX i1;
415 DROP VIEW v1;
416 DROP TRIGGER trig1;
417 DROP TABLE t1;
418 } db2
419} {}
danielk1977a96a7102006-01-16 12:46:41 +0000420do_test shared-$av.5.1.2 {
danielk1977aaf22682006-01-06 15:03:48 +0000421 execsql {
422 SELECT * FROM sqlite_master UNION ALL SELECT * FROM test1.sqlite_master
423 } db1
424} {}
425
danielk1977c00da102006-01-07 13:21:04 +0000426#--------------------------------------------------------------------------
427# Tests shared-6.* test that a query obtains all the read-locks it needs
428# before starting execution of the query. This means that there is no chance
429# some rows of data will be returned before a lock fails and SQLITE_LOCK
430# is returned.
431#
danielk1977a96a7102006-01-16 12:46:41 +0000432do_test shared-$av.6.1.1 {
danielk1977c00da102006-01-07 13:21:04 +0000433 execsql {
434 CREATE TABLE t1(a, b);
435 CREATE TABLE t2(a, b);
436 INSERT INTO t1 VALUES(1, 2);
437 INSERT INTO t2 VALUES(3, 4);
438 } db1
439 execsql {
440 SELECT * FROM t1 UNION ALL SELECT * FROM t2;
441 } db2
442} {1 2 3 4}
danielk1977a96a7102006-01-16 12:46:41 +0000443do_test shared-$av.6.1.2 {
danielk1977c00da102006-01-07 13:21:04 +0000444 # Establish a write lock on table t2 via connection db2. Then make a
445 # UNION all query using connection db1 that first accesses t1, followed
446 # by t2. If the locks are grabbed at the start of the statement (as
447 # they should be), no rows are returned. If (as was previously the case)
448 # they are grabbed as the tables are accessed, the t1 rows will be
449 # returned before the query fails.
450 #
451 execsql {
452 BEGIN;
453 INSERT INTO t2 VALUES(5, 6);
454 } db2
455 set ret [list]
456 catch {
457 db1 eval {SELECT * FROM t1 UNION ALL SELECT * FROM t2} {
458 lappend ret $a $b
459 }
460 }
461 set ret
462} {}
danielk1977a96a7102006-01-16 12:46:41 +0000463do_test shared-$av.6.1.3 {
danielk1977c00da102006-01-07 13:21:04 +0000464 execsql {
465 COMMIT;
466 BEGIN;
467 INSERT INTO t1 VALUES(7, 8);
468 } db2
469 set ret [list]
470 catch {
471 db1 eval {
472 SELECT (CASE WHEN a>4 THEN (SELECT a FROM t1) ELSE 0 END) AS d FROM t2;
473 } {
474 lappend ret $d
475 }
476 }
477 set ret
478} {}
479
danielk1977aaf22682006-01-06 15:03:48 +0000480catch {db1 close}
481catch {db2 close}
danielk1977e501b892006-01-09 06:29:47 +0000482foreach f [list test.db test2.db] {
483 file delete -force $f ${f}-journal
484}
485
486#--------------------------------------------------------------------------
487# Tests shared-7.* test auto-vacuum does not invalidate cursors from
488# other shared-cache users when it reorganizes the database on
489# COMMIT.
490#
danielk1977a96a7102006-01-16 12:46:41 +0000491do_test shared-$av.7.1 {
danielk197714db2662006-01-09 16:12:04 +0000492 # This test case sets up a test database in auto-vacuum mode consisting
493 # of two tables, t1 and t2. Both have a single index. Table t1 is
494 # populated first (so consists of pages toward the start of the db file),
495 # t2 second (pages toward the end of the file).
danielk1977e501b892006-01-09 06:29:47 +0000496 sqlite3 db test.db
497 sqlite3 db2 test.db
498 execsql {
499 PRAGMA auto_vacuum = 1;
500 BEGIN;
501 CREATE TABLE t1(a PRIMARY KEY, b);
502 CREATE TABLE t2(a PRIMARY KEY, b);
503 }
drh7a91dd82006-01-11 01:08:34 +0000504 set ::contents {}
danielk1977e501b892006-01-09 06:29:47 +0000505 for {set i 0} {$i < 100} {incr i} {
506 set a [string repeat "$i " 20]
507 set b [string repeat "$i " 20]
508 db eval {
509 INSERT INTO t1 VALUES($a, $b);
510 }
511 lappend ::contents [list [expr $i+1] $a $b]
512 }
513 execsql {
514 INSERT INTO t2 SELECT * FROM t1;
515 COMMIT;
516 }
517 execsql {
518 PRAGMA auto_vacuum;
519 }
520} {1}
danielk1977a96a7102006-01-16 12:46:41 +0000521do_test shared-$av.7.2 {
danielk197714db2662006-01-09 16:12:04 +0000522 # This test case deletes the contents of table t1 (the one at the start of
523 # the file) while many cursors are open on table t2 and it's index. All of
524 # the non-root pages will be moved from the end to the start of the file
525 # when the DELETE is committed - this test verifies that moving the pages
526 # does not disturb the open cursors.
527 #
528
danielk1977e501b892006-01-09 06:29:47 +0000529 proc lockrow {db tbl oids body} {
530 set ret [list]
531 db eval "SELECT oid AS i, a, b FROM $tbl ORDER BY a" {
532 if {$i==[lindex $oids 0]} {
533 set noids [lrange $oids 1 end]
534 if {[llength $noids]==0} {
535 set subret [eval $body]
536 } else {
537 set subret [lockrow $db $tbl $noids $body]
538 }
539 }
540 lappend ret [list $i $a $b]
541 }
542 return [linsert $subret 0 $ret]
543 }
544 proc locktblrows {db tbl body} {
545 set oids [db eval "SELECT oid FROM $tbl"]
546 lockrow $db $tbl $oids $body
547 }
548
549 set scans [locktblrows db t2 {
550 execsql {
551 DELETE FROM t1;
552 } db2
553 }]
554 set error 0
danielk197714db2662006-01-09 16:12:04 +0000555
556 # Test that each SELECT query returned the expected contents of t2.
danielk1977e501b892006-01-09 06:29:47 +0000557 foreach s $scans {
558 if {[lsort -integer -index 0 $s]!=$::contents} {
559 set error 1
560 }
561 }
562 set error
563} {0}
564
565catch {db close}
566catch {db2 close}
drh7a91dd82006-01-11 01:08:34 +0000567unset -nocomplain contents
danielk1977aaf22682006-01-06 15:03:48 +0000568
danielk197714db2662006-01-09 16:12:04 +0000569#--------------------------------------------------------------------------
570# The following tests try to trick the shared-cache code into assuming
571# the wrong encoding for a database.
572#
573file delete -force test.db test.db-journal
danielk1977a96a7102006-01-16 12:46:41 +0000574do_test shared-$av.8.1.1 {
danielk197714db2662006-01-09 16:12:04 +0000575 sqlite3 db test.db
576 execsql {
577 PRAGMA encoding = 'UTF-16';
578 SELECT * FROM sqlite_master;
579 }
580} {}
danielk1977a96a7102006-01-16 12:46:41 +0000581do_test shared-$av.8.1.2 {
danielk197714db2662006-01-09 16:12:04 +0000582 string range [execsql {PRAGMA encoding;}] 0 end-2
583} {UTF-16}
danielk1977a96a7102006-01-16 12:46:41 +0000584do_test shared-$av.8.1.3 {
danielk197714db2662006-01-09 16:12:04 +0000585 sqlite3 db2 test.db
586 execsql {
587 PRAGMA encoding = 'UTF-8';
588 CREATE TABLE abc(a, b, c);
589 } db2
590} {}
danielk1977a96a7102006-01-16 12:46:41 +0000591do_test shared-$av.8.1.4 {
danielk197714db2662006-01-09 16:12:04 +0000592 execsql {
593 SELECT * FROM sqlite_master;
594 }
595} "table abc abc [expr $AUTOVACUUM?3:2] {CREATE TABLE abc(a, b, c)}"
danielk1977a96a7102006-01-16 12:46:41 +0000596do_test shared-$av.8.1.5 {
danielk197714db2662006-01-09 16:12:04 +0000597 db2 close
598 execsql {
599 PRAGMA encoding;
600 }
601} {UTF-8}
602file delete -force test2.db test2.db-journal
danielk1977a96a7102006-01-16 12:46:41 +0000603do_test shared-$av.8.2.1 {
danielk197714db2662006-01-09 16:12:04 +0000604 execsql {
605 ATTACH 'test2.db' AS aux;
606 SELECT * FROM aux.sqlite_master;
607 }
608} {}
danielk1977a96a7102006-01-16 12:46:41 +0000609do_test shared-$av.8.2.2 {
danielk197714db2662006-01-09 16:12:04 +0000610 sqlite3 db2 test2.db
611 execsql {
612 PRAGMA encoding = 'UTF-16';
613 CREATE TABLE def(d, e, f);
614 } db2
615 string range [execsql {PRAGMA encoding;} db2] 0 end-2
616} {UTF-16}
danielk1977a96a7102006-01-16 12:46:41 +0000617do_test shared-$av.8.2.3 {
danielk197714db2662006-01-09 16:12:04 +0000618 catchsql {
619 SELECT * FROM aux.sqlite_master;
620 }
621} {1 {attached databases must use the same text encoding as main database}}
622
623catch {db close}
624catch {db2 close}
danielk1977eecfb3e2006-01-10 12:31:39 +0000625file delete -force test.db test2.db
626
danielk1977eecfb3e2006-01-10 12:31:39 +0000627#---------------------------------------------------------------------------
628# The following tests - shared-9.* - test interactions between TEMP triggers
629# and shared-schemas.
630#
631ifcapable trigger&&tempdb {
632
danielk1977a96a7102006-01-16 12:46:41 +0000633do_test shared-$av.9.1 {
danielk1977eecfb3e2006-01-10 12:31:39 +0000634 sqlite3 db test.db
635 sqlite3 db2 test.db
636 execsql {
637 CREATE TABLE abc(a, b, c);
638 CREATE TABLE abc_mirror(a, b, c);
639 CREATE TEMP TRIGGER BEFORE INSERT ON abc BEGIN
640 INSERT INTO abc_mirror(a, b, c) VALUES(new.a, new.b, new.c);
641 END;
642 INSERT INTO abc VALUES(1, 2, 3);
643 SELECT * FROM abc_mirror;
644 }
645} {1 2 3}
danielk1977a96a7102006-01-16 12:46:41 +0000646do_test shared-$av.9.2 {
danielk1977eecfb3e2006-01-10 12:31:39 +0000647 execsql {
648 INSERT INTO abc VALUES(4, 5, 6);
649 SELECT * FROM abc_mirror;
650 } db2
651} {1 2 3}
danielk1977a96a7102006-01-16 12:46:41 +0000652do_test shared-$av.9.3 {
danielk1977eecfb3e2006-01-10 12:31:39 +0000653 db close
654 db2 close
655} {}
656
657} ; # End shared-9.*
danielk197714db2662006-01-09 16:12:04 +0000658
danielk1977b597f742006-01-15 11:39:18 +0000659#---------------------------------------------------------------------------
660# The following tests - shared-10.* - test that the library behaves
661# correctly when a connection to a shared-cache is closed.
662#
danielk1977a96a7102006-01-16 12:46:41 +0000663do_test shared-$av.10.1 {
danielk1977b597f742006-01-15 11:39:18 +0000664 # Create a small sample database with two connections to it (db and db2).
665 file delete -force test.db
666 sqlite3 db test.db
667 sqlite3 db2 test.db
668 execsql {
669 CREATE TABLE ab(a PRIMARY KEY, b);
670 CREATE TABLE de(d PRIMARY KEY, e);
671 INSERT INTO ab VALUES('Chiang Mai', 100000);
672 INSERT INTO ab VALUES('Bangkok', 8000000);
673 INSERT INTO de VALUES('Ubon', 120000);
674 INSERT INTO de VALUES('Khon Kaen', 200000);
675 }
676} {}
danielk1977a96a7102006-01-16 12:46:41 +0000677do_test shared-$av.10.2 {
danielk1977b597f742006-01-15 11:39:18 +0000678 # Open a read-transaction with the first connection, a write-transaction
679 # with the second.
680 execsql {
681 BEGIN;
682 SELECT * FROM ab;
683 }
684 execsql {
685 BEGIN;
686 INSERT INTO de VALUES('Pataya', 30000);
687 } db2
688} {}
danielk1977a96a7102006-01-16 12:46:41 +0000689do_test shared-$av.10.3 {
danielk1977b597f742006-01-15 11:39:18 +0000690 # An external connection should be able to read the database, but not
691 # prepare a write operation.
692 sqlite3 db3 ./test.db
693 execsql {
694 SELECT * FROM ab;
695 } db3
696 catchsql {
697 BEGIN;
698 INSERT INTO de VALUES('Pataya', 30000);
699 } db3
700} {1 {database is locked}}
danielk1977a96a7102006-01-16 12:46:41 +0000701do_test shared-$av.10.4 {
danielk1977b597f742006-01-15 11:39:18 +0000702 # Close the connection with the write-transaction open
703 db2 close
704} {}
danielk1977a96a7102006-01-16 12:46:41 +0000705do_test shared-$av.10.5 {
danielk1977b597f742006-01-15 11:39:18 +0000706 # Test that the db2 transaction has been automatically rolled back.
707 # If it has not the ('Pataya', 30000) entry will still be in the table.
708 execsql {
709 SELECT * FROM de;
710 }
711} {Ubon 120000 {Khon Kaen} 200000}
danielk1977a96a7102006-01-16 12:46:41 +0000712do_test shared-$av.10.5 {
danielk1977b597f742006-01-15 11:39:18 +0000713 # Closing db2 should have dropped the shared-cache back to a read-lock.
714 # So db3 should be able to prepare a write...
715 catchsql {INSERT INTO de VALUES('Pataya', 30000);} db3
716} {0 {}}
danielk1977a96a7102006-01-16 12:46:41 +0000717do_test shared-$av.10.6 {
danielk1977b597f742006-01-15 11:39:18 +0000718 # ... but not commit it.
719 catchsql {COMMIT} db3
720} {1 {database is locked}}
danielk1977a96a7102006-01-16 12:46:41 +0000721do_test shared-$av.10.7 {
danielk1977b597f742006-01-15 11:39:18 +0000722 # Commit the (read-only) db transaction. Check via db3 to make sure the
723 # contents of table "de" are still as they should be.
724 execsql {
725 COMMIT;
726 }
727 execsql {
728 SELECT * FROM de;
729 } db3
730} {Ubon 120000 {Khon Kaen} 200000 Pataya 30000}
danielk1977a96a7102006-01-16 12:46:41 +0000731do_test shared-$av.10.9 {
danielk1977b597f742006-01-15 11:39:18 +0000732 # Commit the external transaction.
733 catchsql {COMMIT} db3
734} {0 {}}
danielk1977a96a7102006-01-16 12:46:41 +0000735integrity_check shared-$av.10.10
736do_test shared-$av.10.11 {
danielk1977b597f742006-01-15 11:39:18 +0000737 db close
738 db3 close
739} {}
740
danielk1977a96a7102006-01-16 12:46:41 +0000741}
742
danielk1977aef0bf62005-12-30 16:28:01 +0000743sqlite3_enable_shared_cache $::enable_shared_cache
danielk1977a96a7102006-01-16 12:46:41 +0000744finish_test