OLD | NEW |
1 # 2014 November 12 | 1 # 2014 November 12 |
2 # | 2 # |
3 # The author disclaims copyright to this source code. In place of | 3 # The author disclaims copyright to this source code. In place of |
4 # a legal notice, here is a blessing: | 4 # a legal notice, here is a blessing: |
5 # | 5 # |
6 # May you do good and not evil. | 6 # May you do good and not evil. |
7 # May you find forgiveness for yourself and forgive others. | 7 # May you find forgiveness for yourself and forgive others. |
8 # May you share freely, never taking more than you give. | 8 # May you share freely, never taking more than you give. |
9 # | 9 # |
10 #*********************************************************************** | 10 #*********************************************************************** |
11 # | 11 # |
| 12 # This file containst tests to verify that ROLLBACK or ROLLBACK TO |
| 13 # operations interact correctly with ongoing SELECT statements. |
| 14 # |
12 | 15 |
13 set testdir [file dirname $argv0] | 16 set testdir [file dirname $argv0] |
14 source $testdir/tester.tcl | 17 source $testdir/tester.tcl |
15 set ::testprefix rollback2 | 18 set ::testprefix rollback2 |
16 | 19 |
17 proc int2hex {i} { format %.2X $i } | 20 proc int2hex {i} { format %.2X $i } |
18 db func int2hex int2hex | 21 db func int2hex int2hex |
19 | |
20 do_execsql_test 1.0 { | 22 do_execsql_test 1.0 { |
21 SELECT int2hex(0), int2hex(100), int2hex(255) | 23 SELECT int2hex(0), int2hex(100), int2hex(255) |
22 } {00 64 FF} | 24 } {00 64 FF} |
23 do_execsql_test 1.1 { | 25 do_execsql_test 1.1 { |
24 CREATE TABLE t1(i, h); | 26 CREATE TABLE t1(i, h); |
25 CREATE INDEX i1 ON t1(h); | 27 CREATE INDEX i1 ON t1(h); |
26 WITH data(a, b) AS ( | 28 WITH data(a, b) AS ( |
27 SELECT 1, int2hex(1) | 29 SELECT 1, int2hex(1) |
28 UNION ALL | 30 UNION ALL |
29 SELECT a+1, int2hex(a+1) FROM data WHERE a<40 | 31 SELECT a+1, int2hex(a+1) FROM data WHERE a<40 |
30 ) | 32 ) |
31 INSERT INTO t1 SELECT * FROM data; | 33 INSERT INTO t1 SELECT * FROM data; |
32 } {} | 34 } {} |
33 | 35 |
34 | 36 |
| 37 # do_rollback_test ID SWITCHES |
| 38 # |
| 39 # where SWITCHES are: |
| 40 # |
| 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 |
| 46 # execution. |
| 47 # |
35 proc do_rollback_test {tn args} { | 48 proc do_rollback_test {tn args} { |
36 set A(-setup) "" | 49 set A(-setup) "" |
37 set A(-select) "" | 50 set A(-select) "" |
38 set A(-result) "" | 51 set A(-result) "" |
39 set A(-rollback) ROLLBACK | 52 set A(-rollback) ROLLBACK |
40 | 53 |
41 array set O $args | 54 array set O $args |
42 foreach k [array names O] { | 55 foreach k [array names O] { |
43 if {[info exists A($k)]==0} { error "unknown option: $k" } | 56 if {[info exists A($k)]==0} { error "unknown option: $k" } |
44 set A($k) $O($k) | 57 set A($k) $O($k) |
45 } | 58 } |
46 | 59 |
47 for {set iRollback 0} 1 {incr iRollback} { | 60 for {set iRollback 0} 1 {incr iRollback} { |
48 catch { db eval ROLLBACK } | 61 catch { db eval ROLLBACK } |
49 set res [list] | 62 set res [list] |
50 db eval $A(-setup) | 63 db eval $A(-setup) |
51 | 64 |
52 set i 0 | 65 set i 0 |
53 db eval $A(-select) x { | 66 db eval $A(-select) x { |
54 if {$i==$iRollback} { db eval $A(-rollback) } | 67 if {$i==$iRollback} { db eval $A(-rollback) } |
55 foreach k $x(*) { lappend res $x($k) } | 68 foreach k $x(*) { lappend res $x($k) } |
56 incr i | 69 incr i |
57 } | 70 } |
58 | 71 |
59 do_test $tn.$iRollback [list set {} $res] [list {*}$A(-result)] | 72 do_test $tn.$iRollback [list set {} $res] [list {*}$A(-result)] |
60 if {$i < $iRollback} break | 73 if {$i < $iRollback} break |
61 } | 74 } |
62 } | 75 } |
63 | 76 |
64 do_rollback_test 2 -setup { | 77 do_rollback_test 2.1 -setup { |
65 BEGIN; | 78 BEGIN; |
66 DELETE FROM t1 WHERE (i%2)==1; | 79 DELETE FROM t1 WHERE (i%2)==1; |
67 } -select { | 80 } -select { |
68 SELECT i FROM t1 WHERE (i%2)==0 | 81 SELECT i FROM t1 WHERE (i%2)==0 |
69 } -result { | 82 } -result { |
70 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 | 83 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 |
71 } | 84 } |
72 | 85 |
| 86 do_rollback_test 2.2 -setup { |
| 87 BEGIN; |
| 88 DELETE FROM t1 WHERE (i%4)==1; |
| 89 SAVEPOINT one; |
| 90 DELETE FROM t1 WHERE (i%2)==1; |
| 91 } -rollback { |
| 92 ROLLBACK TO one; |
| 93 } -select { |
| 94 SELECT i FROM t1 WHERE (i%2)==0 |
| 95 } -result { |
| 96 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 |
| 97 } |
| 98 |
| 99 #-------------------------------------------------------------------- |
| 100 # Try with some index scans |
| 101 # |
| 102 do_eqp_test 3.1 { |
| 103 SELECT i FROM t1 WHERE (i%2)==0 ORDER BY h DESC; |
| 104 } {0 0 0 {SCAN TABLE t1 USING INDEX i1}} |
| 105 do_rollback_test 3.2 -setup { |
| 106 BEGIN; |
| 107 DELETE FROM t1 WHERE (i%2)==1; |
| 108 } -select { |
| 109 SELECT i FROM t1 WHERE (i%2)==0 ORDER BY h DESC; |
| 110 } -result { |
| 111 40 38 36 34 32 30 28 26 24 22 20 18 16 14 12 10 8 6 4 2 |
| 112 } |
| 113 do_rollback_test 3.3 -setup { |
| 114 BEGIN; |
| 115 DELETE FROM t1 WHERE (i%4)==1; |
| 116 SAVEPOINT one; |
| 117 DELETE FROM t1 WHERE (i%2)==1; |
| 118 } -rollback { |
| 119 ROLLBACK TO one; |
| 120 } -select { |
| 121 SELECT i FROM t1 WHERE (i%2)==0 ORDER BY h DESC; |
| 122 } -result { |
| 123 40 38 36 34 32 30 28 26 24 22 20 18 16 14 12 10 8 6 4 2 |
| 124 } |
| 125 |
| 126 #-------------------------------------------------------------------- |
| 127 # Now with some index scans that feature overflow keys. |
| 128 # |
| 129 set leader [string repeat "abcdefghij" 70] |
| 130 do_execsql_test 4.1 { UPDATE t1 SET h = $leader || h; } |
| 131 |
| 132 do_eqp_test 4.2 { |
| 133 SELECT i FROM t1 WHERE (i%2)==0 ORDER BY h ASC; |
| 134 } {0 0 0 {SCAN TABLE t1 USING INDEX i1}} |
| 135 do_rollback_test 4.3 -setup { |
| 136 BEGIN; |
| 137 DELETE FROM t1 WHERE (i%2)==1; |
| 138 } -select { |
| 139 SELECT i FROM t1 WHERE (i%2)==0 ORDER BY h ASC; |
| 140 } -result { |
| 141 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 |
| 142 } |
| 143 do_rollback_test 4.4 -setup { |
| 144 BEGIN; |
| 145 DELETE FROM t1 WHERE (i%4)==1; |
| 146 SAVEPOINT one; |
| 147 DELETE FROM t1 WHERE (i%2)==1; |
| 148 } -rollback { |
| 149 ROLLBACK TO one; |
| 150 } -select { |
| 151 SELECT i FROM t1 WHERE (i%2)==0 ORDER BY h ASC; |
| 152 } -result { |
| 153 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 |
| 154 } |
| 155 |
73 finish_test | 156 finish_test |
74 | 157 |
OLD | NEW |