3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
12 # $Id: shared.test,v 1.36 2009/03/16 13:19:36 danielk1977 Exp $
14 set testdir [file dirname $argv0]
15 source $testdir/tester.tcl
18 # These tests cannot be run without the ATTACH command.
20 ifcapable !shared_cache||!attach {
25 set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
27 foreach av [list 0 1] {
29 # Open the database connection and execute the auto-vacuum pragma
33 ifcapable autovacuum {
34 do_test shared-[expr $av+1].1.0 {
35 execsql "pragma auto_vacuum=$::av"
36 execsql {pragma auto_vacuum}
45 # if we're using proxy locks, we use 2 filedescriptors for a db
46 # that is open but NOT yet locked, after a lock is taken we'll have 3,
47 # normally sqlite uses 1 (proxy locking adds the conch and the local lock)
49 foreach {name value} [array get env SQLITE_FORCE_PROXY_LOCKING] {
50 set using_proxy $value
52 set extrafds_prelock 0
53 set extrafds_postlock 0
55 set extrafds_prelock 1
56 set extrafds_postlock 2
59 # $av is currently 0 if this loop iteration is to test with auto-vacuum turned
60 # off, and 1 if it is turned on. Increment it so that (1 -> no auto-vacuum)
61 # and (2 -> auto-vacuum). The sole reason for this is so that it looks nicer
62 # when we use this variable as part of test-case names.
68 # shared-1.*: Simple test to verify basic sanity of table level locking when
69 # two connections share a pager cache.
70 # shared-2.*: Test that a read transaction can co-exist with a
71 # write-transaction, including a simple test to ensure the
72 # external locking protocol is still working.
73 # shared-3.*: Simple test of read-uncommitted mode.
74 # shared-4.*: Check that the schema is locked and unlocked correctly.
75 # shared-5.*: Test that creating/dropping schema items works when databases
76 # are attached in different orders to different handles.
77 # shared-6.*: Locking, UNION ALL queries and sub-queries.
78 # shared-7.*: Autovacuum and shared-cache.
79 # shared-8.*: Tests related to the text encoding of shared-cache databases.
80 # shared-9.*: TEMP triggers and shared-cache databases.
81 # shared-10.*: Tests of sqlite3_close().
82 # shared-11.*: Test transaction locking.
85 do_test shared-$av.1.1 {
86 # Open a second database on the file test.db. It should use the same pager
87 # cache and schema as the original connection. Verify that only 1 file is
90 set ::sqlite_open_file_count
91 expr $sqlite_open_file_count-$extrafds_postlock
93 do_test shared-$av.1.2 {
94 # Add a table and a single row of data via the first connection.
95 # Ensure that the second connection can see them.
97 CREATE TABLE abc(a, b, c);
98 INSERT INTO abc VALUES(1, 2, 3);
104 do_test shared-$av.1.3 {
105 # Have the first connection begin a transaction and obtain a read-lock
106 # on table abc. This should not prevent the second connection from
116 do_test shared-$av.1.4 {
117 # Try to insert a row into abc via connection 2. This should fail because
118 # of the read-lock connection 1 is holding on table abc (obtained in the
119 # previous test case).
121 INSERT INTO abc VALUES(4, 5, 6);
123 } {1 {database table is locked: abc}}
124 do_test shared-$av.1.5 {
125 # Using connection 2 (the one without the open transaction), try to create
126 # a new table. This should fail because of the open read transaction
127 # held by connection 1.
129 CREATE TABLE def(d, e, f);
131 } {1 {database table is locked: sqlite_master}}
132 do_test shared-$av.1.6 {
133 # Upgrade connection 1's transaction to a write transaction. Create
134 # a new table - def - and insert a row into it. Because the connection 1
135 # transaction modifies the schema, it should not be possible for
136 # connection 2 to access the database at all until the connection 1
137 # has finished the transaction.
139 CREATE TABLE def(d, e, f);
140 INSERT INTO def VALUES('IV', 'V', 'VI');
143 do_test shared-$av.1.7 {
144 # Read from the sqlite_master table with connection 1 (inside the
145 # transaction). Then test that we can not do this with connection 2. This
146 # is because of the schema-modified lock established by connection 1
147 # in the previous test case.
149 SELECT * FROM sqlite_master;
152 SELECT * FROM sqlite_master;
154 } {1 {database schema is locked: main}}
155 do_test shared-$av.1.8 {
156 # Commit the connection 1 transaction.
162 do_test shared-$av.2.1 {
163 # Open connection db3 to the database.
164 if {$::tcl_platform(platform)=="unix"} {
165 sqlite3 db3 "file:test.db?cache=private" -uri 1
169 set ::sqlite_open_file_count
170 expr $sqlite_open_file_count-($extrafds_prelock+$extrafds_postlock)
172 do_test shared-$av.2.2 {
173 # Start read transactions on db and db2 (the shared pager cache). Ensure
174 # db3 cannot write to the database.
184 INSERT INTO abc VALUES(1, 2, 3);
186 } {1 {database table is locked: abc}}
187 do_test shared-$av.2.3 {
188 # Turn db's transaction into a write-transaction. db3 should still be
189 # able to read from table def (but will not see the new row). Connection
190 # db2 should not be able to read def (because of the write-lock).
192 # Todo: The failed "INSERT INTO abc ..." statement in the above test
193 # has started a write-transaction on db2 (should this be so?). This
194 # would prevent connection db from starting a write-transaction. So roll the
195 # db2 transaction back and replace it with a new read transaction.
203 INSERT INTO def VALUES('VII', 'VIII', 'IX');
206 catchsql { SELECT * FROM def; } db3
208 catchsql { SELECT * FROM def; } db2
210 } {0 {IV V VI} 1 {database table is locked: def}}
211 do_test shared-$av.2.4 {
212 # Commit the open transaction on db. db2 still holds a read-transaction.
213 # This should prevent db3 from writing to the database, but not from
219 catchsql { SELECT * FROM def; } db3
221 catchsql { INSERT INTO def VALUES('X', 'XI', 'XII'); } db3
223 } {0 {IV V VI VII VIII IX} 1 {database is locked}}
227 do_test shared-$av.3.1.1 {
228 # This test case starts a linear scan of table 'seq' using a
229 # read-uncommitted connection. In the middle of the scan, rows are added
230 # to the end of the seq table (ahead of the current cursor position).
231 # The uncommitted rows should be included in the results of the scan.
233 CREATE TABLE seq(i PRIMARY KEY, x);
234 INSERT INTO seq VALUES(1, '[string repeat X 500]');
235 INSERT INTO seq VALUES(2, '[string repeat X 500]');
237 execsql {SELECT * FROM sqlite_master} db2
238 execsql {PRAGMA read_uncommitted = 1} db2
241 db2 eval {SELECT i FROM seq ORDER BY i} {
243 set max [execsql {SELECT max(i) FROM seq}]
245 INSERT INTO seq SELECT i + :max, x FROM seq;
251 } {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16}
252 do_test shared-$av.3.1.2 {
253 # Another linear scan through table seq using a read-uncommitted connection.
254 # This time, delete each row as it is read. Should not affect the results of
255 # the scan, but the table should be empty after the scan is concluded
256 # (test 3.1.3 verifies this).
258 db2 eval {SELECT i FROM seq} {
259 db eval {DELETE FROM seq WHERE i = :i}
263 } {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16}
264 do_test shared-$av.3.1.3 {
274 #--------------------------------------------------------------------------
275 # Tests shared-4.* test that the schema locking rules are applied
278 # 1. All transactions require a read-lock on the schemas of databases they
280 # 2. Transactions that modify a database schema require a write-lock on that
282 # 3. It is not possible to compile a statement while another handle has a
283 # write-lock on the schema.
286 # Open two database handles db and db2. Each has a single attach database
289 # db.main -> ./test.db
290 # db.test2 -> ./test2.db
291 # db2.main -> ./test2.db
292 # db2.test -> ./test.db
296 forcedelete test2.db-journal
299 do_test shared-$av.4.1.1 {
300 set sqlite_open_file_count
301 expr $sqlite_open_file_count-($extrafds_prelock*2)
303 do_test shared-$av.4.1.2 {
304 execsql {ATTACH 'test2.db' AS test2}
305 set sqlite_open_file_count
306 expr $sqlite_open_file_count-($extrafds_postlock*2)
308 do_test shared-$av.4.1.3 {
309 execsql {ATTACH 'test.db' AS test} db2
310 set sqlite_open_file_count
311 expr $sqlite_open_file_count-($extrafds_postlock*2)
314 # Sanity check: Create a table in ./test.db via handle db, and test that handle
315 # db2 can "see" the new table immediately. A handle using a seperate pager
316 # cache would have to reload the database schema before this were possible.
318 do_test shared-$av.4.2.1 {
320 CREATE TABLE abc(a, b, c);
321 CREATE TABLE def(d, e, f);
322 INSERT INTO abc VALUES('i', 'ii', 'iii');
323 INSERT INTO def VALUES('I', 'II', 'III');
326 do_test shared-$av.4.2.2 {
328 SELECT * FROM test.abc;
332 # Open a read-transaction and read from table abc via handle 2. Check that
333 # handle 1 can read table abc. Check that handle 1 cannot modify table abc
334 # or the database schema. Then check that handle 1 can modify table def.
336 do_test shared-$av.4.3.1 {
339 SELECT * FROM test.abc;
342 do_test shared-$av.4.3.2 {
344 INSERT INTO abc VALUES('iv', 'v', 'vi');
346 } {1 {database table is locked: abc}}
347 do_test shared-$av.4.3.3 {
349 CREATE TABLE ghi(g, h, i);
351 } {1 {database table is locked: sqlite_master}}
352 do_test shared-$av.4.3.3 {
354 INSERT INTO def VALUES('IV', 'V', 'VI');
357 do_test shared-$av.4.3.4 {
358 # Cleanup: commit the transaction opened by db2.
364 # Open a write-transaction using handle 1 and modify the database schema.
365 # Then try to execute a compiled statement to read from the same
366 # database via handle 2 (fails to get the lock on sqlite_master). Also
367 # try to compile a read of the same database using handle 2 (also fails).
368 # Finally, compile a read of the other database using handle 2. This
372 do_test shared-$av.4.4.1.2 {
373 # Sanity check 1: Check that the schema is what we think it is when viewed
376 CREATE TABLE test2.ghi(g, h, i);
377 SELECT 'test.db:'||name FROM sqlite_master
379 SELECT 'test2.db:'||name FROM test2.sqlite_master;
381 } {test.db:abc test.db:def test2.db:ghi}
382 do_test shared-$av.4.4.1.2 {
383 # Sanity check 2: Check that the schema is what we think it is when viewed
386 SELECT 'test2.db:'||name FROM sqlite_master
388 SELECT 'test.db:'||name FROM test.sqlite_master;
390 } {test2.db:ghi test.db:abc test.db:def}
393 do_test shared-$av.4.4.2 {
394 set ::DB2 [sqlite3_connection_pointer db2]
395 set sql {SELECT * FROM abc}
396 set ::STMT1 [sqlite3_prepare $::DB2 $sql -1 DUMMY]
399 CREATE TABLE jkl(j, k, l);
401 sqlite3_step $::STMT1
403 do_test shared-$av.4.4.3 {
404 sqlite3_finalize $::STMT1
406 do_test shared-$av.4.4.4 {
408 set ::STMT1 [sqlite3_prepare $::DB2 $sql -1 DUMMY]
411 } {1 {(6) database schema is locked: test}}
412 do_test shared-$av.4.4.5 {
414 set ::STMT1 [sqlite3_prepare $::DB2 "SELECT * FROM ghi" -1 DUMMY]
417 } {1 {(6) database schema is locked: test}}
423 #--------------------------------------------------------------------------
426 foreach db [list test.db test1.db test2.db test3.db] {
427 forcedelete $db ${db}-journal
429 do_test shared-$av.5.1.1 {
433 ATTACH 'test1.db' AS test1;
434 ATTACH 'test2.db' AS test2;
435 ATTACH 'test3.db' AS test3;
438 ATTACH 'test3.db' AS test3;
439 ATTACH 'test2.db' AS test2;
440 ATTACH 'test1.db' AS test1;
443 do_test shared-$av.5.1.2 {
445 CREATE TABLE test1.t1(a, b);
446 CREATE INDEX test1.i1 ON t1(a, b);
450 do_test shared-$av.5.1.3 {
452 CREATE VIEW test1.v1 AS SELECT * FROM t1;
457 do_test shared-$av.5.1.4 {
459 CREATE TRIGGER test1.trig1 AFTER INSERT ON t1 BEGIN
460 INSERT INTO t1 VALUES(new.a, new.b);
465 do_test shared-$av.5.1.5 {
471 do_test shared-$av.5.1.6 {
478 do_test shared-$av.5.1.7 {
484 do_test shared-$av.5.1.8 {
490 do_test shared-$av.5.1.9 {
492 SELECT * FROM sqlite_master UNION ALL SELECT * FROM test1.sqlite_master
497 #--------------------------------------------------------------------------
498 # Tests shared-6.* test that a query obtains all the read-locks it needs
499 # before starting execution of the query. This means that there is no chance
500 # some rows of data will be returned before a lock fails and SQLITE_LOCK
503 do_test shared-$av.6.1.1 {
505 CREATE TABLE t1(a, b);
506 CREATE TABLE t2(a, b);
507 INSERT INTO t1 VALUES(1, 2);
508 INSERT INTO t2 VALUES(3, 4);
512 do_test shared-$av.6.1.2 {
514 SELECT * FROM t1 UNION ALL SELECT * FROM t2;
518 do_test shared-$av.6.1.3 {
519 # Establish a write lock on table t2 via connection db2. Then make a
520 # UNION all query using connection db1 that first accesses t1, followed
521 # by t2. If the locks are grabbed at the start of the statement (as
522 # they should be), no rows are returned. If (as was previously the case)
523 # they are grabbed as the tables are accessed, the t1 rows will be
524 # returned before the query fails.
528 INSERT INTO t2 VALUES(5, 6);
532 db1 eval {SELECT * FROM t1 UNION ALL SELECT * FROM t2} {
538 do_test shared-$av.6.1.4 {
542 INSERT INTO t1 VALUES(7, 8);
547 SELECT (CASE WHEN a>4 THEN (SELECT a FROM t1) ELSE 0 END) AS d FROM t2;
557 foreach f [list test.db test2.db] {
558 forcedelete $f ${f}-journal
561 #--------------------------------------------------------------------------
562 # Tests shared-7.* test auto-vacuum does not invalidate cursors from
563 # other shared-cache users when it reorganizes the database on
566 do_test shared-$av.7.1 {
567 # This test case sets up a test database in auto-vacuum mode consisting
568 # of two tables, t1 and t2. Both have a single index. Table t1 is
569 # populated first (so consists of pages toward the start of the db file),
570 # t2 second (pages toward the end of the file).
575 CREATE TABLE t1(a PRIMARY KEY, b);
576 CREATE TABLE t2(a PRIMARY KEY, b);
579 for {set i 0} {$i < 100} {incr i} {
580 set a [string repeat "$i " 20]
581 set b [string repeat "$i " 20]
583 INSERT INTO t1 VALUES(:a, :b);
585 lappend ::contents [list [expr $i+1] $a $b]
588 INSERT INTO t2 SELECT * FROM t1;
592 do_test shared-$av.7.2 {
593 # This test case deletes the contents of table t1 (the one at the start of
594 # the file) while many cursors are open on table t2 and its index. All of
595 # the non-root pages will be moved from the end to the start of the file
596 # when the DELETE is committed - this test verifies that moving the pages
597 # does not disturb the open cursors.
600 proc lockrow {db tbl oids body} {
602 db eval "SELECT oid AS i, a, b FROM $tbl ORDER BY a" {
603 if {$i==[lindex $oids 0]} {
604 set noids [lrange $oids 1 end]
605 if {[llength $noids]==0} {
606 set subret [eval $body]
608 set subret [lockrow $db $tbl $noids $body]
611 lappend ret [list $i $a $b]
613 return [linsert $subret 0 $ret]
615 proc locktblrows {db tbl body} {
616 set oids [db eval "SELECT oid FROM $tbl"]
617 lockrow $db $tbl $oids $body
620 set scans [locktblrows db t2 {
627 # Test that each SELECT query returned the expected contents of t2.
629 if {[lsort -integer -index 0 $s]!=$::contents} {
638 unset -nocomplain contents
640 #--------------------------------------------------------------------------
641 # The following tests try to trick the shared-cache code into assuming
642 # the wrong encoding for a database.
644 forcedelete test.db test.db-journal
646 do_test shared-$av.8.1.1 {
649 PRAGMA encoding = 'UTF-16';
650 SELECT * FROM sqlite_master;
653 do_test shared-$av.8.1.2 {
654 string range [execsql {PRAGMA encoding;}] 0 end-2
657 do_test shared-$av.8.1.3 {
660 PRAGMA encoding = 'UTF-8';
661 CREATE TABLE abc(a, b, c);
664 do_test shared-$av.8.1.4 {
666 SELECT * FROM sqlite_master;
668 } "table abc abc [expr $AUTOVACUUM?3:2] {CREATE TABLE abc(a, b, c)}"
669 do_test shared-$av.8.1.5 {
676 forcedelete test2.db test2.db-journal
677 do_test shared-$av.8.2.1 {
679 ATTACH 'test2.db' AS aux;
680 SELECT * FROM aux.sqlite_master;
683 do_test shared-$av.8.2.2 {
686 PRAGMA encoding = 'UTF-16';
687 CREATE TABLE def(d, e, f);
689 string range [execsql {PRAGMA encoding;} db2] 0 end-2
694 forcedelete test.db test2.db
696 do_test shared-$av.8.3.2 {
698 execsql { CREATE TABLE def(d, e, f) }
699 execsql { PRAGMA encoding }
701 do_test shared-$av.8.3.3 {
702 set zDb16 "[encoding convertto unicode test.db]\x00\x00"
703 set db16 [sqlite3_open16 $zDb16 {}]
705 set stmt [sqlite3_prepare $db16 "SELECT sql FROM sqlite_master" -1 DUMMY]
707 set sql [sqlite3_column_text $stmt 0]
708 sqlite3_finalize $stmt
710 } {CREATE TABLE def(d, e, f)}
711 do_test shared-$av.8.3.4 {
712 set stmt [sqlite3_prepare $db16 "PRAGMA encoding" -1 DUMMY]
714 set enc [sqlite3_column_text $stmt 0]
715 sqlite3_finalize $stmt
721 # Bug #2547 is causing this to fail.
723 do_test shared-$av.8.2.3 {
725 SELECT * FROM aux.sqlite_master;
727 } {1 {attached databases must use the same text encoding as main database}}
733 forcedelete test.db test2.db
735 #---------------------------------------------------------------------------
736 # The following tests - shared-9.* - test interactions between TEMP triggers
737 # and shared-schemas.
739 ifcapable trigger&&tempdb {
741 do_test shared-$av.9.1 {
745 CREATE TABLE abc(a, b, c);
746 CREATE TABLE abc_mirror(a, b, c);
747 CREATE TEMP TRIGGER BEFORE INSERT ON abc BEGIN
748 INSERT INTO abc_mirror(a, b, c) VALUES(new.a, new.b, new.c);
750 INSERT INTO abc VALUES(1, 2, 3);
751 SELECT * FROM abc_mirror;
754 do_test shared-$av.9.2 {
756 INSERT INTO abc VALUES(4, 5, 6);
757 SELECT * FROM abc_mirror;
760 do_test shared-$av.9.3 {
767 #---------------------------------------------------------------------------
768 # The following tests - shared-10.* - test that the library behaves
769 # correctly when a connection to a shared-cache is closed.
771 do_test shared-$av.10.1 {
772 # Create a small sample database with two connections to it (db and db2).
777 CREATE TABLE ab(a PRIMARY KEY, b);
778 CREATE TABLE de(d PRIMARY KEY, e);
779 INSERT INTO ab VALUES('Chiang Mai', 100000);
780 INSERT INTO ab VALUES('Bangkok', 8000000);
781 INSERT INTO de VALUES('Ubon', 120000);
782 INSERT INTO de VALUES('Khon Kaen', 200000);
785 do_test shared-$av.10.2 {
786 # Open a read-transaction with the first connection, a write-transaction
794 INSERT INTO de VALUES('Pataya', 30000);
797 do_test shared-$av.10.3 {
798 # An external connection should be able to read the database, but not
799 # prepare a write operation.
800 if {$::tcl_platform(platform)=="unix"} {
801 sqlite3 db3 "file:test.db?cache=private" -uri 1
810 INSERT INTO de VALUES('Pataya', 30000);
812 } {1 {database is locked}}
813 do_test shared-$av.10.4 {
814 # Close the connection with the write-transaction open
817 do_test shared-$av.10.5 {
818 # Test that the db2 transaction has been automatically rolled back.
819 # If it has not the ('Pataya', 30000) entry will still be in the table.
823 } {Ubon 120000 {Khon Kaen} 200000}
824 do_test shared-$av.10.5 {
825 # Closing db2 should have dropped the shared-cache back to a read-lock.
826 # So db3 should be able to prepare a write...
827 catchsql {INSERT INTO de VALUES('Pataya', 30000);} db3
829 do_test shared-$av.10.6 {
830 # ... but not commit it.
831 catchsql {COMMIT} db3
832 } {1 {database is locked}}
833 do_test shared-$av.10.7 {
834 # Commit the (read-only) db transaction. Check via db3 to make sure the
835 # contents of table "de" are still as they should be.
842 } {Ubon 120000 {Khon Kaen} 200000 Pataya 30000}
843 do_test shared-$av.10.9 {
844 # Commit the external transaction.
845 catchsql {COMMIT} db3
847 integrity_check shared-$av.10.10
848 do_test shared-$av.10.11 {
853 do_test shared-$av.11.1 {
858 CREATE TABLE abc(a, b, c);
859 CREATE TABLE abc2(a, b, c);
861 INSERT INTO abc VALUES(1, 2, 3);
864 do_test shared-$av.11.2 {
865 catchsql {BEGIN;} db2
866 catchsql {SELECT * FROM abc;} db2
867 } {1 {database table is locked: abc}}
868 do_test shared-$av.11.3 {
870 } {1 {cannot start a transaction within a transaction}}
871 do_test shared-$av.11.4 {
872 catchsql {SELECT * FROM abc2;} db2
874 do_test shared-$av.11.5 {
875 catchsql {INSERT INTO abc2 VALUES(1, 2, 3);} db2
876 } {1 {database table is locked}}
877 do_test shared-$av.11.6 {
878 catchsql {SELECT * FROM abc2}
880 do_test shared-$av.11.6 {
883 PRAGMA read_uncommitted = 1;
886 do_test shared-$av.11.7 {
888 INSERT INTO abc2 VALUES(4, 5, 6);
889 INSERT INTO abc2 VALUES(7, 8, 9);
892 do_test shared-$av.11.8 {
895 SELECT abc.a as I, abc2.a as II FROM abc, abc2;
898 DELETE FROM abc WHERE 1;
904 if {[llength [info command sqlite3_shared_cache_report]]==1} {
906 do_test shared-$av.11.9 {
907 string tolower [sqlite3_shared_cache_report]
908 } [string tolower [list [file nativename [file normalize test.db]] 2]]
912 do_test shared-$av.11.11 {
917 # This tests that if it is impossible to free any pages, SQLite will
918 # exceed the limit set by PRAGMA cache_size.
919 forcedelete test.db test.db-journal
921 ifcapable pager_pragmas {
922 do_test shared-$av.12.1 {
924 PRAGMA cache_size = 10;
929 do_test shared-$av.12.2 {
930 set ::db_handles [list]
931 for {set i 1} {$i < 15} {incr i} {
932 lappend ::db_handles db$i
934 execsql "CREATE TABLE db${i}(a, b, c)" db$i
935 execsql "INSERT INTO db${i} VALUES(1, 2, 3)"
938 proc nested_select {handles} {
939 [lindex $handles 0] eval "SELECT * FROM [lindex $handles 0]" {
940 lappend ::res $a $b $c
941 if {[llength $handles]>1} {
942 nested_select [lrange $handles 1 end]
946 do_test shared-$av.12.3 {
948 nested_select $::db_handles
950 } [string range [string repeat "1 2 3 " [llength $::db_handles]] 0 end-1]
952 do_test shared-$av.12.X {
954 foreach h $::db_handles {
959 # Internally, locks are acquired on shared B-Tree structures in the order
960 # that the structures appear in the virtual memory address space. This
961 # test case attempts to cause the order of the structures in memory
962 # to be different from the order in which they are attached to a given
963 # database handle. This covers an extra line or two.
965 do_test shared-$av.13.1 {
966 forcedelete test2.db test3.db test4.db test5.db
969 ATTACH 'test2.db' AS aux2;
970 ATTACH 'test3.db' AS aux3;
971 ATTACH 'test4.db' AS aux4;
972 ATTACH 'test5.db' AS aux5;
976 ATTACH 'test2.db' AS aux2;
977 ATTACH 'test3.db' AS aux3;
978 ATTACH 'test4.db' AS aux4;
981 do_test shared-$av.13.2 {
983 CREATE TABLE t1(a, b, c);
984 CREATE TABLE aux2.t2(a, b, c);
985 CREATE TABLE aux3.t3(a, b, c);
986 CREATE TABLE aux4.t4(a, b, c);
987 CREATE TABLE aux5.t5(a, b, c);
995 do_test shared-$av.13.3 {
999 # Test that nothing horrible happens if a connection to a shared B-Tree
1000 # structure is closed while some other connection has an open cursor.
1002 do_test shared-$av.14.1 {
1005 execsql {SELECT name FROM sqlite_master}
1006 } {db1 db2 db3 db4 db5 db6 db7 db8 db9 db10 db11 db12 db13 db14}
1007 do_test shared-$av.14.2 {
1009 db eval {SELECT name FROM sqlite_master} {
1010 if {$name eq "db7"} {
1016 } {db1 db2 db3 db4 db5 db6 db7 db8 db9 db10 db11 db12 db13 db14}
1017 do_test shared-$av.14.3 {
1021 # Populate a database schema using connection [db]. Then drop it using
1022 # [db2]. This is to try to find any points where shared-schema elements
1023 # are allocated using the lookaside buffer of [db].
1025 # Mutexes are enabled for this test as that activates a couple of useful
1026 # assert() statements in the C code.
1028 do_test shared-$av-15.1 {
1030 sqlite3 db test.db -fullmutex 1
1031 sqlite3 db2 test.db -fullmutex 1
1033 CREATE TABLE t1(a, b, c);
1034 CREATE INDEX i1 ON t1(a, b);
1035 CREATE VIEW v1 AS SELECT * FROM t1;
1036 CREATE VIEW v2 AS SELECT * FROM t1, v1
1037 WHERE t1.c=v1.c GROUP BY t1.a ORDER BY v1.b;
1038 CREATE TRIGGER tr1 AFTER INSERT ON t1
1041 DELETE FROM t1 WHERE a=5;
1042 INSERT INTO t1 VALUES(1, 2, 3);
1043 UPDATE t1 SET c=c+1;
1046 INSERT INTO t1 VALUES(5, 6, 7);
1047 INSERT INTO t1 VALUES(8, 9, 10);
1048 INSERT INTO t1 VALUES(11, 12, 13);
1052 } {1 2 6 8 9 12 1 2 5 11 12 14 1 2 4}
1053 do_test shared-$av-15.2 {
1054 execsql { DROP TABLE t1 } db2
1059 # Shared cache on a :memory: database. This only works for URI filenames.
1061 do_test shared-$av-16.1 {
1062 sqlite3 db1 file::memory: -uri 1
1063 sqlite3 db2 file::memory: -uri 1
1065 CREATE TABLE t1(x); INSERT INTO t1 VALUES(1),(2),(3);
1068 SELECT x FROM t1 ORDER BY x;
1071 do_test shared-$av-16.2 {
1073 INSERT INTO t1 VALUES(99);
1074 DELETE FROM t1 WHERE x=2;
1077 SELECT x FROM t1 ORDER BY x;
1081 # Verify that there is no cache sharing ordinary (non-URI) filenames are
1084 do_test shared-$av-16.3 {
1087 sqlite3 db1 :memory:
1088 sqlite3 db2 :memory:
1090 CREATE TABLE t1(x); INSERT INTO t1 VALUES(4),(5),(6);
1095 } {1 {no such table: t1}}
1097 # Shared cache on named memory databases.
1099 do_test shared-$av-16.4 {
1102 forcedelete test.db test.db-wal test.db-journal
1103 sqlite3 db1 file:test.db?mode=memory -uri 1
1104 sqlite3 db2 file:test.db?mode=memory -uri 1
1106 CREATE TABLE t1(x); INSERT INTO t1 VALUES(1),(2),(3);
1109 SELECT x FROM t1 ORDER BY x;
1112 do_test shared-$av-16.5 {
1114 INSERT INTO t1 VALUES(99);
1115 DELETE FROM t1 WHERE x=2;
1118 SELECT x FROM t1 ORDER BY x;
1121 do_test shared-$av-16.6 {
1123 } {0} ;# Verify that the database is in-memory
1125 # Shared cache on named memory databases with different names.
1127 do_test shared-$av-16.7 {
1130 forcedelete test1.db test2.db
1131 sqlite3 db1 file:test1.db?mode=memory -uri 1
1132 sqlite3 db2 file:test2.db?mode=memory -uri 1
1134 CREATE TABLE t1(x); INSERT INTO t1 VALUES(1),(2),(3);
1137 SELECT x FROM t1 ORDER BY x;
1139 } {1 {no such table: t1}}
1140 do_test shared-$av-16.8 {
1141 file exists test1.db
1142 } {0} ;# Verify that the database is in-memory
1144 # Shared cache on named memory databases attached to readonly connections.
1146 if {![sqlite3 -has-codec]} {
1147 do_test shared-$av-16.8.1 {
1153 CREATE TABLE yy(a, b);
1154 INSERT INTO yy VALUES(77, 88);
1158 sqlite3 db1 test1.db -uri 1 -readonly 1
1159 sqlite3 db2 test2.db -uri 1
1162 ATTACH 'file:mem?mode=memory&cache=shared' AS shared;
1163 CREATE TABLE shared.xx(a, b);
1164 INSERT INTO xx VALUES(55, 66);
1167 ATTACH 'file:mem?mode=memory&cache=shared' AS shared;
1172 do_test shared-$av-16.8.2 { db1 eval { SELECT * FROM yy } } {77 88}
1173 do_test shared-$av-16.8.3 {
1174 list [catch {db1 eval { INSERT INTO yy VALUES(1, 2) }} msg] $msg
1175 } {1 {attempt to write a readonly database}}
1181 } ;# end of autovacuum on/off loop
1183 sqlite3_enable_shared_cache $::enable_shared_cache