/ Check-in [fab3b383]
Login
SQLite training in Houston TX on 2019-11-05 (details)
Part of the 2019 Tcl Conference

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add tests to e_delete.test.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: fab3b383bb2c4764a56811f22ff4c783441918e8
User & Date: dan 2010-09-23 18:47:37
Context
2010-09-24
08:00
Modify testable statement ids in a few test files to account for recent docsrc changes. check-in: 7893e525 user: dan tags: trunk
2010-09-23
18:47
Add tests to e_delete.test. check-in: fab3b383 user: dan tags: trunk
2010-09-22
19:06
Further tests and changes to make the r-tree module more robust. check-in: 7ff3574b user: dan tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to ext/rtree/rtree.c.

2318
2319
2320
2321
2322
2323
2324






2325
2326
2327
2328
2329
2330
2331
2332
2333
2334







2335
2336
2337
2338
2339
2340
2341
2342
  return rc;
}

/*
** If node pLeaf is not the root of the r-tree and its pParent pointer is 
** still NULL, load all ancestor nodes of pLeaf into memory and populate
** the pLeaf->pParent chain all the way up to the root node.






*/
static int fixLeafParent(Rtree *pRtree, RtreeNode *pLeaf){
  int rc = SQLITE_OK;
  RtreeNode *pChild = pLeaf;
  while( rc==SQLITE_OK && pChild->iNode!=1 && pChild->pParent==0 ){
    int rc2 = SQLITE_OK;          /* sqlite3_reset() return code */
    sqlite3_bind_int64(pRtree->pReadParent, 1, pChild->iNode);
    rc = sqlite3_step(pRtree->pReadParent);
    if( rc==SQLITE_ROW ){
      RtreeNode *pTest;







      i64 iNode = sqlite3_column_int64(pRtree->pReadParent, 0);
      for(pTest=pLeaf; pTest && pTest->iNode!=iNode; pTest=pTest->pParent);
      if( !pTest ){
        rc2 = nodeAcquire(pRtree, iNode, 0, &pChild->pParent);
      }
    }
    rc = sqlite3_reset(pRtree->pReadParent);
    if( rc==SQLITE_OK ) rc = rc2;







>
>
>
>
>
>









|
>
>
>
>
>
>
>
|







2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
  return rc;
}

/*
** If node pLeaf is not the root of the r-tree and its pParent pointer is 
** still NULL, load all ancestor nodes of pLeaf into memory and populate
** the pLeaf->pParent chain all the way up to the root node.
**
** This operation is required when a row is deleted (or updated - an update
** is implemented as a delete followed by an insert). SQLite provides the
** rowid of the row to delete, which can be used to find the leaf on which
** the entry resides (argument pLeaf). Once the leaf is located, this 
** function is called to determine its ancestry.
*/
static int fixLeafParent(Rtree *pRtree, RtreeNode *pLeaf){
  int rc = SQLITE_OK;
  RtreeNode *pChild = pLeaf;
  while( rc==SQLITE_OK && pChild->iNode!=1 && pChild->pParent==0 ){
    int rc2 = SQLITE_OK;          /* sqlite3_reset() return code */
    sqlite3_bind_int64(pRtree->pReadParent, 1, pChild->iNode);
    rc = sqlite3_step(pRtree->pReadParent);
    if( rc==SQLITE_ROW ){
      RtreeNode *pTest;           /* Used to test for reference loops */
      i64 iNode;                  /* Node number of parent node */

      /* Before setting pChild->pParent, test that we are not creating a
      ** loop of references (as we would if, say, pChild==pParent). We don't
      ** want to do this as it leads to a memory leak when trying to delete
      ** the referenced counted node structures.
      */
      iNode = sqlite3_column_int64(pRtree->pReadParent, 0);
      for(pTest=pLeaf; pTest && pTest->iNode!=iNode; pTest=pTest->pParent);
      if( !pTest ){
        rc2 = nodeAcquire(pRtree, iNode, 0, &pChild->pParent);
      }
    }
    rc = sqlite3_reset(pRtree->pReadParent);
    if( rc==SQLITE_OK ) rc = rc2;

Changes to ext/rtree/rtreeA.test.

20
21
22
23
24
25
26

27
28
29
30
31
32
33
..
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
...
209
210
211
212
213
214
215

216
217
218
219
ifcapable !rtree { finish_test ; return }

proc create_t1 {} {
  db close
  forcedelete test.db
  sqlite3 db test.db
  execsql {

    CREATE VIRTUAL TABLE t1 USING rtree(id, x1, x2, y1, y2);
  }
}
proc populate_t1 {} {
  execsql BEGIN
  for {set i 0} {$i < 500} {incr i} {
    set x2 [expr $i+5]
................................................................................

  set blob [binary format a*Sua* \
    [string range $blob 0 1] $newvalue [string range $blob 4 end]
  ]
  db eval "UPDATE ${tbl}_node SET data = \$blob WHERE nodeno=$nodeno"
  return [set_entry_count $tbl $nodeno]
}



proc do_corruption_tests {prefix args} {
  set testarray [lindex $args end]
  set errormsg {database disk image is malformed}

  foreach {z value} [lrange $args 0 end-1] {
................................................................................
create_t1
populate_t1
do_execsql_test rtreeA-6.1.0 { 
  UPDATE t1_parent set parentnode = parentnode+1
} {}
do_corruption_tests rtreeA-6.1 {
  1   "DELETE FROM t1 WHERE rowid = 5"

}


finish_test







>







 







<







 







>




20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
..
68
69
70
71
72
73
74

75
76
77
78
79
80
81
...
209
210
211
212
213
214
215
216
217
218
219
220
ifcapable !rtree { finish_test ; return }

proc create_t1 {} {
  db close
  forcedelete test.db
  sqlite3 db test.db
  execsql {
    PRAGMA page_size = 1024;
    CREATE VIRTUAL TABLE t1 USING rtree(id, x1, x2, y1, y2);
  }
}
proc populate_t1 {} {
  execsql BEGIN
  for {set i 0} {$i < 500} {incr i} {
    set x2 [expr $i+5]
................................................................................

  set blob [binary format a*Sua* \
    [string range $blob 0 1] $newvalue [string range $blob 4 end]
  ]
  db eval "UPDATE ${tbl}_node SET data = \$blob WHERE nodeno=$nodeno"
  return [set_entry_count $tbl $nodeno]
}



proc do_corruption_tests {prefix args} {
  set testarray [lindex $args end]
  set errormsg {database disk image is malformed}

  foreach {z value} [lrange $args 0 end-1] {
................................................................................
create_t1
populate_t1
do_execsql_test rtreeA-6.1.0 { 
  UPDATE t1_parent set parentnode = parentnode+1
} {}
do_corruption_tests rtreeA-6.1 {
  1   "DELETE FROM t1 WHERE rowid = 5"
  2   "UPDATE t1 SET x1=x1+1, x2=x2+1"
}


finish_test

Changes to test/e_delete.test.

37
38
39
40
41
42
43
44
45
46

47







































































































































































































































































































































































































































  6  "DELETE FROM main.t1 NOT INDEXED"             {}
  7  "DELETE FROM t1 WHERE a>2"                    {}
  8  "DELETE FROM t1 INDEXED BY i1 WHERE a>2"      {}
  9  "DELETE FROM t1 NOT INDEXED WHERE a>2"        {}
  10 "DELETE FROM main.t1 WHERE a>2"               {}
  11 "DELETE FROM main.t1 INDEXED BY i1 WHERE a>2" {}
  12 "DELETE FROM main.t1 NOT INDEXED WHERE a>2"   {}
}
 
finish_test

















































































































































































































































































































































































































































|
|
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
  6  "DELETE FROM main.t1 NOT INDEXED"             {}
  7  "DELETE FROM t1 WHERE a>2"                    {}
  8  "DELETE FROM t1 INDEXED BY i1 WHERE a>2"      {}
  9  "DELETE FROM t1 NOT INDEXED WHERE a>2"        {}
  10 "DELETE FROM main.t1 WHERE a>2"               {}
  11 "DELETE FROM main.t1 INDEXED BY i1 WHERE a>2" {}
  12 "DELETE FROM main.t1 NOT INDEXED WHERE a>2"   {}
}

# EVIDENCE-OF: R-20205-17349 If the WHERE clause is not present, all
# records in the table are deleted.
#
drop_all_tables
do_test e_delete-1.0 {
  db transaction {
    foreach t {t1 t2 t3 t4 t5 t6} {
      execsql [string map [list %T% $t] {
        CREATE TABLE %T%(x, y);
        INSERT INTO %T% VALUES(1, 'one');
        INSERT INTO %T% VALUES(2, 'two');
        INSERT INTO %T% VALUES(3, 'three');
        INSERT INTO %T% VALUES(4, 'four');
        INSERT INTO %T% VALUES(5, 'five');
      }]
    }
  }
} {}
do_delete_tests e_delete-1.1 {
  1  "DELETE FROM t1       ; SELECT * FROM t1"       {}
  2  "DELETE FROM main.t2  ; SELECT * FROM t2"       {}
}

# EVIDENCE-OF: R-25092-63878 If a WHERE clause is supplied, then only
# those rows for which evaluating the WHERE clause and casting the
# result to a NUMERIC value produces a result other than NULL or zero
# (integer value 0 or real value 0.0).
#
do_delete_tests e_delete-1.2 {
  1  "DELETE FROM t3 WHERE 1       ; SELECT x FROM t3"       {}
  2  "DELETE FROM main.t4 WHERE 0  ; SELECT x FROM t4"       {1 2 3 4 5}
  3  "DELETE FROM t4 WHERE 0.0     ; SELECT x FROM t4"       {1 2 3 4 5}
  4  "DELETE FROM t4 WHERE NULL    ; SELECT x FROM t4"       {1 2 3 4 5}
  5  "DELETE FROM t4 WHERE y!='two'; SELECT x FROM t4"       {2}
  6  "DELETE FROM t4 WHERE y='two' ; SELECT x FROM t4"       {}
  7  "DELETE FROM t5 WHERE x=(SELECT max(x) FROM t5);SELECT x FROM t5" {1 2 3 4}
  8  "DELETE FROM t5 WHERE (SELECT max(x) FROM t4)  ;SELECT x FROM t5" {1 2 3 4}
  9  "DELETE FROM t5 WHERE (SELECT max(x) FROM t6)  ;SELECT x FROM t5" {}
  10 "DELETE FROM t6 WHERE y>'seven' ; SELECT y FROM t6"     {one four five}
}


#-------------------------------------------------------------------------
# Tests for restrictions on DELETE statements that appear within trigger
# programs.
#
forcedelete test.db2
forcedelete test.db3
do_execsql_test e_delete-2.0 {
  ATTACH 'test.db2' AS aux;
  ATTACH 'test.db3' AS aux2;

  CREATE TABLE temp.t7(a, b);   INSERT INTO temp.t7 VALUES(1, 2);
  CREATE TABLE main.t7(a, b);   INSERT INTO main.t7 VALUES(3, 4);
  CREATE TABLE aux.t7(a, b);    INSERT INTO aux.t7 VALUES(5, 6);
  CREATE TABLE aux2.t7(a, b);   INSERT INTO aux2.t7 VALUES(7, 8);

  CREATE TABLE main.t8(a, b);   INSERT INTO main.t8 VALUES(1, 2);
  CREATE TABLE aux.t8(a, b);    INSERT INTO aux.t8 VALUES(3, 4);
  CREATE TABLE aux2.t8(a, b);   INSERT INTO aux2.t8 VALUES(5, 6);

  CREATE TABLE aux.t9(a, b);    INSERT INTO aux.t9 VALUES(1, 2);
  CREATE TABLE aux2.t9(a, b);   INSERT INTO aux2.t9 VALUES(3, 4);

  CREATE TABLE aux2.t10(a, b);  INSERT INTO aux2.t10 VALUES(1, 2);
} {}


# EVIDENCE-OF: R-09681-58560 The table-name specified as part of a
# DELETE statement within a trigger body must be unqualified.
#
# EVIDENCE-OF: R-36771-43788 In other words, the database-name. prefix
# on the table name is not allowed within triggers.
#
do_delete_tests e_delete-2.1 -error {
  qualified table names are not allowed on INSERT, UPDATE, and DELETE statements within triggers
} {
  1 {
      CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN
        DELETE FROM main.t2;
      END;
  } {}

  2 {
      CREATE TRIGGER tr1 BEFORE UPDATE ON t2 BEGIN
        DELETE FROM temp.t7 WHERE a=new.a;
      END;
  } {}

  3 {
      CREATE TRIGGER tr1 AFTER UPDATE ON t8 BEGIN
        DELETE FROM aux2.t8 WHERE b!=a;
      END;
  } {}
}

# EVIDENCE-OF: R-28818-63526 If the table to which the trigger is
# attached is not in the temp database, then DELETE statements within
# the trigger body must operate on tables within the same database as
# it.
# 
#   This is tested in two parts. First, check that if a table of the
#   specified name does not exist, an error is raised. Secondly, test
#   that if tables with the specified name exist in multiple databases,
#   the local database table is used.
#
do_delete_tests e_delete-2.2.1 -error { no such table: %s } {
  1 {
      CREATE TRIGGER main.tr1 AFTER INSERT ON main.t7 BEGIN
        DELETE FROM t9;
      END;
      INSERT INTO main.t7 VALUES(1, 2);
  } {main.t9}

  2 {
      CREATE TRIGGER aux.tr2 BEFORE UPDATE ON t9 BEGIN
        DELETE FROM t10;
      END;
      UPDATE t9 SET a=1;
  } {aux.t10}
}
do_execsql_test e_delete-2.2.X {
  DROP TRIGGER main.tr1;
  DROP TRIGGER aux.tr2;
} {}

do_delete_tests e_delete-2.2.2 {
  1 {
      CREATE TRIGGER aux.tr1 AFTER INSERT ON t8 BEGIN
        DELETE FROM t9;
      END;
      INSERT INTO aux.t8 VALUES(1, 2);

      SELECT count(*) FROM aux.t9 
        UNION ALL
      SELECT count(*) FROM aux2.t9;
  } {0 1}

  2 {
      CREATE TRIGGER main.tr1 AFTER INSERT ON t8 BEGIN
        DELETE FROM t7;
      END;
      INSERT INTO main.t8 VALUES(1, 2);

      SELECT count(*) FROM temp.t7 
        UNION ALL
      SELECT count(*) FROM main.t7
        UNION ALL
      SELECT count(*) FROM aux.t7
        UNION ALL
      SELECT count(*) FROM aux2.t7;
  } {1 0 1 1}
}

# EVIDENCE-OF: R-31567-38587 If the table to which the trigger is
# attached is in the TEMP database, then the unqualified name of the
# table being deleted is resolved in the same way as it is for a
# top-level statement (by searching first the TEMP database, then the
# main database, then any other databases in the order they were
# attached).
#
do_execsql_test e_delete-2.3.0 {
  DROP TRIGGER aux.tr1;
  DROP TRIGGER main.tr1;
  DELETE FROM main.t8 WHERE oid>1;
  DELETE FROM aux.t8 WHERE oid>1;
  INSERT INTO aux.t9 VALUES(1, 2);
  INSERT INTO main.t7 VALUES(3, 4);
} {}
do_execsql_test e_delete-2.3.1 {
  SELECT count(*) FROM temp.t7 UNION ALL SELECT count(*) FROM main.t7 UNION ALL
  SELECT count(*) FROM aux.t7  UNION ALL SELECT count(*) FROM aux2.t7;

  SELECT count(*) FROM main.t8 UNION ALL SELECT count(*) FROM aux.t8  
  UNION ALL SELECT count(*) FROM aux2.t8;

  SELECT count(*) FROM aux.t9  UNION ALL SELECT count(*) FROM aux2.t9;

  SELECT count(*) FROM aux2.t10;
} {1 1 1 1 1 1 1 1 1 1}
do_execsql_test e_delete-2.3.2 {
  CREATE TRIGGER temp.tr1 AFTER INSERT ON t7 BEGIN
    DELETE FROM t7;
    DELETE FROM t8;
    DELETE FROM t9;
    DELETE FROM t10;
  END;
  INSERT INTO temp.t7 VALUES('hello', 'world');
} {}
do_execsql_test e_delete-2.3.3 {
  SELECT count(*) FROM temp.t7 UNION ALL SELECT count(*) FROM main.t7 UNION ALL
  SELECT count(*) FROM aux.t7  UNION ALL SELECT count(*) FROM aux2.t7;

  SELECT count(*) FROM main.t8 UNION ALL SELECT count(*) FROM aux.t8  
  UNION ALL SELECT count(*) FROM aux2.t8;

  SELECT count(*) FROM aux.t9  UNION ALL SELECT count(*) FROM aux2.t9;

  SELECT count(*) FROM aux2.t10;
} {0 1 1 1 0 1 1 0 1 0}

# EVIDENCE-OF: R-28691-49464 The INDEXED BY and NOT INDEXED clauses are
# not allowed on DELETE statements within triggers.
#
do_execsql_test e_delete-2.4.0 {
  CREATE INDEX i8 ON t8(a, b);
} {}
do_delete_tests e_delete-2.4 -error {
  the %s %s clause is not allowed on UPDATE or DELETE statements within triggers
} {
  1 {
    CREATE TRIGGER tr3 AFTER INSERT ON t8 BEGIN
      DELETE FROM t8 INDEXED BY i8 WHERE a=5;
    END;
  } {INDEXED BY}
  2 {
    CREATE TRIGGER tr3 AFTER INSERT ON t8 BEGIN
      DELETE FROM t8 NOT INDEXED WHERE a=5;
    END;
  } {NOT INDEXED}
}

ifcapable update_delete_limit {

# EVIDENCE-OF: R-64942-06615 The LIMIT and ORDER BY clauses (described
# below) are unsupported for DELETE statements within triggers.
#
do_delete_tests e_delete-2.5 -error { near "%s": syntax error } {
  1 {
    CREATE TRIGGER tr3 AFTER INSERT ON t8 BEGIN
      DELETE FROM t8 LIMIT 10;
    END;
  } {LIMIT}
  2 {
    CREATE TRIGGER tr3 AFTER INSERT ON t8 BEGIN
      DELETE FROM t8 ORDER BY a LIMIT 5;
    END;
  } {ORDER}
}

# EVIDENCE-OF: R-40026-10531 If SQLite is compiled with the
# SQLITE_ENABLE_UPDATE_DELETE_LIMIT compile-time option, then the syntax
# of the DELETE statement is extended by the addition of optional ORDER
# BY and LIMIT clauses:
#
# EVIDENCE-OF: R-49959-20251 -- syntax diagram delete-stmt-limited
#
do_delete_tests e_delete-3.1 {
  1   "DELETE FROM t1 LIMIT 5"                                    {}
  2   "DELETE FROM t1 LIMIT 5-1 OFFSET 2+2"                       {}
  3   "DELETE FROM t1 LIMIT 2+2, 16/4"                            {}
  4   "DELETE FROM t1 ORDER BY x LIMIT 5"                         {}
  5   "DELETE FROM t1 ORDER BY x LIMIT 5-1 OFFSET 2+2"            {}
  6   "DELETE FROM t1 ORDER BY x LIMIT 2+2, 16/4"                 {}
  7   "DELETE FROM t1 WHERE x>2 LIMIT 5"                          {}
  8   "DELETE FROM t1 WHERE x>2 LIMIT 5-1 OFFSET 2+2"             {}
  9   "DELETE FROM t1 WHERE x>2 LIMIT 2+2, 16/4"                  {}
  10  "DELETE FROM t1 WHERE x>2 ORDER BY x LIMIT 5"               {}
  11  "DELETE FROM t1 WHERE x>2 ORDER BY x LIMIT 5-1 OFFSET 2+2"  {}
  12  "DELETE FROM t1 WHERE x>2 ORDER BY x LIMIT 2+2, 16/4"       {}
}

drop_all_tables
proc rebuild_t1 {} {
  catchsql { DROP TABLE t1 }
  execsql {
    CREATE TABLE t1(a, b);
    INSERT INTO t1 VALUES(1, 'one');
    INSERT INTO t1 VALUES(2, 'two');
    INSERT INTO t1 VALUES(3, 'three');
    INSERT INTO t1 VALUES(4, 'four');
    INSERT INTO t1 VALUES(5, 'five');
  }
}

# EVIDENCE-OF: R-44062-08550 If a DELETE statement has a LIMIT clause,
# the maximum number of rows that will be deleted is found by evaluating
# the accompanying expression and casting it to an integer value.
#
rebuild_t1
do_delete_tests e_delete-3.2 -repair rebuild_t1 -query {
  SELECT a FROM t1
} {
  1   "DELETE FROM t1 LIMIT 3"       {4 5}
  2   "DELETE FROM t1 LIMIT 1+1"     {3 4 5}
  3   "DELETE FROM t1 LIMIT '4'"     {5}
  4   "DELETE FROM t1 LIMIT '1.0'"   {2 3 4 5}
}

# EVIDENCE-OF: R-02661-56399 If the result of the evaluating the LIMIT
# clause cannot be losslessly converted to an integer value, it is an
# error.
#
do_delete_tests e_delete-3.3 -error { datatype mismatch } {
  1   "DELETE FROM t1 LIMIT 'abc'"   {}
  2   "DELETE FROM t1 LIMIT NULL"    {}
  3   "DELETE FROM t1 LIMIT X'ABCD'" {}
  4   "DELETE FROM t1 LIMIT 1.2"     {}
}

# EVIDENCE-OF: R-00598-03741 A negative LIMIT value is interpreted as
# "no limit".
#
do_delete_tests e_delete-3.4 -repair rebuild_t1 -query {
  SELECT a FROM t1
} {
  1   "DELETE FROM t1 LIMIT -1"       {}
  2   "DELETE FROM t1 LIMIT 2-4"      {}
  3   "DELETE FROM t1 LIMIT -4.0"     {}
  4   "DELETE FROM t1 LIMIT 5*-1"     {}
}

# EVIDENCE-OF: R-26377-49195 If the DELETE statement also has an OFFSET
# clause, then it is similarly evaluated and cast to an integer value.
# Again, it is an error if the value cannot be losslessly converted to
# an integer.
#
do_delete_tests e_delete-3.5 -error { datatype mismatch } {
  1   "DELETE FROM t1 LIMIT 1 OFFSET 'abc'"   {}
  2   "DELETE FROM t1 LIMIT 1 OFFSET NULL"    {}
  3   "DELETE FROM t1 LIMIT 1 OFFSET X'ABCD'" {}
  4   "DELETE FROM t1 LIMIT 1 OFFSET 1.2"     {}
  5   "DELETE FROM t1 LIMIT 'abc', 1"         {}
  6   "DELETE FROM t1 LIMIT NULL, 1"          {}
  7   "DELETE FROM t1 LIMIT X'ABCD', 1"       {}
  8   "DELETE FROM t1 LIMIT 1.2, 1"           {}
}


# EVIDENCE-OF: R-64004-53814 If there is no OFFSET clause, or the
# calculated integer value is negative, the effective OFFSET value is
# zero.
#
do_delete_tests e_delete-3.6 -repair rebuild_t1 -query {
  SELECT a FROM t1
} {
  1a  "DELETE FROM t1 LIMIT 3 OFFSET 0"        {4 5}
  1b  "DELETE FROM t1 LIMIT 3"                 {4 5}
  1c  "DELETE FROM t1 LIMIT 3 OFFSET -1"       {4 5}
  2a  "DELETE FROM t1 LIMIT 1+1 OFFSET 0"      {3 4 5}
  2b  "DELETE FROM t1 LIMIT 1+1"               {3 4 5}
  2c  "DELETE FROM t1 LIMIT 1+1 OFFSET 2-5"    {3 4 5}
  3a  "DELETE FROM t1 LIMIT '4' OFFSET 0"      {5}
  3b  "DELETE FROM t1 LIMIT '4'"               {5}
  3c  "DELETE FROM t1 LIMIT '4' OFFSET -1.0"   {5}
  4a  "DELETE FROM t1 LIMIT '1.0' OFFSET 0"    {2 3 4 5}
  4b  "DELETE FROM t1 LIMIT '1.0'"             {2 3 4 5}
  4c  "DELETE FROM t1 LIMIT '1.0' OFFSET -11"  {2 3 4 5}
}

# EVIDENCE-OF: R-48141-52334 If the DELETE statement has an ORDER BY
# clause, then all rows that would be deleted in the absence of the
# LIMIT clause are sorted according to the ORDER BY. The first M rows,
# where M is the value found by evaluating the OFFSET clause expression,
# are skipped, and the following N, where N is the value of the LIMIT
# expression, are deleted.
#
do_delete_tests e_delete-3.7 -repair rebuild_t1 -query {
  SELECT a FROM t1
} {
  1   "DELETE FROM t1 ORDER BY b LIMIT 2"               {1 2 3}
  2   "DELETE FROM t1 ORDER BY length(b), a LIMIT 3"    {3 5}
  3   "DELETE FROM t1 ORDER BY a DESC LIMIT 1 OFFSET 0"  {1 2 3 4}
  4   "DELETE FROM t1 ORDER BY a DESC LIMIT 1 OFFSET 1"  {1 2 3 5}
  5   "DELETE FROM t1 ORDER BY a DESC LIMIT 1 OFFSET 2"  {1 2 4 5}
}

# EVIDENCE-OF: R-64535-08414 If there are less than N rows remaining
# after taking the OFFSET clause into account, or if the LIMIT clause
# evaluated to a negative value, then all remaining rows are deleted.
#
do_delete_tests e_delete-3.8 -repair rebuild_t1 -query {
  SELECT a FROM t1
} {
  1   "DELETE FROM t1 ORDER BY a ASC LIMIT 10"           {}
  2   "DELETE FROM t1 ORDER BY a ASC LIMIT -1"           {}
  3   "DELETE FROM t1 ORDER BY a ASC LIMIT 4 OFFSET 2"   {1 2}
}

# EVIDENCE-OF: R-37284-06965 If the DELETE statement has no ORDER BY
# clause, then all rows that would be deleted in the absence of the
# LIMIT clause are assembled in an arbitrary order before applying the
# LIMIT and OFFSET clauses to determine the subset that are actually
# deleted.
#
#     In practice, the "arbitrary order" is rowid order.
#
do_delete_tests e_delete-3.9 -repair rebuild_t1 -query {
  SELECT a FROM t1
} {
  1   "DELETE FROM t1 LIMIT 2"               {3 4 5}
  2   "DELETE FROM t1 LIMIT 3"               {4 5}
  3   "DELETE FROM t1 LIMIT 1 OFFSET 0"      {2 3 4 5}
  4   "DELETE FROM t1 LIMIT 1 OFFSET 1"      {1 3 4 5}
  5   "DELETE FROM t1 LIMIT 1 OFFSET 2"      {1 2 4 5}
}


# EVIDENCE-OF: R-26627-30313 The ORDER BY clause on an DELETE statement
# is used only to determine which rows fall within the LIMIT. The order
# in which rows are deleted is arbitrary and is not influenced by the
# ORDER BY clause.
#
#     In practice, rows are always deleted in rowid order.
#
do_delete_tests e_delete-3.10 -repair {
  rebuild_t1 
  catchsql { DROP TABLE t1log }
  execsql {
    CREATE TABLE t1log(x);
    CREATE TRIGGER tr1 AFTER DELETE ON t1 BEGIN
      INSERT INTO t1log VALUES(old.a);
    END;
  }
} -query {
  SELECT x FROM t1log
} {
  1   "DELETE FROM t1 ORDER BY a DESC LIMIT 2"   {4 5}
  2   "DELETE FROM t1 ORDER BY a DESC LIMIT -1"  {1 2 3 4 5}
  3   "DELETE FROM t1 ORDER BY a ASC LIMIT 2"    {1 2}
  4   "DELETE FROM t1 ORDER BY a ASC LIMIT -1"   {1 2 3 4 5}
}

}
 
finish_test

Changes to test/tester.tcl.

347
348
349
350
351
352
353

354
355
356
357
358
359
360
361
362

363
364
365
366
367
368
369
370


371
372
373
374
375
376
377
...
379
380
381
382
383
384
385

386
387
388
389
390
391
392
...
395
396
397
398
399
400
401

402

403
404
405
406
407
408
409
#   Usage: do_select_tests PREFIX ?SWITCHES? TESTLIST
#
# Where switches are:
#
#   -errorformat FMTSTRING
#   -count
#   -query SQL

#
proc do_select_tests {prefix args} {

  set testlist [lindex $args end]
  set switches [lrange $args 0 end-1]

  set errfmt ""
  set countonly 0
  set query ""


  for {set i 0} {$i < [llength $switches]} {incr i} {
    set s [lindex $switches $i]
    set n [string length $s]
    if {$n>=2 && [string equal -length $n $s "-query"]} {
      set query [lindex $switches [incr i]]
    } elseif {$n>=2 && [string equal -length $n $s "-errorformat"]} {
      set errfmt [lindex $switches [incr i]]


    } elseif {$n>=2 && [string equal -length $n $s "-count"]} {
      set countonly 1
    } else {
      error "unknown switch: $s"
    }
  }

................................................................................
    error "Cannot use -count and -errorformat together"
  }
  set nTestlist [llength $testlist]
  if {$nTestlist%3 || $nTestlist==0 } {
    error "SELECT test list contains [llength $testlist] elements"
  }


  foreach {tn sql res} $testlist {
    if {$query != ""} {
      execsql $sql
      set sql $query
    }

    if {$countonly} {
................................................................................
      uplevel do_test ${prefix}.$tn [list [list set {} $nRow]] [list $res]
    } elseif {$errfmt==""} {
      uplevel do_execsql_test ${prefix}.${tn} [list $sql] [list [list {*}$res]]
    } else {
      set res [list 1 [string trim [format $errfmt {*}$res]]]
      uplevel do_catchsql_test ${prefix}.${tn} [list $sql] [list $res]
    }

  }

}

proc delete_all_data {} {
  db eval {SELECT tbl_name AS t FROM sqlite_master WHERE type = 'table'} {
    db eval "DELETE FROM '[string map {' ''} $t]'"
  }
}







>









>








>
>







 







>







 







>

>







347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
...
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
...
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#   Usage: do_select_tests PREFIX ?SWITCHES? TESTLIST
#
# Where switches are:
#
#   -errorformat FMTSTRING
#   -count
#   -query SQL
#   -repair TCL
#
proc do_select_tests {prefix args} {

  set testlist [lindex $args end]
  set switches [lrange $args 0 end-1]

  set errfmt ""
  set countonly 0
  set query ""
  set repair ""

  for {set i 0} {$i < [llength $switches]} {incr i} {
    set s [lindex $switches $i]
    set n [string length $s]
    if {$n>=2 && [string equal -length $n $s "-query"]} {
      set query [lindex $switches [incr i]]
    } elseif {$n>=2 && [string equal -length $n $s "-errorformat"]} {
      set errfmt [lindex $switches [incr i]]
    } elseif {$n>=2 && [string equal -length $n $s "-repair"]} {
      set repair [lindex $switches [incr i]]
    } elseif {$n>=2 && [string equal -length $n $s "-count"]} {
      set countonly 1
    } else {
      error "unknown switch: $s"
    }
  }

................................................................................
    error "Cannot use -count and -errorformat together"
  }
  set nTestlist [llength $testlist]
  if {$nTestlist%3 || $nTestlist==0 } {
    error "SELECT test list contains [llength $testlist] elements"
  }

  eval $repair
  foreach {tn sql res} $testlist {
    if {$query != ""} {
      execsql $sql
      set sql $query
    }

    if {$countonly} {
................................................................................
      uplevel do_test ${prefix}.$tn [list [list set {} $nRow]] [list $res]
    } elseif {$errfmt==""} {
      uplevel do_execsql_test ${prefix}.${tn} [list $sql] [list [list {*}$res]]
    } else {
      set res [list 1 [string trim [format $errfmt {*}$res]]]
      uplevel do_catchsql_test ${prefix}.${tn} [list $sql] [list $res]
    }
    eval $repair
  }

}

proc delete_all_data {} {
  db eval {SELECT tbl_name AS t FROM sqlite_master WHERE type = 'table'} {
    db eval "DELETE FROM '[string map {' ''} $t]'"
  }
}