OLD | NEW |
1 # 2001 September 15 | 1 # 2001 September 15 |
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 #*********************************************************************** |
(...skipping 12 matching lines...) Expand all Loading... |
23 # the OP_OpenWrite, an asterix is appended to the object name. The list | 23 # the OP_OpenWrite, an asterix is appended to the object name. The list |
24 # is sorted in [lsort] order before it is returned. | 24 # is sorted in [lsort] order before it is returned. |
25 # | 25 # |
26 proc analyze_delete_program {sql} { | 26 proc analyze_delete_program {sql} { |
27 # Build a map from root page to table/index name. | 27 # Build a map from root page to table/index name. |
28 db eval { | 28 db eval { |
29 SELECT name, rootpage FROM sqlite_master | 29 SELECT name, rootpage FROM sqlite_master |
30 } { | 30 } { |
31 set T($rootpage) $name | 31 set T($rootpage) $name |
32 } | 32 } |
| 33 |
| 34 # For each OpenWrite instruction generated for the proposed DELETE |
| 35 # statement, add the following array entries: |
| 36 # |
| 37 # $M(<cursor number>) -> <object name> |
| 38 # $O(<object name>) -> "*" | "" |
| 39 # |
| 40 # The O() entry is set to "*" if the BTREE_FORDELETE flag is specified, |
| 41 # or "" otherwise. |
| 42 # |
| 43 db eval "EXPLAIN $sql" R { |
| 44 if {$R(opcode)=="OpenWrite"} { |
| 45 set root $R(p2) |
| 46 set csr $R(p1) |
| 47 if {[info exists T($root)]} { set M($csr) $T($root) } |
33 | 48 |
34 # Calculate the results. | 49 set obj $T($root) |
| 50 set O($obj) "" |
| 51 if {"0x$R(p5)" & 0x08} { |
| 52 set O($obj) * |
| 53 } else { |
| 54 set O($obj) "" |
| 55 } |
| 56 } |
| 57 } |
| 58 |
| 59 db eval "EXPLAIN $sql" R { |
| 60 if {$R(opcode) == "Delete"} { |
| 61 set csr $R(p1) |
| 62 if {[info exists M($csr)]} { |
| 63 set idxdelete [expr {("0x$R(p5)" & 0x04) ? 1 : 0}] |
| 64 if {$idxdelete} { |
| 65 append O($M($csr)) "+" |
| 66 } |
| 67 } |
| 68 } |
| 69 } |
| 70 |
35 set res [list] | 71 set res [list] |
36 db eval "EXPLAIN $sql" R { | 72 foreach {k v} [array get O] { |
37 if {$R(opcode) == "OpenWrite"} { | 73 lappend res "${k}${v}" |
38 set obj $T($R(p2)) | |
39 if {"0x$R(p5)" & 0x08} { append obj *} | |
40 lappend res $obj | |
41 } | |
42 } | 74 } |
43 | 75 |
44 lsort $res | 76 lsort $res |
45 } | 77 } |
46 | 78 |
47 proc do_adp_test {tn sql res} { | 79 proc do_adp_test {tn sql res} { |
48 uplevel [list do_test $tn [list analyze_delete_program $sql] [list {*}$res]] | 80 uplevel [list do_test $tn [list analyze_delete_program $sql] [list {*}$res]] |
49 } | 81 } |
50 | 82 |
51 do_execsql_test 1.0 { | 83 do_execsql_test 1.0 { |
52 CREATE TABLE t1(a PRIMARY KEY, b); | 84 CREATE TABLE t1(a PRIMARY KEY, b); |
53 } | 85 } |
54 | 86 |
55 foreach {tn sql res} { | 87 foreach {tn sql res} { |
56 1 { DELETE FROM t1 WHERE a=?} { sqlite_autoindex_t1_1 t1* } | 88 1 { DELETE FROM t1 WHERE a=?} { sqlite_autoindex_t1_1 t1*+ } |
57 2 { DELETE FROM t1 WHERE a=? AND b=? } { sqlite_autoindex_t1_1 t1 } | 89 2 { DELETE FROM t1 WHERE a=? AND b=? } { sqlite_autoindex_t1_1 t1+ } |
58 3 { DELETE FROM t1 WHERE a>? } { sqlite_autoindex_t1_1 t1* } | 90 3 { DELETE FROM t1 WHERE a>? } { sqlite_autoindex_t1_1 t1*+ } |
59 4 { DELETE FROM t1 WHERE rowid=? } { sqlite_autoindex_t1_1* t1 } | 91 4 { DELETE FROM t1 WHERE rowid=? } { sqlite_autoindex_t1_1* t1 } |
60 } { | 92 } { |
61 do_adp_test 1.$tn $sql $res | 93 do_adp_test 1.$tn $sql $res |
62 } | 94 } |
63 | 95 |
64 do_execsql_test 2.0 { | 96 do_execsql_test 2.0 { |
65 CREATE TABLE t2(a, b, c); | 97 CREATE TABLE t2(a, b, c); |
66 CREATE INDEX t2a ON t2(a); | 98 CREATE INDEX t2a ON t2(a); |
67 CREATE INDEX t2b ON t2(b); | 99 CREATE INDEX t2b ON t2(b); |
68 CREATE INDEX t2c ON t2(c); | 100 CREATE INDEX t2c ON t2(c); |
69 } | 101 } |
70 foreach {tn sql res} { | 102 foreach {tn sql res} { |
71 1 { DELETE FROM t2 WHERE a=?} { t2* t2a t2b* t2c* } | 103 1 { DELETE FROM t2 WHERE a=?} { t2*+ t2a t2b* t2c* } |
72 2 { DELETE FROM t2 WHERE a=? AND +b=?} { t2 t2a t2b* t2c* } | 104 2 { DELETE FROM t2 WHERE a=? AND +b=?} { t2+ t2a t2b* t2c* } |
73 3 { DELETE FROM t2 WHERE a=? OR b=?} { t2 t2a* t2b* t2c* } | 105 3 { DELETE FROM t2 WHERE a=? OR b=?} { t2 t2a* t2b* t2c* } |
74 4 { DELETE FROM t2 WHERE +a=? } { t2 t2a* t2b* t2c* } | 106 4 { DELETE FROM t2 WHERE +a=? } { t2 t2a* t2b* t2c* } |
75 5 { DELETE FROM t2 WHERE rowid=? } { t2 t2a* t2b* t2c* } | 107 5 { DELETE FROM t2 WHERE rowid=? } { t2 t2a* t2b* t2c* } |
76 } { | 108 } { |
77 do_adp_test 2.$tn $sql $res | 109 do_adp_test 2.$tn $sql $res |
78 } | 110 } |
79 | 111 |
80 #------------------------------------------------------------------------- | 112 #------------------------------------------------------------------------- |
81 # Test that a record that consists of the bytes: | 113 # Test that a record that consists of the bytes: |
82 # | 114 # |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 set csr [btree_cursor $bt $root 1] | 151 set csr [btree_cursor $bt $root 1] |
120 btree_insert $csr 6 "\000" | 152 btree_insert $csr 6 "\000" |
121 btree_close_cursor $csr | 153 btree_close_cursor $csr |
122 db eval { COMMIT } | 154 db eval { COMMIT } |
123 | 155 |
124 db eval { | 156 db eval { |
125 SELECT * FROM x2; | 157 SELECT * FROM x2; |
126 } | 158 } |
127 } {6 {} {} {}} | 159 } {6 {} {} {}} |
128 | 160 |
| 161 |
| 162 #------------------------------------------------------------------------- |
| 163 # |
| 164 reset_db |
| 165 do_execsql_test 4.0 { |
| 166 CREATE TABLE log(x); |
| 167 CREATE TABLE p1(one PRIMARY KEY, two); |
| 168 |
| 169 CREATE TRIGGER tr_bd BEFORE DELETE ON p1 BEGIN |
| 170 INSERT INTO log VALUES('delete'); |
| 171 END; |
| 172 INSERT INTO p1 VALUES('a', 'A'), ('b', 'B'), ('c', 'C'); |
| 173 DELETE FROM p1 WHERE one = 'a'; |
| 174 } |
| 175 |
| 176 reset_db |
| 177 do_execsql_test 4.1 { |
| 178 BEGIN TRANSACTION; |
| 179 CREATE TABLE tbl(a PRIMARY KEY, b, c); |
| 180 CREATE TABLE log(a, b, c); |
| 181 INSERT INTO "tbl" VALUES(1,2,3); |
| 182 CREATE TRIGGER the_trigger BEFORE DELETE ON tbl BEGIN |
| 183 INSERT INTO log VALUES(1, 2,3); |
| 184 END; |
| 185 COMMIT; |
| 186 DELETE FROM tbl WHERE a=1; |
| 187 } |
| 188 |
| 189 reset_db |
| 190 do_execsql_test 5.1 { |
| 191 PRAGMA foreign_keys = 1; |
| 192 CREATE TABLE t1(a INTEGER PRIMARY KEY, b); |
| 193 CREATE TABLE t2( |
| 194 c INTEGER PRIMARY KEY, |
| 195 d INTEGER DEFAULT 1 REFERENCES t1 ON DELETE SET DEFAULT |
| 196 ); |
| 197 } {} |
| 198 do_execsql_test 5.2 { |
| 199 INSERT INTO t1 VALUES(1, 'one'); |
| 200 INSERT INTO t1 VALUES(2, 'two'); |
| 201 INSERT INTO t2 VALUES(1, 2); |
| 202 SELECT * FROM t2; |
| 203 } {1 2} |
| 204 do_execsql_test 5.3 { |
| 205 DELETE FROM t1 WHERE a = 2; |
| 206 } {} |
| 207 |
| 208 |
129 finish_test | 209 finish_test |
130 | |
OLD | NEW |