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 #***********************************************************************
11 # This file implements regression tests for SQLite library. The
12 # focus of this file is testing the sqlite3_unlock_notify() API.
15 set testdir [file dirname $argv0]
16 source $testdir/tester.tcl
18 # This script only runs if shared-cache and unlock-notify are available.
20 ifcapable !unlock_notify||!shared_cache {
25 set esc [sqlite3_enable_shared_cache 1]
28 forcedelete test.db2 test.db2-journal test.db2-wal
33 CREATE TABLE t1(a, b);
34 INSERT INTO t1 VALUES('t1 A', 't1 B');
39 CREATE TABLE t2(a, b);
40 INSERT INTO t2 VALUES('t2 A', 't2 B');
47 INSERT INTO t2 VALUES('t2 C', 't2 D');
51 catchsql { ATTACH 'test.db2' AS aux }
55 catchsql { SELECT * FROM t2 }
56 } {1 {database schema is locked: aux}}
59 list [sqlite3_errcode db] [sqlite3_extended_errcode db]
60 } {SQLITE_LOCKED SQLITE_LOCKED_SHAREDCACHE}
63 sqlite3_extended_result_codes db 1
64 catch { set ::stmt [sqlite3_prepare_v2 db "SELECT * FROM t2" -1 tail] } msg
66 } {(262) database schema is locked: aux}
70 db unlock_notify { set ::res $::when }
72 execsql { COMMIT } db2
76 catchsql { SELECT * FROM t2 }
77 } {0 {{t2 A} {t2 B} {t2 C} {t2 D}}}
81 set err {{1 {unable to open database: test.db2}}}
84 # When a new database is attached, the connection doing the attaching
85 # tries to load any unloaded schemas for both the new database and any
86 # already attached databases (including the main database). If it is
87 # unable to load any such schemas, then the ATTACH statement fails.
89 # This block tests that if the loading of schemas as a result of an
90 # ATTACH fails due to locks on the schema table held by other shared-cache
91 # connections the extended error code is SQLITE_LOCKED_SHAREDCACHE and
92 # it is possible to use the unlock-notify mechanism to determine when
93 # the ATTACH might succeed.
95 # This test does not work for test-permutations that specify SQL to
96 # be executed as part of the [sqlite3] command that opens the database.
97 # Executing such SQL causes SQLite to load the database schema into memory
98 # earlier than expected, causing test cases to fail.
100 if {[presql] == ""} {
105 enable_extended_errors
109 0 0 0 0 $err SQLITE_LOCKED SQLITE_LOCKED_SHAREDCACHE
110 1 0 0 1 $err SQLITE_LOCKED_SHAREDCACHE SQLITE_LOCKED_SHAREDCACHE
111 2 0 1 0 $noerr SQLITE_OK SQLITE_OK
112 3 0 1 1 $noerr SQLITE_OK SQLITE_OK
113 4 1 0 0 $err SQLITE_LOCKED SQLITE_LOCKED_SHAREDCACHE
114 5 1 0 1 $err SQLITE_LOCKED_SHAREDCACHE SQLITE_LOCKED_SHAREDCACHE
115 6 1 1 0 $noerr SQLITE_OK SQLITE_OK
116 7 1 1 1 $noerr SQLITE_OK SQLITE_OK
119 do_test notify3-2.$tn.1 {
125 sqlite3_extended_result_codes db1 $enable_extended_errors
126 sqlite3_extended_result_codes db2 $enable_extended_errors
128 if { $db1_loaded } { db1 eval "SELECT * FROM sqlite_master" }
129 if { $db2_loaded } { db2 eval "SELECT * FROM sqlite_master" }
131 db2 eval "BEGIN EXCLUSIVE"
132 catchsql "ATTACH 'test.db2' AS two" db1
135 do_test notify3-2.$tn.2 {
136 list [sqlite3_errcode db1] [sqlite3_extended_errcode db1]
137 } [list $error1 $error2]
139 do_test notify3-2.$tn.3 {
140 db1 unlock_notify {set invoked 1}
151 sqlite3_enable_shared_cache $esc