xref: /sqlite-3.40.0/test/shared.test (revision cf77a45a)
1# 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#
12# $Id: shared.test,v 1.32 2008/05/19 20:11:40 shane Exp $
13
14set testdir [file dirname $argv0]
15source $testdir/tester.tcl
16db close
17
18# These tests cannot be run without the ATTACH command.
19#
20ifcapable !shared_cache||!attach {
21  finish_test
22  return
23}
24
25set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
26
27foreach av [list 0 1] {
28
29# Open the database connection and execute the auto-vacuum pragma
30file delete -force test.db
31sqlite3 db test.db
32
33ifcapable autovacuum {
34  do_test shared-[expr $av+1].1.0 {
35    execsql "pragma auto_vacuum=$::av"
36    execsql {pragma auto_vacuum}
37  } "$av"
38} else {
39  if {$av} {
40    db close
41    break
42  }
43}
44
45# $av is currently 0 if this loop iteration is to test with auto-vacuum turned
46# off, and 1 if it is turned on. Increment it so that (1 -> no auto-vacuum)
47# and (2 -> auto-vacuum). The sole reason for this is so that it looks nicer
48# when we use this variable as part of test-case names.
49#
50incr av
51
52# Test organization:
53#
54# shared-1.*: Simple test to verify basic sanity of table level locking when
55#             two connections share a pager cache.
56# shared-2.*: Test that a read transaction can co-exist with a
57#             write-transaction, including a simple test to ensure the
58#             external locking protocol is still working.
59# shared-3.*: Simple test of read-uncommitted mode.
60# shared-4.*: Check that the schema is locked and unlocked correctly.
61# shared-5.*: Test that creating/dropping schema items works when databases
62#             are attached in different orders to different handles.
63# shared-6.*: Locking, UNION ALL queries and sub-queries.
64# shared-7.*: Autovacuum and shared-cache.
65# shared-8.*: Tests related to the text encoding of shared-cache databases.
66# shared-9.*: TEMP triggers and shared-cache databases.
67# shared-10.*: Tests of sqlite3_close().
68# shared-11.*: Test transaction locking.
69#
70
71do_test shared-$av.1.1 {
72  # Open a second database on the file test.db. It should use the same pager
73  # cache and schema as the original connection. Verify that only 1 file is
74  # opened.
75  sqlite3 db2 test.db
76  set ::sqlite_open_file_count
77} {1}
78do_test shared-$av.1.2 {
79  # Add a table and a single row of data via the first connection.
80  # Ensure that the second connection can see them.
81  execsql {
82    CREATE TABLE abc(a, b, c);
83    INSERT INTO abc VALUES(1, 2, 3);
84  } db
85  execsql {
86    SELECT * FROM abc;
87  } db2
88} {1 2 3}
89do_test shared-$av.1.3 {
90  # Have the first connection begin a transaction and obtain a read-lock
91  # on table abc. This should not prevent the second connection from
92  # querying abc.
93  execsql {
94    BEGIN;
95    SELECT * FROM abc;
96  }
97  execsql {
98    SELECT * FROM abc;
99  } db2
100} {1 2 3}
101do_test shared-$av.1.4 {
102  # Try to insert a row into abc via connection 2. This should fail because
103  # of the read-lock connection 1 is holding on table abc (obtained in the
104  # previous test case).
105  catchsql {
106    INSERT INTO abc VALUES(4, 5, 6);
107  } db2
108} {1 {database table is locked: abc}}
109do_test shared-$av.1.5 {
110  # Using connection 2 (the one without the open transaction), try to create
111  # a new table. This should fail because of the open read transaction
112  # held by connection 1.
113  catchsql {
114    CREATE TABLE def(d, e, f);
115  } db2
116} {1 {database table is locked: sqlite_master}}
117do_test shared-$av.1.6 {
118  # Upgrade connection 1's transaction to a write transaction. Create
119  # a new table - def - and insert a row into it. Because the connection 1
120  # transaction modifies the schema, it should not be possible for
121  # connection 2 to access the database at all until the connection 1
122  # has finished the transaction.
123  execsql {
124    CREATE TABLE def(d, e, f);
125    INSERT INTO def VALUES('IV', 'V', 'VI');
126  }
127} {}
128do_test shared-$av.1.7 {
129  # Read from the sqlite_master table with connection 1 (inside the
130  # transaction). Then test that we can not do this with connection 2. This
131  # is because of the schema-modified lock established by connection 1
132  # in the previous test case.
133  execsql {
134    SELECT * FROM sqlite_master;
135  }
136  catchsql {
137    SELECT * FROM sqlite_master;
138  } db2
139} {1 {database schema is locked: main}}
140do_test shared-$av.1.8 {
141  # Commit the connection 1 transaction.
142  execsql {
143    COMMIT;
144  }
145} {}
146
147do_test shared-$av.2.1 {
148  # Open connection db3 to the database. Use a different path to the same
149  # file so that db3 does *not* share the same pager cache as db and db2
150  # (there should be two open file handles).
151  if {$::tcl_platform(platform)=="unix"} {
152    sqlite3 db3 ./test.db
153  } else {
154    sqlite3 db3 TEST.DB
155  }
156  set ::sqlite_open_file_count
157} {2}
158do_test shared-$av.2.2 {
159  # Start read transactions on db and db2 (the shared pager cache). Ensure
160  # db3 cannot write to the database.
161  execsql {
162    BEGIN;
163    SELECT * FROM abc;
164  }
165  execsql {
166    BEGIN;
167    SELECT * FROM abc;
168  } db2
169  catchsql {
170    INSERT INTO abc VALUES(1, 2, 3);
171  } db2
172} {1 {database table is locked: abc}}
173do_test shared-$av.2.3 {
174  # Turn db's transaction into a write-transaction. db3 should still be
175  # able to read from table def (but will not see the new row). Connection
176  # db2 should not be able to read def (because of the write-lock).
177
178# Todo: The failed "INSERT INTO abc ..." statement in the above test
179# has started a write-transaction on db2 (should this be so?). This
180# would prevent connection db from starting a write-transaction. So roll the
181# db2 transaction back and replace it with a new read transaction.
182  execsql {
183    ROLLBACK;
184    BEGIN;
185    SELECT * FROM abc;
186  } db2
187
188  execsql {
189    INSERT INTO def VALUES('VII', 'VIII', 'IX');
190  }
191  concat [
192    catchsql { SELECT * FROM def; } db3
193  ] [
194    catchsql { SELECT * FROM def; } db2
195  ]
196} {0 {IV V VI} 1 {database table is locked: def}}
197do_test shared-$av.2.4 {
198  # Commit the open transaction on db. db2 still holds a read-transaction.
199  # This should prevent db3 from writing to the database, but not from
200  # reading.
201  execsql {
202    COMMIT;
203  }
204  concat [
205    catchsql { SELECT * FROM def; } db3
206  ] [
207    catchsql { INSERT INTO def VALUES('X', 'XI', 'XII'); } db3
208  ]
209} {0 {IV V VI VII VIII IX} 1 {database is locked}}
210
211catchsql COMMIT db2
212
213do_test shared-$av.3.1.1 {
214  # This test case starts a linear scan of table 'seq' using a
215  # read-uncommitted connection. In the middle of the scan, rows are added
216  # to the end of the seq table (ahead of the current cursor position).
217  # The uncommitted rows should be included in the results of the scan.
218  execsql "
219    CREATE TABLE seq(i PRIMARY KEY, x);
220    INSERT INTO seq VALUES(1, '[string repeat X 500]');
221    INSERT INTO seq VALUES(2, '[string repeat X 500]');
222  "
223  execsql {SELECT * FROM sqlite_master} db2
224  execsql {PRAGMA read_uncommitted = 1} db2
225
226  set ret [list]
227  db2 eval {SELECT i FROM seq ORDER BY i} {
228    if {$i < 4} {
229      set max [execsql {SELECT max(i) FROM seq}]
230      db eval {
231        INSERT INTO seq SELECT i + :max, x FROM seq;
232      }
233    }
234    lappend ret $i
235  }
236  set ret
237} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16}
238do_test shared-$av.3.1.2 {
239  # Another linear scan through table seq using a read-uncommitted connection.
240  # This time, delete each row as it is read. Should not affect the results of
241  # the scan, but the table should be empty after the scan is concluded
242  # (test 3.1.3 verifies this).
243  set ret [list]
244  db2 eval {SELECT i FROM seq} {
245    db eval {DELETE FROM seq WHERE i = :i}
246    lappend ret $i
247  }
248  set ret
249} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16}
250do_test shared-$av.3.1.3 {
251  execsql {
252    SELECT * FROM seq;
253  }
254} {}
255
256catch {db close}
257catch {db2 close}
258catch {db3 close}
259
260#--------------------------------------------------------------------------
261# Tests shared-4.* test that the schema locking rules are applied
262# correctly. i.e.:
263#
264# 1. All transactions require a read-lock on the schemas of databases they
265#    access.
266# 2. Transactions that modify a database schema require a write-lock on that
267#    schema.
268# 3. It is not possible to compile a statement while another handle has a
269#    write-lock on the schema.
270#
271
272# Open two database handles db and db2. Each has a single attach database
273# (as well as main):
274#
275#     db.main   ->   ./test.db
276#     db.test2  ->   ./test2.db
277#     db2.main  ->   ./test2.db
278#     db2.test  ->   ./test.db
279#
280file delete -force test.db
281file delete -force test2.db
282file delete -force test2.db-journal
283sqlite3 db  test.db
284sqlite3 db2 test2.db
285do_test shared-$av.4.1.1 {
286  set sqlite_open_file_count
287} {2}
288do_test shared-$av.4.1.2 {
289  execsql {ATTACH 'test2.db' AS test2}
290  set sqlite_open_file_count
291} {2}
292do_test shared-$av.4.1.3 {
293  execsql {ATTACH 'test.db' AS test} db2
294  set sqlite_open_file_count
295} {2}
296
297# Sanity check: Create a table in ./test.db via handle db, and test that handle
298# db2 can "see" the new table immediately. A handle using a seperate pager
299# cache would have to reload the database schema before this were possible.
300#
301do_test shared-$av.4.2.1 {
302  execsql {
303    CREATE TABLE abc(a, b, c);
304    CREATE TABLE def(d, e, f);
305    INSERT INTO abc VALUES('i', 'ii', 'iii');
306    INSERT INTO def VALUES('I', 'II', 'III');
307  }
308} {}
309do_test shared-$av.4.2.2 {
310  execsql {
311    SELECT * FROM test.abc;
312  } db2
313} {i ii iii}
314
315# Open a read-transaction and read from table abc via handle 2. Check that
316# handle 1 can read table abc. Check that handle 1 cannot modify table abc
317# or the database schema. Then check that handle 1 can modify table def.
318#
319do_test shared-$av.4.3.1 {
320  execsql {
321    BEGIN;
322    SELECT * FROM test.abc;
323  } db2
324} {i ii iii}
325do_test shared-$av.4.3.2 {
326  catchsql {
327    INSERT INTO abc VALUES('iv', 'v', 'vi');
328  }
329} {1 {database table is locked: abc}}
330do_test shared-$av.4.3.3 {
331  catchsql {
332    CREATE TABLE ghi(g, h, i);
333  }
334} {1 {database table is locked: sqlite_master}}
335do_test shared-$av.4.3.3 {
336  catchsql {
337    INSERT INTO def VALUES('IV', 'V', 'VI');
338  }
339} {0 {}}
340do_test shared-$av.4.3.4 {
341  # Cleanup: commit the transaction opened by db2.
342  execsql {
343    COMMIT
344  } db2
345} {}
346
347# Open a write-transaction using handle 1 and modify the database schema.
348# Then try to execute a compiled statement to read from the same
349# database via handle 2 (fails to get the lock on sqlite_master). Also
350# try to compile a read of the same database using handle 2 (also fails).
351# Finally, compile a read of the other database using handle 2. This
352# should also fail.
353#
354ifcapable compound {
355  do_test shared-$av.4.4.1.2 {
356    # Sanity check 1: Check that the schema is what we think it is when viewed
357    # via handle 1.
358    execsql {
359      CREATE TABLE test2.ghi(g, h, i);
360      SELECT 'test.db:'||name FROM sqlite_master
361      UNION ALL
362      SELECT 'test2.db:'||name FROM test2.sqlite_master;
363    }
364  } {test.db:abc test.db:def test2.db:ghi}
365  do_test shared-$av.4.4.1.2 {
366    # Sanity check 2: Check that the schema is what we think it is when viewed
367    # via handle 2.
368    execsql {
369      SELECT 'test2.db:'||name FROM sqlite_master
370      UNION ALL
371      SELECT 'test.db:'||name FROM test.sqlite_master;
372    } db2
373  } {test2.db:ghi test.db:abc test.db:def}
374}
375
376do_test shared-$av.4.4.2 {
377  set ::DB2 [sqlite3_connection_pointer db2]
378  set sql {SELECT * FROM abc}
379  set ::STMT1 [sqlite3_prepare $::DB2 $sql -1 DUMMY]
380  execsql {
381    BEGIN;
382    CREATE TABLE jkl(j, k, l);
383  }
384  sqlite3_step $::STMT1
385} {SQLITE_ERROR}
386do_test shared-$av.4.4.3 {
387  sqlite3_finalize $::STMT1
388} {SQLITE_LOCKED}
389do_test shared-$av.4.4.4 {
390  set rc [catch {
391    set ::STMT1 [sqlite3_prepare $::DB2 $sql -1 DUMMY]
392  } msg]
393  list $rc $msg
394} {1 {(6) database schema is locked: test}}
395do_test shared-$av.4.4.5 {
396  set rc [catch {
397    set ::STMT1 [sqlite3_prepare $::DB2 "SELECT * FROM ghi" -1 DUMMY]
398  } msg]
399  list $rc $msg
400} {1 {(6) database schema is locked: test}}
401
402
403catch {db2 close}
404catch {db close}
405
406#--------------------------------------------------------------------------
407# Tests shared-5.*
408#
409foreach db [list test.db test1.db test2.db test3.db] {
410  file delete -force $db ${db}-journal
411}
412do_test shared-$av.5.1.1 {
413  sqlite3 db1 test.db
414  sqlite3 db2 test.db
415  execsql {
416    ATTACH 'test1.db' AS test1;
417    ATTACH 'test2.db' AS test2;
418    ATTACH 'test3.db' AS test3;
419  } db1
420  execsql {
421    ATTACH 'test3.db' AS test3;
422    ATTACH 'test2.db' AS test2;
423    ATTACH 'test1.db' AS test1;
424  } db2
425} {}
426do_test shared-$av.5.1.2 {
427  execsql {
428    CREATE TABLE test1.t1(a, b);
429    CREATE INDEX test1.i1 ON t1(a, b);
430  } db1
431} {}
432ifcapable view {
433  do_test shared-$av.5.1.3 {
434    execsql {
435      CREATE VIEW test1.v1 AS SELECT * FROM t1;
436    } db1
437  } {}
438}
439ifcapable trigger {
440  do_test shared-$av.5.1.4 {
441    execsql {
442      CREATE TRIGGER test1.trig1 AFTER INSERT ON t1 BEGIN
443        INSERT INTO t1 VALUES(new.a, new.b);
444      END;
445    } db1
446  } {}
447}
448do_test shared-$av.5.1.5 {
449  execsql {
450    DROP INDEX i1;
451  } db2
452} {}
453ifcapable view {
454  do_test shared-$av.5.1.6 {
455    execsql {
456      DROP VIEW v1;
457    } db2
458  } {}
459}
460ifcapable trigger {
461  do_test shared-$av.5.1.7 {
462    execsql {
463      DROP TRIGGER trig1;
464    } db2
465  } {}
466}
467do_test shared-$av.5.1.8 {
468  execsql {
469    DROP TABLE t1;
470  } db2
471} {}
472ifcapable compound {
473  do_test shared-$av.5.1.9 {
474    execsql {
475      SELECT * FROM sqlite_master UNION ALL SELECT * FROM test1.sqlite_master
476    } db1
477  } {}
478}
479
480#--------------------------------------------------------------------------
481# Tests shared-6.* test that a query obtains all the read-locks it needs
482# before starting execution of the query. This means that there is no chance
483# some rows of data will be returned before a lock fails and SQLITE_LOCK
484# is returned.
485#
486do_test shared-$av.6.1.1 {
487  execsql {
488    CREATE TABLE t1(a, b);
489    CREATE TABLE t2(a, b);
490    INSERT INTO t1 VALUES(1, 2);
491    INSERT INTO t2 VALUES(3, 4);
492  } db1
493} {}
494ifcapable compound {
495  do_test shared-$av.6.1.2 {
496    execsql {
497      SELECT * FROM t1 UNION ALL SELECT * FROM t2;
498    } db2
499  } {1 2 3 4}
500}
501do_test shared-$av.6.1.3 {
502  # Establish a write lock on table t2 via connection db2. Then make a
503  # UNION all query using connection db1 that first accesses t1, followed
504  # by t2. If the locks are grabbed at the start of the statement (as
505  # they should be), no rows are returned. If (as was previously the case)
506  # they are grabbed as the tables are accessed, the t1 rows will be
507  # returned before the query fails.
508  #
509  execsql {
510    BEGIN;
511    INSERT INTO t2 VALUES(5, 6);
512  } db2
513  set ret [list]
514  catch {
515    db1 eval {SELECT * FROM t1 UNION ALL SELECT * FROM t2} {
516      lappend ret $a $b
517    }
518  }
519  set ret
520} {}
521do_test shared-$av.6.1.4 {
522  execsql {
523    COMMIT;
524    BEGIN;
525    INSERT INTO t1 VALUES(7, 8);
526  } db2
527  set ret [list]
528  catch {
529    db1 eval {
530      SELECT (CASE WHEN a>4 THEN (SELECT a FROM t1) ELSE 0 END) AS d FROM t2;
531    } {
532      lappend ret $d
533    }
534  }
535  set ret
536} {}
537
538catch {db1 close}
539catch {db2 close}
540foreach f [list test.db test2.db] {
541  file delete -force $f ${f}-journal
542}
543
544#--------------------------------------------------------------------------
545# Tests shared-7.* test auto-vacuum does not invalidate cursors from
546# other shared-cache users when it reorganizes the database on
547# COMMIT.
548#
549do_test shared-$av.7.1 {
550  # This test case sets up a test database in auto-vacuum mode consisting
551  # of two tables, t1 and t2. Both have a single index. Table t1 is
552  # populated first (so consists of pages toward the start of the db file),
553  # t2 second (pages toward the end of the file).
554  sqlite3 db test.db
555  sqlite3 db2 test.db
556  execsql {
557    BEGIN;
558    CREATE TABLE t1(a PRIMARY KEY, b);
559    CREATE TABLE t2(a PRIMARY KEY, b);
560  }
561  set ::contents {}
562  for {set i 0} {$i < 100} {incr i} {
563    set a [string repeat "$i " 20]
564    set b [string repeat "$i " 20]
565    db eval {
566      INSERT INTO t1 VALUES(:a, :b);
567    }
568    lappend ::contents [list [expr $i+1] $a $b]
569  }
570  execsql {
571    INSERT INTO t2 SELECT * FROM t1;
572    COMMIT;
573  }
574} {}
575do_test shared-$av.7.2 {
576  # This test case deletes the contents of table t1 (the one at the start of
577  # the file) while many cursors are open on table t2 and its index. All of
578  # the non-root pages will be moved from the end to the start of the file
579  # when the DELETE is committed - this test verifies that moving the pages
580  # does not disturb the open cursors.
581  #
582
583  proc lockrow {db tbl oids body} {
584    set ret [list]
585    db eval "SELECT oid AS i, a, b FROM $tbl ORDER BY a" {
586      if {$i==[lindex $oids 0]} {
587        set noids [lrange $oids 1 end]
588        if {[llength $noids]==0} {
589          set subret [eval $body]
590        } else {
591          set subret [lockrow $db $tbl $noids $body]
592        }
593      }
594      lappend ret [list $i $a $b]
595    }
596    return [linsert $subret 0 $ret]
597  }
598  proc locktblrows {db tbl body} {
599    set oids [db eval "SELECT oid FROM $tbl"]
600    lockrow $db $tbl $oids $body
601  }
602
603  set scans [locktblrows db t2 {
604    execsql {
605      DELETE FROM t1;
606    } db2
607  }]
608  set error 0
609
610  # Test that each SELECT query returned the expected contents of t2.
611  foreach s $scans {
612    if {[lsort -integer -index 0 $s]!=$::contents} {
613      set error 1
614    }
615  }
616  set error
617} {0}
618
619catch {db close}
620catch {db2 close}
621unset -nocomplain contents
622
623#--------------------------------------------------------------------------
624# The following tests try to trick the shared-cache code into assuming
625# the wrong encoding for a database.
626#
627file delete -force test.db test.db-journal
628ifcapable utf16 {
629  do_test shared-$av.8.1.1 {
630    sqlite3 db test.db
631    execsql {
632      PRAGMA encoding = 'UTF-16';
633      SELECT * FROM sqlite_master;
634    }
635  } {}
636  do_test shared-$av.8.1.2 {
637    string range [execsql {PRAGMA encoding;}] 0 end-2
638  } {UTF-16}
639
640  do_test shared-$av.8.1.3 {
641    sqlite3 db2 test.db
642    execsql {
643      PRAGMA encoding = 'UTF-8';
644      CREATE TABLE abc(a, b, c);
645    } db2
646  } {}
647  do_test shared-$av.8.1.4 {
648    execsql {
649      SELECT * FROM sqlite_master;
650    }
651  } "table abc abc [expr $AUTOVACUUM?3:2] {CREATE TABLE abc(a, b, c)}"
652  do_test shared-$av.8.1.5 {
653    db2 close
654    execsql {
655      PRAGMA encoding;
656    }
657  } {UTF-8}
658
659  file delete -force test2.db test2.db-journal
660  do_test shared-$av.8.2.1 {
661    execsql {
662      ATTACH 'test2.db' AS aux;
663      SELECT * FROM aux.sqlite_master;
664    }
665  } {}
666  do_test shared-$av.8.2.2 {
667    sqlite3 db2 test2.db
668    execsql {
669      PRAGMA encoding = 'UTF-16';
670      CREATE TABLE def(d, e, f);
671    } db2
672    string range [execsql {PRAGMA encoding;} db2] 0 end-2
673  } {UTF-16}
674
675  catch {db close}
676  catch {db2 close}
677  file delete -force test.db test2.db
678
679  do_test shared-$av.8.3.2 {
680    sqlite3 db test.db
681    execsql { CREATE TABLE def(d, e, f) }
682    execsql { PRAGMA encoding }
683  } {UTF-8}
684  do_test shared-$av.8.3.3 {
685    set zDb16 "[encoding convertto unicode test.db]\x00\x00"
686    set db16 [sqlite3_open16 $zDb16 {}]
687
688    set stmt [sqlite3_prepare $db16 "SELECT sql FROM sqlite_master" -1 DUMMY]
689    sqlite3_step $stmt
690    set sql [sqlite3_column_text $stmt 0]
691    sqlite3_finalize $stmt
692    set sql
693  } {CREATE TABLE def(d, e, f)}
694  do_test shared-$av.8.3.4 {
695    set stmt [sqlite3_prepare $db16 "PRAGMA encoding" -1 DUMMY]
696    sqlite3_step $stmt
697    set enc [sqlite3_column_text $stmt 0]
698    sqlite3_finalize $stmt
699    set enc
700  } {UTF-8}
701
702  sqlite3_close $db16
703
704# Bug #2547 is causing this to fail.
705if 0 {
706  do_test shared-$av.8.2.3 {
707    catchsql {
708      SELECT * FROM aux.sqlite_master;
709    }
710  } {1 {attached databases must use the same text encoding as main database}}
711}
712}
713
714catch {db close}
715catch {db2 close}
716file delete -force test.db test2.db
717
718#---------------------------------------------------------------------------
719# The following tests - shared-9.* - test interactions between TEMP triggers
720# and shared-schemas.
721#
722ifcapable trigger&&tempdb {
723
724do_test shared-$av.9.1 {
725  sqlite3 db test.db
726  sqlite3 db2 test.db
727  execsql {
728    CREATE TABLE abc(a, b, c);
729    CREATE TABLE abc_mirror(a, b, c);
730    CREATE TEMP TRIGGER BEFORE INSERT ON abc BEGIN
731      INSERT INTO abc_mirror(a, b, c) VALUES(new.a, new.b, new.c);
732    END;
733    INSERT INTO abc VALUES(1, 2, 3);
734    SELECT * FROM abc_mirror;
735  }
736} {1 2 3}
737do_test shared-$av.9.2 {
738  execsql {
739    INSERT INTO abc VALUES(4, 5, 6);
740    SELECT * FROM abc_mirror;
741  } db2
742} {1 2 3}
743do_test shared-$av.9.3 {
744  db close
745  db2 close
746} {}
747
748} ; # End shared-9.*
749
750#---------------------------------------------------------------------------
751# The following tests - shared-10.* - test that the library behaves
752# correctly when a connection to a shared-cache is closed.
753#
754do_test shared-$av.10.1 {
755  # Create a small sample database with two connections to it (db and db2).
756  file delete -force test.db
757  sqlite3 db  test.db
758  sqlite3 db2 test.db
759  execsql {
760    CREATE TABLE ab(a PRIMARY KEY, b);
761    CREATE TABLE de(d PRIMARY KEY, e);
762    INSERT INTO ab VALUES('Chiang Mai', 100000);
763    INSERT INTO ab VALUES('Bangkok', 8000000);
764    INSERT INTO de VALUES('Ubon', 120000);
765    INSERT INTO de VALUES('Khon Kaen', 200000);
766  }
767} {}
768do_test shared-$av.10.2 {
769  # Open a read-transaction with the first connection, a write-transaction
770  # with the second.
771  execsql {
772    BEGIN;
773    SELECT * FROM ab;
774  }
775  execsql {
776    BEGIN;
777    INSERT INTO de VALUES('Pataya', 30000);
778  } db2
779} {}
780do_test shared-$av.10.3 {
781  # An external connection should be able to read the database, but not
782  # prepare a write operation.
783  if {$::tcl_platform(platform)=="unix"} {
784    sqlite3 db3 ./test.db
785  } else {
786    sqlite3 db3 TEST.DB
787  }
788  execsql {
789    SELECT * FROM ab;
790  } db3
791  catchsql {
792    BEGIN;
793    INSERT INTO de VALUES('Pataya', 30000);
794  } db3
795} {1 {database is locked}}
796do_test shared-$av.10.4 {
797  # Close the connection with the write-transaction open
798  db2 close
799} {}
800do_test shared-$av.10.5 {
801  # Test that the db2 transaction has been automatically rolled back.
802  # If it has not the ('Pataya', 30000) entry will still be in the table.
803  execsql {
804    SELECT * FROM de;
805  }
806} {Ubon 120000 {Khon Kaen} 200000}
807do_test shared-$av.10.5 {
808  # Closing db2 should have dropped the shared-cache back to a read-lock.
809  # So db3 should be able to prepare a write...
810  catchsql {INSERT INTO de VALUES('Pataya', 30000);} db3
811} {0 {}}
812do_test shared-$av.10.6 {
813  # ... but not commit it.
814  catchsql {COMMIT} db3
815} {1 {database is locked}}
816do_test shared-$av.10.7 {
817  # Commit the (read-only) db transaction. Check via db3 to make sure the
818  # contents of table "de" are still as they should be.
819  execsql {
820    COMMIT;
821  }
822  execsql {
823    SELECT * FROM de;
824  } db3
825} {Ubon 120000 {Khon Kaen} 200000 Pataya 30000}
826do_test shared-$av.10.9 {
827  # Commit the external transaction.
828  catchsql {COMMIT} db3
829} {0 {}}
830integrity_check shared-$av.10.10
831do_test shared-$av.10.11 {
832  db close
833  db3 close
834} {}
835
836do_test shared-$av.11.1 {
837  file delete -force test.db
838  sqlite3 db  test.db
839  sqlite3 db2 test.db
840  execsql {
841    CREATE TABLE abc(a, b, c);
842    CREATE TABLE abc2(a, b, c);
843    BEGIN;
844    INSERT INTO abc VALUES(1, 2, 3);
845  }
846} {}
847do_test shared-$av.11.2 {
848  catchsql {BEGIN;} db2
849  catchsql {SELECT * FROM abc;} db2
850} {1 {database table is locked: abc}}
851do_test shared-$av.11.3 {
852  catchsql {BEGIN} db2
853} {1 {cannot start a transaction within a transaction}}
854do_test shared-$av.11.4 {
855  catchsql {SELECT * FROM abc2;} db2
856} {0 {}}
857do_test shared-$av.11.5 {
858  catchsql {INSERT INTO abc2 VALUES(1, 2, 3);} db2
859} {1 {database is locked}}
860do_test shared-$av.11.6 {
861  catchsql {SELECT * FROM abc2}
862} {0 {}}
863do_test shared-$av.11.6 {
864  execsql {
865    ROLLBACK;
866    PRAGMA read_uncommitted = 1;
867  } db2
868} {}
869do_test shared-$av.11.7 {
870  execsql {
871    INSERT INTO abc2 VALUES(4, 5, 6);
872    INSERT INTO abc2 VALUES(7, 8, 9);
873  }
874} {}
875do_test shared-$av.11.8 {
876  set res [list]
877  breakpoint
878  db2 eval {
879    SELECT abc.a as I, abc2.a as II FROM abc, abc2;
880  } {
881    execsql {
882      DELETE FROM abc WHERE 1;
883    }
884    lappend res $I $II
885  }
886  set res
887} {1 4 {} 7}
888if {[llength [info command sqlite3_shared_cache_report]]==1} {
889  do_test shared-$av.11.9 {
890    string tolower [sqlite3_shared_cache_report]
891  } [string tolower [list [file nativename [file normalize test.db]] 2]]
892}
893
894do_test shared-$av.11.11 {
895  db close
896  db2 close
897} {}
898
899# This tests that if it is impossible to free any pages, SQLite will
900# exceed the limit set by PRAGMA cache_size.
901file delete -force test.db test.db-journal
902sqlite3 db test.db
903ifcapable pager_pragmas {
904  do_test shared-$av.12.1 {
905    execsql {
906      PRAGMA cache_size = 10;
907      PRAGMA cache_size;
908    }
909  } {10}
910}
911do_test shared-$av.12.2 {
912  set ::db_handles [list]
913  for {set i 1} {$i < 15} {incr i} {
914    lappend ::db_handles db$i
915    sqlite3 db$i test.db
916    execsql "CREATE TABLE db${i}(a, b, c)" db$i
917    execsql "INSERT INTO db${i} VALUES(1, 2, 3)"
918  }
919} {}
920proc nested_select {handles} {
921  [lindex $handles 0] eval "SELECT * FROM [lindex $handles 0]" {
922    lappend ::res $a $b $c
923    if {[llength $handles]>1} {
924      nested_select [lrange $handles 1 end]
925    }
926  }
927}
928do_test shared-$av.12.3 {
929  set ::res [list]
930  nested_select $::db_handles
931  set ::res
932} [string range [string repeat "1 2 3 " [llength $::db_handles]] 0 end-1]
933
934do_test shared-$av.12.X {
935  db close
936  foreach h $::db_handles {
937    $h close
938  }
939} {}
940
941}
942
943sqlite3_enable_shared_cache $::enable_shared_cache
944finish_test
945