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 | # |
danielk1977 | 1576cd9 | 2006-01-14 08:02:28 +0000 | [diff] [blame^] | 12 | # $Id: shared.test,v 1.12 2006/01/14 08:02:29 danielk1977 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 | |
| 18 | ifcapable !shared_cache { |
| 19 | finish_test |
| 20 | return |
| 21 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 22 | set ::enable_shared_cache [sqlite3_enable_shared_cache 1] |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 23 | |
| 24 | # Test organization: |
| 25 | # |
| 26 | # shared-1.*: Simple test to verify basic sanity of table level locking when |
| 27 | # two connections share a pager cache. |
| 28 | # shared-2.*: Test that a read transaction can co-exist with a |
| 29 | # write-transaction, including a simple test to ensure the |
| 30 | # external locking protocol is still working. |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 31 | # shared-3.*: Simple test of read-uncommitted mode. |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 32 | # shared-4.*: Check that the schema is locked and unlocked correctly. |
danielk1977 | aaf2268 | 2006-01-06 15:03:48 +0000 | [diff] [blame] | 33 | # shared-5.*: Test that creating/dropping schema items works when databases |
| 34 | # are attached in different orders to different handles. |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 35 | # shared-6.*: Locking, UNION ALL queries and sub-queries. |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 36 | # shared-7.*: Autovacuum and shared-cache. |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 37 | # |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 38 | |
| 39 | do_test shared-1.1 { |
| 40 | # Open a second database on the file test.db. It should use the same pager |
| 41 | # cache and schema as the original connection. Verify that only 1 file is |
| 42 | # opened. |
| 43 | sqlite3 db2 test.db |
| 44 | sqlite3 db test.db |
| 45 | set ::sqlite_open_file_count |
| 46 | } {1} |
| 47 | do_test shared-1.2 { |
| 48 | # Add a table and a single row of data via the first connection. |
| 49 | # Ensure that the second connection can see them. |
| 50 | execsql { |
| 51 | CREATE TABLE abc(a, b, c); |
| 52 | INSERT INTO abc VALUES(1, 2, 3); |
| 53 | } db |
| 54 | execsql { |
| 55 | SELECT * FROM abc; |
| 56 | } db2 |
| 57 | } {1 2 3} |
| 58 | do_test shared-1.3 { |
| 59 | # Have the first connection begin a transaction and obtain a read-lock |
| 60 | # on table abc. This should not prevent the second connection from |
| 61 | # querying abc. |
| 62 | execsql { |
| 63 | BEGIN; |
| 64 | SELECT * FROM abc; |
| 65 | } |
| 66 | execsql { |
| 67 | SELECT * FROM abc; |
| 68 | } db2 |
| 69 | } {1 2 3} |
| 70 | do_test shared-1.4 { |
| 71 | # Try to insert a row into abc via connection 2. This should fail because |
| 72 | # of the read-lock connection 1 is holding on table abc (obtained in the |
| 73 | # previous test case). |
| 74 | catchsql { |
| 75 | INSERT INTO abc VALUES(4, 5, 6); |
| 76 | } db2 |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 77 | } {1 {database table is locked: abc}} |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 78 | do_test shared-1.5 { |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 79 | # Using connection 2 (the one without the open transaction), try to create |
| 80 | # a new table. This should fail because of the open read transaction |
| 81 | # held by connection 1. |
| 82 | catchsql { |
| 83 | CREATE TABLE def(d, e, f); |
| 84 | } db2 |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 85 | } {1 {database table is locked: sqlite_master}} |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 86 | do_test shared-1.6 { |
| 87 | # Upgrade connection 1's transaction to a write transaction. Create |
| 88 | # a new table - def - and insert a row into it. Because the connection 1 |
| 89 | # transaction modifies the schema, it should not be possible for |
| 90 | # connection 2 to access the database at all until the connection 1 |
| 91 | # has finished the transaction. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 92 | execsql { |
| 93 | CREATE TABLE def(d, e, f); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 94 | INSERT INTO def VALUES('IV', 'V', 'VI'); |
| 95 | } |
| 96 | } {} |
| 97 | do_test shared-1.7 { |
| 98 | # Read from the sqlite_master table with connection 1 (inside the |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 99 | # transaction). Then test that we can not do this with connection 2. This |
| 100 | # is because of the schema-modified lock established by connection 1 |
| 101 | # in the previous test case. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 102 | execsql { |
| 103 | SELECT * FROM sqlite_master; |
| 104 | } |
| 105 | catchsql { |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 106 | SELECT * FROM sqlite_master; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 107 | } db2 |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 108 | } {1 {database schema is locked: main}} |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 109 | do_test shared-1.8 { |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 110 | # Commit the connection 1 transaction. |
| 111 | execsql { |
| 112 | COMMIT; |
| 113 | } |
| 114 | } {} |
| 115 | |
| 116 | do_test shared-2.1 { |
| 117 | # Open connection db3 to the database. Use a different path to the same |
| 118 | # file so that db3 does *not* share the same pager cache as db and db2 |
| 119 | # (there should be two open file handles). |
| 120 | sqlite3 db3 ./test.db |
| 121 | set ::sqlite_open_file_count |
| 122 | } {2} |
| 123 | do_test shared-2.2 { |
| 124 | # Start read transactions on db and db2 (the shared pager cache). Ensure |
| 125 | # db3 cannot write to the database. |
| 126 | execsql { |
| 127 | BEGIN; |
| 128 | SELECT * FROM abc; |
| 129 | } |
| 130 | execsql { |
| 131 | BEGIN; |
| 132 | SELECT * FROM abc; |
| 133 | } db2 |
| 134 | catchsql { |
| 135 | INSERT INTO abc VALUES(1, 2, 3); |
| 136 | } db2 |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 137 | } {1 {database table is locked: abc}} |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 138 | do_test shared-2.3 { |
| 139 | # Turn db's transaction into a write-transaction. db3 should still be |
| 140 | # able to read from table def (but will not see the new row). Connection |
| 141 | # db2 should not be able to read def (because of the write-lock). |
| 142 | |
| 143 | # Todo: The failed "INSERT INTO abc ..." statement in the above test |
| 144 | # has started a write-transaction on db2 (should this be so?). This |
| 145 | # would prevent connection db from starting a write-transaction. So roll the |
| 146 | # db2 transaction back and replace it with a new read transaction. |
| 147 | execsql { |
| 148 | ROLLBACK; |
| 149 | BEGIN; |
| 150 | SELECT * FROM abc; |
| 151 | } db2 |
| 152 | |
| 153 | execsql { |
| 154 | INSERT INTO def VALUES('VII', 'VIII', 'IX'); |
| 155 | } |
| 156 | concat [ |
| 157 | catchsql { SELECT * FROM def; } db3 |
| 158 | ] [ |
| 159 | catchsql { SELECT * FROM def; } db2 |
| 160 | ] |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 161 | } {0 {IV V VI} 1 {database table is locked: def}} |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 162 | do_test shared-2.4 { |
| 163 | # Commit the open transaction on db. db2 still holds a read-transaction. |
| 164 | # This should prevent db3 from writing to the database, but not from |
| 165 | # reading. |
| 166 | execsql { |
| 167 | COMMIT; |
| 168 | } |
| 169 | concat [ |
| 170 | catchsql { SELECT * FROM def; } db3 |
| 171 | ] [ |
| 172 | catchsql { INSERT INTO def VALUES('X', 'XI', 'XII'); } db3 |
| 173 | ] |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 174 | } {0 {IV V VI VII VIII IX} 1 {database is locked}} |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 175 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 176 | catchsql COMMIT db2 |
| 177 | |
| 178 | do_test shared-3.1.1 { |
| 179 | # This test case starts a linear scan of table 'seq' using a |
| 180 | # read-uncommitted connection. In the middle of the scan, rows are added |
| 181 | # to the end of the seq table (ahead of the current cursor position). |
| 182 | # The uncommitted rows should be included in the results of the scan. |
| 183 | execsql " |
| 184 | CREATE TABLE seq(i, x); |
| 185 | INSERT INTO seq VALUES(1, '[string repeat X 500]'); |
| 186 | INSERT INTO seq VALUES(2, '[string repeat X 500]'); |
| 187 | " |
| 188 | execsql {SELECT * FROM sqlite_master} db2 |
| 189 | execsql {PRAGMA read_uncommitted = 1} db2 |
| 190 | |
| 191 | set ret [list] |
| 192 | db2 eval {SELECT i FROM seq} { |
| 193 | if {$i < 4} { |
danielk1977 | 1576cd9 | 2006-01-14 08:02:28 +0000 | [diff] [blame^] | 194 | set max [execsql {SELECT max(i) FROM seq}] |
| 195 | db eval { |
| 196 | INSERT INTO seq SELECT i + $max, x FROM seq; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | lappend ret $i |
| 200 | } |
| 201 | set ret |
| 202 | } {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16} |
| 203 | do_test shared-3.1.2 { |
| 204 | # Another linear scan through table seq using a read-uncommitted connection. |
| 205 | # This time, delete each row as it is read. Should not affect the results of |
| 206 | # the scan, but the table should be empty after the scan is concluded |
| 207 | # (test 3.1.3 verifies this). |
| 208 | set ret [list] |
| 209 | db2 eval {SELECT i FROM seq} { |
| 210 | db eval {DELETE FROM seq WHERE i = $i} |
| 211 | lappend ret $i |
| 212 | } |
| 213 | set ret |
| 214 | } {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16} |
| 215 | do_test shared-3.1.3 { |
| 216 | execsql { |
| 217 | SELECT * FROM seq; |
| 218 | } |
| 219 | } {} |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 220 | |
| 221 | catch {db close} |
| 222 | catch {db2 close} |
| 223 | catch {db3 close} |
| 224 | |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 225 | #-------------------------------------------------------------------------- |
| 226 | # Tests shared-4.* test that the schema locking rules are applied |
| 227 | # correctly. i.e.: |
| 228 | # |
| 229 | # 1. All transactions require a read-lock on the schemas of databases they |
| 230 | # access. |
| 231 | # 2. Transactions that modify a database schema require a write-lock on that |
| 232 | # schema. |
| 233 | # 3. It is not possible to compile a statement while another handle has a |
| 234 | # write-lock on the schema. |
| 235 | # |
| 236 | |
| 237 | # Open two database handles db and db2. Each has a single attach database |
| 238 | # (as well as main): |
| 239 | # |
| 240 | # db.main -> ./test.db |
| 241 | # db.test2 -> ./test2.db |
| 242 | # db2.main -> ./test2.db |
| 243 | # db2.test -> ./test.db |
| 244 | # |
| 245 | file delete -force test.db |
| 246 | file delete -force test2.db |
| 247 | file delete -force test2.db-journal |
| 248 | sqlite3 db test.db |
| 249 | sqlite3 db2 test2.db |
| 250 | do_test shared-4.1.1 { |
| 251 | set sqlite_open_file_count |
| 252 | } {2} |
| 253 | do_test shared-4.1.2 { |
| 254 | execsql {ATTACH 'test2.db' AS test2} |
| 255 | set sqlite_open_file_count |
| 256 | } {2} |
| 257 | do_test shared-4.1.3 { |
| 258 | execsql {ATTACH 'test.db' AS test} db2 |
| 259 | set sqlite_open_file_count |
| 260 | } {2} |
| 261 | |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 262 | # Sanity check: Create a table in ./test.db via handle db, and test that handle |
| 263 | # db2 can "see" the new table immediately. A handle using a seperate pager |
| 264 | # cache would have to reload the database schema before this were possible. |
| 265 | # |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 266 | do_test shared-4.2.1 { |
| 267 | execsql { |
| 268 | CREATE TABLE abc(a, b, c); |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 269 | CREATE TABLE def(d, e, f); |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 270 | INSERT INTO abc VALUES('i', 'ii', 'iii'); |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 271 | INSERT INTO def VALUES('I', 'II', 'III'); |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 272 | } |
| 273 | } {} |
| 274 | do_test shared-4.2.2 { |
| 275 | execsql { |
| 276 | SELECT * FROM test.abc; |
| 277 | } db2 |
| 278 | } {i ii iii} |
| 279 | |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 280 | # Open a read-transaction and read from table abc via handle 2. Check that |
| 281 | # handle 1 can read table abc. Check that handle 1 cannot modify table abc |
| 282 | # or the database schema. Then check that handle 1 can modify table def. |
| 283 | # |
| 284 | do_test shared-4.3.1 { |
| 285 | execsql { |
| 286 | BEGIN; |
| 287 | SELECT * FROM test.abc; |
| 288 | } db2 |
| 289 | } {i ii iii} |
| 290 | do_test shared-4.3.2 { |
| 291 | catchsql { |
| 292 | INSERT INTO abc VALUES('iv', 'v', 'vi'); |
| 293 | } |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 294 | } {1 {database table is locked: abc}} |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 295 | do_test shared-4.3.3 { |
| 296 | catchsql { |
| 297 | CREATE TABLE ghi(g, h, i); |
| 298 | } |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 299 | } {1 {database table is locked: sqlite_master}} |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 300 | do_test shared-4.3.3 { |
| 301 | catchsql { |
| 302 | INSERT INTO def VALUES('IV', 'V', 'VI'); |
| 303 | } |
| 304 | } {0 {}} |
| 305 | do_test shared-4.3.4 { |
| 306 | # Cleanup: commit the transaction opened by db2. |
| 307 | execsql { |
| 308 | COMMIT |
| 309 | } db2 |
| 310 | } {} |
| 311 | |
| 312 | # Open a write-transaction using handle 1 and modify the database schema. |
| 313 | # Then try to execute a compiled statement to read from the same |
| 314 | # database via handle 2 (fails to get the lock on sqlite_master). Also |
| 315 | # try to compile a read of the same database using handle 2 (also fails). |
| 316 | # Finally, compile a read of the other database using handle 2. This |
| 317 | # should also fail. |
| 318 | # |
| 319 | do_test shared-4.4.1.2 { |
| 320 | # Sanity check 1: Check that the schema is what we think it is when viewed |
| 321 | # via handle 1. |
| 322 | execsql { |
| 323 | CREATE TABLE test2.ghi(g, h, i); |
| 324 | SELECT 'test.db:'||name FROM sqlite_master |
| 325 | UNION ALL |
| 326 | SELECT 'test2.db:'||name FROM test2.sqlite_master; |
| 327 | } |
| 328 | } {test.db:abc test.db:def test2.db:ghi} |
| 329 | do_test shared-4.4.1.2 { |
| 330 | # Sanity check 2: Check that the schema is what we think it is when viewed |
| 331 | # via handle 2. |
| 332 | execsql { |
| 333 | SELECT 'test2.db:'||name FROM sqlite_master |
| 334 | UNION ALL |
| 335 | SELECT 'test.db:'||name FROM test.sqlite_master; |
| 336 | } db2 |
| 337 | } {test2.db:ghi test.db:abc test.db:def} |
| 338 | |
| 339 | do_test shared-4.4.2 { |
| 340 | set ::DB2 [sqlite3_connection_pointer db2] |
| 341 | set sql {SELECT * FROM abc} |
| 342 | set ::STMT1 [sqlite3_prepare $::DB2 $sql -1 DUMMY] |
| 343 | execsql { |
| 344 | BEGIN; |
| 345 | CREATE TABLE jkl(j, k, l); |
| 346 | } |
| 347 | sqlite3_step $::STMT1 |
| 348 | } {SQLITE_ERROR} |
| 349 | do_test shared-4.4.3 { |
| 350 | sqlite3_finalize $::STMT1 |
| 351 | } {SQLITE_LOCKED} |
| 352 | do_test shared-4.4.4 { |
| 353 | set rc [catch { |
| 354 | set ::STMT1 [sqlite3_prepare $::DB2 $sql -1 DUMMY] |
| 355 | } msg] |
| 356 | list $rc $msg |
| 357 | } {1 {(6) database schema is locked: test}} |
| 358 | do_test shared-4.4.5 { |
| 359 | set rc [catch { |
| 360 | set ::STMT1 [sqlite3_prepare $::DB2 "SELECT * FROM ghi" -1 DUMMY] |
| 361 | } msg] |
| 362 | list $rc $msg |
| 363 | } {1 {(6) database schema is locked: test}} |
| 364 | |
danielk1977 | aaf2268 | 2006-01-06 15:03:48 +0000 | [diff] [blame] | 365 | |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 366 | catch {db2 close} |
| 367 | catch {db close} |
| 368 | |
danielk1977 | aaf2268 | 2006-01-06 15:03:48 +0000 | [diff] [blame] | 369 | #-------------------------------------------------------------------------- |
| 370 | # Tests shared-5.* |
| 371 | # |
| 372 | foreach db [list test.db test1.db test2.db test3.db] { |
| 373 | file delete -force $db ${db}-journal |
| 374 | } |
| 375 | do_test shared-5.1.1 { |
| 376 | sqlite3 db1 test.db |
| 377 | sqlite3 db2 test.db |
| 378 | execsql { |
| 379 | ATTACH 'test1.db' AS test1; |
| 380 | ATTACH 'test2.db' AS test2; |
| 381 | ATTACH 'test3.db' AS test3; |
| 382 | } db1 |
| 383 | execsql { |
| 384 | ATTACH 'test3.db' AS test3; |
| 385 | ATTACH 'test2.db' AS test2; |
| 386 | ATTACH 'test1.db' AS test1; |
| 387 | } db2 |
| 388 | } {} |
| 389 | do_test shared-5.1.2 { |
| 390 | execsql { |
| 391 | CREATE TABLE test1.t1(a, b); |
| 392 | CREATE INDEX test1.i1 ON t1(a, b); |
| 393 | CREATE VIEW test1.v1 AS SELECT * FROM t1; |
| 394 | CREATE TRIGGER test1.trig1 AFTER INSERT ON t1 BEGIN |
| 395 | INSERT INTO t1 VALUES(new.a, new.b); |
| 396 | END; |
| 397 | } db1 |
| 398 | execsql { |
| 399 | DROP INDEX i1; |
| 400 | DROP VIEW v1; |
| 401 | DROP TRIGGER trig1; |
| 402 | DROP TABLE t1; |
| 403 | } db2 |
| 404 | } {} |
| 405 | do_test shared-5.1.2 { |
| 406 | execsql { |
| 407 | SELECT * FROM sqlite_master UNION ALL SELECT * FROM test1.sqlite_master |
| 408 | } db1 |
| 409 | } {} |
| 410 | |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 411 | #-------------------------------------------------------------------------- |
| 412 | # Tests shared-6.* test that a query obtains all the read-locks it needs |
| 413 | # before starting execution of the query. This means that there is no chance |
| 414 | # some rows of data will be returned before a lock fails and SQLITE_LOCK |
| 415 | # is returned. |
| 416 | # |
| 417 | do_test shared-6.1.1 { |
| 418 | execsql { |
| 419 | CREATE TABLE t1(a, b); |
| 420 | CREATE TABLE t2(a, b); |
| 421 | INSERT INTO t1 VALUES(1, 2); |
| 422 | INSERT INTO t2 VALUES(3, 4); |
| 423 | } db1 |
| 424 | execsql { |
| 425 | SELECT * FROM t1 UNION ALL SELECT * FROM t2; |
| 426 | } db2 |
| 427 | } {1 2 3 4} |
| 428 | do_test shared-6.1.2 { |
| 429 | # Establish a write lock on table t2 via connection db2. Then make a |
| 430 | # UNION all query using connection db1 that first accesses t1, followed |
| 431 | # by t2. If the locks are grabbed at the start of the statement (as |
| 432 | # they should be), no rows are returned. If (as was previously the case) |
| 433 | # they are grabbed as the tables are accessed, the t1 rows will be |
| 434 | # returned before the query fails. |
| 435 | # |
| 436 | execsql { |
| 437 | BEGIN; |
| 438 | INSERT INTO t2 VALUES(5, 6); |
| 439 | } db2 |
| 440 | set ret [list] |
| 441 | catch { |
| 442 | db1 eval {SELECT * FROM t1 UNION ALL SELECT * FROM t2} { |
| 443 | lappend ret $a $b |
| 444 | } |
| 445 | } |
| 446 | set ret |
| 447 | } {} |
| 448 | do_test shared-6.1.3 { |
| 449 | execsql { |
| 450 | COMMIT; |
| 451 | BEGIN; |
| 452 | INSERT INTO t1 VALUES(7, 8); |
| 453 | } db2 |
| 454 | set ret [list] |
| 455 | catch { |
| 456 | db1 eval { |
| 457 | SELECT (CASE WHEN a>4 THEN (SELECT a FROM t1) ELSE 0 END) AS d FROM t2; |
| 458 | } { |
| 459 | lappend ret $d |
| 460 | } |
| 461 | } |
| 462 | set ret |
| 463 | } {} |
| 464 | |
danielk1977 | aaf2268 | 2006-01-06 15:03:48 +0000 | [diff] [blame] | 465 | catch {db1 close} |
| 466 | catch {db2 close} |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 467 | foreach f [list test.db test2.db] { |
| 468 | file delete -force $f ${f}-journal |
| 469 | } |
| 470 | |
| 471 | #-------------------------------------------------------------------------- |
| 472 | # Tests shared-7.* test auto-vacuum does not invalidate cursors from |
| 473 | # other shared-cache users when it reorganizes the database on |
| 474 | # COMMIT. |
| 475 | # |
| 476 | do_test shared-7.1 { |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 477 | # This test case sets up a test database in auto-vacuum mode consisting |
| 478 | # of two tables, t1 and t2. Both have a single index. Table t1 is |
| 479 | # populated first (so consists of pages toward the start of the db file), |
| 480 | # t2 second (pages toward the end of the file). |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 481 | sqlite3 db test.db |
| 482 | sqlite3 db2 test.db |
| 483 | execsql { |
| 484 | PRAGMA auto_vacuum = 1; |
| 485 | BEGIN; |
| 486 | CREATE TABLE t1(a PRIMARY KEY, b); |
| 487 | CREATE TABLE t2(a PRIMARY KEY, b); |
| 488 | } |
drh | 7a91dd8 | 2006-01-11 01:08:34 +0000 | [diff] [blame] | 489 | set ::contents {} |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 490 | for {set i 0} {$i < 100} {incr i} { |
| 491 | set a [string repeat "$i " 20] |
| 492 | set b [string repeat "$i " 20] |
| 493 | db eval { |
| 494 | INSERT INTO t1 VALUES($a, $b); |
| 495 | } |
| 496 | lappend ::contents [list [expr $i+1] $a $b] |
| 497 | } |
| 498 | execsql { |
| 499 | INSERT INTO t2 SELECT * FROM t1; |
| 500 | COMMIT; |
| 501 | } |
| 502 | execsql { |
| 503 | PRAGMA auto_vacuum; |
| 504 | } |
| 505 | } {1} |
| 506 | do_test shared-7.2 { |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 507 | # This test case deletes the contents of table t1 (the one at the start of |
| 508 | # the file) while many cursors are open on table t2 and it's index. All of |
| 509 | # the non-root pages will be moved from the end to the start of the file |
| 510 | # when the DELETE is committed - this test verifies that moving the pages |
| 511 | # does not disturb the open cursors. |
| 512 | # |
| 513 | |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 514 | proc lockrow {db tbl oids body} { |
| 515 | set ret [list] |
| 516 | db eval "SELECT oid AS i, a, b FROM $tbl ORDER BY a" { |
| 517 | if {$i==[lindex $oids 0]} { |
| 518 | set noids [lrange $oids 1 end] |
| 519 | if {[llength $noids]==0} { |
| 520 | set subret [eval $body] |
| 521 | } else { |
| 522 | set subret [lockrow $db $tbl $noids $body] |
| 523 | } |
| 524 | } |
| 525 | lappend ret [list $i $a $b] |
| 526 | } |
| 527 | return [linsert $subret 0 $ret] |
| 528 | } |
| 529 | proc locktblrows {db tbl body} { |
| 530 | set oids [db eval "SELECT oid FROM $tbl"] |
| 531 | lockrow $db $tbl $oids $body |
| 532 | } |
| 533 | |
| 534 | set scans [locktblrows db t2 { |
| 535 | execsql { |
| 536 | DELETE FROM t1; |
| 537 | } db2 |
| 538 | }] |
| 539 | set error 0 |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 540 | |
| 541 | # Test that each SELECT query returned the expected contents of t2. |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 542 | foreach s $scans { |
| 543 | if {[lsort -integer -index 0 $s]!=$::contents} { |
| 544 | set error 1 |
| 545 | } |
| 546 | } |
| 547 | set error |
| 548 | } {0} |
| 549 | |
| 550 | catch {db close} |
| 551 | catch {db2 close} |
drh | 7a91dd8 | 2006-01-11 01:08:34 +0000 | [diff] [blame] | 552 | unset -nocomplain contents |
danielk1977 | aaf2268 | 2006-01-06 15:03:48 +0000 | [diff] [blame] | 553 | |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 554 | #-------------------------------------------------------------------------- |
| 555 | # The following tests try to trick the shared-cache code into assuming |
| 556 | # the wrong encoding for a database. |
| 557 | # |
| 558 | file delete -force test.db test.db-journal |
| 559 | do_test shared-8.1.1 { |
| 560 | sqlite3 db test.db |
| 561 | execsql { |
| 562 | PRAGMA encoding = 'UTF-16'; |
| 563 | SELECT * FROM sqlite_master; |
| 564 | } |
| 565 | } {} |
| 566 | do_test shared-8.1.2 { |
| 567 | string range [execsql {PRAGMA encoding;}] 0 end-2 |
| 568 | } {UTF-16} |
| 569 | do_test shared-8.1.3 { |
| 570 | sqlite3 db2 test.db |
| 571 | execsql { |
| 572 | PRAGMA encoding = 'UTF-8'; |
| 573 | CREATE TABLE abc(a, b, c); |
| 574 | } db2 |
| 575 | } {} |
| 576 | do_test shared-8.1.4 { |
| 577 | execsql { |
| 578 | SELECT * FROM sqlite_master; |
| 579 | } |
| 580 | } "table abc abc [expr $AUTOVACUUM?3:2] {CREATE TABLE abc(a, b, c)}" |
| 581 | do_test shared-8.1.5 { |
| 582 | db2 close |
| 583 | execsql { |
| 584 | PRAGMA encoding; |
| 585 | } |
| 586 | } {UTF-8} |
| 587 | file delete -force test2.db test2.db-journal |
| 588 | do_test shared-8.2.1 { |
| 589 | execsql { |
| 590 | ATTACH 'test2.db' AS aux; |
| 591 | SELECT * FROM aux.sqlite_master; |
| 592 | } |
| 593 | } {} |
| 594 | do_test shared-8.2.2 { |
| 595 | sqlite3 db2 test2.db |
| 596 | execsql { |
| 597 | PRAGMA encoding = 'UTF-16'; |
| 598 | CREATE TABLE def(d, e, f); |
| 599 | } db2 |
| 600 | string range [execsql {PRAGMA encoding;} db2] 0 end-2 |
| 601 | } {UTF-16} |
| 602 | do_test shared-8.2.3 { |
| 603 | catchsql { |
| 604 | SELECT * FROM aux.sqlite_master; |
| 605 | } |
| 606 | } {1 {attached databases must use the same text encoding as main database}} |
| 607 | |
| 608 | catch {db close} |
| 609 | catch {db2 close} |
danielk1977 | eecfb3e | 2006-01-10 12:31:39 +0000 | [diff] [blame] | 610 | file delete -force test.db test2.db |
| 611 | |
danielk1977 | eecfb3e | 2006-01-10 12:31:39 +0000 | [diff] [blame] | 612 | #--------------------------------------------------------------------------- |
| 613 | # The following tests - shared-9.* - test interactions between TEMP triggers |
| 614 | # and shared-schemas. |
| 615 | # |
| 616 | ifcapable trigger&&tempdb { |
| 617 | |
| 618 | do_test shared-9.1 { |
| 619 | sqlite3 db test.db |
| 620 | sqlite3 db2 test.db |
| 621 | execsql { |
| 622 | CREATE TABLE abc(a, b, c); |
| 623 | CREATE TABLE abc_mirror(a, b, c); |
| 624 | CREATE TEMP TRIGGER BEFORE INSERT ON abc BEGIN |
| 625 | INSERT INTO abc_mirror(a, b, c) VALUES(new.a, new.b, new.c); |
| 626 | END; |
| 627 | INSERT INTO abc VALUES(1, 2, 3); |
| 628 | SELECT * FROM abc_mirror; |
| 629 | } |
| 630 | } {1 2 3} |
| 631 | do_test shared-9.2 { |
| 632 | execsql { |
| 633 | INSERT INTO abc VALUES(4, 5, 6); |
| 634 | SELECT * FROM abc_mirror; |
| 635 | } db2 |
| 636 | } {1 2 3} |
| 637 | do_test shared-9.3 { |
| 638 | db close |
| 639 | db2 close |
| 640 | } {} |
| 641 | |
| 642 | } ; # End shared-9.* |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 643 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 644 | finish_test |
| 645 | sqlite3_enable_shared_cache $::enable_shared_cache |