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