summaryrefslogtreecommitdiff
path: root/test/tkt3718.test
blob: e5eb247481310bf1cb16f90a30b2a66f9a747c16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# 2001 September 15
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library.  The
# focus of this file is testing the execution of SQL statements from
# within callbacks generated by VMs that themselves open statement 
# transactions.
#
# $Id: tkt3718.test,v 1.2 2009/06/05 17:09:12 drh Exp $

set testdir [file dirname $argv0]
source $testdir/tester.tcl

do_test tkt3718-1.1 {
  execsql {
    CREATE TABLE t1(a PRIMARY KEY, b);
    INSERT INTO t1 VALUES(1, 'one');
    INSERT INTO t1 VALUES(2, 'two');
    INSERT INTO t1 VALUES(3, 'three');
    INSERT INTO t1 VALUES(4, 'four');
    INSERT INTO t1 VALUES(5, 'five');
    CREATE TABLE t2(a PRIMARY KEY, b);
  }
} {}

# SQL scalar function:
#
#   f1(<arg>)
#
# Uses database handle [db] to execute "SELECT f2(<arg>)". Returns either
# the results or error message from the "SELECT f2(<arg>)" query to the
# caller.
#
proc f1 {args} {
  set a [lindex $args 0]
  catch { db eval {SELECT f2($a)} } msg
  set msg
}

# SQL scalar function:
#
#   f2(<arg>)
#
# Return the value of <arg>. Unless <arg> is "three", in which case throw
# an exception.
#
proc f2 {args} {
  set a [lindex $args 0]
  if {$a == "three"} { error "Three!!" }
  return $a
}

db func f1 f1
db func f2 f2

# The second INSERT statement below uses the f1 user function such that
# half-way through the INSERT operation f1() will run an SQL statement
# that throws an exception. At one point, before #3718 was fixed, this
# caused the statement transaction belonging to the INSERT statement to
# be rolled back. The result was that some (but not all) of the rows that 
# should have been inserted went missing.
#
do_test tkt3718-1.2 {
  execsql {
    BEGIN;
    INSERT INTO t2 SELECT a, b FROM t1;
    INSERT INTO t2 SELECT a+5, f1(b) FROM t1;
    COMMIT;
  }
  execsql {
    SELECT a FROM t2;
  }
} {1 2 3 4 5 6 7 8 9 10}

# This test turns on the count_changes pragma (causing DML statements to
# return SQLITE_ROW once, with a single integer result value reporting the
# number of rows affected by the statement). It then executes an INSERT
# statement that requires a statement journal. After stepping the statement
# once, so that it returns SQLITE_ROW, a second SQL statement that throws an
# exception is run. At one point, before #3718 was fixed, this caused the
# statement transaction belonging to the INSERT statement to be rolled back.
# The result was that none of the rows were actually inserted.
# 
#
do_test tkt3718-1.3 {
  execsql { 
    DELETE FROM t2 WHERE a > 5;
    PRAGMA count_changes = 1;
    BEGIN;
  }
  db eval {INSERT INTO t2 SELECT a+5, b||'+5' FROM t1} {
    catch { db eval {SELECT f2('three')} } msg
  }
  execsql {
    COMMIT;
    SELECT a FROM t2;
  }
} {1 2 3 4 5 6 7 8 9 10}

do_test tkt3718-1.4 {
  execsql {pragma count_changes=0}
} {}

# This SQL function executes the SQL specified as an argument against
# database [db].
#
proc sql {doit zSql} {
  if {$doit} { catchsql $zSql }
}
db func sql [list sql]

# The following tests, tkt3718-2.*, test that a nested statement 
# transaction can be successfully committed or reverted without 
# affecting the parent statement transaction.
#
do_test tkt3718-2.1 {
  execsql { SELECT sql(1, 'DELETE FROM t2 WHERE a = '||a ) FROM t2 WHERE a>5 }
  execsql { SELECT a from t2 }
} {1 2 3 4 5}
do_test tkt3718-2.2 {
  execsql {
    DELETE FROM t2 WHERE a > 5;
    BEGIN;
    INSERT INTO t2 SELECT a+5, sql(a==3,
        'INSERT INTO t2 SELECT a+10, f2(b) FROM t1'
    ) FROM t1;
  }
  execsql {
    COMMIT;
    SELECT a FROM t2;
  }
} {1 2 3 4 5 6 7 8 9 10}
do_test tkt3718-2.3 {
  execsql {
    DELETE FROM t2 WHERE a > 5;
    BEGIN;
    INSERT INTO t2 SELECT a+5, sql(a==3,
        'INSERT INTO t2 SELECT a+10, b FROM t1'
    ) FROM t1;
    COMMIT;
  }
  execsql { SELECT a FROM t2 ORDER BY a+0}
} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15}
integrity_check tkt3718.2-4

# The next set of tests, tkt3718-3.*, test that a statement transaction
# that has a committed statement transaction nested inside of it can
# be committed or reverted.
#
foreach {tn io ii results} {
  1 0 10 {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20}
  2 1 10 {6 7 8 9 10 16 17 18 19 20}
  3 0 11 {1 2 3 4 5 6 7 8 9 10 16 17 18 19 20}
  4 1 11 {6 7 8 9 10 16 17 18 19 20}
} {
  do_test tkt3718-3.$tn {
    execsql { 
      DELETE FROM t2;
      INSERT INTO t2 SELECT a+5, b FROM t1;
      INSERT INTO t2 SELECT a+15, b FROM t1;
    }

    catchsql "
      BEGIN;
      INSERT INTO t2 SELECT a+$io, sql(a==3,
          'INSERT INTO t2 SELECT a+$ii, b FROM t1'
      ) FROM t1;
    "

    execsql { COMMIT }

    execsql { SELECT a FROM t2 ORDER BY a+0}
  } $results

  integrity_check tkt3718-3.$tn.integrity
}

# This is the same test as tkt3718-3.*, but with 3 levels of nesting.
#
foreach {tn i1 i2 i3 results} {
  1   0 10 20   {5 10 15 20 25 30}
  2   0 10 21   {5 10 15 20 30}
  3   0 11 20   {5 10 20 30}
  4   0 11 21   {5 10 20 30}
  5   1 10 20   {10 20 30}
  6   1 10 21   {10 20 30}
  7   1 11 20   {10 20 30}
  8   1 11 21   {10 20 30}
} {
  do_test tkt3718-4.$tn {
    execsql { 
      DELETE FROM t2;
      INSERT INTO t2 SELECT a+5, b FROM t1;
      INSERT INTO t2 SELECT a+15, b FROM t1;
      INSERT INTO t2 SELECT a+25, b FROM t1;
    }

    catchsql "
      BEGIN;
      INSERT INTO t2 SELECT a+$i1, sql(a==3,
          'INSERT INTO t2 SELECT a+$i2, sql(a==3, 
             ''INSERT INTO t2 SELECT a+$i3, b FROM t1''
           ) FROM t1'
      ) FROM t1;
    "

    execsql { COMMIT }

    execsql { SELECT a FROM t2 WHERE (a%5)==0 ORDER BY a+0}
  } $results

  do_test tkt3718-4.$tn.extra {
    execsql {
      SELECT 
        (SELECT sum(a) FROM t2)==(SELECT sum(a*5-10) FROM t2 WHERE (a%5)==0)
    }
  } {1}

  integrity_check tkt3718-4.$tn.integrity
}


finish_test