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 UPDATE statement.
|
sl@0
|
13 |
#
|
sl@0
|
14 |
# $Id: update.test,v 1.19 2008/04/10 18:44:36 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 update an non-existent table
|
sl@0
|
20 |
#
|
sl@0
|
21 |
do_test update-1.1 {
|
sl@0
|
22 |
set v [catch {execsql {UPDATE test1 SET f2=5 WHERE f1<1}} msg]
|
sl@0
|
23 |
lappend v $msg
|
sl@0
|
24 |
} {1 {no such table: test1}}
|
sl@0
|
25 |
|
sl@0
|
26 |
# Try to update a read-only table
|
sl@0
|
27 |
#
|
sl@0
|
28 |
do_test update-2.1 {
|
sl@0
|
29 |
set v [catch \
|
sl@0
|
30 |
{execsql {UPDATE sqlite_master SET name='xyz' WHERE name='123'}} msg]
|
sl@0
|
31 |
lappend v $msg
|
sl@0
|
32 |
} {1 {table sqlite_master may not be modified}}
|
sl@0
|
33 |
|
sl@0
|
34 |
# Create a table to work with
|
sl@0
|
35 |
#
|
sl@0
|
36 |
do_test update-3.1 {
|
sl@0
|
37 |
execsql {CREATE TABLE test1(f1 int,f2 int)}
|
sl@0
|
38 |
for {set i 1} {$i<=10} {incr i} {
|
sl@0
|
39 |
set sql "INSERT INTO test1 VALUES($i,[expr {1<<$i}])"
|
sl@0
|
40 |
execsql $sql
|
sl@0
|
41 |
}
|
sl@0
|
42 |
execsql {SELECT * FROM test1 ORDER BY f1}
|
sl@0
|
43 |
} {1 2 2 4 3 8 4 16 5 32 6 64 7 128 8 256 9 512 10 1024}
|
sl@0
|
44 |
|
sl@0
|
45 |
# Unknown column name in an expression
|
sl@0
|
46 |
#
|
sl@0
|
47 |
do_test update-3.2 {
|
sl@0
|
48 |
set v [catch {execsql {UPDATE test1 SET f1=f3*2 WHERE f2==32}} msg]
|
sl@0
|
49 |
lappend v $msg
|
sl@0
|
50 |
} {1 {no such column: f3}}
|
sl@0
|
51 |
do_test update-3.3 {
|
sl@0
|
52 |
set v [catch {execsql {UPDATE test1 SET f1=test2.f1*2 WHERE f2==32}} msg]
|
sl@0
|
53 |
lappend v $msg
|
sl@0
|
54 |
} {1 {no such column: test2.f1}}
|
sl@0
|
55 |
do_test update-3.4 {
|
sl@0
|
56 |
set v [catch {execsql {UPDATE test1 SET f3=f1*2 WHERE f2==32}} msg]
|
sl@0
|
57 |
lappend v $msg
|
sl@0
|
58 |
} {1 {no such column: f3}}
|
sl@0
|
59 |
|
sl@0
|
60 |
# Actually do some updates
|
sl@0
|
61 |
#
|
sl@0
|
62 |
do_test update-3.5 {
|
sl@0
|
63 |
execsql {UPDATE test1 SET f2=f2*3}
|
sl@0
|
64 |
} {}
|
sl@0
|
65 |
do_test update-3.5.1 {
|
sl@0
|
66 |
db changes
|
sl@0
|
67 |
} {10}
|
sl@0
|
68 |
|
sl@0
|
69 |
# verify that SELECT does not reset the change counter
|
sl@0
|
70 |
do_test update-3.5.2 {
|
sl@0
|
71 |
db eval {SELECT count(*) FROM test1}
|
sl@0
|
72 |
} {10}
|
sl@0
|
73 |
do_test update-3.5.3 {
|
sl@0
|
74 |
db changes
|
sl@0
|
75 |
} {10}
|
sl@0
|
76 |
|
sl@0
|
77 |
do_test update-3.6 {
|
sl@0
|
78 |
execsql {SELECT * FROM test1 ORDER BY f1}
|
sl@0
|
79 |
} {1 6 2 12 3 24 4 48 5 96 6 192 7 384 8 768 9 1536 10 3072}
|
sl@0
|
80 |
do_test update-3.7 {
|
sl@0
|
81 |
execsql {PRAGMA count_changes=on}
|
sl@0
|
82 |
execsql {UPDATE test1 SET f2=f2/3 WHERE f1<=5}
|
sl@0
|
83 |
} {5}
|
sl@0
|
84 |
do_test update-3.8 {
|
sl@0
|
85 |
execsql {SELECT * FROM test1 ORDER BY f1}
|
sl@0
|
86 |
} {1 2 2 4 3 8 4 16 5 32 6 192 7 384 8 768 9 1536 10 3072}
|
sl@0
|
87 |
do_test update-3.9 {
|
sl@0
|
88 |
execsql {UPDATE test1 SET f2=f2/3 WHERE f1>5}
|
sl@0
|
89 |
} {5}
|
sl@0
|
90 |
do_test update-3.10 {
|
sl@0
|
91 |
execsql {SELECT * FROM test1 ORDER BY f1}
|
sl@0
|
92 |
} {1 2 2 4 3 8 4 16 5 32 6 64 7 128 8 256 9 512 10 1024}
|
sl@0
|
93 |
|
sl@0
|
94 |
# Swap the values of f1 and f2 for all elements
|
sl@0
|
95 |
#
|
sl@0
|
96 |
do_test update-3.11 {
|
sl@0
|
97 |
execsql {UPDATE test1 SET F2=f1, F1=f2}
|
sl@0
|
98 |
} {10}
|
sl@0
|
99 |
do_test update-3.12 {
|
sl@0
|
100 |
execsql {SELECT * FROM test1 ORDER BY F1}
|
sl@0
|
101 |
} {2 1 4 2 8 3 16 4 32 5 64 6 128 7 256 8 512 9 1024 10}
|
sl@0
|
102 |
do_test update-3.13 {
|
sl@0
|
103 |
execsql {PRAGMA count_changes=off}
|
sl@0
|
104 |
execsql {UPDATE test1 SET F2=f1, F1=f2}
|
sl@0
|
105 |
} {}
|
sl@0
|
106 |
do_test update-3.14 {
|
sl@0
|
107 |
execsql {SELECT * FROM test1 ORDER BY F1}
|
sl@0
|
108 |
} {1 2 2 4 3 8 4 16 5 32 6 64 7 128 8 256 9 512 10 1024}
|
sl@0
|
109 |
|
sl@0
|
110 |
# Create duplicate entries and make sure updating still
|
sl@0
|
111 |
# works.
|
sl@0
|
112 |
#
|
sl@0
|
113 |
do_test update-4.0 {
|
sl@0
|
114 |
execsql {
|
sl@0
|
115 |
DELETE FROM test1 WHERE f1<=5;
|
sl@0
|
116 |
INSERT INTO test1(f1,f2) VALUES(8,88);
|
sl@0
|
117 |
INSERT INTO test1(f1,f2) VALUES(8,888);
|
sl@0
|
118 |
INSERT INTO test1(f1,f2) VALUES(77,128);
|
sl@0
|
119 |
INSERT INTO test1(f1,f2) VALUES(777,128);
|
sl@0
|
120 |
}
|
sl@0
|
121 |
execsql {SELECT * FROM test1 ORDER BY f1,f2}
|
sl@0
|
122 |
} {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
|
sl@0
|
123 |
do_test update-4.1 {
|
sl@0
|
124 |
execsql {UPDATE test1 SET f2=f2+1 WHERE f1==8}
|
sl@0
|
125 |
execsql {SELECT * FROM test1 ORDER BY f1,f2}
|
sl@0
|
126 |
} {6 64 7 128 8 89 8 257 8 889 9 512 10 1024 77 128 777 128}
|
sl@0
|
127 |
do_test update-4.2 {
|
sl@0
|
128 |
execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2>800}
|
sl@0
|
129 |
execsql {SELECT * FROM test1 ORDER BY f1,f2}
|
sl@0
|
130 |
} {6 64 7 128 8 89 8 257 8 888 9 512 10 1024 77 128 777 128}
|
sl@0
|
131 |
do_test update-4.3 {
|
sl@0
|
132 |
execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2<800}
|
sl@0
|
133 |
execsql {SELECT * FROM test1 ORDER BY f1,f2}
|
sl@0
|
134 |
} {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
|
sl@0
|
135 |
do_test update-4.4 {
|
sl@0
|
136 |
execsql {UPDATE test1 SET f1=f1+1 WHERE f2==128}
|
sl@0
|
137 |
execsql {SELECT * FROM test1 ORDER BY f1,f2}
|
sl@0
|
138 |
} {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 778 128}
|
sl@0
|
139 |
do_test update-4.5 {
|
sl@0
|
140 |
execsql {UPDATE test1 SET f1=f1-1 WHERE f1>100 and f2==128}
|
sl@0
|
141 |
execsql {SELECT * FROM test1 ORDER BY f1,f2}
|
sl@0
|
142 |
} {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 777 128}
|
sl@0
|
143 |
do_test update-4.6 {
|
sl@0
|
144 |
execsql {
|
sl@0
|
145 |
PRAGMA count_changes=on;
|
sl@0
|
146 |
UPDATE test1 SET f1=f1-1 WHERE f1<=100 and f2==128;
|
sl@0
|
147 |
}
|
sl@0
|
148 |
} {2}
|
sl@0
|
149 |
do_test update-4.7 {
|
sl@0
|
150 |
execsql {
|
sl@0
|
151 |
PRAGMA count_changes=off;
|
sl@0
|
152 |
SELECT * FROM test1 ORDER BY f1,f2
|
sl@0
|
153 |
}
|
sl@0
|
154 |
} {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
|
sl@0
|
155 |
|
sl@0
|
156 |
# Repeat the previous sequence of tests with an index.
|
sl@0
|
157 |
#
|
sl@0
|
158 |
do_test update-5.0 {
|
sl@0
|
159 |
execsql {CREATE INDEX idx1 ON test1(f1)}
|
sl@0
|
160 |
execsql {SELECT * FROM test1 ORDER BY f1,f2}
|
sl@0
|
161 |
} {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
|
sl@0
|
162 |
do_test update-5.1 {
|
sl@0
|
163 |
execsql {UPDATE test1 SET f2=f2+1 WHERE f1==8}
|
sl@0
|
164 |
execsql {SELECT * FROM test1 ORDER BY f1,f2}
|
sl@0
|
165 |
} {6 64 7 128 8 89 8 257 8 889 9 512 10 1024 77 128 777 128}
|
sl@0
|
166 |
do_test update-5.2 {
|
sl@0
|
167 |
execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2>800}
|
sl@0
|
168 |
execsql {SELECT * FROM test1 ORDER BY f1,f2}
|
sl@0
|
169 |
} {6 64 7 128 8 89 8 257 8 888 9 512 10 1024 77 128 777 128}
|
sl@0
|
170 |
do_test update-5.3 {
|
sl@0
|
171 |
execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2<800}
|
sl@0
|
172 |
execsql {SELECT * FROM test1 ORDER BY f1,f2}
|
sl@0
|
173 |
} {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
|
sl@0
|
174 |
do_test update-5.4 {
|
sl@0
|
175 |
execsql {UPDATE test1 SET f1=f1+1 WHERE f2==128}
|
sl@0
|
176 |
execsql {SELECT * FROM test1 ORDER BY f1,f2}
|
sl@0
|
177 |
} {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 778 128}
|
sl@0
|
178 |
do_test update-5.4.1 {
|
sl@0
|
179 |
execsql {SELECT * FROM test1 WHERE f1==78 ORDER BY f1,f2}
|
sl@0
|
180 |
} {78 128}
|
sl@0
|
181 |
do_test update-5.4.2 {
|
sl@0
|
182 |
execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
|
sl@0
|
183 |
} {778 128}
|
sl@0
|
184 |
do_test update-5.4.3 {
|
sl@0
|
185 |
execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
|
sl@0
|
186 |
} {8 88 8 128 8 256 8 888}
|
sl@0
|
187 |
do_test update-5.5 {
|
sl@0
|
188 |
execsql {UPDATE test1 SET f1=f1-1 WHERE f1>100 and f2==128}
|
sl@0
|
189 |
} {}
|
sl@0
|
190 |
do_test update-5.5.1 {
|
sl@0
|
191 |
execsql {SELECT * FROM test1 ORDER BY f1,f2}
|
sl@0
|
192 |
} {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 777 128}
|
sl@0
|
193 |
do_test update-5.5.2 {
|
sl@0
|
194 |
execsql {SELECT * FROM test1 WHERE f1==78 ORDER BY f1,f2}
|
sl@0
|
195 |
} {78 128}
|
sl@0
|
196 |
do_test update-5.5.3 {
|
sl@0
|
197 |
execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
|
sl@0
|
198 |
} {}
|
sl@0
|
199 |
do_test update-5.5.4 {
|
sl@0
|
200 |
execsql {SELECT * FROM test1 WHERE f1==777 ORDER BY f1,f2}
|
sl@0
|
201 |
} {777 128}
|
sl@0
|
202 |
do_test update-5.5.5 {
|
sl@0
|
203 |
execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
|
sl@0
|
204 |
} {8 88 8 128 8 256 8 888}
|
sl@0
|
205 |
do_test update-5.6 {
|
sl@0
|
206 |
execsql {
|
sl@0
|
207 |
PRAGMA count_changes=on;
|
sl@0
|
208 |
UPDATE test1 SET f1=f1-1 WHERE f1<=100 and f2==128;
|
sl@0
|
209 |
}
|
sl@0
|
210 |
} {2}
|
sl@0
|
211 |
do_test update-5.6.1 {
|
sl@0
|
212 |
execsql {
|
sl@0
|
213 |
PRAGMA count_changes=off;
|
sl@0
|
214 |
SELECT * FROM test1 ORDER BY f1,f2
|
sl@0
|
215 |
}
|
sl@0
|
216 |
} {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
|
sl@0
|
217 |
do_test update-5.6.2 {
|
sl@0
|
218 |
execsql {SELECT * FROM test1 WHERE f1==77 ORDER BY f1,f2}
|
sl@0
|
219 |
} {77 128}
|
sl@0
|
220 |
do_test update-5.6.3 {
|
sl@0
|
221 |
execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
|
sl@0
|
222 |
} {}
|
sl@0
|
223 |
do_test update-5.6.4 {
|
sl@0
|
224 |
execsql {SELECT * FROM test1 WHERE f1==777 ORDER BY f1,f2}
|
sl@0
|
225 |
} {777 128}
|
sl@0
|
226 |
do_test update-5.6.5 {
|
sl@0
|
227 |
execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
|
sl@0
|
228 |
} {8 88 8 256 8 888}
|
sl@0
|
229 |
|
sl@0
|
230 |
# Repeat the previous sequence of tests with a different index.
|
sl@0
|
231 |
#
|
sl@0
|
232 |
execsql {PRAGMA synchronous=FULL}
|
sl@0
|
233 |
do_test update-6.0 {
|
sl@0
|
234 |
execsql {DROP INDEX idx1}
|
sl@0
|
235 |
execsql {CREATE INDEX idx1 ON test1(f2)}
|
sl@0
|
236 |
execsql {SELECT * FROM test1 ORDER BY f1,f2}
|
sl@0
|
237 |
} {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
|
sl@0
|
238 |
do_test update-6.1 {
|
sl@0
|
239 |
execsql {UPDATE test1 SET f2=f2+1 WHERE f1==8}
|
sl@0
|
240 |
execsql {SELECT * FROM test1 ORDER BY f1,f2}
|
sl@0
|
241 |
} {6 64 7 128 8 89 8 257 8 889 9 512 10 1024 77 128 777 128}
|
sl@0
|
242 |
do_test update-6.1.1 {
|
sl@0
|
243 |
execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
|
sl@0
|
244 |
} {8 89 8 257 8 889}
|
sl@0
|
245 |
do_test update-6.1.2 {
|
sl@0
|
246 |
execsql {SELECT * FROM test1 WHERE f2==89 ORDER BY f1,f2}
|
sl@0
|
247 |
} {8 89}
|
sl@0
|
248 |
do_test update-6.1.3 {
|
sl@0
|
249 |
execsql {SELECT * FROM test1 WHERE f1==88 ORDER BY f1,f2}
|
sl@0
|
250 |
} {}
|
sl@0
|
251 |
do_test update-6.2 {
|
sl@0
|
252 |
execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2>800}
|
sl@0
|
253 |
execsql {SELECT * FROM test1 ORDER BY f1,f2}
|
sl@0
|
254 |
} {6 64 7 128 8 89 8 257 8 888 9 512 10 1024 77 128 777 128}
|
sl@0
|
255 |
do_test update-6.3 {
|
sl@0
|
256 |
execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2<800}
|
sl@0
|
257 |
execsql {SELECT * FROM test1 ORDER BY f1,f2}
|
sl@0
|
258 |
} {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
|
sl@0
|
259 |
do_test update-6.3.1 {
|
sl@0
|
260 |
execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
|
sl@0
|
261 |
} {8 88 8 256 8 888}
|
sl@0
|
262 |
do_test update-6.3.2 {
|
sl@0
|
263 |
execsql {SELECT * FROM test1 WHERE f2==89 ORDER BY f1,f2}
|
sl@0
|
264 |
} {}
|
sl@0
|
265 |
do_test update-6.3.3 {
|
sl@0
|
266 |
execsql {SELECT * FROM test1 WHERE f2==88 ORDER BY f1,f2}
|
sl@0
|
267 |
} {8 88}
|
sl@0
|
268 |
do_test update-6.4 {
|
sl@0
|
269 |
execsql {UPDATE test1 SET f1=f1+1 WHERE f2==128}
|
sl@0
|
270 |
execsql {SELECT * FROM test1 ORDER BY f1,f2}
|
sl@0
|
271 |
} {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 778 128}
|
sl@0
|
272 |
do_test update-6.4.1 {
|
sl@0
|
273 |
execsql {SELECT * FROM test1 WHERE f1==78 ORDER BY f1,f2}
|
sl@0
|
274 |
} {78 128}
|
sl@0
|
275 |
do_test update-6.4.2 {
|
sl@0
|
276 |
execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
|
sl@0
|
277 |
} {778 128}
|
sl@0
|
278 |
do_test update-6.4.3 {
|
sl@0
|
279 |
execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
|
sl@0
|
280 |
} {8 88 8 128 8 256 8 888}
|
sl@0
|
281 |
do_test update-6.5 {
|
sl@0
|
282 |
execsql {UPDATE test1 SET f1=f1-1 WHERE f1>100 and f2==128}
|
sl@0
|
283 |
execsql {SELECT * FROM test1 ORDER BY f1,f2}
|
sl@0
|
284 |
} {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 777 128}
|
sl@0
|
285 |
do_test update-6.5.1 {
|
sl@0
|
286 |
execsql {SELECT * FROM test1 WHERE f1==78 ORDER BY f1,f2}
|
sl@0
|
287 |
} {78 128}
|
sl@0
|
288 |
do_test update-6.5.2 {
|
sl@0
|
289 |
execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
|
sl@0
|
290 |
} {}
|
sl@0
|
291 |
do_test update-6.5.3 {
|
sl@0
|
292 |
execsql {SELECT * FROM test1 WHERE f1==777 ORDER BY f1,f2}
|
sl@0
|
293 |
} {777 128}
|
sl@0
|
294 |
do_test update-6.5.4 {
|
sl@0
|
295 |
execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
|
sl@0
|
296 |
} {8 88 8 128 8 256 8 888}
|
sl@0
|
297 |
do_test update-6.6 {
|
sl@0
|
298 |
execsql {UPDATE test1 SET f1=f1-1 WHERE f1<=100 and f2==128}
|
sl@0
|
299 |
execsql {SELECT * FROM test1 ORDER BY f1,f2}
|
sl@0
|
300 |
} {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
|
sl@0
|
301 |
do_test update-6.6.1 {
|
sl@0
|
302 |
execsql {SELECT * FROM test1 WHERE f1==77 ORDER BY f1,f2}
|
sl@0
|
303 |
} {77 128}
|
sl@0
|
304 |
do_test update-6.6.2 {
|
sl@0
|
305 |
execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
|
sl@0
|
306 |
} {}
|
sl@0
|
307 |
do_test update-6.6.3 {
|
sl@0
|
308 |
execsql {SELECT * FROM test1 WHERE f1==777 ORDER BY f1,f2}
|
sl@0
|
309 |
} {777 128}
|
sl@0
|
310 |
do_test update-6.6.4 {
|
sl@0
|
311 |
execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
|
sl@0
|
312 |
} {8 88 8 256 8 888}
|
sl@0
|
313 |
|
sl@0
|
314 |
# Repeat the previous sequence of tests with multiple
|
sl@0
|
315 |
# indices
|
sl@0
|
316 |
#
|
sl@0
|
317 |
do_test update-7.0 {
|
sl@0
|
318 |
execsql {CREATE INDEX idx2 ON test1(f2)}
|
sl@0
|
319 |
execsql {CREATE INDEX idx3 ON test1(f1,f2)}
|
sl@0
|
320 |
execsql {SELECT * FROM test1 ORDER BY f1,f2}
|
sl@0
|
321 |
} {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
|
sl@0
|
322 |
do_test update-7.1 {
|
sl@0
|
323 |
execsql {UPDATE test1 SET f2=f2+1 WHERE f1==8}
|
sl@0
|
324 |
execsql {SELECT * FROM test1 ORDER BY f1,f2}
|
sl@0
|
325 |
} {6 64 7 128 8 89 8 257 8 889 9 512 10 1024 77 128 777 128}
|
sl@0
|
326 |
do_test update-7.1.1 {
|
sl@0
|
327 |
execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
|
sl@0
|
328 |
} {8 89 8 257 8 889}
|
sl@0
|
329 |
do_test update-7.1.2 {
|
sl@0
|
330 |
execsql {SELECT * FROM test1 WHERE f2==89 ORDER BY f1,f2}
|
sl@0
|
331 |
} {8 89}
|
sl@0
|
332 |
do_test update-7.1.3 {
|
sl@0
|
333 |
execsql {SELECT * FROM test1 WHERE f1==88 ORDER BY f1,f2}
|
sl@0
|
334 |
} {}
|
sl@0
|
335 |
do_test update-7.2 {
|
sl@0
|
336 |
execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2>800}
|
sl@0
|
337 |
execsql {SELECT * FROM test1 ORDER BY f1,f2}
|
sl@0
|
338 |
} {6 64 7 128 8 89 8 257 8 888 9 512 10 1024 77 128 777 128}
|
sl@0
|
339 |
do_test update-7.3 {
|
sl@0
|
340 |
# explain {UPDATE test1 SET f2=f2-1 WHERE f1==8 and F2<300}
|
sl@0
|
341 |
execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2<800}
|
sl@0
|
342 |
execsql {SELECT * FROM test1 ORDER BY f1,f2}
|
sl@0
|
343 |
} {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
|
sl@0
|
344 |
do_test update-7.3.1 {
|
sl@0
|
345 |
execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
|
sl@0
|
346 |
} {8 88 8 256 8 888}
|
sl@0
|
347 |
do_test update-7.3.2 {
|
sl@0
|
348 |
execsql {SELECT * FROM test1 WHERE f2==89 ORDER BY f1,f2}
|
sl@0
|
349 |
} {}
|
sl@0
|
350 |
do_test update-7.3.3 {
|
sl@0
|
351 |
execsql {SELECT * FROM test1 WHERE f2==88 ORDER BY f1,f2}
|
sl@0
|
352 |
} {8 88}
|
sl@0
|
353 |
do_test update-7.4 {
|
sl@0
|
354 |
execsql {UPDATE test1 SET f1=f1+1 WHERE f2==128}
|
sl@0
|
355 |
execsql {SELECT * FROM test1 ORDER BY f1,f2}
|
sl@0
|
356 |
} {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 778 128}
|
sl@0
|
357 |
do_test update-7.4.1 {
|
sl@0
|
358 |
execsql {SELECT * FROM test1 WHERE f1==78 ORDER BY f1,f2}
|
sl@0
|
359 |
} {78 128}
|
sl@0
|
360 |
do_test update-7.4.2 {
|
sl@0
|
361 |
execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
|
sl@0
|
362 |
} {778 128}
|
sl@0
|
363 |
do_test update-7.4.3 {
|
sl@0
|
364 |
execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
|
sl@0
|
365 |
} {8 88 8 128 8 256 8 888}
|
sl@0
|
366 |
do_test update-7.5 {
|
sl@0
|
367 |
execsql {UPDATE test1 SET f1=f1-1 WHERE f1>100 and f2==128}
|
sl@0
|
368 |
execsql {SELECT * FROM test1 ORDER BY f1,f2}
|
sl@0
|
369 |
} {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 777 128}
|
sl@0
|
370 |
do_test update-7.5.1 {
|
sl@0
|
371 |
execsql {SELECT * FROM test1 WHERE f1==78 ORDER BY f1,f2}
|
sl@0
|
372 |
} {78 128}
|
sl@0
|
373 |
do_test update-7.5.2 {
|
sl@0
|
374 |
execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
|
sl@0
|
375 |
} {}
|
sl@0
|
376 |
do_test update-7.5.3 {
|
sl@0
|
377 |
execsql {SELECT * FROM test1 WHERE f1==777 ORDER BY f1,f2}
|
sl@0
|
378 |
} {777 128}
|
sl@0
|
379 |
do_test update-7.5.4 {
|
sl@0
|
380 |
execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
|
sl@0
|
381 |
} {8 88 8 128 8 256 8 888}
|
sl@0
|
382 |
do_test update-7.6 {
|
sl@0
|
383 |
execsql {UPDATE test1 SET f1=f1-1 WHERE f1<=100 and f2==128}
|
sl@0
|
384 |
execsql {SELECT * FROM test1 ORDER BY f1,f2}
|
sl@0
|
385 |
} {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
|
sl@0
|
386 |
do_test update-7.6.1 {
|
sl@0
|
387 |
execsql {SELECT * FROM test1 WHERE f1==77 ORDER BY f1,f2}
|
sl@0
|
388 |
} {77 128}
|
sl@0
|
389 |
do_test update-7.6.2 {
|
sl@0
|
390 |
execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
|
sl@0
|
391 |
} {}
|
sl@0
|
392 |
do_test update-7.6.3 {
|
sl@0
|
393 |
execsql {SELECT * FROM test1 WHERE f1==777 ORDER BY f1,f2}
|
sl@0
|
394 |
} {777 128}
|
sl@0
|
395 |
do_test update-7.6.4 {
|
sl@0
|
396 |
execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
|
sl@0
|
397 |
} {8 88 8 256 8 888}
|
sl@0
|
398 |
|
sl@0
|
399 |
# Error messages
|
sl@0
|
400 |
#
|
sl@0
|
401 |
do_test update-9.1 {
|
sl@0
|
402 |
set v [catch {execsql {
|
sl@0
|
403 |
UPDATE test1 SET x=11 WHERE f1=1025
|
sl@0
|
404 |
}} msg]
|
sl@0
|
405 |
lappend v $msg
|
sl@0
|
406 |
} {1 {no such column: x}}
|
sl@0
|
407 |
do_test update-9.2 {
|
sl@0
|
408 |
set v [catch {execsql {
|
sl@0
|
409 |
UPDATE test1 SET f1=x(11) WHERE f1=1025
|
sl@0
|
410 |
}} msg]
|
sl@0
|
411 |
lappend v $msg
|
sl@0
|
412 |
} {1 {no such function: x}}
|
sl@0
|
413 |
do_test update-9.3 {
|
sl@0
|
414 |
set v [catch {execsql {
|
sl@0
|
415 |
UPDATE test1 SET f1=11 WHERE x=1025
|
sl@0
|
416 |
}} msg]
|
sl@0
|
417 |
lappend v $msg
|
sl@0
|
418 |
} {1 {no such column: x}}
|
sl@0
|
419 |
do_test update-9.4 {
|
sl@0
|
420 |
set v [catch {execsql {
|
sl@0
|
421 |
UPDATE test1 SET f1=11 WHERE x(f1)=1025
|
sl@0
|
422 |
}} msg]
|
sl@0
|
423 |
lappend v $msg
|
sl@0
|
424 |
} {1 {no such function: x}}
|
sl@0
|
425 |
|
sl@0
|
426 |
# Try doing updates on a unique column where the value does not
|
sl@0
|
427 |
# really change.
|
sl@0
|
428 |
#
|
sl@0
|
429 |
do_test update-10.1 {
|
sl@0
|
430 |
execsql {
|
sl@0
|
431 |
DROP TABLE test1;
|
sl@0
|
432 |
CREATE TABLE t1(
|
sl@0
|
433 |
a integer primary key,
|
sl@0
|
434 |
b UNIQUE,
|
sl@0
|
435 |
c, d,
|
sl@0
|
436 |
e, f,
|
sl@0
|
437 |
UNIQUE(c,d)
|
sl@0
|
438 |
);
|
sl@0
|
439 |
INSERT INTO t1 VALUES(1,2,3,4,5,6);
|
sl@0
|
440 |
INSERT INTO t1 VALUES(2,3,4,4,6,7);
|
sl@0
|
441 |
SELECT * FROM t1
|
sl@0
|
442 |
}
|
sl@0
|
443 |
} {1 2 3 4 5 6 2 3 4 4 6 7}
|
sl@0
|
444 |
do_test update-10.2 {
|
sl@0
|
445 |
catchsql {
|
sl@0
|
446 |
UPDATE t1 SET a=1, e=9 WHERE f=6;
|
sl@0
|
447 |
SELECT * FROM t1;
|
sl@0
|
448 |
}
|
sl@0
|
449 |
} {0 {1 2 3 4 9 6 2 3 4 4 6 7}}
|
sl@0
|
450 |
do_test update-10.3 {
|
sl@0
|
451 |
catchsql {
|
sl@0
|
452 |
UPDATE t1 SET a=1, e=10 WHERE f=7;
|
sl@0
|
453 |
SELECT * FROM t1;
|
sl@0
|
454 |
}
|
sl@0
|
455 |
} {1 {PRIMARY KEY must be unique}}
|
sl@0
|
456 |
do_test update-10.4 {
|
sl@0
|
457 |
catchsql {
|
sl@0
|
458 |
SELECT * FROM t1;
|
sl@0
|
459 |
}
|
sl@0
|
460 |
} {0 {1 2 3 4 9 6 2 3 4 4 6 7}}
|
sl@0
|
461 |
do_test update-10.5 {
|
sl@0
|
462 |
catchsql {
|
sl@0
|
463 |
UPDATE t1 SET b=2, e=11 WHERE f=6;
|
sl@0
|
464 |
SELECT * FROM t1;
|
sl@0
|
465 |
}
|
sl@0
|
466 |
} {0 {1 2 3 4 11 6 2 3 4 4 6 7}}
|
sl@0
|
467 |
do_test update-10.6 {
|
sl@0
|
468 |
catchsql {
|
sl@0
|
469 |
UPDATE t1 SET b=2, e=12 WHERE f=7;
|
sl@0
|
470 |
SELECT * FROM t1;
|
sl@0
|
471 |
}
|
sl@0
|
472 |
} {1 {column b is not unique}}
|
sl@0
|
473 |
do_test update-10.7 {
|
sl@0
|
474 |
catchsql {
|
sl@0
|
475 |
SELECT * FROM t1;
|
sl@0
|
476 |
}
|
sl@0
|
477 |
} {0 {1 2 3 4 11 6 2 3 4 4 6 7}}
|
sl@0
|
478 |
do_test update-10.8 {
|
sl@0
|
479 |
catchsql {
|
sl@0
|
480 |
UPDATE t1 SET c=3, d=4, e=13 WHERE f=6;
|
sl@0
|
481 |
SELECT * FROM t1;
|
sl@0
|
482 |
}
|
sl@0
|
483 |
} {0 {1 2 3 4 13 6 2 3 4 4 6 7}}
|
sl@0
|
484 |
do_test update-10.9 {
|
sl@0
|
485 |
catchsql {
|
sl@0
|
486 |
UPDATE t1 SET c=3, d=4, e=14 WHERE f=7;
|
sl@0
|
487 |
SELECT * FROM t1;
|
sl@0
|
488 |
}
|
sl@0
|
489 |
} {1 {columns c, d are not unique}}
|
sl@0
|
490 |
do_test update-10.10 {
|
sl@0
|
491 |
catchsql {
|
sl@0
|
492 |
SELECT * FROM t1;
|
sl@0
|
493 |
}
|
sl@0
|
494 |
} {0 {1 2 3 4 13 6 2 3 4 4 6 7}}
|
sl@0
|
495 |
|
sl@0
|
496 |
# Make sure we can handle a subquery in the where clause.
|
sl@0
|
497 |
#
|
sl@0
|
498 |
ifcapable subquery {
|
sl@0
|
499 |
do_test update-11.1 {
|
sl@0
|
500 |
execsql {
|
sl@0
|
501 |
UPDATE t1 SET e=e+1 WHERE b IN (SELECT b FROM t1);
|
sl@0
|
502 |
SELECT b,e FROM t1;
|
sl@0
|
503 |
}
|
sl@0
|
504 |
} {2 14 3 7}
|
sl@0
|
505 |
do_test update-11.2 {
|
sl@0
|
506 |
execsql {
|
sl@0
|
507 |
UPDATE t1 SET e=e+1 WHERE a IN (SELECT a FROM t1);
|
sl@0
|
508 |
SELECT a,e FROM t1;
|
sl@0
|
509 |
}
|
sl@0
|
510 |
} {1 15 2 8}
|
sl@0
|
511 |
}
|
sl@0
|
512 |
|
sl@0
|
513 |
integrity_check update-12.1
|
sl@0
|
514 |
|
sl@0
|
515 |
# Ticket 602. Updates should occur in the same order as the records
|
sl@0
|
516 |
# were discovered in the WHERE clause.
|
sl@0
|
517 |
#
|
sl@0
|
518 |
do_test update-13.1 {
|
sl@0
|
519 |
execsql {
|
sl@0
|
520 |
BEGIN;
|
sl@0
|
521 |
CREATE TABLE t2(a);
|
sl@0
|
522 |
INSERT INTO t2 VALUES(1);
|
sl@0
|
523 |
INSERT INTO t2 VALUES(2);
|
sl@0
|
524 |
INSERT INTO t2 SELECT a+2 FROM t2;
|
sl@0
|
525 |
INSERT INTO t2 SELECT a+4 FROM t2;
|
sl@0
|
526 |
INSERT INTO t2 SELECT a+8 FROM t2;
|
sl@0
|
527 |
INSERT INTO t2 SELECT a+16 FROM t2;
|
sl@0
|
528 |
INSERT INTO t2 SELECT a+32 FROM t2;
|
sl@0
|
529 |
INSERT INTO t2 SELECT a+64 FROM t2;
|
sl@0
|
530 |
INSERT INTO t2 SELECT a+128 FROM t2;
|
sl@0
|
531 |
INSERT INTO t2 SELECT a+256 FROM t2;
|
sl@0
|
532 |
INSERT INTO t2 SELECT a+512 FROM t2;
|
sl@0
|
533 |
INSERT INTO t2 SELECT a+1024 FROM t2;
|
sl@0
|
534 |
COMMIT;
|
sl@0
|
535 |
SELECT count(*) FROM t2;
|
sl@0
|
536 |
}
|
sl@0
|
537 |
} {2048}
|
sl@0
|
538 |
do_test update-13.2 {
|
sl@0
|
539 |
execsql {
|
sl@0
|
540 |
SELECT count(*) FROM t2 WHERE a=rowid;
|
sl@0
|
541 |
}
|
sl@0
|
542 |
} {2048}
|
sl@0
|
543 |
do_test update-13.3 {
|
sl@0
|
544 |
execsql {
|
sl@0
|
545 |
UPDATE t2 SET rowid=rowid-1;
|
sl@0
|
546 |
SELECT count(*) FROM t2 WHERE a=rowid+1;
|
sl@0
|
547 |
}
|
sl@0
|
548 |
} {2048}
|
sl@0
|
549 |
do_test update-13.3 {
|
sl@0
|
550 |
execsql {
|
sl@0
|
551 |
UPDATE t2 SET rowid=rowid+10000;
|
sl@0
|
552 |
UPDATE t2 SET rowid=rowid-9999;
|
sl@0
|
553 |
SELECT count(*) FROM t2 WHERE a=rowid;
|
sl@0
|
554 |
}
|
sl@0
|
555 |
} {2048}
|
sl@0
|
556 |
do_test update-13.4 {
|
sl@0
|
557 |
execsql {
|
sl@0
|
558 |
BEGIN;
|
sl@0
|
559 |
INSERT INTO t2 SELECT a+2048 FROM t2;
|
sl@0
|
560 |
INSERT INTO t2 SELECT a+4096 FROM t2;
|
sl@0
|
561 |
INSERT INTO t2 SELECT a+8192 FROM t2;
|
sl@0
|
562 |
SELECT count(*) FROM t2 WHERE a=rowid;
|
sl@0
|
563 |
COMMIT;
|
sl@0
|
564 |
}
|
sl@0
|
565 |
} 16384
|
sl@0
|
566 |
do_test update-13.5 {
|
sl@0
|
567 |
execsql {
|
sl@0
|
568 |
UPDATE t2 SET rowid=rowid-1;
|
sl@0
|
569 |
SELECT count(*) FROM t2 WHERE a=rowid+1;
|
sl@0
|
570 |
}
|
sl@0
|
571 |
} 16384
|
sl@0
|
572 |
|
sl@0
|
573 |
integrity_check update-13.6
|
sl@0
|
574 |
|
sl@0
|
575 |
ifcapable {trigger} {
|
sl@0
|
576 |
# Test for proper detection of malformed WHEN clauses on UPDATE triggers.
|
sl@0
|
577 |
#
|
sl@0
|
578 |
do_test update-14.1 {
|
sl@0
|
579 |
execsql {
|
sl@0
|
580 |
CREATE TABLE t3(a,b,c);
|
sl@0
|
581 |
CREATE TRIGGER t3r1 BEFORE UPDATE on t3 WHEN nosuchcol BEGIN
|
sl@0
|
582 |
SELECT 'illegal WHEN clause';
|
sl@0
|
583 |
END;
|
sl@0
|
584 |
}
|
sl@0
|
585 |
} {}
|
sl@0
|
586 |
do_test update-14.2 {
|
sl@0
|
587 |
catchsql {
|
sl@0
|
588 |
UPDATE t3 SET a=1;
|
sl@0
|
589 |
}
|
sl@0
|
590 |
} {1 {no such column: nosuchcol}}
|
sl@0
|
591 |
do_test update-14.3 {
|
sl@0
|
592 |
execsql {
|
sl@0
|
593 |
CREATE TABLE t4(a,b,c);
|
sl@0
|
594 |
CREATE TRIGGER t4r1 AFTER UPDATE on t4 WHEN nosuchcol BEGIN
|
sl@0
|
595 |
SELECT 'illegal WHEN clause';
|
sl@0
|
596 |
END;
|
sl@0
|
597 |
}
|
sl@0
|
598 |
} {}
|
sl@0
|
599 |
do_test update-14.4 {
|
sl@0
|
600 |
catchsql {
|
sl@0
|
601 |
UPDATE t4 SET a=1;
|
sl@0
|
602 |
}
|
sl@0
|
603 |
} {1 {no such column: nosuchcol}}
|
sl@0
|
604 |
|
sl@0
|
605 |
} ;# ifcapable {trigger}
|
sl@0
|
606 |
|
sl@0
|
607 |
|
sl@0
|
608 |
finish_test
|