misc4.test 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. # 2004 Jun 27
  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 miscellanous features that were
  14. # left out of other test files.
  15. #
  16. # $Id: misc4.test,v 1.23 2007/12/08 18:01:31 drh Exp $
  17. set testdir [file dirname $argv0]
  18. source $testdir/tester.tcl
  19. # Prepare a statement that will create a temporary table. Then do
  20. # a rollback. Then try to execute the prepared statement.
  21. #
  22. do_test misc4-1.1 {
  23. set DB [sqlite3_connection_pointer db]
  24. execsql {
  25. CREATE TABLE t1(x);
  26. INSERT INTO t1 VALUES(1);
  27. }
  28. } {}
  29. ifcapable tempdb {
  30. do_test misc4-1.2 {
  31. set sql {CREATE TEMP TABLE t2 AS SELECT * FROM t1}
  32. set stmt [sqlite3_prepare $DB $sql -1 TAIL]
  33. execsql {
  34. BEGIN;
  35. CREATE TABLE t3(a,b,c);
  36. INSERT INTO t1 SELECT * FROM t1;
  37. ROLLBACK;
  38. }
  39. } {}
  40. # Because the previous transaction included a DDL statement and
  41. # was rolled back, statement $stmt was marked as expired. Executing it
  42. # now returns SQLITE_SCHEMA.
  43. do_test misc4-1.2.1 {
  44. list [sqlite3_step $stmt] [sqlite3_finalize $stmt]
  45. } {SQLITE_ERROR SQLITE_SCHEMA}
  46. do_test misc4-1.2.2 {
  47. set stmt [sqlite3_prepare $DB $sql -1 TAIL]
  48. set TAIL
  49. } {}
  50. do_test misc4-1.3 {
  51. sqlite3_step $stmt
  52. } SQLITE_DONE
  53. do_test misc4-1.4 {
  54. execsql {
  55. SELECT * FROM temp.t2;
  56. }
  57. } {1}
  58. # Drop the temporary table, then rerun the prepared statement to
  59. # recreate it again. This recreates ticket #807.
  60. #
  61. do_test misc4-1.5 {
  62. execsql {DROP TABLE t2}
  63. sqlite3_reset $stmt
  64. sqlite3_step $stmt
  65. } {SQLITE_ERROR}
  66. do_test misc4-1.6 {
  67. sqlite3_finalize $stmt
  68. } {SQLITE_SCHEMA}
  69. }
  70. # Prepare but do not execute various CREATE statements. Then before
  71. # those statements are executed, try to use the tables, indices, views,
  72. # are triggers that were created.
  73. #
  74. do_test misc4-2.1 {
  75. set stmt [sqlite3_prepare $DB {CREATE TABLE t3(x);} -1 TAIL]
  76. catchsql {
  77. INSERT INTO t3 VALUES(1);
  78. }
  79. } {1 {no such table: t3}}
  80. do_test misc4-2.2 {
  81. sqlite3_step $stmt
  82. } SQLITE_DONE
  83. do_test misc4-2.3 {
  84. sqlite3_finalize $stmt
  85. } SQLITE_OK
  86. do_test misc4-2.4 {
  87. catchsql {
  88. INSERT INTO t3 VALUES(1);
  89. }
  90. } {0 {}}
  91. # Ticket #966
  92. #
  93. do_test misc4-3.1 {
  94. execsql {
  95. CREATE TABLE Table1(ID integer primary key, Value TEXT);
  96. INSERT INTO Table1 VALUES(1, 'x');
  97. CREATE TABLE Table2(ID integer NOT NULL, Value TEXT);
  98. INSERT INTO Table2 VALUES(1, 'z');
  99. INSERT INTO Table2 VALUES (1, 'a');
  100. }
  101. catchsql {
  102. SELECT ID, max(Value) FROM Table2 GROUP BY 1, 2 ORDER BY 1, 2;
  103. }
  104. } {1 {aggregate functions are not allowed in the GROUP BY clause}}
  105. ifcapable compound {
  106. do_test misc4-3.2 {
  107. execsql {
  108. SELECT ID, Value FROM Table1
  109. UNION SELECT ID, max(Value) FROM Table2 GROUP BY 1
  110. ORDER BY 1, 2;
  111. }
  112. } {1 x 1 z}
  113. do_test misc4-3.3 {
  114. catchsql {
  115. SELECT ID, Value FROM Table1
  116. UNION SELECT ID, max(Value) FROM Table2 GROUP BY 1, 2
  117. ORDER BY 1, 2;
  118. }
  119. } {1 {aggregate functions are not allowed in the GROUP BY clause}}
  120. do_test misc4-3.4 {
  121. catchsql {
  122. SELECT ID, max(Value) FROM Table2 GROUP BY 1, 2
  123. UNION SELECT ID, Value FROM Table1
  124. ORDER BY 1, 2;
  125. }
  126. } {1 {aggregate functions are not allowed in the GROUP BY clause}}
  127. } ;# ifcapable compound
  128. # Ticket #1047. Make sure column types are preserved in subqueries.
  129. #
  130. ifcapable subquery {
  131. do_test misc4-4.1 {
  132. execsql {
  133. create table a(key varchar, data varchar);
  134. create table b(key varchar, period integer);
  135. insert into a values('01','data01');
  136. insert into a values('+1','data+1');
  137. insert into b values ('01',1);
  138. insert into b values ('01',2);
  139. insert into b values ('+1',3);
  140. insert into b values ('+1',4);
  141. select a.*, x.*
  142. from a, (select key,sum(period) from b group by key) as x
  143. where a.key=x.key order by 1 desc;
  144. }
  145. } {01 data01 01 3 +1 data+1 +1 7}
  146. # This test case tests the same property as misc4-4.1, but it is
  147. # a bit smaller which makes it easier to work with while debugging.
  148. do_test misc4-4.2 {
  149. execsql {
  150. CREATE TABLE ab(a TEXT, b TEXT);
  151. INSERT INTO ab VALUES('01', '1');
  152. }
  153. execsql {
  154. select * from ab, (select b from ab) as x where x.b = ab.a;
  155. }
  156. } {}
  157. }
  158. # Ticket #1036. When creating tables from a SELECT on a view, use the
  159. # short names of columns.
  160. #
  161. ifcapable view {
  162. do_test misc4-5.1 {
  163. execsql {
  164. create table t4(a,b);
  165. create table t5(a,c);
  166. insert into t4 values (1,2);
  167. insert into t5 values (1,3);
  168. create view myview as select t4.a a from t4 inner join t5 on t4.a=t5.a;
  169. create table problem as select * from myview;
  170. }
  171. execsql2 {
  172. select * FROM problem;
  173. }
  174. } {a 1}
  175. do_test misc4-5.2 {
  176. execsql2 {
  177. create table t6 as select * from t4, t5;
  178. select * from t6;
  179. }
  180. } {a 1 b 2 a:1 1 c 3}
  181. }
  182. # Ticket #1086
  183. do_test misc4-6.1 {
  184. execsql {
  185. CREATE TABLE abc(a);
  186. INSERT INTO abc VALUES(1);
  187. CREATE TABLE def(d, e, f, PRIMARY KEY(d, e));
  188. }
  189. } {}
  190. do_test misc4-6.2 {
  191. execsql {
  192. SELECT a FROM abc LEFT JOIN def ON (abc.a=def.d);
  193. }
  194. } {1}
  195. finish_test