1bba02a95Sdan# 2012 May 15 2bba02a95Sdan# 3bba02a95Sdan# The author disclaims copyright to this source code. In place of 4bba02a95Sdan# a legal notice, here is a blessing: 5bba02a95Sdan# 6bba02a95Sdan# May you do good and not evil. 7bba02a95Sdan# May you find forgiveness for yourself and forgive others. 8bba02a95Sdan# May you share freely, never taking more than you give. 9bba02a95Sdan# 10bba02a95Sdan#*********************************************************************** 11bba02a95Sdan# 12bba02a95Sdan# The tests in this file are intended to show that closing one database 13bba02a95Sdan# connection to a shared-cache while there exist other connections (a) 14bba02a95Sdan# does not cause the schema to be reloaded and (b) does not cause any 15bba02a95Sdan# other problems. 16bba02a95Sdan# 17bba02a95Sdan 18bba02a95Sdanset testdir [file dirname $argv0] 19bba02a95Sdansource $testdir/tester.tcl 20bba02a95Sdanifcapable !shared_cache { finish_test ; return } 21bba02a95Sdanset testprefix shared8 22bba02a95Sdan 23bba02a95Sdandb close 24bba02a95Sdanset ::enable_shared_cache [sqlite3_enable_shared_cache 1] 25bba02a95Sdando_test 0.0 { sqlite3_enable_shared_cache } {1} 26bba02a95Sdan 27bba02a95Sdanproc roman {n} { 28bba02a95Sdan array set R {1 i 2 ii 3 iii 4 iv 5 v 6 vi 7 vii 8 viii 9 ix 10 x} 29bba02a95Sdan set R($n) 30bba02a95Sdan} 31bba02a95Sdan 32bba02a95Sdan#------------------------------------------------------------------------- 33bba02a95Sdan# The following tests work as follows: 34bba02a95Sdan# 35bba02a95Sdan# 1.0: Open connection [db1] and populate the database. 36bba02a95Sdan# 37bba02a95Sdan# 1.1: Using "PRAGMA writable_schema", destroy the database schema on 38bba02a95Sdan# disk. The schema is still in memory, so it is possible to keep 39bba02a95Sdan# using it, but any attempt to reload it from disk will fail. 40bba02a95Sdan# 41bba02a95Sdan# 1.3-4: Open connection db2. Check that it can see the db schema. Then 42bba02a95Sdan# close db1 and check that db2 still works. This shows that closing 43bba02a95Sdan# db1 did not reset the in-memory schema. 44bba02a95Sdan# 45bba02a95Sdan# 1.5-7: Similar to 1.3-4. 46bba02a95Sdan# 47bba02a95Sdan# 1.8: Close all database connections (deleting the in-memory schema). 48bba02a95Sdan# Then open a new connection and check that it cannot read the db. 49bba02a95Sdan# 50bba02a95Sdando_test 1.0 { 51bba02a95Sdan sqlite3 db1 test.db 52bba02a95Sdan db1 func roman roman 53bba02a95Sdan execsql { 54bba02a95Sdan CREATE TABLE t1(a, b); 55bba02a95Sdan INSERT INTO t1 VALUES(1, 1); 56bba02a95Sdan INSERT INTO t1 VALUES(2, 2); 57bba02a95Sdan INSERT INTO t1 VALUES(3, 3); 58bba02a95Sdan INSERT INTO t1 VALUES(4, 4); 59bba02a95Sdan CREATE VIEW v1 AS SELECT a, roman(b) FROM t1; 60bba02a95Sdan SELECT * FROM v1; 61bba02a95Sdan } db1 62bba02a95Sdan} {1 i 2 ii 3 iii 4 iv} 63bba02a95Sdan 64bba02a95Sdando_test 1.1 { 65*6ab91a7aSdrh sqlite3_db_config db1 DEFENSIVE 0 66bba02a95Sdan execsql { 67bba02a95Sdan PRAGMA writable_schema = 1; 68bba02a95Sdan DELETE FROM sqlite_master WHERE 1; 69bba02a95Sdan PRAGMA writable_schema = 0; 70bba02a95Sdan SELECT * FROM sqlite_master; 71bba02a95Sdan } db1 72bba02a95Sdan} {} 73bba02a95Sdan 74bba02a95Sdando_test 1.2 { 75bba02a95Sdan execsql { SELECT * FROM v1 } db1 76bba02a95Sdan} {1 i 2 ii 3 iii 4 iv} 77bba02a95Sdan 78bba02a95Sdando_test 1.3 { 79bba02a95Sdan sqlite3 db2 test.db 80bba02a95Sdan db2 func roman roman 81bba02a95Sdan execsql { SELECT * FROM v1 } db2 82bba02a95Sdan} {1 i 2 ii 3 iii 4 iv} 83bba02a95Sdan 84bba02a95Sdando_test 1.4 { 85bba02a95Sdan db1 close 86bba02a95Sdan execsql { SELECT * FROM v1 } db2 87bba02a95Sdan} {1 i 2 ii 3 iii 4 iv} 88bba02a95Sdan 89bba02a95Sdando_test 1.5 { 90bba02a95Sdan sqlite3 db3 test.db 91bba02a95Sdan db3 func roman roman 92bba02a95Sdan execsql { SELECT * FROM v1 } db3 93bba02a95Sdan} {1 i 2 ii 3 iii 4 iv} 94bba02a95Sdan 95bba02a95Sdando_test 1.6 { 96bba02a95Sdan execsql { SELECT * FROM v1 } db2 97bba02a95Sdan} {1 i 2 ii 3 iii 4 iv} 98bba02a95Sdan 99bba02a95Sdando_test 1.7 { 100bba02a95Sdan db2 close 101bba02a95Sdan execsql { SELECT * FROM v1 } db3 102bba02a95Sdan} {1 i 2 ii 3 iii 4 iv} 103bba02a95Sdan 104bba02a95Sdando_test 1.8 { 105bba02a95Sdan db3 close 106bba02a95Sdan sqlite3 db4 test.db 107bba02a95Sdan catchsql { SELECT * FROM v1 } db4 108bba02a95Sdan} {1 {no such table: v1}} 109bba02a95Sdan 110bba02a95Sdan 111bba02a95Sdanforeach db {db1 db2 db3 db4} { catch { $db close } } 112bba02a95Sdansqlite3_enable_shared_cache $::enable_shared_cache 113bba02a95Sdanfinish_test 114