shared8.test 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # 2012 May 15
  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. # The tests in this file are intended to show that closing one database
  13. # connection to a shared-cache while there exist other connections (a)
  14. # does not cause the schema to be reloaded and (b) does not cause any
  15. # other problems.
  16. #
  17. set testdir [file dirname $argv0]
  18. source $testdir/tester.tcl
  19. ifcapable !shared_cache { finish_test ; return }
  20. set testprefix shared8
  21. db close
  22. set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
  23. do_test 0.0 { sqlite3_enable_shared_cache } {1}
  24. proc roman {n} {
  25. array set R {1 i 2 ii 3 iii 4 iv 5 v 6 vi 7 vii 8 viii 9 ix 10 x}
  26. set R($n)
  27. }
  28. #-------------------------------------------------------------------------
  29. # The following tests work as follows:
  30. #
  31. # 1.0: Open connection [db1] and populate the database.
  32. #
  33. # 1.1: Using "PRAGMA writable_schema", destroy the database schema on
  34. # disk. The schema is still in memory, so it is possible to keep
  35. # using it, but any attempt to reload it from disk will fail.
  36. #
  37. # 1.3-4: Open connection db2. Check that it can see the db schema. Then
  38. # close db1 and check that db2 still works. This shows that closing
  39. # db1 did not reset the in-memory schema.
  40. #
  41. # 1.5-7: Similar to 1.3-4.
  42. #
  43. # 1.8: Close all database connections (deleting the in-memory schema).
  44. # Then open a new connection and check that it cannot read the db.
  45. #
  46. do_test 1.0 {
  47. sqlite3 db1 test.db
  48. db1 func roman roman
  49. execsql {
  50. CREATE TABLE t1(a, b);
  51. INSERT INTO t1 VALUES(1, 1);
  52. INSERT INTO t1 VALUES(2, 2);
  53. INSERT INTO t1 VALUES(3, 3);
  54. INSERT INTO t1 VALUES(4, 4);
  55. CREATE VIEW v1 AS SELECT a, roman(b) FROM t1;
  56. SELECT * FROM v1;
  57. } db1
  58. } {1 i 2 ii 3 iii 4 iv}
  59. do_test 1.1 {
  60. execsql {
  61. PRAGMA writable_schema = 1;
  62. DELETE FROM sqlite_master WHERE 1;
  63. PRAGMA writable_schema = 0;
  64. SELECT * FROM sqlite_master;
  65. } db1
  66. } {}
  67. do_test 1.2 {
  68. execsql { SELECT * FROM v1 } db1
  69. } {1 i 2 ii 3 iii 4 iv}
  70. do_test 1.3 {
  71. sqlite3 db2 test.db
  72. db2 func roman roman
  73. execsql { SELECT * FROM v1 } db2
  74. } {1 i 2 ii 3 iii 4 iv}
  75. do_test 1.4 {
  76. db1 close
  77. execsql { SELECT * FROM v1 } db2
  78. } {1 i 2 ii 3 iii 4 iv}
  79. do_test 1.5 {
  80. sqlite3 db3 test.db
  81. db3 func roman roman
  82. execsql { SELECT * FROM v1 } db3
  83. } {1 i 2 ii 3 iii 4 iv}
  84. do_test 1.6 {
  85. execsql { SELECT * FROM v1 } db2
  86. } {1 i 2 ii 3 iii 4 iv}
  87. do_test 1.7 {
  88. db2 close
  89. execsql { SELECT * FROM v1 } db3
  90. } {1 i 2 ii 3 iii 4 iv}
  91. do_test 1.8 {
  92. db3 close
  93. sqlite3 db4 test.db
  94. catchsql { SELECT * FROM v1 } db4
  95. } {1 {no such table: v1}}
  96. foreach db {db1 db2 db3 db4} { catch { $db close } }
  97. sqlite3_enable_shared_cache $::enable_shared_cache
  98. finish_test