sl@0
|
1 |
# 2001 September 15
|
sl@0
|
2 |
#
|
sl@0
|
3 |
# The author disclaims copyright to this source code. In place of
|
sl@0
|
4 |
# a legal notice, here is a blessing:
|
sl@0
|
5 |
#
|
sl@0
|
6 |
# May you do good and not evil.
|
sl@0
|
7 |
# May you find forgiveness for yourself and forgive others.
|
sl@0
|
8 |
# May you share freely, never taking more than you give.
|
sl@0
|
9 |
#
|
sl@0
|
10 |
#***********************************************************************
|
sl@0
|
11 |
# This file implements regression tests for SQLite library. The
|
sl@0
|
12 |
# focus of this file is testing the DELETE FROM statement.
|
sl@0
|
13 |
#
|
sl@0
|
14 |
# $Id: delete.test,v 1.23 2008/04/19 20:53:26 drh Exp $
|
sl@0
|
15 |
|
sl@0
|
16 |
set testdir [file dirname $argv0]
|
sl@0
|
17 |
source $testdir/tester.tcl
|
sl@0
|
18 |
|
sl@0
|
19 |
# Try to delete from a non-existant table.
|
sl@0
|
20 |
#
|
sl@0
|
21 |
do_test delete-1.1 {
|
sl@0
|
22 |
set v [catch {execsql {DELETE FROM test1}} msg]
|
sl@0
|
23 |
lappend v $msg
|
sl@0
|
24 |
} {1 {no such table: test1}}
|
sl@0
|
25 |
|
sl@0
|
26 |
# Try to delete from sqlite_master
|
sl@0
|
27 |
#
|
sl@0
|
28 |
do_test delete-2.1 {
|
sl@0
|
29 |
set v [catch {execsql {DELETE FROM sqlite_master}} msg]
|
sl@0
|
30 |
lappend v $msg
|
sl@0
|
31 |
} {1 {table sqlite_master may not be modified}}
|
sl@0
|
32 |
|
sl@0
|
33 |
# Delete selected entries from a table with and without an index.
|
sl@0
|
34 |
#
|
sl@0
|
35 |
do_test delete-3.1.1 {
|
sl@0
|
36 |
execsql {CREATE TABLE table1(f1 int, f2 int)}
|
sl@0
|
37 |
execsql {INSERT INTO table1 VALUES(1,2)}
|
sl@0
|
38 |
execsql {INSERT INTO table1 VALUES(2,4)}
|
sl@0
|
39 |
execsql {INSERT INTO table1 VALUES(3,8)}
|
sl@0
|
40 |
execsql {INSERT INTO table1 VALUES(4,16)}
|
sl@0
|
41 |
execsql {SELECT * FROM table1 ORDER BY f1}
|
sl@0
|
42 |
} {1 2 2 4 3 8 4 16}
|
sl@0
|
43 |
do_test delete-3.1.2 {
|
sl@0
|
44 |
execsql {DELETE FROM table1 WHERE f1=3}
|
sl@0
|
45 |
} {}
|
sl@0
|
46 |
do_test delete-3.1.3 {
|
sl@0
|
47 |
execsql {SELECT * FROM table1 ORDER BY f1}
|
sl@0
|
48 |
} {1 2 2 4 4 16}
|
sl@0
|
49 |
do_test delete-3.1.4 {
|
sl@0
|
50 |
execsql {CREATE INDEX index1 ON table1(f1)}
|
sl@0
|
51 |
execsql {PRAGMA count_changes=on}
|
sl@0
|
52 |
ifcapable explain {
|
sl@0
|
53 |
execsql {EXPLAIN DELETE FROM table1 WHERE f1=3}
|
sl@0
|
54 |
}
|
sl@0
|
55 |
execsql {DELETE FROM 'table1' WHERE f1=3}
|
sl@0
|
56 |
} {0}
|
sl@0
|
57 |
do_test delete-3.1.5 {
|
sl@0
|
58 |
execsql {SELECT * FROM table1 ORDER BY f1}
|
sl@0
|
59 |
} {1 2 2 4 4 16}
|
sl@0
|
60 |
do_test delete-3.1.6.1 {
|
sl@0
|
61 |
execsql {DELETE FROM table1 WHERE f1=2}
|
sl@0
|
62 |
} {1}
|
sl@0
|
63 |
do_test delete-3.1.6.2 {
|
sl@0
|
64 |
db changes
|
sl@0
|
65 |
} 1
|
sl@0
|
66 |
do_test delete-3.1.7 {
|
sl@0
|
67 |
execsql {SELECT * FROM table1 ORDER BY f1}
|
sl@0
|
68 |
} {1 2 4 16}
|
sl@0
|
69 |
integrity_check delete-3.2
|
sl@0
|
70 |
|
sl@0
|
71 |
|
sl@0
|
72 |
# Semantic errors in the WHERE clause
|
sl@0
|
73 |
#
|
sl@0
|
74 |
do_test delete-4.1 {
|
sl@0
|
75 |
execsql {CREATE TABLE table2(f1 int, f2 int)}
|
sl@0
|
76 |
set v [catch {execsql {DELETE FROM table2 WHERE f3=5}} msg]
|
sl@0
|
77 |
lappend v $msg
|
sl@0
|
78 |
} {1 {no such column: f3}}
|
sl@0
|
79 |
|
sl@0
|
80 |
do_test delete-4.2 {
|
sl@0
|
81 |
set v [catch {execsql {DELETE FROM table2 WHERE xyzzy(f1+4)}} msg]
|
sl@0
|
82 |
lappend v $msg
|
sl@0
|
83 |
} {1 {no such function: xyzzy}}
|
sl@0
|
84 |
integrity_check delete-4.3
|
sl@0
|
85 |
|
sl@0
|
86 |
# Lots of deletes
|
sl@0
|
87 |
#
|
sl@0
|
88 |
do_test delete-5.1.1 {
|
sl@0
|
89 |
execsql {DELETE FROM table1}
|
sl@0
|
90 |
} {2}
|
sl@0
|
91 |
do_test delete-5.1.2 {
|
sl@0
|
92 |
execsql {SELECT count(*) FROM table1}
|
sl@0
|
93 |
} {0}
|
sl@0
|
94 |
do_test delete-5.2.1 {
|
sl@0
|
95 |
execsql {BEGIN TRANSACTION}
|
sl@0
|
96 |
for {set i 1} {$i<=200} {incr i} {
|
sl@0
|
97 |
execsql "INSERT INTO table1 VALUES($i,[expr {$i*$i}])"
|
sl@0
|
98 |
}
|
sl@0
|
99 |
execsql {COMMIT}
|
sl@0
|
100 |
execsql {SELECT count(*) FROM table1}
|
sl@0
|
101 |
} {200}
|
sl@0
|
102 |
do_test delete-5.2.2 {
|
sl@0
|
103 |
execsql {DELETE FROM table1}
|
sl@0
|
104 |
} {200}
|
sl@0
|
105 |
do_test delete-5.2.3 {
|
sl@0
|
106 |
execsql {BEGIN TRANSACTION}
|
sl@0
|
107 |
for {set i 1} {$i<=200} {incr i} {
|
sl@0
|
108 |
execsql "INSERT INTO table1 VALUES($i,[expr {$i*$i}])"
|
sl@0
|
109 |
}
|
sl@0
|
110 |
execsql {COMMIT}
|
sl@0
|
111 |
execsql {SELECT count(*) FROM table1}
|
sl@0
|
112 |
} {200}
|
sl@0
|
113 |
do_test delete-5.2.4 {
|
sl@0
|
114 |
execsql {PRAGMA count_changes=off}
|
sl@0
|
115 |
execsql {DELETE FROM table1}
|
sl@0
|
116 |
} {}
|
sl@0
|
117 |
do_test delete-5.2.5 {
|
sl@0
|
118 |
execsql {SELECT count(*) FROM table1}
|
sl@0
|
119 |
} {0}
|
sl@0
|
120 |
do_test delete-5.2.6 {
|
sl@0
|
121 |
execsql {BEGIN TRANSACTION}
|
sl@0
|
122 |
for {set i 1} {$i<=200} {incr i} {
|
sl@0
|
123 |
execsql "INSERT INTO table1 VALUES($i,[expr {$i*$i}])"
|
sl@0
|
124 |
}
|
sl@0
|
125 |
execsql {COMMIT}
|
sl@0
|
126 |
execsql {SELECT count(*) FROM table1}
|
sl@0
|
127 |
} {200}
|
sl@0
|
128 |
do_test delete-5.3 {
|
sl@0
|
129 |
for {set i 1} {$i<=200} {incr i 4} {
|
sl@0
|
130 |
execsql "DELETE FROM table1 WHERE f1==$i"
|
sl@0
|
131 |
}
|
sl@0
|
132 |
execsql {SELECT count(*) FROM table1}
|
sl@0
|
133 |
} {150}
|
sl@0
|
134 |
do_test delete-5.4.1 {
|
sl@0
|
135 |
execsql "DELETE FROM table1 WHERE f1>50"
|
sl@0
|
136 |
db changes
|
sl@0
|
137 |
} [db one {SELECT count(*) FROM table1 WHERE f1>50}]
|
sl@0
|
138 |
do_test delete-5.4.2 {
|
sl@0
|
139 |
execsql {SELECT count(*) FROM table1}
|
sl@0
|
140 |
} {37}
|
sl@0
|
141 |
do_test delete-5.5 {
|
sl@0
|
142 |
for {set i 1} {$i<=70} {incr i 3} {
|
sl@0
|
143 |
execsql "DELETE FROM table1 WHERE f1==$i"
|
sl@0
|
144 |
}
|
sl@0
|
145 |
execsql {SELECT f1 FROM table1 ORDER BY f1}
|
sl@0
|
146 |
} {2 3 6 8 11 12 14 15 18 20 23 24 26 27 30 32 35 36 38 39 42 44 47 48 50}
|
sl@0
|
147 |
do_test delete-5.6 {
|
sl@0
|
148 |
for {set i 1} {$i<40} {incr i} {
|
sl@0
|
149 |
execsql "DELETE FROM table1 WHERE f1==$i"
|
sl@0
|
150 |
}
|
sl@0
|
151 |
execsql {SELECT f1 FROM table1 ORDER BY f1}
|
sl@0
|
152 |
} {42 44 47 48 50}
|
sl@0
|
153 |
do_test delete-5.7 {
|
sl@0
|
154 |
execsql "DELETE FROM table1 WHERE f1!=48"
|
sl@0
|
155 |
execsql {SELECT f1 FROM table1 ORDER BY f1}
|
sl@0
|
156 |
} {48}
|
sl@0
|
157 |
integrity_check delete-5.8
|
sl@0
|
158 |
|
sl@0
|
159 |
|
sl@0
|
160 |
# Delete large quantities of data. We want to test the List overflow
|
sl@0
|
161 |
# mechanism in the vdbe.
|
sl@0
|
162 |
#
|
sl@0
|
163 |
do_test delete-6.1 {
|
sl@0
|
164 |
execsql {BEGIN; DELETE FROM table1}
|
sl@0
|
165 |
for {set i 1} {$i<=3000} {incr i} {
|
sl@0
|
166 |
execsql "INSERT INTO table1 VALUES($i,[expr {$i*$i}])"
|
sl@0
|
167 |
}
|
sl@0
|
168 |
execsql {DELETE FROM table2}
|
sl@0
|
169 |
for {set i 1} {$i<=3000} {incr i} {
|
sl@0
|
170 |
execsql "INSERT INTO table2 VALUES($i,[expr {$i*$i}])"
|
sl@0
|
171 |
}
|
sl@0
|
172 |
execsql {COMMIT}
|
sl@0
|
173 |
execsql {SELECT count(*) FROM table1}
|
sl@0
|
174 |
} {3000}
|
sl@0
|
175 |
do_test delete-6.2 {
|
sl@0
|
176 |
execsql {SELECT count(*) FROM table2}
|
sl@0
|
177 |
} {3000}
|
sl@0
|
178 |
do_test delete-6.3 {
|
sl@0
|
179 |
execsql {SELECT f1 FROM table1 WHERE f1<10 ORDER BY f1}
|
sl@0
|
180 |
} {1 2 3 4 5 6 7 8 9}
|
sl@0
|
181 |
do_test delete-6.4 {
|
sl@0
|
182 |
execsql {SELECT f1 FROM table2 WHERE f1<10 ORDER BY f1}
|
sl@0
|
183 |
} {1 2 3 4 5 6 7 8 9}
|
sl@0
|
184 |
do_test delete-6.5.1 {
|
sl@0
|
185 |
execsql {DELETE FROM table1 WHERE f1>7}
|
sl@0
|
186 |
db changes
|
sl@0
|
187 |
} {2993}
|
sl@0
|
188 |
do_test delete-6.5.2 {
|
sl@0
|
189 |
execsql {SELECT f1 FROM table1 ORDER BY f1}
|
sl@0
|
190 |
} {1 2 3 4 5 6 7}
|
sl@0
|
191 |
do_test delete-6.6 {
|
sl@0
|
192 |
execsql {DELETE FROM table2 WHERE f1>7}
|
sl@0
|
193 |
execsql {SELECT f1 FROM table2 ORDER BY f1}
|
sl@0
|
194 |
} {1 2 3 4 5 6 7}
|
sl@0
|
195 |
do_test delete-6.7 {
|
sl@0
|
196 |
execsql {DELETE FROM table1}
|
sl@0
|
197 |
execsql {SELECT f1 FROM table1}
|
sl@0
|
198 |
} {}
|
sl@0
|
199 |
do_test delete-6.8 {
|
sl@0
|
200 |
execsql {INSERT INTO table1 VALUES(2,3)}
|
sl@0
|
201 |
execsql {SELECT f1 FROM table1}
|
sl@0
|
202 |
} {2}
|
sl@0
|
203 |
do_test delete-6.9 {
|
sl@0
|
204 |
execsql {DELETE FROM table2}
|
sl@0
|
205 |
execsql {SELECT f1 FROM table2}
|
sl@0
|
206 |
} {}
|
sl@0
|
207 |
do_test delete-6.10 {
|
sl@0
|
208 |
execsql {INSERT INTO table2 VALUES(2,3)}
|
sl@0
|
209 |
execsql {SELECT f1 FROM table2}
|
sl@0
|
210 |
} {2}
|
sl@0
|
211 |
integrity_check delete-6.11
|
sl@0
|
212 |
|
sl@0
|
213 |
do_test delete-7.1 {
|
sl@0
|
214 |
execsql {
|
sl@0
|
215 |
CREATE TABLE t3(a);
|
sl@0
|
216 |
INSERT INTO t3 VALUES(1);
|
sl@0
|
217 |
INSERT INTO t3 SELECT a+1 FROM t3;
|
sl@0
|
218 |
INSERT INTO t3 SELECT a+2 FROM t3;
|
sl@0
|
219 |
SELECT * FROM t3;
|
sl@0
|
220 |
}
|
sl@0
|
221 |
} {1 2 3 4}
|
sl@0
|
222 |
ifcapable {trigger} {
|
sl@0
|
223 |
do_test delete-7.2 {
|
sl@0
|
224 |
execsql {
|
sl@0
|
225 |
CREATE TABLE cnt(del);
|
sl@0
|
226 |
INSERT INTO cnt VALUES(0);
|
sl@0
|
227 |
CREATE TRIGGER r1 AFTER DELETE ON t3 FOR EACH ROW BEGIN
|
sl@0
|
228 |
UPDATE cnt SET del=del+1;
|
sl@0
|
229 |
END;
|
sl@0
|
230 |
DELETE FROM t3 WHERE a<2;
|
sl@0
|
231 |
SELECT * FROM t3;
|
sl@0
|
232 |
}
|
sl@0
|
233 |
} {2 3 4}
|
sl@0
|
234 |
do_test delete-7.3 {
|
sl@0
|
235 |
execsql {
|
sl@0
|
236 |
SELECT * FROM cnt;
|
sl@0
|
237 |
}
|
sl@0
|
238 |
} {1}
|
sl@0
|
239 |
do_test delete-7.4 {
|
sl@0
|
240 |
execsql {
|
sl@0
|
241 |
DELETE FROM t3;
|
sl@0
|
242 |
SELECT * FROM t3;
|
sl@0
|
243 |
}
|
sl@0
|
244 |
} {}
|
sl@0
|
245 |
do_test delete-7.5 {
|
sl@0
|
246 |
execsql {
|
sl@0
|
247 |
SELECT * FROM cnt;
|
sl@0
|
248 |
}
|
sl@0
|
249 |
} {4}
|
sl@0
|
250 |
do_test delete-7.6 {
|
sl@0
|
251 |
execsql {
|
sl@0
|
252 |
INSERT INTO t3 VALUES(1);
|
sl@0
|
253 |
INSERT INTO t3 SELECT a+1 FROM t3;
|
sl@0
|
254 |
INSERT INTO t3 SELECT a+2 FROM t3;
|
sl@0
|
255 |
CREATE TABLE t4 AS SELECT * FROM t3;
|
sl@0
|
256 |
PRAGMA count_changes=ON;
|
sl@0
|
257 |
DELETE FROM t3;
|
sl@0
|
258 |
DELETE FROM t4;
|
sl@0
|
259 |
}
|
sl@0
|
260 |
} {4 4}
|
sl@0
|
261 |
} ;# endif trigger
|
sl@0
|
262 |
ifcapable {!trigger} {
|
sl@0
|
263 |
execsql {DELETE FROM t3}
|
sl@0
|
264 |
}
|
sl@0
|
265 |
integrity_check delete-7.7
|
sl@0
|
266 |
|
sl@0
|
267 |
# Make sure error messages are consistent when attempting to delete
|
sl@0
|
268 |
# from a read-only database. Ticket #304.
|
sl@0
|
269 |
#
|
sl@0
|
270 |
do_test delete-8.0 {
|
sl@0
|
271 |
execsql {
|
sl@0
|
272 |
PRAGMA count_changes=OFF;
|
sl@0
|
273 |
INSERT INTO t3 VALUES(123);
|
sl@0
|
274 |
SELECT * FROM t3;
|
sl@0
|
275 |
}
|
sl@0
|
276 |
} {123}
|
sl@0
|
277 |
db close
|
sl@0
|
278 |
catch {file delete -force test.db-journal}
|
sl@0
|
279 |
catch {file attributes test.db -permissions 0444}
|
sl@0
|
280 |
catch {file attributes test.db -readonly 1}
|
sl@0
|
281 |
sqlite3 db test.db
|
sl@0
|
282 |
set ::DB [sqlite3_connection_pointer db]
|
sl@0
|
283 |
do_test delete-8.1 {
|
sl@0
|
284 |
catchsql {
|
sl@0
|
285 |
DELETE FROM t3;
|
sl@0
|
286 |
}
|
sl@0
|
287 |
} {1 {attempt to write a readonly database}}
|
sl@0
|
288 |
do_test delete-8.2 {
|
sl@0
|
289 |
execsql {SELECT * FROM t3}
|
sl@0
|
290 |
} {123}
|
sl@0
|
291 |
do_test delete-8.3 {
|
sl@0
|
292 |
catchsql {
|
sl@0
|
293 |
DELETE FROM t3 WHERE 1;
|
sl@0
|
294 |
}
|
sl@0
|
295 |
} {1 {attempt to write a readonly database}}
|
sl@0
|
296 |
do_test delete-8.4 {
|
sl@0
|
297 |
execsql {SELECT * FROM t3}
|
sl@0
|
298 |
} {123}
|
sl@0
|
299 |
|
sl@0
|
300 |
# Update for v3: In v2 the DELETE statement would succeed because no
|
sl@0
|
301 |
# database writes actually occur. Version 3 refuses to open a transaction
|
sl@0
|
302 |
# on a read-only file, so the statement fails.
|
sl@0
|
303 |
do_test delete-8.5 {
|
sl@0
|
304 |
catchsql {
|
sl@0
|
305 |
DELETE FROM t3 WHERE a<100;
|
sl@0
|
306 |
}
|
sl@0
|
307 |
# v2 result: {0 {}}
|
sl@0
|
308 |
} {1 {attempt to write a readonly database}}
|
sl@0
|
309 |
do_test delete-8.6 {
|
sl@0
|
310 |
execsql {SELECT * FROM t3}
|
sl@0
|
311 |
} {123}
|
sl@0
|
312 |
integrity_check delete-8.7
|
sl@0
|
313 |
|
sl@0
|
314 |
# Need to do the following for tcl 8.5 on mac. On that configuration, the
|
sl@0
|
315 |
# -readonly flag is taken so seriously that a subsequent [file delete -force]
|
sl@0
|
316 |
# (required before the next test file can be executed) will fail.
|
sl@0
|
317 |
#
|
sl@0
|
318 |
catch {file attributes test.db -readonly 0}
|
sl@0
|
319 |
|
sl@0
|
320 |
finish_test
|