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 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame^] | 12 | # $Id: shared.test,v 1.8 2006/01/09 16:12:05 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} { |
| 194 | execsql { |
| 195 | INSERT INTO seq SELECT i + (SELECT max(i) FROM seq), x FROM seq; |
| 196 | } |
| 197 | } |
| 198 | lappend ret $i |
| 199 | } |
| 200 | set ret |
| 201 | } {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16} |
| 202 | do_test shared-3.1.2 { |
| 203 | # Another linear scan through table seq using a read-uncommitted connection. |
| 204 | # This time, delete each row as it is read. Should not affect the results of |
| 205 | # the scan, but the table should be empty after the scan is concluded |
| 206 | # (test 3.1.3 verifies this). |
| 207 | set ret [list] |
| 208 | db2 eval {SELECT i FROM seq} { |
| 209 | db eval {DELETE FROM seq WHERE i = $i} |
| 210 | lappend ret $i |
| 211 | } |
| 212 | set ret |
| 213 | } {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16} |
| 214 | do_test shared-3.1.3 { |
| 215 | execsql { |
| 216 | SELECT * FROM seq; |
| 217 | } |
| 218 | } {} |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 219 | |
| 220 | catch {db close} |
| 221 | catch {db2 close} |
| 222 | catch {db3 close} |
| 223 | |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 224 | #-------------------------------------------------------------------------- |
| 225 | # Tests shared-4.* test that the schema locking rules are applied |
| 226 | # correctly. i.e.: |
| 227 | # |
| 228 | # 1. All transactions require a read-lock on the schemas of databases they |
| 229 | # access. |
| 230 | # 2. Transactions that modify a database schema require a write-lock on that |
| 231 | # schema. |
| 232 | # 3. It is not possible to compile a statement while another handle has a |
| 233 | # write-lock on the schema. |
| 234 | # |
| 235 | |
| 236 | # Open two database handles db and db2. Each has a single attach database |
| 237 | # (as well as main): |
| 238 | # |
| 239 | # db.main -> ./test.db |
| 240 | # db.test2 -> ./test2.db |
| 241 | # db2.main -> ./test2.db |
| 242 | # db2.test -> ./test.db |
| 243 | # |
| 244 | file delete -force test.db |
| 245 | file delete -force test2.db |
| 246 | file delete -force test2.db-journal |
| 247 | sqlite3 db test.db |
| 248 | sqlite3 db2 test2.db |
| 249 | do_test shared-4.1.1 { |
| 250 | set sqlite_open_file_count |
| 251 | } {2} |
| 252 | do_test shared-4.1.2 { |
| 253 | execsql {ATTACH 'test2.db' AS test2} |
| 254 | set sqlite_open_file_count |
| 255 | } {2} |
| 256 | do_test shared-4.1.3 { |
| 257 | execsql {ATTACH 'test.db' AS test} db2 |
| 258 | set sqlite_open_file_count |
| 259 | } {2} |
| 260 | |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 261 | # Sanity check: Create a table in ./test.db via handle db, and test that handle |
| 262 | # db2 can "see" the new table immediately. A handle using a seperate pager |
| 263 | # cache would have to reload the database schema before this were possible. |
| 264 | # |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 265 | do_test shared-4.2.1 { |
| 266 | execsql { |
| 267 | CREATE TABLE abc(a, b, c); |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 268 | CREATE TABLE def(d, e, f); |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 269 | INSERT INTO abc VALUES('i', 'ii', 'iii'); |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 270 | INSERT INTO def VALUES('I', 'II', 'III'); |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 271 | } |
| 272 | } {} |
| 273 | do_test shared-4.2.2 { |
| 274 | execsql { |
| 275 | SELECT * FROM test.abc; |
| 276 | } db2 |
| 277 | } {i ii iii} |
| 278 | |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 279 | # Open a read-transaction and read from table abc via handle 2. Check that |
| 280 | # handle 1 can read table abc. Check that handle 1 cannot modify table abc |
| 281 | # or the database schema. Then check that handle 1 can modify table def. |
| 282 | # |
| 283 | do_test shared-4.3.1 { |
| 284 | execsql { |
| 285 | BEGIN; |
| 286 | SELECT * FROM test.abc; |
| 287 | } db2 |
| 288 | } {i ii iii} |
| 289 | do_test shared-4.3.2 { |
| 290 | catchsql { |
| 291 | INSERT INTO abc VALUES('iv', 'v', 'vi'); |
| 292 | } |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 293 | } {1 {database table is locked: abc}} |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 294 | do_test shared-4.3.3 { |
| 295 | catchsql { |
| 296 | CREATE TABLE ghi(g, h, i); |
| 297 | } |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 298 | } {1 {database table is locked: sqlite_master}} |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 299 | do_test shared-4.3.3 { |
| 300 | catchsql { |
| 301 | INSERT INTO def VALUES('IV', 'V', 'VI'); |
| 302 | } |
| 303 | } {0 {}} |
| 304 | do_test shared-4.3.4 { |
| 305 | # Cleanup: commit the transaction opened by db2. |
| 306 | execsql { |
| 307 | COMMIT |
| 308 | } db2 |
| 309 | } {} |
| 310 | |
| 311 | # Open a write-transaction using handle 1 and modify the database schema. |
| 312 | # Then try to execute a compiled statement to read from the same |
| 313 | # database via handle 2 (fails to get the lock on sqlite_master). Also |
| 314 | # try to compile a read of the same database using handle 2 (also fails). |
| 315 | # Finally, compile a read of the other database using handle 2. This |
| 316 | # should also fail. |
| 317 | # |
| 318 | do_test shared-4.4.1.2 { |
| 319 | # Sanity check 1: Check that the schema is what we think it is when viewed |
| 320 | # via handle 1. |
| 321 | execsql { |
| 322 | CREATE TABLE test2.ghi(g, h, i); |
| 323 | SELECT 'test.db:'||name FROM sqlite_master |
| 324 | UNION ALL |
| 325 | SELECT 'test2.db:'||name FROM test2.sqlite_master; |
| 326 | } |
| 327 | } {test.db:abc test.db:def test2.db:ghi} |
| 328 | do_test shared-4.4.1.2 { |
| 329 | # Sanity check 2: Check that the schema is what we think it is when viewed |
| 330 | # via handle 2. |
| 331 | execsql { |
| 332 | SELECT 'test2.db:'||name FROM sqlite_master |
| 333 | UNION ALL |
| 334 | SELECT 'test.db:'||name FROM test.sqlite_master; |
| 335 | } db2 |
| 336 | } {test2.db:ghi test.db:abc test.db:def} |
| 337 | |
| 338 | do_test shared-4.4.2 { |
| 339 | set ::DB2 [sqlite3_connection_pointer db2] |
| 340 | set sql {SELECT * FROM abc} |
| 341 | set ::STMT1 [sqlite3_prepare $::DB2 $sql -1 DUMMY] |
| 342 | execsql { |
| 343 | BEGIN; |
| 344 | CREATE TABLE jkl(j, k, l); |
| 345 | } |
| 346 | sqlite3_step $::STMT1 |
| 347 | } {SQLITE_ERROR} |
| 348 | do_test shared-4.4.3 { |
| 349 | sqlite3_finalize $::STMT1 |
| 350 | } {SQLITE_LOCKED} |
| 351 | do_test shared-4.4.4 { |
| 352 | set rc [catch { |
| 353 | set ::STMT1 [sqlite3_prepare $::DB2 $sql -1 DUMMY] |
| 354 | } msg] |
| 355 | list $rc $msg |
| 356 | } {1 {(6) database schema is locked: test}} |
| 357 | do_test shared-4.4.5 { |
| 358 | set rc [catch { |
| 359 | set ::STMT1 [sqlite3_prepare $::DB2 "SELECT * FROM ghi" -1 DUMMY] |
| 360 | } msg] |
| 361 | list $rc $msg |
| 362 | } {1 {(6) database schema is locked: test}} |
| 363 | |
danielk1977 | aaf2268 | 2006-01-06 15:03:48 +0000 | [diff] [blame] | 364 | |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 365 | catch {db2 close} |
| 366 | catch {db close} |
| 367 | |
danielk1977 | aaf2268 | 2006-01-06 15:03:48 +0000 | [diff] [blame] | 368 | #-------------------------------------------------------------------------- |
| 369 | # Tests shared-5.* |
| 370 | # |
| 371 | foreach db [list test.db test1.db test2.db test3.db] { |
| 372 | file delete -force $db ${db}-journal |
| 373 | } |
| 374 | do_test shared-5.1.1 { |
| 375 | sqlite3 db1 test.db |
| 376 | sqlite3 db2 test.db |
| 377 | execsql { |
| 378 | ATTACH 'test1.db' AS test1; |
| 379 | ATTACH 'test2.db' AS test2; |
| 380 | ATTACH 'test3.db' AS test3; |
| 381 | } db1 |
| 382 | execsql { |
| 383 | ATTACH 'test3.db' AS test3; |
| 384 | ATTACH 'test2.db' AS test2; |
| 385 | ATTACH 'test1.db' AS test1; |
| 386 | } db2 |
| 387 | } {} |
| 388 | do_test shared-5.1.2 { |
| 389 | execsql { |
| 390 | CREATE TABLE test1.t1(a, b); |
| 391 | CREATE INDEX test1.i1 ON t1(a, b); |
| 392 | CREATE VIEW test1.v1 AS SELECT * FROM t1; |
| 393 | CREATE TRIGGER test1.trig1 AFTER INSERT ON t1 BEGIN |
| 394 | INSERT INTO t1 VALUES(new.a, new.b); |
| 395 | END; |
| 396 | } db1 |
| 397 | execsql { |
| 398 | DROP INDEX i1; |
| 399 | DROP VIEW v1; |
| 400 | DROP TRIGGER trig1; |
| 401 | DROP TABLE t1; |
| 402 | } db2 |
| 403 | } {} |
| 404 | do_test shared-5.1.2 { |
| 405 | execsql { |
| 406 | SELECT * FROM sqlite_master UNION ALL SELECT * FROM test1.sqlite_master |
| 407 | } db1 |
| 408 | } {} |
| 409 | |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 410 | #-------------------------------------------------------------------------- |
| 411 | # Tests shared-6.* test that a query obtains all the read-locks it needs |
| 412 | # before starting execution of the query. This means that there is no chance |
| 413 | # some rows of data will be returned before a lock fails and SQLITE_LOCK |
| 414 | # is returned. |
| 415 | # |
| 416 | do_test shared-6.1.1 { |
| 417 | execsql { |
| 418 | CREATE TABLE t1(a, b); |
| 419 | CREATE TABLE t2(a, b); |
| 420 | INSERT INTO t1 VALUES(1, 2); |
| 421 | INSERT INTO t2 VALUES(3, 4); |
| 422 | } db1 |
| 423 | execsql { |
| 424 | SELECT * FROM t1 UNION ALL SELECT * FROM t2; |
| 425 | } db2 |
| 426 | } {1 2 3 4} |
| 427 | do_test shared-6.1.2 { |
| 428 | # Establish a write lock on table t2 via connection db2. Then make a |
| 429 | # UNION all query using connection db1 that first accesses t1, followed |
| 430 | # by t2. If the locks are grabbed at the start of the statement (as |
| 431 | # they should be), no rows are returned. If (as was previously the case) |
| 432 | # they are grabbed as the tables are accessed, the t1 rows will be |
| 433 | # returned before the query fails. |
| 434 | # |
| 435 | execsql { |
| 436 | BEGIN; |
| 437 | INSERT INTO t2 VALUES(5, 6); |
| 438 | } db2 |
| 439 | set ret [list] |
| 440 | catch { |
| 441 | db1 eval {SELECT * FROM t1 UNION ALL SELECT * FROM t2} { |
| 442 | lappend ret $a $b |
| 443 | } |
| 444 | } |
| 445 | set ret |
| 446 | } {} |
| 447 | do_test shared-6.1.3 { |
| 448 | execsql { |
| 449 | COMMIT; |
| 450 | BEGIN; |
| 451 | INSERT INTO t1 VALUES(7, 8); |
| 452 | } db2 |
| 453 | set ret [list] |
| 454 | catch { |
| 455 | db1 eval { |
| 456 | SELECT (CASE WHEN a>4 THEN (SELECT a FROM t1) ELSE 0 END) AS d FROM t2; |
| 457 | } { |
| 458 | lappend ret $d |
| 459 | } |
| 460 | } |
| 461 | set ret |
| 462 | } {} |
| 463 | |
danielk1977 | aaf2268 | 2006-01-06 15:03:48 +0000 | [diff] [blame] | 464 | catch {db1 close} |
| 465 | catch {db2 close} |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 466 | foreach f [list test.db test2.db] { |
| 467 | file delete -force $f ${f}-journal |
| 468 | } |
| 469 | |
| 470 | #-------------------------------------------------------------------------- |
| 471 | # Tests shared-7.* test auto-vacuum does not invalidate cursors from |
| 472 | # other shared-cache users when it reorganizes the database on |
| 473 | # COMMIT. |
| 474 | # |
| 475 | do_test shared-7.1 { |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame^] | 476 | # This test case sets up a test database in auto-vacuum mode consisting |
| 477 | # of two tables, t1 and t2. Both have a single index. Table t1 is |
| 478 | # populated first (so consists of pages toward the start of the db file), |
| 479 | # t2 second (pages toward the end of the file). |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 480 | sqlite3 db test.db |
| 481 | sqlite3 db2 test.db |
| 482 | execsql { |
| 483 | PRAGMA auto_vacuum = 1; |
| 484 | BEGIN; |
| 485 | CREATE TABLE t1(a PRIMARY KEY, b); |
| 486 | CREATE TABLE t2(a PRIMARY KEY, b); |
| 487 | } |
| 488 | for {set i 0} {$i < 100} {incr i} { |
| 489 | set a [string repeat "$i " 20] |
| 490 | set b [string repeat "$i " 20] |
| 491 | db eval { |
| 492 | INSERT INTO t1 VALUES($a, $b); |
| 493 | } |
| 494 | lappend ::contents [list [expr $i+1] $a $b] |
| 495 | } |
| 496 | execsql { |
| 497 | INSERT INTO t2 SELECT * FROM t1; |
| 498 | COMMIT; |
| 499 | } |
| 500 | execsql { |
| 501 | PRAGMA auto_vacuum; |
| 502 | } |
| 503 | } {1} |
| 504 | do_test shared-7.2 { |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame^] | 505 | # This test case deletes the contents of table t1 (the one at the start of |
| 506 | # the file) while many cursors are open on table t2 and it's index. All of |
| 507 | # the non-root pages will be moved from the end to the start of the file |
| 508 | # when the DELETE is committed - this test verifies that moving the pages |
| 509 | # does not disturb the open cursors. |
| 510 | # |
| 511 | |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 512 | proc lockrow {db tbl oids body} { |
| 513 | set ret [list] |
| 514 | db eval "SELECT oid AS i, a, b FROM $tbl ORDER BY a" { |
| 515 | if {$i==[lindex $oids 0]} { |
| 516 | set noids [lrange $oids 1 end] |
| 517 | if {[llength $noids]==0} { |
| 518 | set subret [eval $body] |
| 519 | } else { |
| 520 | set subret [lockrow $db $tbl $noids $body] |
| 521 | } |
| 522 | } |
| 523 | lappend ret [list $i $a $b] |
| 524 | } |
| 525 | return [linsert $subret 0 $ret] |
| 526 | } |
| 527 | proc locktblrows {db tbl body} { |
| 528 | set oids [db eval "SELECT oid FROM $tbl"] |
| 529 | lockrow $db $tbl $oids $body |
| 530 | } |
| 531 | |
| 532 | set scans [locktblrows db t2 { |
| 533 | execsql { |
| 534 | DELETE FROM t1; |
| 535 | } db2 |
| 536 | }] |
| 537 | set error 0 |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame^] | 538 | |
| 539 | # Test that each SELECT query returned the expected contents of t2. |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 540 | foreach s $scans { |
| 541 | if {[lsort -integer -index 0 $s]!=$::contents} { |
| 542 | set error 1 |
| 543 | } |
| 544 | } |
| 545 | set error |
| 546 | } {0} |
| 547 | |
| 548 | catch {db close} |
| 549 | catch {db2 close} |
danielk1977 | aaf2268 | 2006-01-06 15:03:48 +0000 | [diff] [blame] | 550 | |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame^] | 551 | #-------------------------------------------------------------------------- |
| 552 | # The following tests try to trick the shared-cache code into assuming |
| 553 | # the wrong encoding for a database. |
| 554 | # |
| 555 | file delete -force test.db test.db-journal |
| 556 | do_test shared-8.1.1 { |
| 557 | sqlite3 db test.db |
| 558 | execsql { |
| 559 | PRAGMA encoding = 'UTF-16'; |
| 560 | SELECT * FROM sqlite_master; |
| 561 | } |
| 562 | } {} |
| 563 | do_test shared-8.1.2 { |
| 564 | string range [execsql {PRAGMA encoding;}] 0 end-2 |
| 565 | } {UTF-16} |
| 566 | do_test shared-8.1.3 { |
| 567 | sqlite3 db2 test.db |
| 568 | execsql { |
| 569 | PRAGMA encoding = 'UTF-8'; |
| 570 | CREATE TABLE abc(a, b, c); |
| 571 | } db2 |
| 572 | } {} |
| 573 | do_test shared-8.1.4 { |
| 574 | execsql { |
| 575 | SELECT * FROM sqlite_master; |
| 576 | } |
| 577 | } "table abc abc [expr $AUTOVACUUM?3:2] {CREATE TABLE abc(a, b, c)}" |
| 578 | do_test shared-8.1.5 { |
| 579 | db2 close |
| 580 | execsql { |
| 581 | PRAGMA encoding; |
| 582 | } |
| 583 | } {UTF-8} |
| 584 | file delete -force test2.db test2.db-journal |
| 585 | do_test shared-8.2.1 { |
| 586 | execsql { |
| 587 | ATTACH 'test2.db' AS aux; |
| 588 | SELECT * FROM aux.sqlite_master; |
| 589 | } |
| 590 | } {} |
| 591 | do_test shared-8.2.2 { |
| 592 | sqlite3 db2 test2.db |
| 593 | execsql { |
| 594 | PRAGMA encoding = 'UTF-16'; |
| 595 | CREATE TABLE def(d, e, f); |
| 596 | } db2 |
| 597 | string range [execsql {PRAGMA encoding;} db2] 0 end-2 |
| 598 | } {UTF-16} |
| 599 | do_test shared-8.2.3 { |
| 600 | catchsql { |
| 601 | SELECT * FROM aux.sqlite_master; |
| 602 | } |
| 603 | } {1 {attached databases must use the same text encoding as main database}} |
| 604 | |
| 605 | catch {db close} |
| 606 | catch {db2 close} |
| 607 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 608 | finish_test |
| 609 | sqlite3_enable_shared_cache $::enable_shared_cache |
| 610 | |