tkt3718.test 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 execution of SQL statements from
  13. # within callbacks generated by VMs that themselves open statement
  14. # transactions.
  15. #
  16. # $Id: tkt3718.test,v 1.2 2009/06/05 17:09:12 drh Exp $
  17. set testdir [file dirname $argv0]
  18. source $testdir/tester.tcl
  19. do_test tkt3718-1.1 {
  20. execsql {
  21. CREATE TABLE t1(a PRIMARY KEY, b);
  22. INSERT INTO t1 VALUES(1, 'one');
  23. INSERT INTO t1 VALUES(2, 'two');
  24. INSERT INTO t1 VALUES(3, 'three');
  25. INSERT INTO t1 VALUES(4, 'four');
  26. INSERT INTO t1 VALUES(5, 'five');
  27. CREATE TABLE t2(a PRIMARY KEY, b);
  28. }
  29. } {}
  30. # SQL scalar function:
  31. #
  32. # f1(<arg>)
  33. #
  34. # Uses database handle [db] to execute "SELECT f2(<arg>)". Returns either
  35. # the results or error message from the "SELECT f2(<arg>)" query to the
  36. # caller.
  37. #
  38. proc f1 {args} {
  39. set a [lindex $args 0]
  40. catch { db eval {SELECT f2($a)} } msg
  41. set msg
  42. }
  43. # SQL scalar function:
  44. #
  45. # f2(<arg>)
  46. #
  47. # Return the value of <arg>. Unless <arg> is "three", in which case throw
  48. # an exception.
  49. #
  50. proc f2 {args} {
  51. set a [lindex $args 0]
  52. if {$a == "three"} { error "Three!!" }
  53. return $a
  54. }
  55. db func f1 f1
  56. db func f2 f2
  57. # The second INSERT statement below uses the f1 user function such that
  58. # half-way through the INSERT operation f1() will run an SQL statement
  59. # that throws an exception. At one point, before #3718 was fixed, this
  60. # caused the statement transaction belonging to the INSERT statement to
  61. # be rolled back. The result was that some (but not all) of the rows that
  62. # should have been inserted went missing.
  63. #
  64. do_test tkt3718-1.2 {
  65. execsql {
  66. BEGIN;
  67. INSERT INTO t2 SELECT a, b FROM t1;
  68. INSERT INTO t2 SELECT a+5, f1(b) FROM t1;
  69. COMMIT;
  70. }
  71. execsql {
  72. SELECT a FROM t2;
  73. }
  74. } {1 2 3 4 5 6 7 8 9 10}
  75. # This test turns on the count_changes pragma (causing DML statements to
  76. # return SQLITE_ROW once, with a single integer result value reporting the
  77. # number of rows affected by the statement). It then executes an INSERT
  78. # statement that requires a statement journal. After stepping the statement
  79. # once, so that it returns SQLITE_ROW, a second SQL statement that throws an
  80. # exception is run. At one point, before #3718 was fixed, this caused the
  81. # statement transaction belonging to the INSERT statement to be rolled back.
  82. # The result was that none of the rows were actually inserted.
  83. #
  84. #
  85. do_test tkt3718-1.3 {
  86. execsql {
  87. DELETE FROM t2 WHERE a > 5;
  88. PRAGMA count_changes = 1;
  89. BEGIN;
  90. }
  91. db eval {INSERT INTO t2 SELECT a+5, b||'+5' FROM t1} {
  92. catch { db eval {SELECT f2('three')} } msg
  93. }
  94. execsql {
  95. COMMIT;
  96. SELECT a FROM t2;
  97. }
  98. } {1 2 3 4 5 6 7 8 9 10}
  99. do_test tkt3718-1.4 {
  100. execsql {pragma count_changes=0}
  101. } {}
  102. # This SQL function executes the SQL specified as an argument against
  103. # database [db].
  104. #
  105. proc sql {doit zSql} {
  106. if {$doit} { catchsql $zSql }
  107. }
  108. db func sql [list sql]
  109. # The following tests, tkt3718-2.*, test that a nested statement
  110. # transaction can be successfully committed or reverted without
  111. # affecting the parent statement transaction.
  112. #
  113. do_test tkt3718-2.1 {
  114. execsql { SELECT sql(1, 'DELETE FROM t2 WHERE a = '||a ) FROM t2 WHERE a>5 }
  115. execsql { SELECT a from t2 }
  116. } {1 2 3 4 5}
  117. do_test tkt3718-2.2 {
  118. execsql {
  119. DELETE FROM t2 WHERE a > 5;
  120. BEGIN;
  121. INSERT INTO t2 SELECT a+5, sql(a==3,
  122. 'INSERT INTO t2 SELECT a+10, f2(b) FROM t1'
  123. ) FROM t1;
  124. }
  125. execsql {
  126. COMMIT;
  127. SELECT a FROM t2;
  128. }
  129. } {1 2 3 4 5 6 7 8 9 10}
  130. do_test tkt3718-2.3 {
  131. execsql {
  132. DELETE FROM t2 WHERE a > 5;
  133. BEGIN;
  134. INSERT INTO t2 SELECT a+5, sql(a==3,
  135. 'INSERT INTO t2 SELECT a+10, b FROM t1'
  136. ) FROM t1;
  137. COMMIT;
  138. }
  139. execsql { SELECT a FROM t2 ORDER BY a+0}
  140. } {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15}
  141. integrity_check tkt3718.2-4
  142. # The next set of tests, tkt3718-3.*, test that a statement transaction
  143. # that has a committed statement transaction nested inside of it can
  144. # be committed or reverted.
  145. #
  146. foreach {tn io ii results} {
  147. 1 0 10 {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20}
  148. 2 1 10 {6 7 8 9 10 16 17 18 19 20}
  149. 3 0 11 {1 2 3 4 5 6 7 8 9 10 16 17 18 19 20}
  150. 4 1 11 {6 7 8 9 10 16 17 18 19 20}
  151. } {
  152. do_test tkt3718-3.$tn {
  153. execsql {
  154. DELETE FROM t2;
  155. INSERT INTO t2 SELECT a+5, b FROM t1;
  156. INSERT INTO t2 SELECT a+15, b FROM t1;
  157. }
  158. catchsql "
  159. BEGIN;
  160. INSERT INTO t2 SELECT a+$io, sql(a==3,
  161. 'INSERT INTO t2 SELECT a+$ii, b FROM t1'
  162. ) FROM t1;
  163. "
  164. execsql { COMMIT }
  165. execsql { SELECT a FROM t2 ORDER BY a+0}
  166. } $results
  167. integrity_check tkt3718-3.$tn.integrity
  168. }
  169. # This is the same test as tkt3718-3.*, but with 3 levels of nesting.
  170. #
  171. foreach {tn i1 i2 i3 results} {
  172. 1 0 10 20 {5 10 15 20 25 30}
  173. 2 0 10 21 {5 10 15 20 30}
  174. 3 0 11 20 {5 10 20 30}
  175. 4 0 11 21 {5 10 20 30}
  176. 5 1 10 20 {10 20 30}
  177. 6 1 10 21 {10 20 30}
  178. 7 1 11 20 {10 20 30}
  179. 8 1 11 21 {10 20 30}
  180. } {
  181. do_test tkt3718-4.$tn {
  182. execsql {
  183. DELETE FROM t2;
  184. INSERT INTO t2 SELECT a+5, b FROM t1;
  185. INSERT INTO t2 SELECT a+15, b FROM t1;
  186. INSERT INTO t2 SELECT a+25, b FROM t1;
  187. }
  188. catchsql "
  189. BEGIN;
  190. INSERT INTO t2 SELECT a+$i1, sql(a==3,
  191. 'INSERT INTO t2 SELECT a+$i2, sql(a==3,
  192. ''INSERT INTO t2 SELECT a+$i3, b FROM t1''
  193. ) FROM t1'
  194. ) FROM t1;
  195. "
  196. execsql { COMMIT }
  197. execsql { SELECT a FROM t2 WHERE (a%5)==0 ORDER BY a+0}
  198. } $results
  199. do_test tkt3718-4.$tn.extra {
  200. execsql {
  201. SELECT
  202. (SELECT sum(a) FROM t2)==(SELECT sum(a*5-10) FROM t2 WHERE (a%5)==0)
  203. }
  204. } {1}
  205. integrity_check tkt3718-4.$tn.integrity
  206. }
  207. finish_test