blob: 0409ba912e7c406e48fb241f42fa3b3951c4fdac [file] [log] [blame]
drhb19a2bc2001-09-16 00:13:26 +00001# 2001 September 15
drh62c68192000-05-30 00:51:26 +00002#
drhb19a2bc2001-09-16 00:13:26 +00003# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
drh62c68192000-05-30 00:51:26 +00005#
drhb19a2bc2001-09-16 00:13:26 +00006# 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.
drh62c68192000-05-30 00:51:26 +00009#
10#***********************************************************************
11# This file implements regression tests for SQLite library. The
12# focus of this file is testing the DELETE FROM statement.
13#
drhdddca282006-01-03 00:33:50 +000014# $Id: delete.test,v 1.21 2006/01/03 00:33:50 drh Exp $
drh62c68192000-05-30 00:51:26 +000015
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18
19# Try to delete from a non-existant table.
20#
21do_test delete-1.1 {
22 set v [catch {execsql {DELETE FROM test1}} msg]
23 lappend v $msg
24} {1 {no such table: test1}}
25
26# Try to delete from sqlite_master
27#
28do_test delete-2.1 {
29 set v [catch {execsql {DELETE FROM sqlite_master}} msg]
30 lappend v $msg
drh1d37e282000-05-30 03:12:21 +000031} {1 {table sqlite_master may not be modified}}
drh62c68192000-05-30 00:51:26 +000032
drh7020f652000-06-03 18:06:52 +000033# Delete selected entries from a table with and without an index.
34#
drh1bee3d72001-10-15 00:44:35 +000035do_test delete-3.1.1 {
drh7020f652000-06-03 18:06:52 +000036 execsql {CREATE TABLE table1(f1 int, f2 int)}
37 execsql {INSERT INTO table1 VALUES(1,2)}
38 execsql {INSERT INTO table1 VALUES(2,4)}
39 execsql {INSERT INTO table1 VALUES(3,8)}
40 execsql {INSERT INTO table1 VALUES(4,16)}
41 execsql {SELECT * FROM table1 ORDER BY f1}
42} {1 2 2 4 3 8 4 16}
drh1bee3d72001-10-15 00:44:35 +000043do_test delete-3.1.2 {
drh7020f652000-06-03 18:06:52 +000044 execsql {DELETE FROM table1 WHERE f1=3}
drh1bee3d72001-10-15 00:44:35 +000045} {}
46do_test delete-3.1.3 {
drh7020f652000-06-03 18:06:52 +000047 execsql {SELECT * FROM table1 ORDER BY f1}
48} {1 2 2 4 4 16}
drh1bee3d72001-10-15 00:44:35 +000049do_test delete-3.1.4 {
drh7020f652000-06-03 18:06:52 +000050 execsql {CREATE INDEX index1 ON table1(f1)}
drh1bee3d72001-10-15 00:44:35 +000051 execsql {PRAGMA count_changes=on}
drhcc43cab2005-10-05 11:35:09 +000052 ifcapable explain {
53 execsql {EXPLAIN DELETE FROM table1 WHERE f1=3}
54 }
drh4cfa7932000-06-08 15:10:46 +000055 execsql {DELETE FROM 'table1' WHERE f1=3}
drh1bee3d72001-10-15 00:44:35 +000056} {0}
57do_test delete-3.1.5 {
drh7020f652000-06-03 18:06:52 +000058 execsql {SELECT * FROM table1 ORDER BY f1}
59} {1 2 2 4 4 16}
drh4ebfef12004-07-15 20:08:39 +000060do_test delete-3.1.6.1 {
drh7020f652000-06-03 18:06:52 +000061 execsql {DELETE FROM table1 WHERE f1=2}
drh1bee3d72001-10-15 00:44:35 +000062} {1}
drh4ebfef12004-07-15 20:08:39 +000063do_test delete-3.1.6.2 {
64 db changes
65} 1
drh1bee3d72001-10-15 00:44:35 +000066do_test delete-3.1.7 {
drh7020f652000-06-03 18:06:52 +000067 execsql {SELECT * FROM table1 ORDER BY f1}
68} {1 2 4 16}
drhed717fe2003-06-15 23:42:24 +000069integrity_check delete-3.2
70
drh62c68192000-05-30 00:51:26 +000071
drh8be51132000-06-03 19:19:41 +000072# Semantic errors in the WHERE clause
73#
74do_test delete-4.1 {
75 execsql {CREATE TABLE table2(f1 int, f2 int)}
76 set v [catch {execsql {DELETE FROM table2 WHERE f3=5}} msg]
77 lappend v $msg
drh967e8b72000-06-21 13:59:10 +000078} {1 {no such column: f3}}
drh8be51132000-06-03 19:19:41 +000079
80do_test delete-4.2 {
81 set v [catch {execsql {DELETE FROM table2 WHERE xyzzy(f1+4)}} msg]
82 lappend v $msg
83} {1 {no such function: xyzzy}}
drhed717fe2003-06-15 23:42:24 +000084integrity_check delete-4.3
drh62c68192000-05-30 00:51:26 +000085
drh3494ffe2001-03-20 12:55:13 +000086# Lots of deletes
87#
drh1bee3d72001-10-15 00:44:35 +000088do_test delete-5.1.1 {
drh3494ffe2001-03-20 12:55:13 +000089 execsql {DELETE FROM table1}
drh1bee3d72001-10-15 00:44:35 +000090} {2}
91do_test delete-5.1.2 {
drh3494ffe2001-03-20 12:55:13 +000092 execsql {SELECT count(*) FROM table1}
drh1bee3d72001-10-15 00:44:35 +000093} {0}
94do_test delete-5.2.1 {
95 execsql {BEGIN TRANSACTION}
drh3494ffe2001-03-20 12:55:13 +000096 for {set i 1} {$i<=200} {incr i} {
97 execsql "INSERT INTO table1 VALUES($i,[expr {$i*$i}])"
98 }
drh1bee3d72001-10-15 00:44:35 +000099 execsql {COMMIT}
100 execsql {SELECT count(*) FROM table1}
101} {200}
102do_test delete-5.2.2 {
103 execsql {DELETE FROM table1}
104} {200}
105do_test delete-5.2.3 {
106 execsql {BEGIN TRANSACTION}
107 for {set i 1} {$i<=200} {incr i} {
108 execsql "INSERT INTO table1 VALUES($i,[expr {$i*$i}])"
109 }
110 execsql {COMMIT}
111 execsql {SELECT count(*) FROM table1}
112} {200}
113do_test delete-5.2.4 {
114 execsql {PRAGMA count_changes=off}
115 execsql {DELETE FROM table1}
116} {}
117do_test delete-5.2.5 {
118 execsql {SELECT count(*) FROM table1}
119} {0}
120do_test delete-5.2.6 {
121 execsql {BEGIN TRANSACTION}
122 for {set i 1} {$i<=200} {incr i} {
123 execsql "INSERT INTO table1 VALUES($i,[expr {$i*$i}])"
124 }
125 execsql {COMMIT}
drh3494ffe2001-03-20 12:55:13 +0000126 execsql {SELECT count(*) FROM table1}
127} {200}
128do_test delete-5.3 {
129 for {set i 1} {$i<=200} {incr i 4} {
130 execsql "DELETE FROM table1 WHERE f1==$i"
131 }
132 execsql {SELECT count(*) FROM table1}
133} {150}
drh4ebfef12004-07-15 20:08:39 +0000134do_test delete-5.4.1 {
drh3494ffe2001-03-20 12:55:13 +0000135 execsql "DELETE FROM table1 WHERE f1>50"
drh4ebfef12004-07-15 20:08:39 +0000136 db changes
137} [db one {SELECT count(*) FROM table1 WHERE f1>50}]
138do_test delete-5.4.2 {
drh3494ffe2001-03-20 12:55:13 +0000139 execsql {SELECT count(*) FROM table1}
140} {37}
141do_test delete-5.5 {
142 for {set i 1} {$i<=70} {incr i 3} {
143 execsql "DELETE FROM table1 WHERE f1==$i"
144 }
145 execsql {SELECT f1 FROM table1 ORDER BY f1}
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}
147do_test delete-5.6 {
148 for {set i 1} {$i<40} {incr i} {
149 execsql "DELETE FROM table1 WHERE f1==$i"
150 }
151 execsql {SELECT f1 FROM table1 ORDER BY f1}
152} {42 44 47 48 50}
153do_test delete-5.7 {
154 execsql "DELETE FROM table1 WHERE f1!=48"
155 execsql {SELECT f1 FROM table1 ORDER BY f1}
156} {48}
drhed717fe2003-06-15 23:42:24 +0000157integrity_check delete-5.8
158
drh3494ffe2001-03-20 12:55:13 +0000159
drh0353ced2001-03-20 22:05:00 +0000160# Delete large quantities of data. We want to test the List overflow
161# mechanism in the vdbe.
162#
163do_test delete-6.1 {
drh5f3b4ab2004-05-27 17:22:54 +0000164 execsql {BEGIN; DELETE FROM table1}
drh0353ced2001-03-20 22:05:00 +0000165 for {set i 1} {$i<=3000} {incr i} {
drh5f3b4ab2004-05-27 17:22:54 +0000166 execsql "INSERT INTO table1 VALUES($i,[expr {$i*$i}])"
drh0353ced2001-03-20 22:05:00 +0000167 }
drh0353ced2001-03-20 22:05:00 +0000168 execsql {DELETE FROM table2}
drh5f3b4ab2004-05-27 17:22:54 +0000169 for {set i 1} {$i<=3000} {incr i} {
170 execsql "INSERT INTO table2 VALUES($i,[expr {$i*$i}])"
171 }
172 execsql {COMMIT}
drh0353ced2001-03-20 22:05:00 +0000173 execsql {SELECT count(*) FROM table1}
174} {3000}
175do_test delete-6.2 {
176 execsql {SELECT count(*) FROM table2}
177} {3000}
178do_test delete-6.3 {
179 execsql {SELECT f1 FROM table1 WHERE f1<10 ORDER BY f1}
180} {1 2 3 4 5 6 7 8 9}
181do_test delete-6.4 {
182 execsql {SELECT f1 FROM table2 WHERE f1<10 ORDER BY f1}
183} {1 2 3 4 5 6 7 8 9}
drh4ebfef12004-07-15 20:08:39 +0000184do_test delete-6.5.1 {
drh0353ced2001-03-20 22:05:00 +0000185 execsql {DELETE FROM table1 WHERE f1>7}
drh4ebfef12004-07-15 20:08:39 +0000186 db changes
187} {2993}
188do_test delete-6.5.2 {
drh0353ced2001-03-20 22:05:00 +0000189 execsql {SELECT f1 FROM table1 ORDER BY f1}
190} {1 2 3 4 5 6 7}
191do_test delete-6.6 {
192 execsql {DELETE FROM table2 WHERE f1>7}
193 execsql {SELECT f1 FROM table2 ORDER BY f1}
194} {1 2 3 4 5 6 7}
195do_test delete-6.7 {
196 execsql {DELETE FROM table1}
197 execsql {SELECT f1 FROM table1}
198} {}
199do_test delete-6.8 {
200 execsql {INSERT INTO table1 VALUES(2,3)}
201 execsql {SELECT f1 FROM table1}
202} {2}
203do_test delete-6.9 {
204 execsql {DELETE FROM table2}
205 execsql {SELECT f1 FROM table2}
206} {}
207do_test delete-6.10 {
208 execsql {INSERT INTO table2 VALUES(2,3)}
209 execsql {SELECT f1 FROM table2}
210} {2}
drhed717fe2003-06-15 23:42:24 +0000211integrity_check delete-6.11
drh0353ced2001-03-20 22:05:00 +0000212
drh26b3e1b2002-07-19 18:52:40 +0000213do_test delete-7.1 {
214 execsql {
215 CREATE TABLE t3(a);
216 INSERT INTO t3 VALUES(1);
217 INSERT INTO t3 SELECT a+1 FROM t3;
218 INSERT INTO t3 SELECT a+2 FROM t3;
219 SELECT * FROM t3;
220 }
221} {1 2 3 4}
drh798da522004-11-04 04:42:28 +0000222ifcapable {trigger} {
223 do_test delete-7.2 {
224 execsql {
225 CREATE TABLE cnt(del);
226 INSERT INTO cnt VALUES(0);
227 CREATE TRIGGER r1 AFTER DELETE ON t3 FOR EACH ROW BEGIN
228 UPDATE cnt SET del=del+1;
229 END;
230 DELETE FROM t3 WHERE a<2;
231 SELECT * FROM t3;
232 }
233 } {2 3 4}
234 do_test delete-7.3 {
235 execsql {
236 SELECT * FROM cnt;
237 }
238 } {1}
239 do_test delete-7.4 {
240 execsql {
241 DELETE FROM t3;
242 SELECT * FROM t3;
243 }
244 } {}
245 do_test delete-7.5 {
246 execsql {
247 SELECT * FROM cnt;
248 }
249 } {4}
250 do_test delete-7.6 {
251 execsql {
252 INSERT INTO t3 VALUES(1);
253 INSERT INTO t3 SELECT a+1 FROM t3;
254 INSERT INTO t3 SELECT a+2 FROM t3;
255 CREATE TABLE t4 AS SELECT * FROM t3;
256 PRAGMA count_changes=ON;
257 DELETE FROM t3;
258 DELETE FROM t4;
259 }
260 } {4 4}
261} ;# endif trigger
262ifcapable {!trigger} {
263 execsql {DELETE FROM t3}
264}
drhed717fe2003-06-15 23:42:24 +0000265integrity_check delete-7.7
drh0353ced2001-03-20 22:05:00 +0000266
drh66b4eba2003-05-04 20:42:56 +0000267# Make sure error messages are consistent when attempting to delete
268# from a read-only database. Ticket #304.
269#
270do_test delete-8.0 {
271 execsql {
272 PRAGMA count_changes=OFF;
273 INSERT INTO t3 VALUES(123);
274 SELECT * FROM t3;
275 }
276} {123}
277db close
278catch {file attributes test.db -permissions 0444}
279catch {file attributes test.db -readonly 1}
drhdddca282006-01-03 00:33:50 +0000280sqlite3 db test.db
281set ::DB [sqlite3_connection_pointer db]
drh66b4eba2003-05-04 20:42:56 +0000282do_test delete-8.1 {
283 catchsql {
284 DELETE FROM t3;
285 }
286} {1 {attempt to write a readonly database}}
287do_test delete-8.2 {
288 execsql {SELECT * FROM t3}
289} {123}
290do_test delete-8.3 {
291 catchsql {
292 DELETE FROM t3 WHERE 1;
293 }
294} {1 {attempt to write a readonly database}}
295do_test delete-8.4 {
296 execsql {SELECT * FROM t3}
297} {123}
danielk19770de0bb32004-06-10 05:59:24 +0000298
299# Update for v3: In v2 the DELETE statement would succeed because no
300# database writes actually occur. Version 3 refuses to open a transaction
301# on a read-only file, so the statement fails.
drh66b4eba2003-05-04 20:42:56 +0000302do_test delete-8.5 {
303 catchsql {
304 DELETE FROM t3 WHERE a<100;
305 }
danielk19770de0bb32004-06-10 05:59:24 +0000306# v2 result: {0 {}}
307} {1 {attempt to write a readonly database}}
drh66b4eba2003-05-04 20:42:56 +0000308do_test delete-8.6 {
309 execsql {SELECT * FROM t3}
310} {123}
drhed717fe2003-06-15 23:42:24 +0000311integrity_check delete-8.7
drh0353ced2001-03-20 22:05:00 +0000312
drh62c68192000-05-30 00:51:26 +0000313finish_test