blob: f41290385c4efb89352ad1c81c7e3d31e0797647 [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#***********************************************************************
11# This file implements regression tests for SQLite library. The
12# focus of this file is testing the SELECT statement.
13#
danielk1977e501b892006-01-09 06:29:47 +000014# $Id: shared.test,v 1.7 2006/01/09 06:29:49 danielk1977 Exp $
danielk1977aef0bf62005-12-30 16:28:01 +000015
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18db close
19
20ifcapable !shared_cache {
21 finish_test
22 return
23}
danielk1977da184232006-01-05 11:34:32 +000024set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
danielk1977aef0bf62005-12-30 16:28:01 +000025
26# Test organization:
27#
28# shared-1.*: Simple test to verify basic sanity of table level locking when
29# two connections share a pager cache.
30# shared-2.*: Test that a read transaction can co-exist with a
31# write-transaction, including a simple test to ensure the
32# external locking protocol is still working.
danielk1977da184232006-01-05 11:34:32 +000033# shared-3.*: Simple test of read-uncommitted mode.
danielk1977de0fe3e2006-01-06 06:33:12 +000034# shared-4.*: Check that the schema is locked and unlocked correctly.
danielk1977aaf22682006-01-06 15:03:48 +000035# shared-5.*: Test that creating/dropping schema items works when databases
36# are attached in different orders to different handles.
danielk1977c00da102006-01-07 13:21:04 +000037# shared-6.*: Locking, UNION ALL queries and sub-queries.
danielk1977e501b892006-01-09 06:29:47 +000038# shared-6.*: Autovacuum and shared-cache.
danielk1977de0fe3e2006-01-06 06:33:12 +000039#
danielk1977aef0bf62005-12-30 16:28:01 +000040
41do_test shared-1.1 {
42 # Open a second database on the file test.db. It should use the same pager
43 # cache and schema as the original connection. Verify that only 1 file is
44 # opened.
45 sqlite3 db2 test.db
46 sqlite3 db test.db
47 set ::sqlite_open_file_count
48} {1}
49do_test shared-1.2 {
50 # Add a table and a single row of data via the first connection.
51 # Ensure that the second connection can see them.
52 execsql {
53 CREATE TABLE abc(a, b, c);
54 INSERT INTO abc VALUES(1, 2, 3);
55 } db
56 execsql {
57 SELECT * FROM abc;
58 } db2
59} {1 2 3}
60do_test shared-1.3 {
61 # Have the first connection begin a transaction and obtain a read-lock
62 # on table abc. This should not prevent the second connection from
63 # querying abc.
64 execsql {
65 BEGIN;
66 SELECT * FROM abc;
67 }
68 execsql {
69 SELECT * FROM abc;
70 } db2
71} {1 2 3}
72do_test shared-1.4 {
73 # Try to insert a row into abc via connection 2. This should fail because
74 # of the read-lock connection 1 is holding on table abc (obtained in the
75 # previous test case).
76 catchsql {
77 INSERT INTO abc VALUES(4, 5, 6);
78 } db2
danielk1977c00da102006-01-07 13:21:04 +000079} {1 {database table is locked: abc}}
danielk1977aef0bf62005-12-30 16:28:01 +000080do_test shared-1.5 {
danielk1977da184232006-01-05 11:34:32 +000081 # Using connection 2 (the one without the open transaction), try to create
82 # a new table. This should fail because of the open read transaction
83 # held by connection 1.
84 catchsql {
85 CREATE TABLE def(d, e, f);
86 } db2
danielk1977c00da102006-01-07 13:21:04 +000087} {1 {database table is locked: sqlite_master}}
danielk1977da184232006-01-05 11:34:32 +000088do_test shared-1.6 {
89 # Upgrade connection 1's transaction to a write transaction. Create
90 # a new table - def - and insert a row into it. Because the connection 1
91 # transaction modifies the schema, it should not be possible for
92 # connection 2 to access the database at all until the connection 1
93 # has finished the transaction.
danielk1977aef0bf62005-12-30 16:28:01 +000094 execsql {
95 CREATE TABLE def(d, e, f);
danielk1977aef0bf62005-12-30 16:28:01 +000096 INSERT INTO def VALUES('IV', 'V', 'VI');
97 }
98} {}
99do_test shared-1.7 {
100 # Read from the sqlite_master table with connection 1 (inside the
danielk1977da184232006-01-05 11:34:32 +0000101 # transaction). Then test that we can not do this with connection 2. This
102 # is because of the schema-modified lock established by connection 1
103 # in the previous test case.
danielk1977aef0bf62005-12-30 16:28:01 +0000104 execsql {
105 SELECT * FROM sqlite_master;
106 }
107 catchsql {
danielk1977da184232006-01-05 11:34:32 +0000108 SELECT * FROM sqlite_master;
danielk1977aef0bf62005-12-30 16:28:01 +0000109 } db2
danielk1977c87d34d2006-01-06 13:00:28 +0000110} {1 {database schema is locked: main}}
danielk1977aef0bf62005-12-30 16:28:01 +0000111do_test shared-1.8 {
danielk1977aef0bf62005-12-30 16:28:01 +0000112 # Commit the connection 1 transaction.
113 execsql {
114 COMMIT;
115 }
116} {}
117
118do_test shared-2.1 {
119 # Open connection db3 to the database. Use a different path to the same
120 # file so that db3 does *not* share the same pager cache as db and db2
121 # (there should be two open file handles).
122 sqlite3 db3 ./test.db
123 set ::sqlite_open_file_count
124} {2}
125do_test shared-2.2 {
126 # Start read transactions on db and db2 (the shared pager cache). Ensure
127 # db3 cannot write to the database.
128 execsql {
129 BEGIN;
130 SELECT * FROM abc;
131 }
132 execsql {
133 BEGIN;
134 SELECT * FROM abc;
135 } db2
136 catchsql {
137 INSERT INTO abc VALUES(1, 2, 3);
138 } db2
danielk1977c00da102006-01-07 13:21:04 +0000139} {1 {database table is locked: abc}}
danielk1977aef0bf62005-12-30 16:28:01 +0000140do_test shared-2.3 {
141 # Turn db's transaction into a write-transaction. db3 should still be
142 # able to read from table def (but will not see the new row). Connection
143 # db2 should not be able to read def (because of the write-lock).
144
145# Todo: The failed "INSERT INTO abc ..." statement in the above test
146# has started a write-transaction on db2 (should this be so?). This
147# would prevent connection db from starting a write-transaction. So roll the
148# db2 transaction back and replace it with a new read transaction.
149 execsql {
150 ROLLBACK;
151 BEGIN;
152 SELECT * FROM abc;
153 } db2
154
155 execsql {
156 INSERT INTO def VALUES('VII', 'VIII', 'IX');
157 }
158 concat [
159 catchsql { SELECT * FROM def; } db3
160 ] [
161 catchsql { SELECT * FROM def; } db2
162 ]
danielk1977c00da102006-01-07 13:21:04 +0000163} {0 {IV V VI} 1 {database table is locked: def}}
danielk1977aef0bf62005-12-30 16:28:01 +0000164do_test shared-2.4 {
165 # Commit the open transaction on db. db2 still holds a read-transaction.
166 # This should prevent db3 from writing to the database, but not from
167 # reading.
168 execsql {
169 COMMIT;
170 }
171 concat [
172 catchsql { SELECT * FROM def; } db3
173 ] [
174 catchsql { INSERT INTO def VALUES('X', 'XI', 'XII'); } db3
175 ]
danielk1977da184232006-01-05 11:34:32 +0000176} {0 {IV V VI VII VIII IX} 1 {database is locked}}
danielk1977aef0bf62005-12-30 16:28:01 +0000177
danielk1977da184232006-01-05 11:34:32 +0000178catchsql COMMIT db2
179
180do_test shared-3.1.1 {
181 # This test case starts a linear scan of table 'seq' using a
182 # read-uncommitted connection. In the middle of the scan, rows are added
183 # to the end of the seq table (ahead of the current cursor position).
184 # The uncommitted rows should be included in the results of the scan.
185 execsql "
186 CREATE TABLE seq(i, x);
187 INSERT INTO seq VALUES(1, '[string repeat X 500]');
188 INSERT INTO seq VALUES(2, '[string repeat X 500]');
189 "
190 execsql {SELECT * FROM sqlite_master} db2
191 execsql {PRAGMA read_uncommitted = 1} db2
192
193 set ret [list]
194 db2 eval {SELECT i FROM seq} {
195 if {$i < 4} {
196 execsql {
197 INSERT INTO seq SELECT i + (SELECT max(i) FROM seq), x FROM seq;
198 }
199 }
200 lappend ret $i
201 }
202 set ret
203} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16}
204do_test shared-3.1.2 {
205 # Another linear scan through table seq using a read-uncommitted connection.
206 # This time, delete each row as it is read. Should not affect the results of
207 # the scan, but the table should be empty after the scan is concluded
208 # (test 3.1.3 verifies this).
209 set ret [list]
210 db2 eval {SELECT i FROM seq} {
211 db eval {DELETE FROM seq WHERE i = $i}
212 lappend ret $i
213 }
214 set ret
215} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16}
216do_test shared-3.1.3 {
217 execsql {
218 SELECT * FROM seq;
219 }
220} {}
danielk1977aef0bf62005-12-30 16:28:01 +0000221
222catch {db close}
223catch {db2 close}
224catch {db3 close}
225
danielk1977de0fe3e2006-01-06 06:33:12 +0000226#--------------------------------------------------------------------------
227# Tests shared-4.* test that the schema locking rules are applied
228# correctly. i.e.:
229#
230# 1. All transactions require a read-lock on the schemas of databases they
231# access.
232# 2. Transactions that modify a database schema require a write-lock on that
233# schema.
234# 3. It is not possible to compile a statement while another handle has a
235# write-lock on the schema.
236#
237
238# Open two database handles db and db2. Each has a single attach database
239# (as well as main):
240#
241# db.main -> ./test.db
242# db.test2 -> ./test2.db
243# db2.main -> ./test2.db
244# db2.test -> ./test.db
245#
246file delete -force test.db
247file delete -force test2.db
248file delete -force test2.db-journal
249sqlite3 db test.db
250sqlite3 db2 test2.db
251do_test shared-4.1.1 {
252 set sqlite_open_file_count
253} {2}
254do_test shared-4.1.2 {
255 execsql {ATTACH 'test2.db' AS test2}
256 set sqlite_open_file_count
257} {2}
258do_test shared-4.1.3 {
259 execsql {ATTACH 'test.db' AS test} db2
260 set sqlite_open_file_count
261} {2}
262
danielk1977c87d34d2006-01-06 13:00:28 +0000263# Sanity check: Create a table in ./test.db via handle db, and test that handle
264# db2 can "see" the new table immediately. A handle using a seperate pager
265# cache would have to reload the database schema before this were possible.
266#
danielk1977de0fe3e2006-01-06 06:33:12 +0000267do_test shared-4.2.1 {
268 execsql {
269 CREATE TABLE abc(a, b, c);
danielk1977c87d34d2006-01-06 13:00:28 +0000270 CREATE TABLE def(d, e, f);
danielk1977de0fe3e2006-01-06 06:33:12 +0000271 INSERT INTO abc VALUES('i', 'ii', 'iii');
danielk1977c87d34d2006-01-06 13:00:28 +0000272 INSERT INTO def VALUES('I', 'II', 'III');
danielk1977de0fe3e2006-01-06 06:33:12 +0000273 }
274} {}
275do_test shared-4.2.2 {
276 execsql {
277 SELECT * FROM test.abc;
278 } db2
279} {i ii iii}
280
danielk1977c87d34d2006-01-06 13:00:28 +0000281# Open a read-transaction and read from table abc via handle 2. Check that
282# handle 1 can read table abc. Check that handle 1 cannot modify table abc
283# or the database schema. Then check that handle 1 can modify table def.
284#
285do_test shared-4.3.1 {
286 execsql {
287 BEGIN;
288 SELECT * FROM test.abc;
289 } db2
290} {i ii iii}
291do_test shared-4.3.2 {
292 catchsql {
293 INSERT INTO abc VALUES('iv', 'v', 'vi');
294 }
danielk1977c00da102006-01-07 13:21:04 +0000295} {1 {database table is locked: abc}}
danielk1977c87d34d2006-01-06 13:00:28 +0000296do_test shared-4.3.3 {
297 catchsql {
298 CREATE TABLE ghi(g, h, i);
299 }
danielk1977c00da102006-01-07 13:21:04 +0000300} {1 {database table is locked: sqlite_master}}
danielk1977c87d34d2006-01-06 13:00:28 +0000301do_test shared-4.3.3 {
302 catchsql {
303 INSERT INTO def VALUES('IV', 'V', 'VI');
304 }
305} {0 {}}
306do_test shared-4.3.4 {
307 # Cleanup: commit the transaction opened by db2.
308 execsql {
309 COMMIT
310 } db2
311} {}
312
313# Open a write-transaction using handle 1 and modify the database schema.
314# Then try to execute a compiled statement to read from the same
315# database via handle 2 (fails to get the lock on sqlite_master). Also
316# try to compile a read of the same database using handle 2 (also fails).
317# Finally, compile a read of the other database using handle 2. This
318# should also fail.
319#
320do_test shared-4.4.1.2 {
321 # Sanity check 1: Check that the schema is what we think it is when viewed
322 # via handle 1.
323 execsql {
324 CREATE TABLE test2.ghi(g, h, i);
325 SELECT 'test.db:'||name FROM sqlite_master
326 UNION ALL
327 SELECT 'test2.db:'||name FROM test2.sqlite_master;
328 }
329} {test.db:abc test.db:def test2.db:ghi}
330do_test shared-4.4.1.2 {
331 # Sanity check 2: Check that the schema is what we think it is when viewed
332 # via handle 2.
333 execsql {
334 SELECT 'test2.db:'||name FROM sqlite_master
335 UNION ALL
336 SELECT 'test.db:'||name FROM test.sqlite_master;
337 } db2
338} {test2.db:ghi test.db:abc test.db:def}
339
340do_test shared-4.4.2 {
341 set ::DB2 [sqlite3_connection_pointer db2]
342 set sql {SELECT * FROM abc}
343 set ::STMT1 [sqlite3_prepare $::DB2 $sql -1 DUMMY]
344 execsql {
345 BEGIN;
346 CREATE TABLE jkl(j, k, l);
347 }
348 sqlite3_step $::STMT1
349} {SQLITE_ERROR}
350do_test shared-4.4.3 {
351 sqlite3_finalize $::STMT1
352} {SQLITE_LOCKED}
353do_test shared-4.4.4 {
354 set rc [catch {
355 set ::STMT1 [sqlite3_prepare $::DB2 $sql -1 DUMMY]
356 } msg]
357 list $rc $msg
358} {1 {(6) database schema is locked: test}}
359do_test shared-4.4.5 {
360 set rc [catch {
361 set ::STMT1 [sqlite3_prepare $::DB2 "SELECT * FROM ghi" -1 DUMMY]
362 } msg]
363 list $rc $msg
364} {1 {(6) database schema is locked: test}}
365
danielk1977aaf22682006-01-06 15:03:48 +0000366
danielk1977de0fe3e2006-01-06 06:33:12 +0000367catch {db2 close}
368catch {db close}
369
danielk1977aaf22682006-01-06 15:03:48 +0000370#--------------------------------------------------------------------------
371# Tests shared-5.*
372#
373foreach db [list test.db test1.db test2.db test3.db] {
374 file delete -force $db ${db}-journal
375}
376do_test shared-5.1.1 {
377 sqlite3 db1 test.db
378 sqlite3 db2 test.db
379 execsql {
380 ATTACH 'test1.db' AS test1;
381 ATTACH 'test2.db' AS test2;
382 ATTACH 'test3.db' AS test3;
383 } db1
384 execsql {
385 ATTACH 'test3.db' AS test3;
386 ATTACH 'test2.db' AS test2;
387 ATTACH 'test1.db' AS test1;
388 } db2
389} {}
390do_test shared-5.1.2 {
391 execsql {
392 CREATE TABLE test1.t1(a, b);
393 CREATE INDEX test1.i1 ON t1(a, b);
394 CREATE VIEW test1.v1 AS SELECT * FROM t1;
395 CREATE TRIGGER test1.trig1 AFTER INSERT ON t1 BEGIN
396 INSERT INTO t1 VALUES(new.a, new.b);
397 END;
398 } db1
399 execsql {
400 DROP INDEX i1;
401 DROP VIEW v1;
402 DROP TRIGGER trig1;
403 DROP TABLE t1;
404 } db2
405} {}
406do_test shared-5.1.2 {
407 execsql {
408 SELECT * FROM sqlite_master UNION ALL SELECT * FROM test1.sqlite_master
409 } db1
410} {}
411
danielk1977c00da102006-01-07 13:21:04 +0000412#--------------------------------------------------------------------------
413# Tests shared-6.* test that a query obtains all the read-locks it needs
414# before starting execution of the query. This means that there is no chance
415# some rows of data will be returned before a lock fails and SQLITE_LOCK
416# is returned.
417#
418do_test shared-6.1.1 {
419 execsql {
420 CREATE TABLE t1(a, b);
421 CREATE TABLE t2(a, b);
422 INSERT INTO t1 VALUES(1, 2);
423 INSERT INTO t2 VALUES(3, 4);
424 } db1
425 execsql {
426 SELECT * FROM t1 UNION ALL SELECT * FROM t2;
427 } db2
428} {1 2 3 4}
429do_test shared-6.1.2 {
430 # Establish a write lock on table t2 via connection db2. Then make a
431 # UNION all query using connection db1 that first accesses t1, followed
432 # by t2. If the locks are grabbed at the start of the statement (as
433 # they should be), no rows are returned. If (as was previously the case)
434 # they are grabbed as the tables are accessed, the t1 rows will be
435 # returned before the query fails.
436 #
437 execsql {
438 BEGIN;
439 INSERT INTO t2 VALUES(5, 6);
440 } db2
441 set ret [list]
442 catch {
443 db1 eval {SELECT * FROM t1 UNION ALL SELECT * FROM t2} {
444 lappend ret $a $b
445 }
446 }
447 set ret
448} {}
449do_test shared-6.1.3 {
450 execsql {
451 COMMIT;
452 BEGIN;
453 INSERT INTO t1 VALUES(7, 8);
454 } db2
455 set ret [list]
456 catch {
457 db1 eval {
458 SELECT (CASE WHEN a>4 THEN (SELECT a FROM t1) ELSE 0 END) AS d FROM t2;
459 } {
460 lappend ret $d
461 }
462 }
463 set ret
464} {}
465
danielk1977aaf22682006-01-06 15:03:48 +0000466catch {db1 close}
467catch {db2 close}
danielk1977e501b892006-01-09 06:29:47 +0000468foreach f [list test.db test2.db] {
469 file delete -force $f ${f}-journal
470}
471
472#--------------------------------------------------------------------------
473# Tests shared-7.* test auto-vacuum does not invalidate cursors from
474# other shared-cache users when it reorganizes the database on
475# COMMIT.
476#
477do_test shared-7.1 {
478 sqlite3 db test.db
479 sqlite3 db2 test.db
480 execsql {
481 PRAGMA auto_vacuum = 1;
482 BEGIN;
483 CREATE TABLE t1(a PRIMARY KEY, b);
484 CREATE TABLE t2(a PRIMARY KEY, b);
485 }
486 for {set i 0} {$i < 100} {incr i} {
487 set a [string repeat "$i " 20]
488 set b [string repeat "$i " 20]
489 db eval {
490 INSERT INTO t1 VALUES($a, $b);
491 }
492 lappend ::contents [list [expr $i+1] $a $b]
493 }
494 execsql {
495 INSERT INTO t2 SELECT * FROM t1;
496 COMMIT;
497 }
498 execsql {
499 PRAGMA auto_vacuum;
500 }
501} {1}
502do_test shared-7.2 {
503 proc lockrow {db tbl oids body} {
504 set ret [list]
505 db eval "SELECT oid AS i, a, b FROM $tbl ORDER BY a" {
506 if {$i==[lindex $oids 0]} {
507 set noids [lrange $oids 1 end]
508 if {[llength $noids]==0} {
509 set subret [eval $body]
510 } else {
511 set subret [lockrow $db $tbl $noids $body]
512 }
513 }
514 lappend ret [list $i $a $b]
515 }
516 return [linsert $subret 0 $ret]
517 }
518 proc locktblrows {db tbl body} {
519 set oids [db eval "SELECT oid FROM $tbl"]
520 lockrow $db $tbl $oids $body
521 }
522
523 set scans [locktblrows db t2 {
524 execsql {
525 DELETE FROM t1;
526 } db2
527 }]
528 set error 0
529 foreach s $scans {
530 if {[lsort -integer -index 0 $s]!=$::contents} {
531 set error 1
532 }
533 }
534 set error
535} {0}
536
537catch {db close}
538catch {db2 close}
danielk1977aaf22682006-01-06 15:03:48 +0000539
danielk1977aef0bf62005-12-30 16:28:01 +0000540finish_test
541sqlite3_enable_shared_cache $::enable_shared_cache
542