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