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