1b19a2bc6Sdrh# 2001 September 15 262c68191Sdrh# 3b19a2bc6Sdrh# The author disclaims copyright to this source code. In place of 4b19a2bc6Sdrh# a legal notice, here is a blessing: 562c68191Sdrh# 6b19a2bc6Sdrh# May you do good and not evil. 7b19a2bc6Sdrh# May you find forgiveness for yourself and forgive others. 8b19a2bc6Sdrh# May you share freely, never taking more than you give. 962c68191Sdrh# 1062c68191Sdrh#*********************************************************************** 1162c68191Sdrh# This file implements regression tests for SQLite library. The 1262c68191Sdrh# focus of this file is testing the DELETE FROM statement. 1362c68191Sdrh# 14*382e28faSdanielk1977# $Id: delete.test,v 1.22 2007/10/05 15:53:29 danielk1977 Exp $ 1562c68191Sdrh 1662c68191Sdrhset testdir [file dirname $argv0] 1762c68191Sdrhsource $testdir/tester.tcl 1862c68191Sdrh 1962c68191Sdrh# Try to delete from a non-existant table. 2062c68191Sdrh# 2162c68191Sdrhdo_test delete-1.1 { 2262c68191Sdrh set v [catch {execsql {DELETE FROM test1}} msg] 2362c68191Sdrh lappend v $msg 2462c68191Sdrh} {1 {no such table: test1}} 2562c68191Sdrh 2662c68191Sdrh# Try to delete from sqlite_master 2762c68191Sdrh# 2862c68191Sdrhdo_test delete-2.1 { 2962c68191Sdrh set v [catch {execsql {DELETE FROM sqlite_master}} msg] 3062c68191Sdrh lappend v $msg 311d37e284Sdrh} {1 {table sqlite_master may not be modified}} 3262c68191Sdrh 337020f651Sdrh# Delete selected entries from a table with and without an index. 347020f651Sdrh# 351bee3d7bSdrhdo_test delete-3.1.1 { 367020f651Sdrh execsql {CREATE TABLE table1(f1 int, f2 int)} 377020f651Sdrh execsql {INSERT INTO table1 VALUES(1,2)} 387020f651Sdrh execsql {INSERT INTO table1 VALUES(2,4)} 397020f651Sdrh execsql {INSERT INTO table1 VALUES(3,8)} 407020f651Sdrh execsql {INSERT INTO table1 VALUES(4,16)} 417020f651Sdrh execsql {SELECT * FROM table1 ORDER BY f1} 427020f651Sdrh} {1 2 2 4 3 8 4 16} 431bee3d7bSdrhdo_test delete-3.1.2 { 447020f651Sdrh execsql {DELETE FROM table1 WHERE f1=3} 451bee3d7bSdrh} {} 461bee3d7bSdrhdo_test delete-3.1.3 { 477020f651Sdrh execsql {SELECT * FROM table1 ORDER BY f1} 487020f651Sdrh} {1 2 2 4 4 16} 491bee3d7bSdrhdo_test delete-3.1.4 { 507020f651Sdrh execsql {CREATE INDEX index1 ON table1(f1)} 511bee3d7bSdrh execsql {PRAGMA count_changes=on} 52cc43cabcSdrh ifcapable explain { 53cc43cabcSdrh execsql {EXPLAIN DELETE FROM table1 WHERE f1=3} 54cc43cabcSdrh } 554cfa7934Sdrh execsql {DELETE FROM 'table1' WHERE f1=3} 561bee3d7bSdrh} {0} 571bee3d7bSdrhdo_test delete-3.1.5 { 587020f651Sdrh execsql {SELECT * FROM table1 ORDER BY f1} 597020f651Sdrh} {1 2 2 4 4 16} 604ebfef14Sdrhdo_test delete-3.1.6.1 { 617020f651Sdrh execsql {DELETE FROM table1 WHERE f1=2} 621bee3d7bSdrh} {1} 634ebfef14Sdrhdo_test delete-3.1.6.2 { 644ebfef14Sdrh db changes 654ebfef14Sdrh} 1 661bee3d7bSdrhdo_test delete-3.1.7 { 677020f651Sdrh execsql {SELECT * FROM table1 ORDER BY f1} 687020f651Sdrh} {1 2 4 16} 69ed717fe3Sdrhintegrity_check delete-3.2 70ed717fe3Sdrh 7162c68191Sdrh 728be51133Sdrh# Semantic errors in the WHERE clause 738be51133Sdrh# 748be51133Sdrhdo_test delete-4.1 { 758be51133Sdrh execsql {CREATE TABLE table2(f1 int, f2 int)} 768be51133Sdrh set v [catch {execsql {DELETE FROM table2 WHERE f3=5}} msg] 778be51133Sdrh lappend v $msg 78967e8b73Sdrh} {1 {no such column: f3}} 798be51133Sdrh 808be51133Sdrhdo_test delete-4.2 { 818be51133Sdrh set v [catch {execsql {DELETE FROM table2 WHERE xyzzy(f1+4)}} msg] 828be51133Sdrh lappend v $msg 838be51133Sdrh} {1 {no such function: xyzzy}} 84ed717fe3Sdrhintegrity_check delete-4.3 8562c68191Sdrh 863494ffe9Sdrh# Lots of deletes 873494ffe9Sdrh# 881bee3d7bSdrhdo_test delete-5.1.1 { 893494ffe9Sdrh execsql {DELETE FROM table1} 901bee3d7bSdrh} {2} 911bee3d7bSdrhdo_test delete-5.1.2 { 923494ffe9Sdrh execsql {SELECT count(*) FROM table1} 931bee3d7bSdrh} {0} 941bee3d7bSdrhdo_test delete-5.2.1 { 951bee3d7bSdrh execsql {BEGIN TRANSACTION} 963494ffe9Sdrh for {set i 1} {$i<=200} {incr i} { 973494ffe9Sdrh execsql "INSERT INTO table1 VALUES($i,[expr {$i*$i}])" 983494ffe9Sdrh } 991bee3d7bSdrh execsql {COMMIT} 1001bee3d7bSdrh execsql {SELECT count(*) FROM table1} 1011bee3d7bSdrh} {200} 1021bee3d7bSdrhdo_test delete-5.2.2 { 1031bee3d7bSdrh execsql {DELETE FROM table1} 1041bee3d7bSdrh} {200} 1051bee3d7bSdrhdo_test delete-5.2.3 { 1061bee3d7bSdrh execsql {BEGIN TRANSACTION} 1071bee3d7bSdrh for {set i 1} {$i<=200} {incr i} { 1081bee3d7bSdrh execsql "INSERT INTO table1 VALUES($i,[expr {$i*$i}])" 1091bee3d7bSdrh } 1101bee3d7bSdrh execsql {COMMIT} 1111bee3d7bSdrh execsql {SELECT count(*) FROM table1} 1121bee3d7bSdrh} {200} 1131bee3d7bSdrhdo_test delete-5.2.4 { 1141bee3d7bSdrh execsql {PRAGMA count_changes=off} 1151bee3d7bSdrh execsql {DELETE FROM table1} 1161bee3d7bSdrh} {} 1171bee3d7bSdrhdo_test delete-5.2.5 { 1181bee3d7bSdrh execsql {SELECT count(*) FROM table1} 1191bee3d7bSdrh} {0} 1201bee3d7bSdrhdo_test delete-5.2.6 { 1211bee3d7bSdrh execsql {BEGIN TRANSACTION} 1221bee3d7bSdrh for {set i 1} {$i<=200} {incr i} { 1231bee3d7bSdrh execsql "INSERT INTO table1 VALUES($i,[expr {$i*$i}])" 1241bee3d7bSdrh } 1251bee3d7bSdrh execsql {COMMIT} 1263494ffe9Sdrh execsql {SELECT count(*) FROM table1} 1273494ffe9Sdrh} {200} 1283494ffe9Sdrhdo_test delete-5.3 { 1293494ffe9Sdrh for {set i 1} {$i<=200} {incr i 4} { 1303494ffe9Sdrh execsql "DELETE FROM table1 WHERE f1==$i" 1313494ffe9Sdrh } 1323494ffe9Sdrh execsql {SELECT count(*) FROM table1} 1333494ffe9Sdrh} {150} 1344ebfef14Sdrhdo_test delete-5.4.1 { 1353494ffe9Sdrh execsql "DELETE FROM table1 WHERE f1>50" 1364ebfef14Sdrh db changes 1374ebfef14Sdrh} [db one {SELECT count(*) FROM table1 WHERE f1>50}] 1384ebfef14Sdrhdo_test delete-5.4.2 { 1393494ffe9Sdrh execsql {SELECT count(*) FROM table1} 1403494ffe9Sdrh} {37} 1413494ffe9Sdrhdo_test delete-5.5 { 1423494ffe9Sdrh for {set i 1} {$i<=70} {incr i 3} { 1433494ffe9Sdrh execsql "DELETE FROM table1 WHERE f1==$i" 1443494ffe9Sdrh } 1453494ffe9Sdrh execsql {SELECT f1 FROM table1 ORDER BY f1} 1463494ffe9Sdrh} {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} 1473494ffe9Sdrhdo_test delete-5.6 { 1483494ffe9Sdrh for {set i 1} {$i<40} {incr i} { 1493494ffe9Sdrh execsql "DELETE FROM table1 WHERE f1==$i" 1503494ffe9Sdrh } 1513494ffe9Sdrh execsql {SELECT f1 FROM table1 ORDER BY f1} 1523494ffe9Sdrh} {42 44 47 48 50} 1533494ffe9Sdrhdo_test delete-5.7 { 1543494ffe9Sdrh execsql "DELETE FROM table1 WHERE f1!=48" 1553494ffe9Sdrh execsql {SELECT f1 FROM table1 ORDER BY f1} 1563494ffe9Sdrh} {48} 157ed717fe3Sdrhintegrity_check delete-5.8 158ed717fe3Sdrh 1593494ffe9Sdrh 1600353ceddSdrh# Delete large quantities of data. We want to test the List overflow 1610353ceddSdrh# mechanism in the vdbe. 1620353ceddSdrh# 1630353ceddSdrhdo_test delete-6.1 { 1645f3b4ab5Sdrh execsql {BEGIN; DELETE FROM table1} 1650353ceddSdrh for {set i 1} {$i<=3000} {incr i} { 1665f3b4ab5Sdrh execsql "INSERT INTO table1 VALUES($i,[expr {$i*$i}])" 1670353ceddSdrh } 1680353ceddSdrh execsql {DELETE FROM table2} 1695f3b4ab5Sdrh for {set i 1} {$i<=3000} {incr i} { 1705f3b4ab5Sdrh execsql "INSERT INTO table2 VALUES($i,[expr {$i*$i}])" 1715f3b4ab5Sdrh } 1725f3b4ab5Sdrh execsql {COMMIT} 1730353ceddSdrh execsql {SELECT count(*) FROM table1} 1740353ceddSdrh} {3000} 1750353ceddSdrhdo_test delete-6.2 { 1760353ceddSdrh execsql {SELECT count(*) FROM table2} 1770353ceddSdrh} {3000} 1780353ceddSdrhdo_test delete-6.3 { 1790353ceddSdrh execsql {SELECT f1 FROM table1 WHERE f1<10 ORDER BY f1} 1800353ceddSdrh} {1 2 3 4 5 6 7 8 9} 1810353ceddSdrhdo_test delete-6.4 { 1820353ceddSdrh execsql {SELECT f1 FROM table2 WHERE f1<10 ORDER BY f1} 1830353ceddSdrh} {1 2 3 4 5 6 7 8 9} 1844ebfef14Sdrhdo_test delete-6.5.1 { 1850353ceddSdrh execsql {DELETE FROM table1 WHERE f1>7} 1864ebfef14Sdrh db changes 1874ebfef14Sdrh} {2993} 1884ebfef14Sdrhdo_test delete-6.5.2 { 1890353ceddSdrh execsql {SELECT f1 FROM table1 ORDER BY f1} 1900353ceddSdrh} {1 2 3 4 5 6 7} 1910353ceddSdrhdo_test delete-6.6 { 1920353ceddSdrh execsql {DELETE FROM table2 WHERE f1>7} 1930353ceddSdrh execsql {SELECT f1 FROM table2 ORDER BY f1} 1940353ceddSdrh} {1 2 3 4 5 6 7} 1950353ceddSdrhdo_test delete-6.7 { 1960353ceddSdrh execsql {DELETE FROM table1} 1970353ceddSdrh execsql {SELECT f1 FROM table1} 1980353ceddSdrh} {} 1990353ceddSdrhdo_test delete-6.8 { 2000353ceddSdrh execsql {INSERT INTO table1 VALUES(2,3)} 2010353ceddSdrh execsql {SELECT f1 FROM table1} 2020353ceddSdrh} {2} 2030353ceddSdrhdo_test delete-6.9 { 2040353ceddSdrh execsql {DELETE FROM table2} 2050353ceddSdrh execsql {SELECT f1 FROM table2} 2060353ceddSdrh} {} 2070353ceddSdrhdo_test delete-6.10 { 2080353ceddSdrh execsql {INSERT INTO table2 VALUES(2,3)} 2090353ceddSdrh execsql {SELECT f1 FROM table2} 2100353ceddSdrh} {2} 211ed717fe3Sdrhintegrity_check delete-6.11 2120353ceddSdrh 21326b3e1bcSdrhdo_test delete-7.1 { 21426b3e1bcSdrh execsql { 21526b3e1bcSdrh CREATE TABLE t3(a); 21626b3e1bcSdrh INSERT INTO t3 VALUES(1); 21726b3e1bcSdrh INSERT INTO t3 SELECT a+1 FROM t3; 21826b3e1bcSdrh INSERT INTO t3 SELECT a+2 FROM t3; 21926b3e1bcSdrh SELECT * FROM t3; 22026b3e1bcSdrh } 22126b3e1bcSdrh} {1 2 3 4} 222798da52cSdrhifcapable {trigger} { 22326b3e1bcSdrh do_test delete-7.2 { 22426b3e1bcSdrh execsql { 22526b3e1bcSdrh CREATE TABLE cnt(del); 22626b3e1bcSdrh INSERT INTO cnt VALUES(0); 22726b3e1bcSdrh CREATE TRIGGER r1 AFTER DELETE ON t3 FOR EACH ROW BEGIN 22826b3e1bcSdrh UPDATE cnt SET del=del+1; 22926b3e1bcSdrh END; 23026b3e1bcSdrh DELETE FROM t3 WHERE a<2; 23126b3e1bcSdrh SELECT * FROM t3; 23226b3e1bcSdrh } 23326b3e1bcSdrh } {2 3 4} 23426b3e1bcSdrh do_test delete-7.3 { 23526b3e1bcSdrh execsql { 23626b3e1bcSdrh SELECT * FROM cnt; 23726b3e1bcSdrh } 23826b3e1bcSdrh } {1} 23926b3e1bcSdrh do_test delete-7.4 { 24026b3e1bcSdrh execsql { 24126b3e1bcSdrh DELETE FROM t3; 24226b3e1bcSdrh SELECT * FROM t3; 24326b3e1bcSdrh } 24426b3e1bcSdrh } {} 24526b3e1bcSdrh do_test delete-7.5 { 24626b3e1bcSdrh execsql { 24726b3e1bcSdrh SELECT * FROM cnt; 24826b3e1bcSdrh } 24926b3e1bcSdrh } {4} 25026b3e1bcSdrh do_test delete-7.6 { 25126b3e1bcSdrh execsql { 25226b3e1bcSdrh INSERT INTO t3 VALUES(1); 25326b3e1bcSdrh INSERT INTO t3 SELECT a+1 FROM t3; 25426b3e1bcSdrh INSERT INTO t3 SELECT a+2 FROM t3; 25526b3e1bcSdrh CREATE TABLE t4 AS SELECT * FROM t3; 25626b3e1bcSdrh PRAGMA count_changes=ON; 25726b3e1bcSdrh DELETE FROM t3; 25826b3e1bcSdrh DELETE FROM t4; 25926b3e1bcSdrh } 26026b3e1bcSdrh } {4 4} 261798da52cSdrh} ;# endif trigger 262798da52cSdrhifcapable {!trigger} { 263798da52cSdrh execsql {DELETE FROM t3} 264798da52cSdrh} 265ed717fe3Sdrhintegrity_check delete-7.7 2660353ceddSdrh 26766b4ebafSdrh# Make sure error messages are consistent when attempting to delete 26866b4ebafSdrh# from a read-only database. Ticket #304. 26966b4ebafSdrh# 27066b4ebafSdrhdo_test delete-8.0 { 27166b4ebafSdrh execsql { 27266b4ebafSdrh PRAGMA count_changes=OFF; 27366b4ebafSdrh INSERT INTO t3 VALUES(123); 27466b4ebafSdrh SELECT * FROM t3; 27566b4ebafSdrh } 27666b4ebafSdrh} {123} 27766b4ebafSdrhdb close 27866b4ebafSdrhcatch {file attributes test.db -permissions 0444} 27966b4ebafSdrhcatch {file attributes test.db -readonly 1} 280dddca286Sdrhsqlite3 db test.db 281dddca286Sdrhset ::DB [sqlite3_connection_pointer db] 28266b4ebafSdrhdo_test delete-8.1 { 28366b4ebafSdrh catchsql { 28466b4ebafSdrh DELETE FROM t3; 28566b4ebafSdrh } 28666b4ebafSdrh} {1 {attempt to write a readonly database}} 28766b4ebafSdrhdo_test delete-8.2 { 28866b4ebafSdrh execsql {SELECT * FROM t3} 28966b4ebafSdrh} {123} 29066b4ebafSdrhdo_test delete-8.3 { 29166b4ebafSdrh catchsql { 29266b4ebafSdrh DELETE FROM t3 WHERE 1; 29366b4ebafSdrh } 29466b4ebafSdrh} {1 {attempt to write a readonly database}} 29566b4ebafSdrhdo_test delete-8.4 { 29666b4ebafSdrh execsql {SELECT * FROM t3} 29766b4ebafSdrh} {123} 2980de0bb33Sdanielk1977 2990de0bb33Sdanielk1977# Update for v3: In v2 the DELETE statement would succeed because no 3000de0bb33Sdanielk1977# database writes actually occur. Version 3 refuses to open a transaction 3010de0bb33Sdanielk1977# on a read-only file, so the statement fails. 30266b4ebafSdrhdo_test delete-8.5 { 30366b4ebafSdrh catchsql { 30466b4ebafSdrh DELETE FROM t3 WHERE a<100; 30566b4ebafSdrh } 3060de0bb33Sdanielk1977# v2 result: {0 {}} 3070de0bb33Sdanielk1977} {1 {attempt to write a readonly database}} 30866b4ebafSdrhdo_test delete-8.6 { 30966b4ebafSdrh execsql {SELECT * FROM t3} 31066b4ebafSdrh} {123} 311ed717fe3Sdrhintegrity_check delete-8.7 3120353ceddSdrh 313*382e28faSdanielk1977# Need to do the following for tcl 8.5 on mac. On that configuration, the 314*382e28faSdanielk1977# -readonly flag is taken so seriously that a subsequent [file delete -force] 315*382e28faSdanielk1977# (required before the next test file can be executed) will fail. 316*382e28faSdanielk1977# 317*382e28faSdanielk1977catch {file attributes test.db -readonly 0} 318*382e28faSdanielk1977 31962c68191Sdrhfinish_test 320