3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
12 # This file containst tests to verify that ROLLBACK or ROLLBACK TO
13 # operations interact correctly with ongoing SELECT statements.
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
18 set ::testprefix rollback2
20 proc int2hex {i} { format %.2X $i }
21 db func int2hex int2hex
23 SELECT int2hex(0), int2hex(100), int2hex(255)
26 CREATE TABLE t1(i, h);
27 CREATE INDEX i1 ON t1(h);
31 SELECT a+1, int2hex(a+1) FROM data WHERE a<40
33 INSERT INTO t1 SELECT * FROM data;
37 # do_rollback_test ID SWITCHES
41 # -setup SQL script to open transaction and begin writing.
42 # -select SELECT to execute after -setup script
43 # -result Expected result of -select statement
44 # -rollback Use this SQL command ("ROLLBACK" or "ROLLBACK TO ...") to
45 # rollback the transaction in the middle of the -select statment
48 proc do_rollback_test {tn args} {
52 set A(-rollback) ROLLBACK
55 foreach k [array names O] {
56 if {[info exists A($k)]==0} { error "unknown option: $k" }
60 for {set iRollback 0} 1 {incr iRollback} {
61 catch { db eval ROLLBACK }
66 db eval $A(-select) x {
67 if {$i==$iRollback} { db eval $A(-rollback) }
68 foreach k $x(*) { lappend res $x($k) }
72 do_test $tn.$iRollback [list set {} $res] [list {*}$A(-result)]
73 if {$i < $iRollback} break
77 do_rollback_test 2.1 -setup {
79 DELETE FROM t1 WHERE (i%2)==1;
81 SELECT i FROM t1 WHERE (i%2)==0
83 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40
86 do_rollback_test 2.2 -setup {
88 DELETE FROM t1 WHERE (i%4)==1;
90 DELETE FROM t1 WHERE (i%2)==1;
94 SELECT i FROM t1 WHERE (i%2)==0
96 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40
99 #--------------------------------------------------------------------
100 # Try with some index scans
103 SELECT i FROM t1 WHERE (i%2)==0 ORDER BY h DESC;
104 } {SCAN t1 USING INDEX i1}
105 do_rollback_test 3.2 -setup {
107 DELETE FROM t1 WHERE (i%2)==1;
109 SELECT i FROM t1 WHERE (i%2)==0 ORDER BY h DESC;
111 40 38 36 34 32 30 28 26 24 22 20 18 16 14 12 10 8 6 4 2
113 do_rollback_test 3.3 -setup {
115 DELETE FROM t1 WHERE (i%4)==1;
117 DELETE FROM t1 WHERE (i%2)==1;
121 SELECT i FROM t1 WHERE (i%2)==0 ORDER BY h DESC;
123 40 38 36 34 32 30 28 26 24 22 20 18 16 14 12 10 8 6 4 2
126 #--------------------------------------------------------------------
127 # Now with some index scans that feature overflow keys.
129 set leader [string repeat "abcdefghij" 70]
130 do_execsql_test 4.1 { UPDATE t1 SET h = $leader || h; }
133 SELECT i FROM t1 WHERE (i%2)==0 ORDER BY h ASC;
134 } {SCAN t1 USING INDEX i1}
135 do_rollback_test 4.3 -setup {
137 DELETE FROM t1 WHERE (i%2)==1;
139 SELECT i FROM t1 WHERE (i%2)==0 ORDER BY h ASC;
141 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40
143 do_rollback_test 4.4 -setup {
145 DELETE FROM t1 WHERE (i%4)==1;
147 DELETE FROM t1 WHERE (i%2)==1;
151 SELECT i FROM t1 WHERE (i%2)==0 ORDER BY h ASC;
153 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40