temptable.test 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. # 2001 October 7
  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 implements tests for temporary tables and indices.
  14. #
  15. # $Id: temptable.test,v 1.6 2002/06/06 23:16:06 drh Exp $
  16. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. # Create an alternative connection to the database
  19. #
  20. do_test temptable-1.0 {
  21. sqlite db2 ./test.db
  22. set dummy {}
  23. } {}
  24. # Create a permanent table.
  25. #
  26. do_test temptable-1.1 {
  27. execsql {CREATE TABLE t1(a,b,c);}
  28. execsql {INSERT INTO t1 VALUES(1,2,3);}
  29. execsql {SELECT * FROM t1}
  30. } {1 2 3}
  31. do_test temptable-1.2 {
  32. catch {db2 eval {SELECT * FROM sqlite_master}}
  33. db2 eval {SELECT * FROM t1}
  34. } {1 2 3}
  35. do_test temptable-1.3 {
  36. execsql {SELECT name FROM sqlite_master}
  37. } {t1}
  38. do_test temptable-1.4 {
  39. db2 eval {SELECT name FROM sqlite_master}
  40. } {t1}
  41. # Create a temporary table. Verify that only one of the two
  42. # processes can see it.
  43. #
  44. do_test temptable-1.5 {
  45. db2 eval {
  46. CREATE TEMP TABLE t2(x,y,z);
  47. INSERT INTO t2 VALUES(4,5,6);
  48. }
  49. db2 eval {SELECT * FROM t2}
  50. } {4 5 6}
  51. do_test temptable-1.6 {
  52. catch {execsql {SELECT * FROM sqlite_master}}
  53. catchsql {SELECT * FROM t2}
  54. } {1 {no such table: t2}}
  55. do_test temptable-1.7 {
  56. catchsql {INSERT INTO t2 VALUES(8,9,0);}
  57. } {1 {no such table: t2}}
  58. do_test temptable-1.8 {
  59. db2 eval {INSERT INTO t2 VALUES(8,9,0);}
  60. db2 eval {SELECT * FROM t2 ORDER BY x}
  61. } {4 5 6 8 9 0}
  62. do_test temptable-1.9 {
  63. db2 eval {DELETE FROM t2 WHERE x==8}
  64. db2 eval {SELECT * FROM t2 ORDER BY x}
  65. } {4 5 6}
  66. do_test temptable-1.10 {
  67. db2 eval {DELETE FROM t2}
  68. db2 eval {SELECT * FROM t2}
  69. } {}
  70. do_test temptable-1.11 {
  71. db2 eval {
  72. INSERT INTO t2 VALUES(7,6,5);
  73. INSERT INTO t2 VALUES(4,3,2);
  74. SELECT * FROM t2 ORDER BY x;
  75. }
  76. } {4 3 2 7 6 5}
  77. do_test temptable-1.12 {
  78. db2 eval {DROP TABLE t2;}
  79. set r [catch {db2 eval {SELECT * FROM t2}} msg]
  80. lappend r $msg
  81. } {1 {no such table: t2}}
  82. # Make sure temporary tables work with transactions
  83. #
  84. do_test temptable-2.1 {
  85. execsql {
  86. BEGIN TRANSACTION;
  87. CREATE TEMPORARY TABLE t2(x,y);
  88. INSERT INTO t2 VALUES(1,2);
  89. SELECT * FROM t2;
  90. }
  91. } {1 2}
  92. do_test temptable-2.2 {
  93. execsql {ROLLBACK}
  94. catchsql {SELECT * FROM t2}
  95. } {1 {no such table: t2}}
  96. do_test temptable-2.3 {
  97. execsql {
  98. BEGIN TRANSACTION;
  99. CREATE TEMPORARY TABLE t2(x,y);
  100. INSERT INTO t2 VALUES(1,2);
  101. SELECT * FROM t2;
  102. }
  103. } {1 2}
  104. do_test temptable-2.4 {
  105. execsql {COMMIT}
  106. catchsql {SELECT * FROM t2}
  107. } {0 {1 2}}
  108. do_test temptable-2.5 {
  109. set r [catch {db2 eval {SELECT * FROM t2}} msg]
  110. lappend r $msg
  111. } {1 {no such table: t2}}
  112. # Make sure indices on temporary tables are also temporary.
  113. #
  114. do_test temptable-3.1 {
  115. execsql {
  116. CREATE INDEX i2 ON t2(x);
  117. SELECT name FROM sqlite_master WHERE type='index';
  118. }
  119. } {}
  120. do_test temptable-3.2 {
  121. execsql {
  122. SELECT y FROM t2 WHERE x=1;
  123. }
  124. } {2}
  125. do_test temptable-3.3 {
  126. execsql {
  127. DROP INDEX i2;
  128. SELECT y FROM t2 WHERE x=1;
  129. }
  130. } {2}
  131. do_test temptable-3.4 {
  132. execsql {
  133. CREATE INDEX i2 ON t2(x);
  134. DROP TABLE t2;
  135. }
  136. catchsql {DROP INDEX i2}
  137. } {1 {no such index: i2}}
  138. # Check for correct name collision processing. A name collision can
  139. # occur when process A creates a temporary table T then process B
  140. # creates a permanent table also named T. The temp table in process A
  141. # hides the existance of the permanent table.
  142. #
  143. do_test temptable-4.1 {
  144. execsql {
  145. CREATE TEMP TABLE t2(x,y);
  146. INSERT INTO t2 VALUES(10,20);
  147. SELECT * FROM t2;
  148. } db2
  149. } {10 20}
  150. do_test temptable-4.2 {
  151. execsql {
  152. CREATE TABLE t2(x,y,z);
  153. INSERT INTO t2 VALUES(9,8,7);
  154. SELECT * FROM t2;
  155. }
  156. } {9 8 7}
  157. do_test temptable-4.3 {
  158. catchsql {
  159. SELECT * FROM t2;
  160. } db2
  161. } {1 {database schema has changed}}
  162. do_test temptable-4.4 {
  163. catchsql {
  164. SELECT * FROM t2;
  165. } db2
  166. } {0 {10 20}}
  167. do_test temptable-4.5 {
  168. catchsql {
  169. DROP TABLE t2;
  170. SELECT * FROM t2;
  171. } db2
  172. } {1 {no such table: t2}}
  173. do_test temptable-4.6 {
  174. db2 close
  175. sqlite db2 ./test.db
  176. catchsql {
  177. SELECT * FROM t2;
  178. } db2
  179. } {0 {9 8 7}}
  180. do_test temptable-4.7 {
  181. catchsql {
  182. DROP TABLE t2;
  183. SELECT * FROM t2;
  184. }
  185. } {1 {no such table: t2}}
  186. do_test temptable-4.8 {
  187. db2 close
  188. sqlite db2 ./test.db
  189. execsql {
  190. CREATE TEMP TABLE t2(x unique,y);
  191. INSERT INTO t2 VALUES(1,2);
  192. SELECT * FROM t2;
  193. } db2
  194. } {1 2}
  195. do_test temptable-4.9 {
  196. execsql {
  197. CREATE TABLE t2(x unique, y);
  198. INSERT INTO t2 VALUES(3,4);
  199. SELECT * FROM t2;
  200. }
  201. } {3 4}
  202. do_test temptable-4.10 {
  203. catchsql {
  204. SELECT * FROM t2;
  205. } db2
  206. } {1 {database schema has changed}}
  207. do_test temptable-4.11 {
  208. execsql {
  209. SELECT * FROM t2;
  210. } db2
  211. } {1 2}
  212. do_test temptable-4.12 {
  213. execsql {
  214. SELECT * FROM t2;
  215. }
  216. } {3 4}
  217. do_test temptable-4.13 {
  218. catchsql {
  219. DROP TABLE t2;
  220. SELECT * FROM t2;
  221. } db2
  222. } {1 {no such table: t2}}
  223. do_test temptable-4.14 {
  224. execsql {
  225. SELECT * FROM t2;
  226. }
  227. } {3 4}
  228. do_test temptable-4.15 {
  229. db2 close
  230. sqlite db2 ./test.db
  231. execsql {
  232. SELECT * FROM t2;
  233. } db2
  234. } {3 4}
  235. # Now create a temporary table in db2 and a permanent index in db. The
  236. # temporary table in db2 should mask the name of the permanent index,
  237. # but the permanent index should still be accessible and should still
  238. # be updated when its corresponding table changes.
  239. #
  240. do_test temptable-5.1 {
  241. execsql {
  242. CREATE TEMP TABLE mask(a,b,c)
  243. } db2
  244. execsql {
  245. CREATE INDEX mask ON t2(x);
  246. SELECT * FROM t2;
  247. }
  248. } {3 4}
  249. do_test temptable-5.2 {
  250. catchsql {
  251. SELECT * FROM t2;
  252. } db2
  253. } {1 {database schema has changed}}
  254. do_test temptable-5.3 {
  255. catchsql {
  256. SELECT * FROM t2;
  257. } db2
  258. } {0 {3 4}}
  259. do_test temptable-5.4 {
  260. execsql {
  261. SELECT y FROM t2 WHERE x=3
  262. }
  263. } {4}
  264. do_test temptable-5.5 {
  265. execsql {
  266. SELECT y FROM t2 WHERE x=3
  267. } db2
  268. } {4}
  269. do_test temptable-5.6 {
  270. execsql {
  271. INSERT INTO t2 VALUES(1,2);
  272. SELECT y FROM t2 WHERE x=1;
  273. } db2
  274. } {2}
  275. do_test temptable-5.7 {
  276. execsql {
  277. SELECT y FROM t2 WHERE x=3
  278. } db2
  279. } {4}
  280. do_test temptable-5.8 {
  281. execsql {
  282. SELECT y FROM t2 WHERE x=1;
  283. }
  284. } {2}
  285. do_test temptable-5.9 {
  286. execsql {
  287. SELECT y FROM t2 WHERE x=3
  288. }
  289. } {4}
  290. db2 close
  291. # Test for correct operation of read-only databases
  292. #
  293. do_test temptable-6.1 {
  294. execsql {
  295. CREATE TABLE t8(x);
  296. INSERT INTO t8 VALUES('xyzzy');
  297. SELECT * FROM t8;
  298. }
  299. } {xyzzy}
  300. do_test temptable-6.2 {
  301. db close
  302. catch {file attributes test.db -permissions 0444}
  303. catch {file attributes test.db -readonly 1}
  304. sqlite db test.db
  305. execsql {
  306. SELECT * FROM t8;
  307. }
  308. } {xyzzy}
  309. do_test temptable-6.3 {
  310. catchsql {
  311. CREATE TABLE t9(x,y);
  312. }
  313. } {1 {attempt to write a readonly database}}
  314. do_test temptable-6.4 {
  315. catchsql {
  316. CREATE TEMP TABLE t9(x,y);
  317. }
  318. } {0 {}}
  319. do_test temptable-6.5 {
  320. catchsql {
  321. INSERT INTO t9 VALUES(1,2);
  322. SELECT * FROM t9;
  323. }
  324. } {0 {1 2}}
  325. do_test temptable-6.6 {
  326. catchsql {
  327. INSERT INTO t8 VALUES('hello');
  328. SELECT * FROM t8;
  329. }
  330. } {1 {attempt to write a readonly database}}
  331. do_test temptable-6.7 {
  332. catchsql {
  333. SELECT * FROM t8,t9;
  334. }
  335. } {0 {xyzzy 1 2}}
  336. do_test temptable-6.8 {
  337. db close
  338. sqlite db test.db
  339. catchsql {
  340. SELECT * FROM t8,t9;
  341. }
  342. } {1 {no such table: t9}}
  343. finish_test