insert2.test 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. # 2001 September 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. # This file implements regression tests for SQLite library. The
  12. # focus of this file is testing the INSERT statement that takes is
  13. # result from a SELECT.
  14. #
  15. # $Id: insert2.test,v 1.9 2002/04/12 10:09:00 drh Exp $
  16. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. # Create some tables with data that we can select against
  19. #
  20. do_test insert2-1.0 {
  21. execsql {CREATE TABLE d1(n int, log int);}
  22. for {set i 1} {$i<=20} {incr i} {
  23. for {set j 0} {pow(2,$j)<$i} {incr j} {}
  24. execsql "INSERT INTO d1 VALUES($i,$j)"
  25. }
  26. execsql {SELECT * FROM d1 ORDER BY n}
  27. } {1 0 2 1 3 2 4 2 5 3 6 3 7 3 8 3 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 5 18 5 19 5 20 5}
  28. # Insert into a new table from the old one.
  29. #
  30. do_test insert2-1.1.1 {
  31. execsql {
  32. CREATE TABLE t1(log int, cnt int);
  33. PRAGMA count_changes=on;
  34. INSERT INTO t1 SELECT log, count(*) FROM d1 GROUP BY log;
  35. }
  36. } {6}
  37. do_test insert2-1.1.2 {
  38. db changes
  39. } {6}
  40. do_test insert2-1.1.3 {
  41. execsql {SELECT * FROM t1 ORDER BY log}
  42. } {0 1 1 1 2 2 3 4 4 8 5 4}
  43. do_test insert2-1.2.1 {
  44. catch {execsql {DROP TABLE t1}}
  45. execsql {
  46. CREATE TABLE t1(log int, cnt int);
  47. INSERT INTO t1
  48. SELECT log, count(*) FROM d1 GROUP BY log
  49. EXCEPT SELECT n-1,log FROM d1;
  50. }
  51. } {4}
  52. do_test insert2-1.2.2 {
  53. execsql {
  54. SELECT * FROM t1 ORDER BY log;
  55. }
  56. } {0 1 3 4 4 8 5 4}
  57. do_test insert2-1.3.1 {
  58. catch {execsql {DROP TABLE t1}}
  59. execsql {
  60. CREATE TABLE t1(log int, cnt int);
  61. PRAGMA count_changes=off;
  62. INSERT INTO t1
  63. SELECT log, count(*) FROM d1 GROUP BY log
  64. INTERSECT SELECT n-1,log FROM d1;
  65. }
  66. } {}
  67. do_test insert2-1.3.2 {
  68. execsql {
  69. SELECT * FROM t1 ORDER BY log;
  70. }
  71. } {1 1 2 2}
  72. do_test insert2-1.4 {
  73. catch {execsql {DROP TABLE t1}}
  74. set r [execsql {
  75. CREATE TABLE t1(log int, cnt int);
  76. CREATE INDEX i1 ON t1(log);
  77. CREATE INDEX i2 ON t1(cnt);
  78. INSERT INTO t1 SELECT log, count() FROM d1 GROUP BY log;
  79. SELECT * FROM t1 ORDER BY log;
  80. }]
  81. lappend r [execsql {SELECT cnt FROM t1 WHERE log=3}]
  82. lappend r [execsql {SELECT log FROM t1 WHERE cnt=4 ORDER BY log}]
  83. } {0 1 1 1 2 2 3 4 4 8 5 4 4 {3 5}}
  84. do_test insert2-2.0 {
  85. execsql {
  86. CREATE TABLE t3(a,b,c);
  87. CREATE TABLE t4(x,y);
  88. INSERT INTO t4 VALUES(1,2);
  89. SELECT * FROM t4;
  90. }
  91. } {1 2}
  92. do_test insert2-2.1 {
  93. execsql {
  94. INSERT INTO t3(a,c) SELECT * FROM t4;
  95. SELECT * FROM t3;
  96. }
  97. } {1 {} 2}
  98. do_test insert2-2.2 {
  99. execsql {
  100. DELETE FROM t3;
  101. INSERT INTO t3(c,b) SELECT * FROM t4;
  102. SELECT * FROM t3;
  103. }
  104. } {{} 2 1}
  105. do_test insert2-2.3 {
  106. execsql {
  107. DELETE FROM t3;
  108. INSERT INTO t3(c,a,b) SELECT x, 'hi', y FROM t4;
  109. SELECT * FROM t3;
  110. }
  111. } {hi 2 1}
  112. do_test insert2-3.0 {
  113. set x [execsql {PRAGMA integrity_check}]
  114. if {$x==""} {set x ok}
  115. set x
  116. } {ok}
  117. # File table t4 with lots of data
  118. #
  119. do_test insert2-3.1 {
  120. execsql {
  121. SELECT * from t4;
  122. }
  123. } {1 2}
  124. do_test insert2-3.2 {
  125. execsql {
  126. BEGIN;
  127. INSERT INTO t4 VALUES(2,4);
  128. INSERT INTO t4 VALUES(3,6);
  129. INSERT INTO t4 VALUES(4,8);
  130. INSERT INTO t4 VALUES(5,10);
  131. INSERT INTO t4 VALUES(6,12);
  132. INSERT INTO t4 VALUES(7,14);
  133. INSERT INTO t4 VALUES(8,16);
  134. INSERT INTO t4 VALUES(9,18);
  135. INSERT INTO t4 VALUES(10,20);
  136. COMMIT;
  137. }
  138. db changes
  139. } {9}
  140. do_test insert2-3.2.1 {
  141. execsql {
  142. SELECT count(*) FROM t4;
  143. }
  144. } {10}
  145. do_test insert2-3.3 {
  146. execsql {
  147. BEGIN;
  148. INSERT INTO t4 SELECT x+(SELECT max(x) FROM t4),y FROM t4;
  149. INSERT INTO t4 SELECT x+(SELECT max(x) FROM t4),y FROM t4;
  150. INSERT INTO t4 SELECT x+(SELECT max(x) FROM t4),y FROM t4;
  151. INSERT INTO t4 SELECT x+(SELECT max(x) FROM t4),y FROM t4;
  152. COMMIT;
  153. SELECT count(*) FROM t4;
  154. }
  155. } {160}
  156. do_test insert2-3.4 {
  157. execsql {
  158. BEGIN;
  159. UPDATE t4 SET y='lots of data for the row where x=' || x
  160. || ' and y=' || y || ' - even more data to fill space';
  161. COMMIT;
  162. SELECT count(*) FROM t4;
  163. }
  164. } {160}
  165. do_test insert2-3.5 {
  166. execsql {
  167. BEGIN;
  168. INSERT INTO t4 SELECT x+(SELECT max(x)+1 FROM t4),y FROM t4;
  169. SELECT count(*) from t4;
  170. ROLLBACK;
  171. }
  172. } {320}
  173. do_test insert2-3.6 {
  174. execsql {
  175. SELECT count(*) FROM t4;
  176. }
  177. } {160}
  178. do_test insert2-3.7 {
  179. execsql {
  180. BEGIN;
  181. DELETE FROM t4 WHERE x!=123;
  182. SELECT count(*) FROM t4;
  183. ROLLBACK;
  184. }
  185. } {1}
  186. do_test insert2-3.8 {
  187. db changes
  188. } {159}
  189. finish_test