danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 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 | #*********************************************************************** |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 11 | # |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame^] | 12 | # $Id: shared.test,v 1.29 2007/12/13 21:54:11 drh Exp $ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 13 | |
| 14 | set testdir [file dirname $argv0] |
| 15 | source $testdir/tester.tcl |
| 16 | db close |
| 17 | |
danielk1977 | 5a8f937 | 2007-10-09 08:29:32 +0000 | [diff] [blame] | 18 | # These tests cannot be run without the ATTACH command. |
| 19 | # |
| 20 | ifcapable !shared_cache||!attach { |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 21 | finish_test |
| 22 | return |
| 23 | } |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 24 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 25 | set ::enable_shared_cache [sqlite3_enable_shared_cache 1] |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 26 | |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 27 | foreach av [list 0 1] { |
| 28 | |
danielk1977 | bab45c6 | 2006-01-16 15:14:27 +0000 | [diff] [blame] | 29 | # Open the database connection and execute the auto-vacuum pragma |
| 30 | file delete -force test.db |
| 31 | sqlite3 db test.db |
| 32 | |
| 33 | ifcapable 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 | } |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 43 | } |
| 44 | |
danielk1977 | 191c3e7 | 2006-01-19 07:18:14 +0000 | [diff] [blame] | 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 | # |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 50 | incr av |
| 51 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 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. |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 59 | # shared-3.*: Simple test of read-uncommitted mode. |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 60 | # shared-4.*: Check that the schema is locked and unlocked correctly. |
danielk1977 | aaf2268 | 2006-01-06 15:03:48 +0000 | [diff] [blame] | 61 | # shared-5.*: Test that creating/dropping schema items works when databases |
| 62 | # are attached in different orders to different handles. |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 63 | # shared-6.*: Locking, UNION ALL queries and sub-queries. |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 64 | # shared-7.*: Autovacuum and shared-cache. |
danielk1977 | ed42931 | 2006-01-19 08:43:31 +0000 | [diff] [blame] | 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(). |
danielk1977 | 4b202ae | 2006-01-23 05:50:58 +0000 | [diff] [blame] | 68 | # shared-11.*: Test transaction locking. |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 69 | # |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 70 | |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 71 | do_test shared-$av.1.1 { |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 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 |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 76 | set ::sqlite_open_file_count |
| 77 | } {1} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 78 | do_test shared-$av.1.2 { |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 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} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 89 | do_test shared-$av.1.3 { |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 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} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 101 | do_test shared-$av.1.4 { |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 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 |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 108 | } {1 {database table is locked: abc}} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 109 | do_test shared-$av.1.5 { |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 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 |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 116 | } {1 {database table is locked: sqlite_master}} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 117 | do_test shared-$av.1.6 { |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 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. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 123 | execsql { |
| 124 | CREATE TABLE def(d, e, f); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 125 | INSERT INTO def VALUES('IV', 'V', 'VI'); |
| 126 | } |
| 127 | } {} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 128 | do_test shared-$av.1.7 { |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 129 | # Read from the sqlite_master table with connection 1 (inside the |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 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. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 133 | execsql { |
| 134 | SELECT * FROM sqlite_master; |
| 135 | } |
| 136 | catchsql { |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 137 | SELECT * FROM sqlite_master; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 138 | } db2 |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 139 | } {1 {database schema is locked: main}} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 140 | do_test shared-$av.1.8 { |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 141 | # Commit the connection 1 transaction. |
| 142 | execsql { |
| 143 | COMMIT; |
| 144 | } |
| 145 | } {} |
| 146 | |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 147 | do_test shared-$av.2.1 { |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 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). |
drh | c693e9e | 2006-01-23 21:38:03 +0000 | [diff] [blame] | 151 | if {$::tcl_platform(platform)=="unix"} { |
| 152 | sqlite3 db3 ./test.db |
| 153 | } else { |
| 154 | sqlite3 db3 TEST.DB |
| 155 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 156 | set ::sqlite_open_file_count |
| 157 | } {2} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 158 | do_test shared-$av.2.2 { |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 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 |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 172 | } {1 {database table is locked: abc}} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 173 | do_test shared-$av.2.3 { |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 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 | ] |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 196 | } {0 {IV V VI} 1 {database table is locked: def}} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 197 | do_test shared-$av.2.4 { |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 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 | ] |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 209 | } {0 {IV V VI VII VIII IX} 1 {database is locked}} |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 210 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 211 | catchsql COMMIT db2 |
| 212 | |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 213 | do_test shared-$av.3.1.1 { |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 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 " |
danielk1977 | 191c3e7 | 2006-01-19 07:18:14 +0000 | [diff] [blame] | 219 | CREATE TABLE seq(i PRIMARY KEY, x); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 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] |
danielk1977 | 191c3e7 | 2006-01-19 07:18:14 +0000 | [diff] [blame] | 227 | db2 eval {SELECT i FROM seq ORDER BY i} { |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 228 | if {$i < 4} { |
danielk1977 | 1576cd9 | 2006-01-14 08:02:28 +0000 | [diff] [blame] | 229 | set max [execsql {SELECT max(i) FROM seq}] |
| 230 | db eval { |
danielk1977 | 3bdca9c | 2006-01-17 09:35:01 +0000 | [diff] [blame] | 231 | INSERT INTO seq SELECT i + :max, x FROM seq; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 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} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 238 | do_test shared-$av.3.1.2 { |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 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} { |
danielk1977 | 3bdca9c | 2006-01-17 09:35:01 +0000 | [diff] [blame] | 245 | db eval {DELETE FROM seq WHERE i = :i} |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 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} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 250 | do_test shared-$av.3.1.3 { |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 251 | execsql { |
| 252 | SELECT * FROM seq; |
| 253 | } |
| 254 | } {} |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 255 | |
| 256 | catch {db close} |
| 257 | catch {db2 close} |
| 258 | catch {db3 close} |
| 259 | |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 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 | # |
| 280 | file delete -force test.db |
| 281 | file delete -force test2.db |
| 282 | file delete -force test2.db-journal |
| 283 | sqlite3 db test.db |
| 284 | sqlite3 db2 test2.db |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 285 | do_test shared-$av.4.1.1 { |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 286 | set sqlite_open_file_count |
| 287 | } {2} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 288 | do_test shared-$av.4.1.2 { |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 289 | execsql {ATTACH 'test2.db' AS test2} |
| 290 | set sqlite_open_file_count |
| 291 | } {2} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 292 | do_test shared-$av.4.1.3 { |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 293 | execsql {ATTACH 'test.db' AS test} db2 |
| 294 | set sqlite_open_file_count |
| 295 | } {2} |
| 296 | |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 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 | # |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 301 | do_test shared-$av.4.2.1 { |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 302 | execsql { |
| 303 | CREATE TABLE abc(a, b, c); |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 304 | CREATE TABLE def(d, e, f); |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 305 | INSERT INTO abc VALUES('i', 'ii', 'iii'); |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 306 | INSERT INTO def VALUES('I', 'II', 'III'); |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 307 | } |
| 308 | } {} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 309 | do_test shared-$av.4.2.2 { |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 310 | execsql { |
| 311 | SELECT * FROM test.abc; |
| 312 | } db2 |
| 313 | } {i ii iii} |
| 314 | |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 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 | # |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 319 | do_test shared-$av.4.3.1 { |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 320 | execsql { |
| 321 | BEGIN; |
| 322 | SELECT * FROM test.abc; |
| 323 | } db2 |
| 324 | } {i ii iii} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 325 | do_test shared-$av.4.3.2 { |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 326 | catchsql { |
| 327 | INSERT INTO abc VALUES('iv', 'v', 'vi'); |
| 328 | } |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 329 | } {1 {database table is locked: abc}} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 330 | do_test shared-$av.4.3.3 { |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 331 | catchsql { |
| 332 | CREATE TABLE ghi(g, h, i); |
| 333 | } |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 334 | } {1 {database table is locked: sqlite_master}} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 335 | do_test shared-$av.4.3.3 { |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 336 | catchsql { |
| 337 | INSERT INTO def VALUES('IV', 'V', 'VI'); |
| 338 | } |
| 339 | } {0 {}} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 340 | do_test shared-$av.4.3.4 { |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 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 | # |
danielk1977 | ff89079 | 2006-01-16 16:24:25 +0000 | [diff] [blame] | 354 | ifcapable 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 | } |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 375 | |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 376 | do_test shared-$av.4.4.2 { |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 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} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 386 | do_test shared-$av.4.4.3 { |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 387 | sqlite3_finalize $::STMT1 |
| 388 | } {SQLITE_LOCKED} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 389 | do_test shared-$av.4.4.4 { |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 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}} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 395 | do_test shared-$av.4.4.5 { |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 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 | |
danielk1977 | aaf2268 | 2006-01-06 15:03:48 +0000 | [diff] [blame] | 402 | |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 403 | catch {db2 close} |
| 404 | catch {db close} |
| 405 | |
danielk1977 | aaf2268 | 2006-01-06 15:03:48 +0000 | [diff] [blame] | 406 | #-------------------------------------------------------------------------- |
| 407 | # Tests shared-5.* |
| 408 | # |
| 409 | foreach db [list test.db test1.db test2.db test3.db] { |
| 410 | file delete -force $db ${db}-journal |
| 411 | } |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 412 | do_test shared-$av.5.1.1 { |
danielk1977 | aaf2268 | 2006-01-06 15:03:48 +0000 | [diff] [blame] | 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 | } {} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 426 | do_test shared-$av.5.1.2 { |
danielk1977 | aaf2268 | 2006-01-06 15:03:48 +0000 | [diff] [blame] | 427 | execsql { |
| 428 | CREATE TABLE test1.t1(a, b); |
| 429 | CREATE INDEX test1.i1 ON t1(a, b); |
danielk1977 | aaf2268 | 2006-01-06 15:03:48 +0000 | [diff] [blame] | 430 | } db1 |
danielk1977 | 3bdca9c | 2006-01-17 09:35:01 +0000 | [diff] [blame] | 431 | } {} |
| 432 | ifcapable view { |
| 433 | do_test shared-$av.5.1.3 { |
| 434 | execsql { |
| 435 | CREATE VIEW test1.v1 AS SELECT * FROM t1; |
| 436 | } db1 |
| 437 | } {} |
| 438 | } |
| 439 | ifcapable 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 | } |
| 448 | do_test shared-$av.5.1.5 { |
danielk1977 | aaf2268 | 2006-01-06 15:03:48 +0000 | [diff] [blame] | 449 | execsql { |
| 450 | DROP INDEX i1; |
danielk1977 | 3bdca9c | 2006-01-17 09:35:01 +0000 | [diff] [blame] | 451 | } db2 |
| 452 | } {} |
| 453 | ifcapable view { |
| 454 | do_test shared-$av.5.1.6 { |
| 455 | execsql { |
| 456 | DROP VIEW v1; |
| 457 | } db2 |
| 458 | } {} |
| 459 | } |
| 460 | ifcapable trigger { |
| 461 | do_test shared-$av.5.1.7 { |
| 462 | execsql { |
| 463 | DROP TRIGGER trig1; |
| 464 | } db2 |
| 465 | } {} |
| 466 | } |
| 467 | do_test shared-$av.5.1.8 { |
| 468 | execsql { |
danielk1977 | aaf2268 | 2006-01-06 15:03:48 +0000 | [diff] [blame] | 469 | DROP TABLE t1; |
| 470 | } db2 |
| 471 | } {} |
danielk1977 | ff89079 | 2006-01-16 16:24:25 +0000 | [diff] [blame] | 472 | ifcapable compound { |
danielk1977 | 3bdca9c | 2006-01-17 09:35:01 +0000 | [diff] [blame] | 473 | do_test shared-$av.5.1.9 { |
danielk1977 | ff89079 | 2006-01-16 16:24:25 +0000 | [diff] [blame] | 474 | execsql { |
| 475 | SELECT * FROM sqlite_master UNION ALL SELECT * FROM test1.sqlite_master |
| 476 | } db1 |
| 477 | } {} |
| 478 | } |
danielk1977 | aaf2268 | 2006-01-06 15:03:48 +0000 | [diff] [blame] | 479 | |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 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 | # |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 486 | do_test shared-$av.6.1.1 { |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 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 |
danielk1977 | ff89079 | 2006-01-16 16:24:25 +0000 | [diff] [blame] | 493 | } {} |
| 494 | ifcapable 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 | } |
| 501 | do_test shared-$av.6.1.3 { |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 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 | } {} |
danielk1977 | ff89079 | 2006-01-16 16:24:25 +0000 | [diff] [blame] | 521 | do_test shared-$av.6.1.4 { |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 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 | |
danielk1977 | aaf2268 | 2006-01-06 15:03:48 +0000 | [diff] [blame] | 538 | catch {db1 close} |
| 539 | catch {db2 close} |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 540 | foreach 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 | # |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 549 | do_test shared-$av.7.1 { |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 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). |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 554 | sqlite3 db test.db |
| 555 | sqlite3 db2 test.db |
| 556 | execsql { |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 557 | BEGIN; |
| 558 | CREATE TABLE t1(a PRIMARY KEY, b); |
| 559 | CREATE TABLE t2(a PRIMARY KEY, b); |
| 560 | } |
drh | 7a91dd8 | 2006-01-11 01:08:34 +0000 | [diff] [blame] | 561 | set ::contents {} |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 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 { |
danielk1977 | 3bdca9c | 2006-01-17 09:35:01 +0000 | [diff] [blame] | 566 | INSERT INTO t1 VALUES(:a, :b); |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 567 | } |
| 568 | lappend ::contents [list [expr $i+1] $a $b] |
| 569 | } |
| 570 | execsql { |
| 571 | INSERT INTO t2 SELECT * FROM t1; |
| 572 | COMMIT; |
| 573 | } |
danielk1977 | bab45c6 | 2006-01-16 15:14:27 +0000 | [diff] [blame] | 574 | } {} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 575 | do_test shared-$av.7.2 { |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 576 | # This test case deletes the contents of table t1 (the one at the start of |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame^] | 577 | # the file) while many cursors are open on table t2 and its index. All of |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 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 | |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 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 |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 609 | |
| 610 | # Test that each SELECT query returned the expected contents of t2. |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 611 | foreach s $scans { |
| 612 | if {[lsort -integer -index 0 $s]!=$::contents} { |
| 613 | set error 1 |
| 614 | } |
| 615 | } |
| 616 | set error |
| 617 | } {0} |
| 618 | |
| 619 | catch {db close} |
| 620 | catch {db2 close} |
drh | 7a91dd8 | 2006-01-11 01:08:34 +0000 | [diff] [blame] | 621 | unset -nocomplain contents |
danielk1977 | aaf2268 | 2006-01-06 15:03:48 +0000 | [diff] [blame] | 622 | |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 623 | #-------------------------------------------------------------------------- |
| 624 | # The following tests try to trick the shared-cache code into assuming |
| 625 | # the wrong encoding for a database. |
| 626 | # |
| 627 | file delete -force test.db test.db-journal |
danielk1977 | 3bdca9c | 2006-01-17 09:35:01 +0000 | [diff] [blame] | 628 | ifcapable 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 | do_test shared-$av.8.1.3 { |
| 640 | sqlite3 db2 test.db |
| 641 | execsql { |
| 642 | PRAGMA encoding = 'UTF-8'; |
| 643 | CREATE TABLE abc(a, b, c); |
| 644 | } db2 |
| 645 | } {} |
| 646 | do_test shared-$av.8.1.4 { |
| 647 | execsql { |
| 648 | SELECT * FROM sqlite_master; |
| 649 | } |
| 650 | } "table abc abc [expr $AUTOVACUUM?3:2] {CREATE TABLE abc(a, b, c)}" |
| 651 | do_test shared-$av.8.1.5 { |
| 652 | db2 close |
| 653 | execsql { |
| 654 | PRAGMA encoding; |
| 655 | } |
| 656 | } {UTF-8} |
| 657 | file delete -force test2.db test2.db-journal |
| 658 | do_test shared-$av.8.2.1 { |
| 659 | execsql { |
| 660 | ATTACH 'test2.db' AS aux; |
| 661 | SELECT * FROM aux.sqlite_master; |
| 662 | } |
| 663 | } {} |
| 664 | do_test shared-$av.8.2.2 { |
| 665 | sqlite3 db2 test2.db |
| 666 | execsql { |
| 667 | PRAGMA encoding = 'UTF-16'; |
| 668 | CREATE TABLE def(d, e, f); |
| 669 | } db2 |
| 670 | string range [execsql {PRAGMA encoding;} db2] 0 end-2 |
| 671 | } {UTF-16} |
danielk1977 | d42f8fd | 2007-08-03 07:33:08 +0000 | [diff] [blame] | 672 | |
| 673 | # Bug #2547 is causing this to fail. |
| 674 | if 0 { |
danielk1977 | 3bdca9c | 2006-01-17 09:35:01 +0000 | [diff] [blame] | 675 | do_test shared-$av.8.2.3 { |
| 676 | catchsql { |
| 677 | SELECT * FROM aux.sqlite_master; |
| 678 | } |
| 679 | } {1 {attached databases must use the same text encoding as main database}} |
| 680 | } |
danielk1977 | d42f8fd | 2007-08-03 07:33:08 +0000 | [diff] [blame] | 681 | } |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 682 | |
| 683 | catch {db close} |
| 684 | catch {db2 close} |
danielk1977 | eecfb3e | 2006-01-10 12:31:39 +0000 | [diff] [blame] | 685 | file delete -force test.db test2.db |
| 686 | |
danielk1977 | eecfb3e | 2006-01-10 12:31:39 +0000 | [diff] [blame] | 687 | #--------------------------------------------------------------------------- |
| 688 | # The following tests - shared-9.* - test interactions between TEMP triggers |
| 689 | # and shared-schemas. |
| 690 | # |
| 691 | ifcapable trigger&&tempdb { |
| 692 | |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 693 | do_test shared-$av.9.1 { |
danielk1977 | eecfb3e | 2006-01-10 12:31:39 +0000 | [diff] [blame] | 694 | sqlite3 db test.db |
| 695 | sqlite3 db2 test.db |
| 696 | execsql { |
| 697 | CREATE TABLE abc(a, b, c); |
| 698 | CREATE TABLE abc_mirror(a, b, c); |
| 699 | CREATE TEMP TRIGGER BEFORE INSERT ON abc BEGIN |
| 700 | INSERT INTO abc_mirror(a, b, c) VALUES(new.a, new.b, new.c); |
| 701 | END; |
| 702 | INSERT INTO abc VALUES(1, 2, 3); |
| 703 | SELECT * FROM abc_mirror; |
| 704 | } |
| 705 | } {1 2 3} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 706 | do_test shared-$av.9.2 { |
danielk1977 | eecfb3e | 2006-01-10 12:31:39 +0000 | [diff] [blame] | 707 | execsql { |
| 708 | INSERT INTO abc VALUES(4, 5, 6); |
| 709 | SELECT * FROM abc_mirror; |
| 710 | } db2 |
| 711 | } {1 2 3} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 712 | do_test shared-$av.9.3 { |
danielk1977 | eecfb3e | 2006-01-10 12:31:39 +0000 | [diff] [blame] | 713 | db close |
| 714 | db2 close |
| 715 | } {} |
| 716 | |
| 717 | } ; # End shared-9.* |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 718 | |
danielk1977 | b597f74 | 2006-01-15 11:39:18 +0000 | [diff] [blame] | 719 | #--------------------------------------------------------------------------- |
| 720 | # The following tests - shared-10.* - test that the library behaves |
| 721 | # correctly when a connection to a shared-cache is closed. |
| 722 | # |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 723 | do_test shared-$av.10.1 { |
danielk1977 | b597f74 | 2006-01-15 11:39:18 +0000 | [diff] [blame] | 724 | # Create a small sample database with two connections to it (db and db2). |
| 725 | file delete -force test.db |
| 726 | sqlite3 db test.db |
| 727 | sqlite3 db2 test.db |
| 728 | execsql { |
| 729 | CREATE TABLE ab(a PRIMARY KEY, b); |
| 730 | CREATE TABLE de(d PRIMARY KEY, e); |
| 731 | INSERT INTO ab VALUES('Chiang Mai', 100000); |
| 732 | INSERT INTO ab VALUES('Bangkok', 8000000); |
| 733 | INSERT INTO de VALUES('Ubon', 120000); |
| 734 | INSERT INTO de VALUES('Khon Kaen', 200000); |
| 735 | } |
| 736 | } {} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 737 | do_test shared-$av.10.2 { |
danielk1977 | b597f74 | 2006-01-15 11:39:18 +0000 | [diff] [blame] | 738 | # Open a read-transaction with the first connection, a write-transaction |
| 739 | # with the second. |
| 740 | execsql { |
| 741 | BEGIN; |
| 742 | SELECT * FROM ab; |
| 743 | } |
| 744 | execsql { |
| 745 | BEGIN; |
| 746 | INSERT INTO de VALUES('Pataya', 30000); |
| 747 | } db2 |
| 748 | } {} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 749 | do_test shared-$av.10.3 { |
danielk1977 | b597f74 | 2006-01-15 11:39:18 +0000 | [diff] [blame] | 750 | # An external connection should be able to read the database, but not |
| 751 | # prepare a write operation. |
drh | c693e9e | 2006-01-23 21:38:03 +0000 | [diff] [blame] | 752 | if {$::tcl_platform(platform)=="unix"} { |
| 753 | sqlite3 db3 ./test.db |
| 754 | } else { |
| 755 | sqlite3 db3 TEST.DB |
| 756 | } |
danielk1977 | b597f74 | 2006-01-15 11:39:18 +0000 | [diff] [blame] | 757 | execsql { |
| 758 | SELECT * FROM ab; |
| 759 | } db3 |
| 760 | catchsql { |
| 761 | BEGIN; |
| 762 | INSERT INTO de VALUES('Pataya', 30000); |
| 763 | } db3 |
| 764 | } {1 {database is locked}} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 765 | do_test shared-$av.10.4 { |
danielk1977 | b597f74 | 2006-01-15 11:39:18 +0000 | [diff] [blame] | 766 | # Close the connection with the write-transaction open |
| 767 | db2 close |
| 768 | } {} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 769 | do_test shared-$av.10.5 { |
danielk1977 | b597f74 | 2006-01-15 11:39:18 +0000 | [diff] [blame] | 770 | # Test that the db2 transaction has been automatically rolled back. |
| 771 | # If it has not the ('Pataya', 30000) entry will still be in the table. |
| 772 | execsql { |
| 773 | SELECT * FROM de; |
| 774 | } |
| 775 | } {Ubon 120000 {Khon Kaen} 200000} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 776 | do_test shared-$av.10.5 { |
danielk1977 | b597f74 | 2006-01-15 11:39:18 +0000 | [diff] [blame] | 777 | # Closing db2 should have dropped the shared-cache back to a read-lock. |
| 778 | # So db3 should be able to prepare a write... |
| 779 | catchsql {INSERT INTO de VALUES('Pataya', 30000);} db3 |
| 780 | } {0 {}} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 781 | do_test shared-$av.10.6 { |
danielk1977 | b597f74 | 2006-01-15 11:39:18 +0000 | [diff] [blame] | 782 | # ... but not commit it. |
| 783 | catchsql {COMMIT} db3 |
| 784 | } {1 {database is locked}} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 785 | do_test shared-$av.10.7 { |
danielk1977 | b597f74 | 2006-01-15 11:39:18 +0000 | [diff] [blame] | 786 | # Commit the (read-only) db transaction. Check via db3 to make sure the |
| 787 | # contents of table "de" are still as they should be. |
| 788 | execsql { |
| 789 | COMMIT; |
| 790 | } |
| 791 | execsql { |
| 792 | SELECT * FROM de; |
| 793 | } db3 |
| 794 | } {Ubon 120000 {Khon Kaen} 200000 Pataya 30000} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 795 | do_test shared-$av.10.9 { |
danielk1977 | b597f74 | 2006-01-15 11:39:18 +0000 | [diff] [blame] | 796 | # Commit the external transaction. |
| 797 | catchsql {COMMIT} db3 |
| 798 | } {0 {}} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 799 | integrity_check shared-$av.10.10 |
| 800 | do_test shared-$av.10.11 { |
danielk1977 | b597f74 | 2006-01-15 11:39:18 +0000 | [diff] [blame] | 801 | db close |
| 802 | db3 close |
| 803 | } {} |
| 804 | |
danielk1977 | 4b202ae | 2006-01-23 05:50:58 +0000 | [diff] [blame] | 805 | do_test shared-$av.11.1 { |
| 806 | file delete -force test.db |
| 807 | sqlite3 db test.db |
| 808 | sqlite3 db2 test.db |
| 809 | execsql { |
| 810 | CREATE TABLE abc(a, b, c); |
| 811 | CREATE TABLE abc2(a, b, c); |
| 812 | BEGIN; |
| 813 | INSERT INTO abc VALUES(1, 2, 3); |
| 814 | } |
| 815 | } {} |
| 816 | do_test shared-$av.11.2 { |
| 817 | catchsql {BEGIN;} db2 |
| 818 | catchsql {SELECT * FROM abc;} db2 |
| 819 | } {1 {database table is locked: abc}} |
| 820 | do_test shared-$av.11.3 { |
| 821 | catchsql {BEGIN} db2 |
| 822 | } {1 {cannot start a transaction within a transaction}} |
| 823 | do_test shared-$av.11.4 { |
| 824 | catchsql {SELECT * FROM abc2;} db2 |
| 825 | } {0 {}} |
| 826 | do_test shared-$av.11.5 { |
| 827 | catchsql {INSERT INTO abc2 VALUES(1, 2, 3);} db2 |
| 828 | } {1 {database is locked}} |
| 829 | do_test shared-$av.11.6 { |
| 830 | catchsql {SELECT * FROM abc2} |
| 831 | } {0 {}} |
| 832 | do_test shared-$av.11.6 { |
| 833 | execsql { |
| 834 | ROLLBACK; |
| 835 | PRAGMA read_uncommitted = 1; |
| 836 | } db2 |
| 837 | } {} |
| 838 | do_test shared-$av.11.7 { |
| 839 | execsql { |
| 840 | INSERT INTO abc2 VALUES(4, 5, 6); |
| 841 | INSERT INTO abc2 VALUES(7, 8, 9); |
| 842 | } |
| 843 | } {} |
| 844 | do_test shared-$av.11.8 { |
| 845 | set res [list] |
| 846 | breakpoint |
| 847 | db2 eval { |
| 848 | SELECT abc.a as I, abc2.a as II FROM abc, abc2; |
| 849 | } { |
| 850 | execsql { |
| 851 | DELETE FROM abc WHERE 1; |
| 852 | } |
| 853 | lappend res $I $II |
| 854 | } |
| 855 | set res |
| 856 | } {1 4 {} 7} |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 857 | if {[llength [info command sqlite3_shared_cache_report]]==1} { |
drh | 7c4ac0c | 2007-04-05 11:25:58 +0000 | [diff] [blame] | 858 | do_test shared-$av.11.9 { |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 859 | sqlite3_shared_cache_report |
drh | 7c4ac0c | 2007-04-05 11:25:58 +0000 | [diff] [blame] | 860 | } [list [file normalize test.db] 2] |
| 861 | } |
danielk1977 | 4b202ae | 2006-01-23 05:50:58 +0000 | [diff] [blame] | 862 | |
| 863 | do_test shared-$av.11.11 { |
| 864 | db close |
| 865 | db2 close |
| 866 | } {} |
| 867 | |
danielk1977 | 843e65f | 2007-09-01 16:16:15 +0000 | [diff] [blame] | 868 | # This tests that if it is impossible to free any pages, SQLite will |
| 869 | # exceed the limit set by PRAGMA cache_size. |
danielk1977 | 4152e67 | 2007-09-12 17:01:45 +0000 | [diff] [blame] | 870 | file delete -force test.db test.db-journal |
| 871 | sqlite3 db test.db |
| 872 | ifcapable pager_pragmas { |
| 873 | do_test shared-$av.12.1 { |
| 874 | execsql { |
| 875 | PRAGMA cache_size = 10; |
| 876 | PRAGMA cache_size; |
| 877 | } |
| 878 | } {10} |
| 879 | } |
danielk1977 | 843e65f | 2007-09-01 16:16:15 +0000 | [diff] [blame] | 880 | do_test shared-$av.12.2 { |
| 881 | set ::db_handles [list] |
| 882 | for {set i 1} {$i < 15} {incr i} { |
| 883 | lappend ::db_handles db$i |
| 884 | sqlite3 db$i test.db |
| 885 | execsql "CREATE TABLE db${i}(a, b, c)" db$i |
| 886 | execsql "INSERT INTO db${i} VALUES(1, 2, 3)" |
| 887 | } |
| 888 | } {} |
| 889 | proc nested_select {handles} { |
| 890 | [lindex $handles 0] eval "SELECT * FROM [lindex $handles 0]" { |
| 891 | lappend ::res $a $b $c |
| 892 | if {[llength $handles]>1} { |
| 893 | nested_select [lrange $handles 1 end] |
| 894 | } |
| 895 | } |
| 896 | } |
| 897 | do_test shared-$av.12.3 { |
| 898 | set ::res [list] |
| 899 | nested_select $::db_handles |
| 900 | set ::res |
| 901 | } [string range [string repeat "1 2 3 " [llength $::db_handles]] 0 end-1] |
| 902 | |
| 903 | do_test shared-$av.12.X { |
| 904 | db close |
| 905 | foreach h $::db_handles { |
| 906 | $h close |
| 907 | } |
| 908 | } {} |
| 909 | |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 910 | } |
| 911 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 912 | sqlite3_enable_shared_cache $::enable_shared_cache |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 913 | finish_test |