schema2.test 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. # 2006 November 08
  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 implements regression tests for SQLite library.
  12. #
  13. # This file tests the various conditions under which an SQLITE_SCHEMA
  14. # error should be returned. This is a copy of schema.test that
  15. # has been altered to use sqlite3_prepare_v2 instead of sqlite3_prepare
  16. #
  17. # $Id: schema2.test,v 1.4 2009/02/04 17:40:58 drh Exp $
  18. #---------------------------------------------------------------------
  19. # When any of the following types of SQL statements or actions are
  20. # executed, all pre-compiled statements are invalidated. An attempt
  21. # to execute an invalidated statement always returns SQLITE_SCHEMA.
  22. #
  23. # CREATE/DROP TABLE...................................schema2-1.*
  24. # CREATE/DROP VIEW....................................schema2-2.*
  25. # CREATE/DROP TRIGGER.................................schema2-3.*
  26. # CREATE/DROP INDEX...................................schema2-4.*
  27. # DETACH..............................................schema2-5.*
  28. # Deleting a user-function............................schema2-6.*
  29. # Deleting a collation sequence.......................schema2-7.*
  30. # Setting or changing the authorization function......schema2-8.*
  31. #
  32. # Test cases schema2-9.* and schema2-10.* test some specific bugs
  33. # that came up during development.
  34. #
  35. # Test cases schema2-11.* test that it is impossible to delete or
  36. # change a collation sequence or user-function while SQL statements
  37. # are executing. Adding new collations or functions is allowed.
  38. #
  39. set testdir [file dirname $argv0]
  40. source $testdir/tester.tcl
  41. do_test schema2-1.1 {
  42. set ::STMT [sqlite3_prepare_v2 $::DB {SELECT * FROM sqlite_master} -1 TAIL]
  43. execsql {
  44. CREATE TABLE abc(a, b, c);
  45. }
  46. sqlite3_step $::STMT
  47. } {SQLITE_ROW}
  48. do_test schema2-1.2 {
  49. sqlite3_finalize $::STMT
  50. } {SQLITE_OK}
  51. do_test schema2-1.3 {
  52. set ::STMT [sqlite3_prepare_v2 $::DB {SELECT * FROM sqlite_master} -1 TAIL]
  53. execsql {
  54. DROP TABLE abc;
  55. }
  56. sqlite3_step $::STMT
  57. } {SQLITE_DONE}
  58. do_test schema2-1.4 {
  59. sqlite3_finalize $::STMT
  60. } {SQLITE_OK}
  61. ifcapable view {
  62. do_test schema2-2.1 {
  63. set ::STMT [sqlite3_prepare_v2 $::DB {SELECT * FROM sqlite_master} -1 TAIL]
  64. execsql {
  65. CREATE VIEW v1 AS SELECT * FROM sqlite_master;
  66. }
  67. sqlite3_step $::STMT
  68. } {SQLITE_ROW}
  69. do_test schema2-2.2 {
  70. sqlite3_finalize $::STMT
  71. } {SQLITE_OK}
  72. do_test schema2-2.3 {
  73. set ::STMT [sqlite3_prepare_v2 $::DB {SELECT * FROM sqlite_master} -1 TAIL]
  74. execsql {
  75. DROP VIEW v1;
  76. }
  77. sqlite3_step $::STMT
  78. } {SQLITE_DONE}
  79. do_test schema2-2.4 {
  80. sqlite3_finalize $::STMT
  81. } {SQLITE_OK}
  82. }
  83. ifcapable trigger {
  84. do_test schema2-3.1 {
  85. execsql {
  86. CREATE TABLE abc(a, b, c);
  87. }
  88. set ::STMT [sqlite3_prepare_v2 $::DB {SELECT * FROM sqlite_master} -1 TAIL]
  89. execsql {
  90. CREATE TRIGGER abc_trig AFTER INSERT ON abc BEGIN
  91. SELECT 1, 2, 3;
  92. END;
  93. }
  94. sqlite3_step $::STMT
  95. } {SQLITE_ROW}
  96. do_test schema2-3.2 {
  97. sqlite3_finalize $::STMT
  98. } {SQLITE_OK}
  99. do_test schema2-3.3 {
  100. set ::STMT [sqlite3_prepare_v2 $::DB {SELECT * FROM sqlite_master} -1 TAIL]
  101. execsql {
  102. DROP TRIGGER abc_trig;
  103. }
  104. sqlite3_step $::STMT
  105. } {SQLITE_ROW}
  106. do_test schema2-3.4 {
  107. sqlite3_finalize $::STMT
  108. } {SQLITE_OK}
  109. }
  110. do_test schema2-4.1 {
  111. catchsql {
  112. CREATE TABLE abc(a, b, c);
  113. }
  114. set ::STMT [sqlite3_prepare_v2 $::DB {SELECT * FROM sqlite_master} -1 TAIL]
  115. execsql {
  116. CREATE INDEX abc_index ON abc(a);
  117. }
  118. sqlite3_step $::STMT
  119. } {SQLITE_ROW}
  120. do_test schema2-4.2 {
  121. sqlite3_finalize $::STMT
  122. } {SQLITE_OK}
  123. do_test schema2-4.3 {
  124. set ::STMT [sqlite3_prepare_v2 $::DB {SELECT * FROM sqlite_master} -1 TAIL]
  125. execsql {
  126. DROP INDEX abc_index;
  127. }
  128. sqlite3_step $::STMT
  129. } {SQLITE_ROW}
  130. do_test schema2-4.4 {
  131. sqlite3_finalize $::STMT
  132. } {SQLITE_OK}
  133. #---------------------------------------------------------------------
  134. # Tests 5.1 to 5.4 check that prepared statements are invalidated when
  135. # a database is DETACHed (but not when one is ATTACHed).
  136. #
  137. ifcapable attach {
  138. do_test schema2-5.1 {
  139. set sql {SELECT * FROM abc;}
  140. set ::STMT [sqlite3_prepare_v2 $::DB $sql -1 TAIL]
  141. execsql {
  142. ATTACH 'test2.db' AS aux;
  143. }
  144. sqlite3_step $::STMT
  145. } {SQLITE_DONE}
  146. do_test schema2-5.2 {
  147. sqlite3_reset $::STMT
  148. } {SQLITE_OK}
  149. do_test schema2-5.3 {
  150. execsql {
  151. DETACH aux;
  152. }
  153. sqlite3_step $::STMT
  154. } {SQLITE_DONE}
  155. do_test schema2-5.4 {
  156. sqlite3_finalize $::STMT
  157. } {SQLITE_OK}
  158. }
  159. #---------------------------------------------------------------------
  160. # Tests 6.* check that prepared statements are invalidated when
  161. # a user-function is deleted (but not when one is added).
  162. do_test schema2-6.1 {
  163. set sql {SELECT * FROM abc;}
  164. set ::STMT [sqlite3_prepare_v2 $::DB $sql -1 TAIL]
  165. db function hello_function {}
  166. sqlite3_step $::STMT
  167. } {SQLITE_DONE}
  168. do_test schema2-6.2 {
  169. sqlite3_reset $::STMT
  170. } {SQLITE_OK}
  171. do_test schema2-6.3 {
  172. sqlite_delete_function $::DB hello_function
  173. sqlite3_step $::STMT
  174. } {SQLITE_DONE}
  175. do_test schema2-6.4 {
  176. sqlite3_finalize $::STMT
  177. } {SQLITE_OK}
  178. #---------------------------------------------------------------------
  179. # Tests 7.* check that prepared statements are invalidated when
  180. # a collation sequence is deleted (but not when one is added).
  181. #
  182. ifcapable utf16 {
  183. do_test schema2-7.1 {
  184. set sql {SELECT * FROM abc;}
  185. set ::STMT [sqlite3_prepare_v2 $::DB $sql -1 TAIL]
  186. add_test_collate $::DB 1 1 1
  187. sqlite3_step $::STMT
  188. } {SQLITE_DONE}
  189. do_test schema2-7.2 {
  190. sqlite3_reset $::STMT
  191. } {SQLITE_OK}
  192. do_test schema2-7.3 {
  193. add_test_collate $::DB 0 0 0
  194. sqlite3_step $::STMT
  195. } {SQLITE_DONE}
  196. do_test schema2-7.4 {
  197. sqlite3_finalize $::STMT
  198. } {SQLITE_OK}
  199. }
  200. #---------------------------------------------------------------------
  201. # Tests 8.1 and 8.2 check that prepared statements are invalidated when
  202. # the authorization function is set.
  203. #
  204. ifcapable auth {
  205. do_test schema2-8.1 {
  206. set ::STMT [sqlite3_prepare_v2 $::DB {SELECT * FROM sqlite_master} -1 TAIL]
  207. db auth {}
  208. sqlite3_step $::STMT
  209. } {SQLITE_ROW}
  210. do_test schema2-8.3 {
  211. sqlite3_finalize $::STMT
  212. } {SQLITE_OK}
  213. }
  214. #---------------------------------------------------------------------
  215. # schema2-9.1: Test that if a table is dropped by one database connection,
  216. # other database connections are aware of the schema change.
  217. # schema2-9.2: Test that if a view is dropped by one database connection,
  218. # other database connections are aware of the schema change.
  219. #
  220. do_test schema2-9.1 {
  221. sqlite3 db2 test.db
  222. execsql {
  223. DROP TABLE abc;
  224. } db2
  225. db2 close
  226. catchsql {
  227. SELECT * FROM abc;
  228. }
  229. } {1 {no such table: abc}}
  230. execsql {
  231. CREATE TABLE abc(a, b, c);
  232. }
  233. ifcapable view {
  234. do_test schema2-9.2 {
  235. execsql {
  236. CREATE VIEW abcview AS SELECT * FROM abc;
  237. }
  238. sqlite3 db2 test.db
  239. execsql {
  240. DROP VIEW abcview;
  241. } db2
  242. db2 close
  243. catchsql {
  244. SELECT * FROM abcview;
  245. }
  246. } {1 {no such table: abcview}}
  247. }
  248. #---------------------------------------------------------------------
  249. # Test that if a CREATE TABLE statement fails because there are other
  250. # btree cursors open on the same database file it does not corrupt
  251. # the sqlite_master table.
  252. #
  253. # 2007-05-02: These tests have been overcome by events. Open btree
  254. # cursors no longer block CREATE TABLE. But there is no reason not
  255. # to keep the tests in the test suite.
  256. #
  257. do_test schema2-10.1 {
  258. execsql {
  259. INSERT INTO abc VALUES(1, 2, 3);
  260. }
  261. set sql {SELECT * FROM abc}
  262. set ::STMT [sqlite3_prepare_v2 $::DB $sql -1 TAIL]
  263. sqlite3_step $::STMT
  264. } {SQLITE_ROW}
  265. do_test schema2-10.2 {
  266. catchsql {
  267. CREATE TABLE t2(a, b, c);
  268. }
  269. } {0 {}}
  270. do_test schema2-10.3 {
  271. sqlite3_finalize $::STMT
  272. } {SQLITE_OK}
  273. do_test schema2-10.4 {
  274. sqlite3 db2 test.db
  275. execsql {
  276. SELECT * FROM abc
  277. } db2
  278. } {1 2 3}
  279. do_test schema2-10.5 {
  280. db2 close
  281. } {}
  282. #---------------------------------------------------------------------
  283. # Attempting to delete or replace a user-function or collation sequence
  284. # while there are active statements returns an SQLITE_BUSY error.
  285. #
  286. # schema2-11.1 - 11.4: User function.
  287. # schema2-11.5 - 11.8: Collation sequence.
  288. #
  289. do_test schema2-11.1 {
  290. db function tstfunc {}
  291. set sql {SELECT * FROM abc}
  292. set ::STMT [sqlite3_prepare_v2 $::DB $sql -1 TAIL]
  293. sqlite3_step $::STMT
  294. } {SQLITE_ROW}
  295. do_test schema2-11.2 {
  296. sqlite_delete_function $::DB tstfunc
  297. } {SQLITE_BUSY}
  298. do_test schema2-11.3 {
  299. set rc [catch {
  300. db function tstfunc {}
  301. } msg]
  302. list $rc $msg
  303. } {1 {unable to delete/modify user-function due to active statements}}
  304. do_test schema2-11.4 {
  305. sqlite3_finalize $::STMT
  306. } {SQLITE_OK}
  307. do_test schema2-11.5 {
  308. db collate tstcollate {}
  309. set sql {SELECT * FROM abc}
  310. set ::STMT [sqlite3_prepare_v2 $::DB $sql -1 TAIL]
  311. sqlite3_step $::STMT
  312. } {SQLITE_ROW}
  313. do_test schema2-11.6 {
  314. sqlite_delete_collation $::DB tstcollate
  315. } {SQLITE_BUSY}
  316. do_test schema2-11.7 {
  317. set rc [catch {
  318. db collate tstcollate {}
  319. } msg]
  320. list $rc $msg
  321. } {1 {unable to delete/modify collation sequence due to active statements}}
  322. do_test schema2-11.8 {
  323. sqlite3_finalize $::STMT
  324. } {SQLITE_OK}
  325. finish_test