blob: c389d20fb55e1b097c527e32b7b0294297eb5645 [file] [log] [blame]
drhb19a2bc2001-09-16 00:13:26 +00001# 2001 September 15
drh1d37e282000-05-30 03:12:21 +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:
drh1d37e282000-05-30 03:12:21 +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.
drh1d37e282000-05-30 03:12:21 +00009#
10#***********************************************************************
11# This file implements regression tests for SQLite library. The
12# focus of this file is testing the UPDATE statement.
13#
drhb19a2bc2001-09-16 00:13:26 +000014# $Id: update.test,v 1.6 2001/09/16 00:13:28 drh Exp $
drh1d37e282000-05-30 03:12:21 +000015
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18
19# Try to update an non-existent table
20#
21do_test update-1.1 {
22 set v [catch {execsql {UPDATE test1 SET f2=5 WHERE f1<1}} msg]
23 lappend v $msg
24} {1 {no such table: test1}}
25
26# Try to update a read-only table
27#
28do_test update-2.1 {
29 set v [catch \
30 {execsql {UPDATE sqlite_master SET name='xyz' WHERE name='123'}} msg]
31 lappend v $msg
32} {1 {table sqlite_master may not be modified}}
33
34# Create a table to work with
35#
36do_test update-3.1 {
37 execsql {CREATE TABLE test1(f1 int,f2 int)}
38 for {set i 1} {$i<=10} {incr i} {
39 set sql "INSERT INTO test1 VALUES($i,[expr {int(pow(2,$i))}])"
40 execsql $sql
41 }
42 execsql {SELECT * FROM test1 ORDER BY f1}
43} {1 2 2 4 3 8 4 16 5 32 6 64 7 128 8 256 9 512 10 1024}
44
drh967e8b72000-06-21 13:59:10 +000045# Unknown column name in an expression
drh1d37e282000-05-30 03:12:21 +000046#
47do_test update-3.2 {
48 set v [catch {execsql {UPDATE test1 SET f1=f3*2 WHERE f2==32}} msg]
49 lappend v $msg
drh967e8b72000-06-21 13:59:10 +000050} {1 {no such column: f3}}
drh1d37e282000-05-30 03:12:21 +000051do_test update-3.3 {
52 set v [catch {execsql {UPDATE test1 SET f1=test2.f1*2 WHERE f2==32}} msg]
53 lappend v $msg
drh967e8b72000-06-21 13:59:10 +000054} {1 {no such column: test2.f1}}
drh1d37e282000-05-30 03:12:21 +000055do_test update-3.4 {
56 set v [catch {execsql {UPDATE test1 SET f3=f1*2 WHERE f2==32}} msg]
57 lappend v $msg
drh967e8b72000-06-21 13:59:10 +000058} {1 {no such column: f3}}
drh1d37e282000-05-30 03:12:21 +000059
60# Actually do some updates
61#
62do_test update-3.5 {
63 execsql {UPDATE test1 SET f2=f2*3}
64 execsql {SELECT * FROM test1 ORDER BY f1}
65} {1 6 2 12 3 24 4 48 5 96 6 192 7 384 8 768 9 1536 10 3072}
66do_test update-3.6 {
67 execsql {UPDATE test1 SET f2=f2/3 WHERE f1<=5}
68 execsql {SELECT * FROM test1 ORDER BY f1}
69} {1 2 2 4 3 8 4 16 5 32 6 192 7 384 8 768 9 1536 10 3072}
70do_test update-3.7 {
71 execsql {UPDATE test1 SET f2=f2/3 WHERE f1>5}
72 execsql {SELECT * FROM test1 ORDER BY f1}
73} {1 2 2 4 3 8 4 16 5 32 6 64 7 128 8 256 9 512 10 1024}
74
75# Swap the values of f1 and f2 for all elements
76#
77do_test update-3.8 {
drh6206d502000-06-19 19:09:08 +000078 execsql {UPDATE test1 SET F2=f1, F1=f2}
79 execsql {SELECT * FROM test1 ORDER BY F1}
drh1d37e282000-05-30 03:12:21 +000080} {2 1 4 2 8 3 16 4 32 5 64 6 128 7 256 8 512 9 1024 10}
drh1d37e282000-05-30 03:12:21 +000081do_test update-3.9 {
drh4f3c63e2001-02-19 18:24:21 +000082 execsql {UPDATE test1 SET F2=f1, F1=f2}
83 execsql {SELECT * FROM test1 ORDER BY F1}
84} {1 2 2 4 3 8 4 16 5 32 6 64 7 128 8 256 9 512 10 1024}
85
86# Create duplicate entries and make sure updating still
87# works.
88#
89do_test update-4.0 {
90 execsql {
91 DELETE FROM test1 WHERE f1<=5;
92 INSERT INTO test1(f1,f2) VALUES(8,88);
93 INSERT INTO test1(f1,f2) VALUES(8,888);
94 INSERT INTO test1(f1,f2) VALUES(77,128);
95 INSERT INTO test1(f1,f2) VALUES(777,128);
96 }
97 execsql {SELECT * FROM test1 ORDER BY f1,f2}
98} {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
99do_test update-4.1 {
100 execsql {UPDATE test1 SET f2=f2+1 WHERE f1==8}
101 execsql {SELECT * FROM test1 ORDER BY f1,f2}
102} {6 64 7 128 8 89 8 257 8 889 9 512 10 1024 77 128 777 128}
103do_test update-4.2 {
104 execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2>800}
105 execsql {SELECT * FROM test1 ORDER BY f1,f2}
106} {6 64 7 128 8 89 8 257 8 888 9 512 10 1024 77 128 777 128}
107do_test update-4.3 {
108 execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2<800}
109 execsql {SELECT * FROM test1 ORDER BY f1,f2}
110} {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
111do_test update-4.4 {
112 execsql {UPDATE test1 SET f1=f1+1 WHERE f2==128}
113 execsql {SELECT * FROM test1 ORDER BY f1,f2}
114} {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 778 128}
115do_test update-4.5 {
116 execsql {UPDATE test1 SET f1=f1-1 WHERE f1>100 and f2==128}
117 execsql {SELECT * FROM test1 ORDER BY f1,f2}
118} {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 777 128}
119do_test update-4.6 {
120 execsql {UPDATE test1 SET f1=f1-1 WHERE f1<=100 and f2==128}
121 execsql {SELECT * FROM test1 ORDER BY f1,f2}
122} {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
123
124# Repeat the previous sequence of tests with an index.
125#
126do_test update-5.0 {
127 execsql {CREATE INDEX idx1 ON test1(f1)}
128 execsql {SELECT * FROM test1 ORDER BY f1,f2}
129} {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
130do_test update-5.1 {
131 execsql {UPDATE test1 SET f2=f2+1 WHERE f1==8}
132 execsql {SELECT * FROM test1 ORDER BY f1,f2}
133} {6 64 7 128 8 89 8 257 8 889 9 512 10 1024 77 128 777 128}
134do_test update-5.2 {
135 execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2>800}
136 execsql {SELECT * FROM test1 ORDER BY f1,f2}
137} {6 64 7 128 8 89 8 257 8 888 9 512 10 1024 77 128 777 128}
138do_test update-5.3 {
139 execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2<800}
140 execsql {SELECT * FROM test1 ORDER BY f1,f2}
141} {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
142do_test update-5.4 {
143 execsql {UPDATE test1 SET f1=f1+1 WHERE f2==128}
144 execsql {SELECT * FROM test1 ORDER BY f1,f2}
145} {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 778 128}
146do_test update-5.4.1 {
147 execsql {SELECT * FROM test1 WHERE f1==78 ORDER BY f1,f2}
148} {78 128}
149do_test update-5.4.2 {
150 execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
151} {778 128}
152do_test update-5.4.3 {
153 execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
154} {8 88 8 128 8 256 8 888}
155do_test update-5.5 {
156 execsql {UPDATE test1 SET f1=f1-1 WHERE f1>100 and f2==128}
157 execsql {SELECT * FROM test1 ORDER BY f1,f2}
158} {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 777 128}
159do_test update-5.5.1 {
160 execsql {SELECT * FROM test1 WHERE f1==78 ORDER BY f1,f2}
161} {78 128}
162do_test update-5.5.2 {
163 execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
164} {}
165do_test update-5.5.3 {
166 execsql {SELECT * FROM test1 WHERE f1==777 ORDER BY f1,f2}
167} {777 128}
168do_test update-5.5.4 {
169 execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
170} {8 88 8 128 8 256 8 888}
171do_test update-5.6 {
172 execsql {UPDATE test1 SET f1=f1-1 WHERE f1<=100 and f2==128}
173 execsql {SELECT * FROM test1 ORDER BY f1,f2}
174} {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
175do_test update-5.6.1 {
176 execsql {SELECT * FROM test1 WHERE f1==77 ORDER BY f1,f2}
177} {77 128}
178do_test update-5.6.2 {
179 execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
180} {}
181do_test update-5.6.3 {
182 execsql {SELECT * FROM test1 WHERE f1==777 ORDER BY f1,f2}
183} {777 128}
184do_test update-5.6.4 {
185 execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
186} {8 88 8 256 8 888}
187
188# Repeat the previous sequence of tests with a different index.
189#
190do_test update-6.0 {
191 execsql {DROP INDEX idx1}
192 execsql {CREATE INDEX idx1 ON test1(f2)}
193 execsql {SELECT * FROM test1 ORDER BY f1,f2}
194} {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
195do_test update-6.1 {
196 execsql {UPDATE test1 SET f2=f2+1 WHERE f1==8}
197 execsql {SELECT * FROM test1 ORDER BY f1,f2}
198} {6 64 7 128 8 89 8 257 8 889 9 512 10 1024 77 128 777 128}
199do_test update-6.1.1 {
200 execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
201} {8 89 8 257 8 889}
202do_test update-6.1.2 {
203 execsql {SELECT * FROM test1 WHERE f2==89 ORDER BY f1,f2}
204} {8 89}
205do_test update-6.1.3 {
206 execsql {SELECT * FROM test1 WHERE f1==88 ORDER BY f1,f2}
207} {}
208do_test update-6.2 {
209 execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2>800}
210 execsql {SELECT * FROM test1 ORDER BY f1,f2}
211} {6 64 7 128 8 89 8 257 8 888 9 512 10 1024 77 128 777 128}
212do_test update-6.3 {
213 execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2<800}
214 execsql {SELECT * FROM test1 ORDER BY f1,f2}
215} {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
216do_test update-6.3.1 {
217 execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
218} {8 88 8 256 8 888}
219do_test update-6.3.2 {
220 execsql {SELECT * FROM test1 WHERE f2==89 ORDER BY f1,f2}
221} {}
222do_test update-6.3.3 {
223 execsql {SELECT * FROM test1 WHERE f2==88 ORDER BY f1,f2}
224} {8 88}
225do_test update-6.4 {
226 execsql {UPDATE test1 SET f1=f1+1 WHERE f2==128}
227 execsql {SELECT * FROM test1 ORDER BY f1,f2}
228} {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 778 128}
229do_test update-6.4.1 {
230 execsql {SELECT * FROM test1 WHERE f1==78 ORDER BY f1,f2}
231} {78 128}
232do_test update-6.4.2 {
233 execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
234} {778 128}
235do_test update-6.4.3 {
236 execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
237} {8 88 8 128 8 256 8 888}
238do_test update-6.5 {
239 execsql {UPDATE test1 SET f1=f1-1 WHERE f1>100 and f2==128}
240 execsql {SELECT * FROM test1 ORDER BY f1,f2}
241} {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 777 128}
242do_test update-6.5.1 {
243 execsql {SELECT * FROM test1 WHERE f1==78 ORDER BY f1,f2}
244} {78 128}
245do_test update-6.5.2 {
246 execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
247} {}
248do_test update-6.5.3 {
249 execsql {SELECT * FROM test1 WHERE f1==777 ORDER BY f1,f2}
250} {777 128}
251do_test update-6.5.4 {
252 execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
253} {8 88 8 128 8 256 8 888}
254do_test update-6.6 {
255 execsql {UPDATE test1 SET f1=f1-1 WHERE f1<=100 and f2==128}
256 execsql {SELECT * FROM test1 ORDER BY f1,f2}
257} {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
258do_test update-6.6.1 {
259 execsql {SELECT * FROM test1 WHERE f1==77 ORDER BY f1,f2}
260} {77 128}
261do_test update-6.6.2 {
262 execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
263} {}
264do_test update-6.6.3 {
265 execsql {SELECT * FROM test1 WHERE f1==777 ORDER BY f1,f2}
266} {777 128}
267do_test update-6.6.4 {
268 execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
269} {8 88 8 256 8 888}
270
271# Repeat the previous sequence of tests with multiple
272# indices
273#
274do_test update-7.0 {
275 execsql {CREATE INDEX idx2 ON test1(f2)}
276 execsql {CREATE INDEX idx3 ON test1(f1,f2)}
277 execsql {SELECT * FROM test1 ORDER BY f1,f2}
278} {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
279do_test update-7.1 {
280 execsql {UPDATE test1 SET f2=f2+1 WHERE f1==8}
281 execsql {SELECT * FROM test1 ORDER BY f1,f2}
282} {6 64 7 128 8 89 8 257 8 889 9 512 10 1024 77 128 777 128}
283do_test update-7.1.1 {
284 execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
285} {8 89 8 257 8 889}
286do_test update-7.1.2 {
287 execsql {SELECT * FROM test1 WHERE f2==89 ORDER BY f1,f2}
288} {8 89}
289do_test update-7.1.3 {
290 execsql {SELECT * FROM test1 WHERE f1==88 ORDER BY f1,f2}
291} {}
292do_test update-7.2 {
293 execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2>800}
294 execsql {SELECT * FROM test1 ORDER BY f1,f2}
295} {6 64 7 128 8 89 8 257 8 888 9 512 10 1024 77 128 777 128}
296do_test update-7.3 {
297 execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2<800}
298 execsql {SELECT * FROM test1 ORDER BY f1,f2}
299} {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
300do_test update-7.3.1 {
301 execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
302} {8 88 8 256 8 888}
303do_test update-7.3.2 {
304 execsql {SELECT * FROM test1 WHERE f2==89 ORDER BY f1,f2}
305} {}
306do_test update-7.3.3 {
307 execsql {SELECT * FROM test1 WHERE f2==88 ORDER BY f1,f2}
308} {8 88}
309do_test update-7.4 {
310 execsql {UPDATE test1 SET f1=f1+1 WHERE f2==128}
311 execsql {SELECT * FROM test1 ORDER BY f1,f2}
312} {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 778 128}
313do_test update-7.4.1 {
314 execsql {SELECT * FROM test1 WHERE f1==78 ORDER BY f1,f2}
315} {78 128}
316do_test update-7.4.2 {
317 execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
318} {778 128}
319do_test update-7.4.3 {
320 execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
321} {8 88 8 128 8 256 8 888}
322do_test update-7.5 {
323 execsql {UPDATE test1 SET f1=f1-1 WHERE f1>100 and f2==128}
324 execsql {SELECT * FROM test1 ORDER BY f1,f2}
325} {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 777 128}
326do_test update-7.5.1 {
327 execsql {SELECT * FROM test1 WHERE f1==78 ORDER BY f1,f2}
328} {78 128}
329do_test update-7.5.2 {
330 execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
331} {}
332do_test update-7.5.3 {
333 execsql {SELECT * FROM test1 WHERE f1==777 ORDER BY f1,f2}
334} {777 128}
335do_test update-7.5.4 {
336 execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
337} {8 88 8 128 8 256 8 888}
338do_test update-7.6 {
339 execsql {UPDATE test1 SET f1=f1-1 WHERE f1<=100 and f2==128}
340 execsql {SELECT * FROM test1 ORDER BY f1,f2}
341} {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
342do_test update-7.6.1 {
343 execsql {SELECT * FROM test1 WHERE f1==77 ORDER BY f1,f2}
344} {77 128}
345do_test update-7.6.2 {
346 execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
347} {}
348do_test update-7.6.3 {
349 execsql {SELECT * FROM test1 WHERE f1==777 ORDER BY f1,f2}
350} {777 128}
351do_test update-7.6.4 {
352 execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
353} {8 88 8 256 8 888}
drh1d37e282000-05-30 03:12:21 +0000354
drhc837e702000-06-08 16:26:24 +0000355# Error messages
356#
drh4f3c63e2001-02-19 18:24:21 +0000357do_test update-9.1 {
drhc837e702000-06-08 16:26:24 +0000358 set v [catch {execsql {
359 UPDATE test1 SET x=11 WHERE f1=1025
360 }} msg]
361 lappend v $msg
drh967e8b72000-06-21 13:59:10 +0000362} {1 {no such column: x}}
drh4f3c63e2001-02-19 18:24:21 +0000363do_test update-9.2 {
drhc837e702000-06-08 16:26:24 +0000364 set v [catch {execsql {
365 UPDATE test1 SET f1=x(11) WHERE f1=1025
366 }} msg]
367 lappend v $msg
368} {1 {no such function: x}}
drh4f3c63e2001-02-19 18:24:21 +0000369do_test update-9.3 {
drhc837e702000-06-08 16:26:24 +0000370 set v [catch {execsql {
371 UPDATE test1 SET f1=11 WHERE x=1025
372 }} msg]
373 lappend v $msg
drh967e8b72000-06-21 13:59:10 +0000374} {1 {no such column: x}}
drh4f3c63e2001-02-19 18:24:21 +0000375do_test update-9.4 {
drhc837e702000-06-08 16:26:24 +0000376 set v [catch {execsql {
377 UPDATE test1 SET f1=11 WHERE x(f1)=1025
378 }} msg]
379 lappend v $msg
380} {1 {no such function: x}}
drh1d37e282000-05-30 03:12:21 +0000381
382
383
384
385
386
387finish_test