1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
# 2008 October 6
#
# The author disclaims copyright to this source code. In place of
# a legal notice, here is a blessing:
#
# May you do good and not evil.
# May you find forgiveness for yourself and forgive others.
# May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library. The
# focus of this file is testing the LIMIT ... OFFSET ... clause
# of UPDATE and DELETE statements.
#
# $Id: wherelimit.test,v 1.2 2008/10/10 18:25:46 shane Exp $
set testdir [file dirname $argv0]
source $testdir/tester.tcl
proc create_test_data {size} {
# Build some test data
#
execsql {
DROP TABLE IF EXISTS t1;
CREATE TABLE t1(x int, y int);
BEGIN;
}
for {set i 1} {$i<=$size} {incr i} {
for {set j 1} {$j<=$size} {incr j} {
execsql "INSERT INTO t1 VALUES([expr {$i}],[expr {$j}])"
}
}
execsql {
COMMIT;
}
return {}
}
ifcapable {update_delete_limit} {
execsql { CREATE TABLE t1(x, y) }
# check syntax error support
do_test wherelimit-0.1 {
catchsql {DELETE FROM t1 ORDER BY x}
} {1 {ORDER BY without LIMIT on DELETE}}
do_test wherelimit-0.2 {
catchsql {DELETE FROM t1 WHERE x=1 ORDER BY x}
} {1 {ORDER BY without LIMIT on DELETE}}
do_test wherelimit-0.3 {
catchsql {UPDATE t1 SET y=1 WHERE x=1 ORDER BY x}
} {1 {ORDER BY without LIMIT on UPDATE}}
# no AS on table sources
#
# UPDATE: As of version 3.24, AS clauses are allowed as part of
# UPDATE or DELETE statements.
do_test wherelimit-0.4 {
catchsql {DELETE FROM t1 AS a WHERE a.x=1}
} {0 {}}
do_test wherelimit-0.5.1 {
catchsql {UPDATE t1 AS a SET y=1 WHERE x=1}
} {0 {}}
do_test wherelimit-0.5.2 {
catchsql {UPDATE t1 AS a SET y=1 WHERE t1.x=1}
} {1 {no such column: t1.x}}
# OFFSET w/o LIMIT
do_test wherelimit-0.6 {
catchsql {DELETE FROM t1 WHERE x=1 OFFSET 2}
} {1 {near "OFFSET": syntax error}}
do_test wherelimit-0.7 {
catchsql {UPDATE t1 SET y=1 WHERE x=1 OFFSET 2}
} {1 {near "OFFSET": syntax error}}
execsql { DROP TABLE t1 }
# check deletes w/o where clauses but with limit/offsets
create_test_data 5
do_test wherelimit-1.0 {
execsql {SELECT count(*) FROM t1}
} {25}
do_test wherelimit-1.1 {
execsql {DELETE FROM t1}
execsql {SELECT count(*) FROM t1}
} {0}
create_test_data 5
do_test wherelimit-1.2 {
execsql {DELETE FROM t1 LIMIT 5}
execsql {SELECT count(*) FROM t1}
} {20}
do_test wherelimit-1.3 {
# limit 5
execsql {DELETE FROM t1 ORDER BY x LIMIT 5}
execsql {SELECT count(*) FROM t1}
} {15}
create_test_data 4
do_test wherelimit-1.3b {
# limit 5
execsql {DELETE FROM t1 RETURNING x, y, '|' ORDER BY x, y LIMIT 5}
} {1 1 | 1 2 | 1 3 | 1 4 | 2 1 |}
do_test wherelimit-1.3c {
execsql {SELECT count(*) FROM t1}
} {11}
do_test wherelimit-1.4 {
# limit 5, offset 2
execsql {DELETE FROM t1 RETURNING x, y, '|' ORDER BY x LIMIT 5 OFFSET 2}
} {2 4 | 3 1 | 3 2 | 3 3 | 3 4 |}
do_test wherelimit-1.4cnt {
execsql {SELECT count(*) FROM t1}
} {6}
do_test wherelimit-1.5 {
# limit 5, offset -2
execsql {DELETE FROM t1 ORDER BY x LIMIT 5 OFFSET -2}
execsql {SELECT count(*) FROM t1}
} {1}
do_test wherelimit-1.6 {
# limit -5 (no limit), offset 2
execsql {DELETE FROM t1 ORDER BY x LIMIT 2, -5}
execsql {SELECT count(*) FROM t1}
} {1}
do_test wherelimit-1.7 {
# limit 5, offset -2 (no offset)
execsql {DELETE FROM t1 ORDER BY x LIMIT -2, 5}
execsql {SELECT count(*) FROM t1}
} {0}
create_test_data 5
do_test wherelimit-1.8 {
# limit -5 (no limit), offset -2 (no offset)
execsql {DELETE FROM t1 ORDER BY x LIMIT -2, -5}
execsql {SELECT count(*) FROM t1}
} {0}
create_test_data 3
do_test wherelimit-1.9 {
# limit 5, offset 2
execsql {DELETE FROM t1 ORDER BY x LIMIT 2, 5}
execsql {SELECT count(*) FROM t1}
} {4}
do_test wherelimit-1.10 {
# limit 5, offset 5
execsql {DELETE FROM t1 ORDER BY x LIMIT 5 OFFSET 5}
execsql {SELECT count(*) FROM t1}
} {4}
do_test wherelimit-1.11 {
# limit 50, offset 30
execsql {DELETE FROM t1 ORDER BY x LIMIT 50 OFFSET 30}
execsql {SELECT count(*) FROM t1}
} {4}
do_test wherelimit-1.12 {
# limit 50, offset 30
execsql {DELETE FROM t1 ORDER BY x LIMIT 30, 50}
execsql {SELECT count(*) FROM t1}
} {4}
do_test wherelimit-1.13 {
execsql {DELETE FROM t1 ORDER BY x LIMIT 50 OFFSET 50}
execsql {SELECT count(*) FROM t1}
} {4}
create_test_data 6
do_test wherelimit-2.0 {
execsql {SELECT count(*) FROM t1}
} {36}
do_test wherelimit-2.1 {
execsql {DELETE FROM t1 WHERE x=1}
execsql {SELECT count(*) FROM t1}
} {30}
create_test_data 6
do_test wherelimit-2.2 {
execsql {DELETE FROM t1 WHERE x=1 LIMIT 5}
execsql {SELECT count(*) FROM t1}
} {31}
do_test wherelimit-2.3 {
# limit 5
execsql {DELETE FROM t1 WHERE x=1 ORDER BY x LIMIT 5}
execsql {SELECT count(*) FROM t1}
} {30}
do_test wherelimit-2.4 {
# limit 5, offset 2
execsql {DELETE FROM t1 WHERE x=2 ORDER BY x LIMIT 5 OFFSET 2}
execsql {SELECT count(*) FROM t1}
} {26}
do_test wherelimit-2.5 {
# limit 5, offset -2
execsql {DELETE FROM t1 WHERE x=2 ORDER BY x LIMIT 5 OFFSET -2}
execsql {SELECT count(*) FROM t1}
} {24}
do_test wherelimit-2.6 {
# limit -5 (no limit), offset 2
execsql {DELETE FROM t1 WHERE x=3 ORDER BY x LIMIT 2, -5}
execsql {SELECT count(*) FROM t1}
} {20}
do_test wherelimit-2.7 {
# limit 5, offset -2 (no offset)
execsql {DELETE FROM t1 WHERE x=3 ORDER BY x LIMIT -2, 5}
execsql {SELECT count(*) FROM t1}
} {18}
do_test wherelimit-2.8 {
# limit -5 (no limit), offset -2 (no offset)
execsql {DELETE FROM t1 WHERE x=4 ORDER BY x LIMIT -2, -5}
execsql {SELECT count(*) FROM t1}
} {12}
create_test_data 6
do_test wherelimit-2.9 {
# limit 5, offset 2
execsql {DELETE FROM t1 WHERE x=5 ORDER BY x LIMIT 2, 5}
execsql {SELECT count(*) FROM t1}
} {32}
do_test wherelimit-2.10 {
# limit 5, offset 5
execsql {DELETE FROM t1 WHERE x=6 ORDER BY x LIMIT 5 OFFSET 5}
execsql {SELECT count(*) FROM t1}
} {31}
do_test wherelimit-2.11 {
# limit 50, offset 30
execsql {DELETE FROM t1 WHERE x=1 ORDER BY x LIMIT 50 OFFSET 30}
execsql {SELECT count(*) FROM t1}
} {31}
do_test wherelimit-2.12 {
# limit 50, offset 30
execsql {DELETE FROM t1 WHERE x=2 ORDER BY x LIMIT 30, 50}
execsql {SELECT count(*) FROM t1}
} {31}
do_test wherelimit-2.13 {
execsql {DELETE FROM t1 WHERE x=3 ORDER BY x LIMIT 50 OFFSET 50}
execsql {SELECT count(*) FROM t1}
} {31}
create_test_data 6
do_test wherelimit-3.0 {
execsql {SELECT count(*) FROM t1}
} {36}
do_test wherelimit-3.1 {
execsql {UPDATE t1 SET y=1 WHERE x=1}
execsql {SELECT count(*) FROM t1 WHERE y=1}
} {11}
create_test_data 6
do_test wherelimit-3.2 {
execsql {UPDATE t1 SET y=1 WHERE x=1 RETURNING x, y, '|' LIMIT 5}
} {1 1 | 1 1 | 1 1 | 1 1 | 1 1 |}
do_test wherelimit-3.2cnt {
execsql {SELECT count(*) FROM t1 WHERE y=1}
} {10}
do_test wherelimit-3.3 {
# limit 5
execsql {UPDATE t1 SET y=2 WHERE x=2 ORDER BY x LIMIT 5}
execsql {SELECT count(*) FROM t1 WHERE y=2}
} {9}
create_test_data 6
do_test wherelimit-3.4 {
# limit 5, offset 2
execsql {UPDATE t1 SET y=2 WHERE x=2 ORDER BY x LIMIT 5 OFFSET 2}
execsql {SELECT count(*) FROM t1 WHERE y=1}
} {6}
do_test wherelimit-3.5 {
# limit 5, offset -2
execsql {UPDATE t1 SET y=2 WHERE x=2 ORDER BY x LIMIT 5 OFFSET -2}
execsql {SELECT count(*) FROM t1 WHERE y=1}
} {5}
do_test wherelimit-3.6 {
# limit -5 (no limit), offset 2
execsql {UPDATE t1 SET y=3 WHERE x=3 ORDER BY x LIMIT 2, -5}
execsql {SELECT count(*) FROM t1 WHERE y=3}
} {8}
do_test wherelimit-3.7 {
# limit 5, offset -2 (no offset)
execsql {UPDATE t1 SET y=3 WHERE x=3 ORDER BY x LIMIT -2, 5}
execsql {SELECT count(*) FROM t1 WHERE y=3}
} {10}
do_test wherelimit-3.8 {
# limit -5 (no limit), offset -2 (no offset)
execsql {UPDATE t1 SET y=4 WHERE x=4 ORDER BY x LIMIT -2, -5}
execsql {SELECT count(*) FROM t1 WHERE y=4}
} {9}
create_test_data 6
do_test wherelimit-3.9 {
# limit 5, offset 2
execsql {UPDATE t1 SET y=4 WHERE x=5 ORDER BY x LIMIT 2, 5}
execsql {SELECT count(*) FROM t1 WHERE y=4}
} {9}
do_test wherelimit-3.10 {
# limit 5, offset 5
execsql {UPDATE t1 SET y=4 WHERE x=6 ORDER BY x LIMIT 5 OFFSET 5}
execsql {SELECT count(*) FROM t1 WHERE y=1}
} {6}
do_test wherelimit-3.11 {
# limit 50, offset 30
execsql {UPDATE t1 SET y=1 WHERE x=1 ORDER BY x LIMIT 50 OFFSET 30}
execsql {SELECT count(*) FROM t1 WHERE y=1}
} {6}
do_test wherelimit-3.12 {
# limit 50, offset 30
execsql {UPDATE t1 SET y=1 WHERE x=2 ORDER BY x LIMIT 30, 50}
execsql {SELECT count(*) FROM t1 WHERE y=1}
} {6}
do_test wherelimit-3.13 {
execsql {UPDATE t1 SET y=1 WHERE x=3 ORDER BY x LIMIT 50 OFFSET 50}
execsql {SELECT count(*) FROM t1 WHERE y=1}
} {6}
# Cannot use a LIMIT for UPDATE or DELETE against a WITHOUT ROWID table
# or a VIEW. (We should fix this someday).
#
db close
sqlite3 db :memory:
do_execsql_test wherelimit-4.1 {
CREATE TABLE t1(a int);
INSERT INTO t1 VALUES(1);
INSERT INTO t1 VALUES(2);
INSERT INTO t1 VALUES(3);
CREATE TABLE t2(a int);
INSERT INTO t2 SELECT a+100 FROM t1;
CREATE VIEW tv(r,a) AS
SELECT rowid, a FROM t2 UNION ALL SELECT rowid, a FROM t1;
CREATE TRIGGER tv_del INSTEAD OF DELETE ON tv
BEGIN
DELETE FROM t1 WHERE rowid=old.r;
DELETE FROM t2 WHERE rowid=old.r;
END;
} {}
do_catchsql_test wherelimit-4.2 {
DELETE FROM tv WHERE 1 LIMIT 2;
} {0 {}}
do_catchsql_test wherelimit-4.3 {
DELETE FROM tv WHERE 1 ORDER BY a LIMIT 2;
} {0 {}}
do_execsql_test wherelimit-4.10 {
CREATE TABLE t3(a,b,c,d TEXT, PRIMARY KEY(a,b)) WITHOUT ROWID;
INSERT INTO t3(a,b,c,d) VALUES(1,2,3,4),(5,6,7,8),(9,10,11,12);
} {}
do_catchsql_test wherelimit-4.11 {
DELETE FROM t3 WHERE a=5 LIMIT 2;
} {0 {}}
do_execsql_test wherelimit-4.12 {
SELECT a,b,c,d FROM t3 ORDER BY 1;
} {1 2 3 4 9 10 11 12}
}
finish_test
|