1
0

vtab_shared.test 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. # 2007 April 16
  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. # This file tests interactions between the virtual table and
  12. # shared-schema functionality.
  13. #
  14. # $Id: vtab_shared.test,v 1.3 2009/07/24 17:58:53 danielk1977 Exp $
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. ifcapable !vtab||!shared_cache {
  18. finish_test
  19. return
  20. }
  21. db close
  22. sqlite3_enable_shared_cache 1
  23. sqlite3 db test.db
  24. sqlite3 db2 test.db
  25. do_test vtab_shared-1.1 {
  26. register_echo_module [sqlite3_connection_pointer db]
  27. execsql {
  28. CREATE TABLE t0(a, b, c);
  29. INSERT INTO t0 VALUES(1, 2, 3);
  30. CREATE VIRTUAL TABLE t1 USING echo(t0);
  31. }
  32. } {}
  33. do_test vtab_shared-1.2 {
  34. execsql { SELECT * FROM t1 } db
  35. } {1 2 3}
  36. # Fails because the 'echo' module has not been registered with connection db2
  37. do_test vtab_shared-1.3 {
  38. catchsql { SELECT * FROM t1 } db2
  39. } {1 {no such module: echo}}
  40. do_test vtab_shared-1.4 {
  41. execsql { SELECT * FROM t0 } db2
  42. } {1 2 3}
  43. do_test vtab_shared-1.5 {
  44. register_echo_module [sqlite3_connection_pointer db2]
  45. execsql { SELECT * FROM t1 } db
  46. } {1 2 3}
  47. # Works after the module is registered with db2
  48. do_test vtab_shared-1.6 {
  49. execsql { SELECT * FROM t1 } db2
  50. } {1 2 3}
  51. # Set a write-lock on table t0 using connection [db]. Then try to read from
  52. # virtual table t1 using [db2]. That this returns an SQLITE_LOCKED error
  53. # shows that the correct sqlite3_vtab is being used.
  54. #
  55. do_test vtab_shared-1.8.1 {
  56. execsql {
  57. BEGIN;
  58. INSERT INTO t1 VALUES(4, 5, 6);
  59. SELECT * FROM t1;
  60. }
  61. } {1 2 3 4 5 6}
  62. do_test vtab_shared-1.8.2 {
  63. catchsql { SELECT * FROM t1 } db2
  64. } {1 {database table is locked}}
  65. do_test vtab_shared-1.8.3 {
  66. catchsql { SELECT * FROM t0 } db2
  67. } {1 {database table is locked: t0}}
  68. do_test vtab_shared-1.8.4 {
  69. execsql { SELECT * FROM t0 } db
  70. } {1 2 3 4 5 6}
  71. do_test vtab_shared-1.8.5 {
  72. execsql { COMMIT } db
  73. execsql { SELECT * FROM t1 } db2
  74. } {1 2 3 4 5 6}
  75. # While a SELECT is active on virtual table t1 via connection [db], close
  76. # [db2]. This causes the schema to be reset internally. Verify that this
  77. # does not cause a problem.
  78. #
  79. foreach {iTest dbSelect dbClose} {
  80. 1 db db2
  81. 2 db db2
  82. 3 db2 db
  83. } {
  84. do_test vtab_shared-1.9.$iTest {
  85. set res [list]
  86. $dbSelect eval { SELECT * FROM t1 } {
  87. if {$a == 1} {$dbClose close}
  88. lappend res $a $b $c
  89. }
  90. sqlite3 $dbClose test.db
  91. register_echo_module [sqlite3_connection_pointer $dbClose]
  92. set res
  93. } {1 2 3 4 5 6}
  94. }
  95. # Ensure that it is not possible for one connection to DROP a virtual
  96. # table while a second connection is reading from the database.
  97. #
  98. do_test vtab_shared-1.10 {
  99. db eval { SELECT * FROM t1 } {
  100. set error [catchsql { DROP TABLE t1 } db2]
  101. break
  102. }
  103. set error
  104. } {1 {database table is locked: sqlite_master}}
  105. do_test vtab_shared-1.11 {
  106. breakpoint
  107. execsql {
  108. CREATE VIRTUAL TABLE t2 USING echo(t0);
  109. CREATE VIRTUAL TABLE t3 USING echo(t0);
  110. }
  111. execsql { SELECT * FROM t3 } db2
  112. } {1 2 3 4 5 6}
  113. ifcapable compound {
  114. do_test vtab_shared-1.12.1 {
  115. db close
  116. execsql {
  117. SELECT * FROM t1 UNION ALL
  118. SELECT * FROM t2 UNION ALL
  119. SELECT * FROM t3
  120. } db2
  121. } {1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6}
  122. do_test vtab_shared-1.12.2 {
  123. sqlite3 db test.db
  124. register_echo_module [sqlite3_connection_pointer db]
  125. execsql {
  126. SELECT * FROM t1 UNION ALL
  127. SELECT * FROM t2 UNION ALL
  128. SELECT * FROM t3
  129. } db
  130. } {1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6}
  131. }
  132. # Try a rename or two.
  133. #
  134. ifcapable altertable {
  135. do_test vtab_shared-1.13.1 {
  136. execsql { ALTER TABLE t1 RENAME TO t4 }
  137. execsql { SELECT * FROM t4 } db
  138. } {1 2 3 4 5 6}
  139. do_test vtab_shared-1.13.2 {
  140. execsql { SELECT * FROM t4 } db2
  141. } {1 2 3 4 5 6}
  142. do_test vtab_shared-1.13.3 {
  143. execsql { ALTER TABLE t2 RENAME TO t5 }
  144. execsql { SELECT * FROM t4 } db2
  145. } {1 2 3 4 5 6}
  146. }
  147. # Try an UPDATE/INSERT/DELETE on a shared vtab as the first statement after a
  148. # schema is loaded.
  149. do_test vtab_shared_1.14.1 {
  150. db2 close
  151. sqlite3 db2 test.db
  152. register_echo_module [sqlite3_connection_pointer db2]
  153. execsql { SELECT * FROM t3 }
  154. } {1 2 3 4 5 6}
  155. do_test vtab_shared_1.14.2 {
  156. execsql {
  157. UPDATE t3 SET c = 'six' WHERE c = 6;
  158. SELECT * FROM t3;
  159. } db2
  160. } {1 2 3 4 5 six}
  161. do_test vtab_shared_1.14.3 {
  162. db2 close
  163. sqlite3 db2 test.db
  164. register_echo_module [sqlite3_connection_pointer db2]
  165. execsql { SELECT * FROM t3 }
  166. } {1 2 3 4 5 six}
  167. do_test vtab_shared_1.14.4 {
  168. execsql {
  169. DELETE FROM t3 WHERE c = 'six';
  170. SELECT * FROM t3;
  171. } db2
  172. } {1 2 3}
  173. do_test vtab_shared_1.14.5 {
  174. db2 close
  175. sqlite3 db2 test.db
  176. register_echo_module [sqlite3_connection_pointer db2]
  177. execsql { SELECT * FROM t3 }
  178. } {1 2 3}
  179. do_test vtab_shared_1.14.6 {
  180. execsql {
  181. INSERT INTO t3 VALUES(4, 5, 6);
  182. SELECT * FROM t3;
  183. } db2
  184. } {1 2 3 4 5 6}
  185. do_test vtab_shared_1.15.1 {
  186. db2 close
  187. sqlite3 db2 test.db
  188. register_echo_module [sqlite3_connection_pointer db2]
  189. execsql {
  190. UPDATE t3 SET c = 'six' WHERE c = 6;
  191. SELECT * FROM t3;
  192. } db2
  193. } {1 2 3 4 5 six}
  194. do_test vtab_shared_1.15.2 {
  195. db2 close
  196. sqlite3 db2 test.db
  197. register_echo_module [sqlite3_connection_pointer db2]
  198. execsql {
  199. DELETE FROM t3 WHERE c = 'six';
  200. SELECT * FROM t3;
  201. } db2
  202. } {1 2 3}
  203. do_test vtab_shared_1.15.3 {
  204. db2 close
  205. sqlite3 db2 test.db
  206. register_echo_module [sqlite3_connection_pointer db2]
  207. execsql {
  208. INSERT INTO t3 VALUES(4, 5, 6);
  209. SELECT * FROM t3;
  210. }
  211. } {1 2 3 4 5 6}
  212. db close
  213. db2 close
  214. sqlite3_enable_shared_cache 0
  215. finish_test