1*1da57d55SToomas Soome# 2c5c4113dSnw141292# 2001 September 15 3c5c4113dSnw141292# 4c5c4113dSnw141292# The author disclaims copyright to this source code. In place of 5c5c4113dSnw141292# a legal notice, here is a blessing: 6c5c4113dSnw141292# 7c5c4113dSnw141292# May you do good and not evil. 8c5c4113dSnw141292# May you find forgiveness for yourself and forgive others. 9c5c4113dSnw141292# May you share freely, never taking more than you give. 10c5c4113dSnw141292# 11c5c4113dSnw141292#*********************************************************************** 12c5c4113dSnw141292# This file implements regression tests for SQLite library. The 13c5c4113dSnw141292# focus of this file is testing the DELETE FROM statement. 14c5c4113dSnw141292# 15c5c4113dSnw141292# $Id: delete.test,v 1.13 2003/06/15 23:42:25 drh Exp $ 16c5c4113dSnw141292 17c5c4113dSnw141292set testdir [file dirname $argv0] 18c5c4113dSnw141292source $testdir/tester.tcl 19c5c4113dSnw141292 20c5c4113dSnw141292# Try to delete from a non-existant table. 21c5c4113dSnw141292# 22c5c4113dSnw141292do_test delete-1.1 { 23c5c4113dSnw141292 set v [catch {execsql {DELETE FROM test1}} msg] 24c5c4113dSnw141292 lappend v $msg 25c5c4113dSnw141292} {1 {no such table: test1}} 26c5c4113dSnw141292 27c5c4113dSnw141292# Try to delete from sqlite_master 28c5c4113dSnw141292# 29c5c4113dSnw141292do_test delete-2.1 { 30c5c4113dSnw141292 set v [catch {execsql {DELETE FROM sqlite_master}} msg] 31c5c4113dSnw141292 lappend v $msg 32c5c4113dSnw141292} {1 {table sqlite_master may not be modified}} 33c5c4113dSnw141292 34c5c4113dSnw141292# Delete selected entries from a table with and without an index. 35c5c4113dSnw141292# 36c5c4113dSnw141292do_test delete-3.1.1 { 37c5c4113dSnw141292 execsql {CREATE TABLE table1(f1 int, f2 int)} 38c5c4113dSnw141292 execsql {INSERT INTO table1 VALUES(1,2)} 39c5c4113dSnw141292 execsql {INSERT INTO table1 VALUES(2,4)} 40c5c4113dSnw141292 execsql {INSERT INTO table1 VALUES(3,8)} 41c5c4113dSnw141292 execsql {INSERT INTO table1 VALUES(4,16)} 42c5c4113dSnw141292 execsql {SELECT * FROM table1 ORDER BY f1} 43c5c4113dSnw141292} {1 2 2 4 3 8 4 16} 44c5c4113dSnw141292do_test delete-3.1.2 { 45c5c4113dSnw141292 execsql {DELETE FROM table1 WHERE f1=3} 46c5c4113dSnw141292} {} 47c5c4113dSnw141292do_test delete-3.1.3 { 48c5c4113dSnw141292 execsql {SELECT * FROM table1 ORDER BY f1} 49c5c4113dSnw141292} {1 2 2 4 4 16} 50c5c4113dSnw141292do_test delete-3.1.4 { 51c5c4113dSnw141292 execsql {CREATE INDEX index1 ON table1(f1)} 52c5c4113dSnw141292 execsql {PRAGMA count_changes=on} 53c5c4113dSnw141292 execsql {DELETE FROM 'table1' WHERE f1=3} 54c5c4113dSnw141292} {0} 55c5c4113dSnw141292do_test delete-3.1.5 { 56c5c4113dSnw141292 execsql {SELECT * FROM table1 ORDER BY f1} 57c5c4113dSnw141292} {1 2 2 4 4 16} 58c5c4113dSnw141292do_test delete-3.1.6 { 59c5c4113dSnw141292 execsql {DELETE FROM table1 WHERE f1=2} 60c5c4113dSnw141292} {1} 61c5c4113dSnw141292do_test delete-3.1.7 { 62c5c4113dSnw141292 execsql {SELECT * FROM table1 ORDER BY f1} 63c5c4113dSnw141292} {1 2 4 16} 64c5c4113dSnw141292integrity_check delete-3.2 65c5c4113dSnw141292 66c5c4113dSnw141292 67c5c4113dSnw141292# Semantic errors in the WHERE clause 68c5c4113dSnw141292# 69c5c4113dSnw141292do_test delete-4.1 { 70c5c4113dSnw141292 execsql {CREATE TABLE table2(f1 int, f2 int)} 71c5c4113dSnw141292 set v [catch {execsql {DELETE FROM table2 WHERE f3=5}} msg] 72c5c4113dSnw141292 lappend v $msg 73c5c4113dSnw141292} {1 {no such column: f3}} 74c5c4113dSnw141292 75c5c4113dSnw141292do_test delete-4.2 { 76c5c4113dSnw141292 set v [catch {execsql {DELETE FROM table2 WHERE xyzzy(f1+4)}} msg] 77c5c4113dSnw141292 lappend v $msg 78c5c4113dSnw141292} {1 {no such function: xyzzy}} 79c5c4113dSnw141292integrity_check delete-4.3 80c5c4113dSnw141292 81c5c4113dSnw141292# Lots of deletes 82c5c4113dSnw141292# 83c5c4113dSnw141292do_test delete-5.1.1 { 84c5c4113dSnw141292 execsql {DELETE FROM table1} 85c5c4113dSnw141292} {2} 86c5c4113dSnw141292do_test delete-5.1.2 { 87c5c4113dSnw141292 execsql {SELECT count(*) FROM table1} 88c5c4113dSnw141292} {0} 89c5c4113dSnw141292do_test delete-5.2.1 { 90c5c4113dSnw141292 execsql {BEGIN TRANSACTION} 91c5c4113dSnw141292 for {set i 1} {$i<=200} {incr i} { 92c5c4113dSnw141292 execsql "INSERT INTO table1 VALUES($i,[expr {$i*$i}])" 93c5c4113dSnw141292 } 94c5c4113dSnw141292 execsql {COMMIT} 95c5c4113dSnw141292 execsql {SELECT count(*) FROM table1} 96c5c4113dSnw141292} {200} 97c5c4113dSnw141292do_test delete-5.2.2 { 98c5c4113dSnw141292 execsql {DELETE FROM table1} 99c5c4113dSnw141292} {200} 100c5c4113dSnw141292do_test delete-5.2.3 { 101c5c4113dSnw141292 execsql {BEGIN TRANSACTION} 102c5c4113dSnw141292 for {set i 1} {$i<=200} {incr i} { 103c5c4113dSnw141292 execsql "INSERT INTO table1 VALUES($i,[expr {$i*$i}])" 104c5c4113dSnw141292 } 105c5c4113dSnw141292 execsql {COMMIT} 106c5c4113dSnw141292 execsql {SELECT count(*) FROM table1} 107c5c4113dSnw141292} {200} 108c5c4113dSnw141292do_test delete-5.2.4 { 109c5c4113dSnw141292 execsql {PRAGMA count_changes=off} 110c5c4113dSnw141292 execsql {DELETE FROM table1} 111c5c4113dSnw141292} {} 112c5c4113dSnw141292do_test delete-5.2.5 { 113c5c4113dSnw141292 execsql {SELECT count(*) FROM table1} 114c5c4113dSnw141292} {0} 115c5c4113dSnw141292do_test delete-5.2.6 { 116c5c4113dSnw141292 execsql {BEGIN TRANSACTION} 117c5c4113dSnw141292 for {set i 1} {$i<=200} {incr i} { 118c5c4113dSnw141292 execsql "INSERT INTO table1 VALUES($i,[expr {$i*$i}])" 119c5c4113dSnw141292 } 120c5c4113dSnw141292 execsql {COMMIT} 121c5c4113dSnw141292 execsql {SELECT count(*) FROM table1} 122c5c4113dSnw141292} {200} 123c5c4113dSnw141292do_test delete-5.3 { 124c5c4113dSnw141292 for {set i 1} {$i<=200} {incr i 4} { 125c5c4113dSnw141292 execsql "DELETE FROM table1 WHERE f1==$i" 126c5c4113dSnw141292 } 127c5c4113dSnw141292 execsql {SELECT count(*) FROM table1} 128c5c4113dSnw141292} {150} 129c5c4113dSnw141292do_test delete-5.4 { 130c5c4113dSnw141292 execsql "DELETE FROM table1 WHERE f1>50" 131c5c4113dSnw141292 execsql {SELECT count(*) FROM table1} 132c5c4113dSnw141292} {37} 133c5c4113dSnw141292do_test delete-5.5 { 134c5c4113dSnw141292 for {set i 1} {$i<=70} {incr i 3} { 135c5c4113dSnw141292 execsql "DELETE FROM table1 WHERE f1==$i" 136c5c4113dSnw141292 } 137c5c4113dSnw141292 execsql {SELECT f1 FROM table1 ORDER BY f1} 138c5c4113dSnw141292} {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} 139c5c4113dSnw141292do_test delete-5.6 { 140c5c4113dSnw141292 for {set i 1} {$i<40} {incr i} { 141c5c4113dSnw141292 execsql "DELETE FROM table1 WHERE f1==$i" 142c5c4113dSnw141292 } 143c5c4113dSnw141292 execsql {SELECT f1 FROM table1 ORDER BY f1} 144c5c4113dSnw141292} {42 44 47 48 50} 145c5c4113dSnw141292do_test delete-5.7 { 146c5c4113dSnw141292 execsql "DELETE FROM table1 WHERE f1!=48" 147c5c4113dSnw141292 execsql {SELECT f1 FROM table1 ORDER BY f1} 148c5c4113dSnw141292} {48} 149c5c4113dSnw141292integrity_check delete-5.8 150c5c4113dSnw141292 151c5c4113dSnw141292 152c5c4113dSnw141292# Delete large quantities of data. We want to test the List overflow 153c5c4113dSnw141292# mechanism in the vdbe. 154c5c4113dSnw141292# 155c5c4113dSnw141292do_test delete-6.1 { 156c5c4113dSnw141292 set fd [open data1.txt w] 157c5c4113dSnw141292 for {set i 1} {$i<=3000} {incr i} { 158c5c4113dSnw141292 puts $fd "[expr {$i}]\t[expr {$i*$i}]" 159c5c4113dSnw141292 } 160c5c4113dSnw141292 close $fd 161c5c4113dSnw141292 execsql {DELETE FROM table1} 162c5c4113dSnw141292 execsql {COPY table1 FROM 'data1.txt'} 163c5c4113dSnw141292 execsql {DELETE FROM table2} 164c5c4113dSnw141292 execsql {COPY table2 FROM 'data1.txt'} 165c5c4113dSnw141292 file delete data1.txt 166c5c4113dSnw141292 execsql {SELECT count(*) FROM table1} 167c5c4113dSnw141292} {3000} 168c5c4113dSnw141292do_test delete-6.2 { 169c5c4113dSnw141292 execsql {SELECT count(*) FROM table2} 170c5c4113dSnw141292} {3000} 171c5c4113dSnw141292do_test delete-6.3 { 172c5c4113dSnw141292 execsql {SELECT f1 FROM table1 WHERE f1<10 ORDER BY f1} 173c5c4113dSnw141292} {1 2 3 4 5 6 7 8 9} 174c5c4113dSnw141292do_test delete-6.4 { 175c5c4113dSnw141292 execsql {SELECT f1 FROM table2 WHERE f1<10 ORDER BY f1} 176c5c4113dSnw141292} {1 2 3 4 5 6 7 8 9} 177c5c4113dSnw141292do_test delete-6.5 { 178c5c4113dSnw141292 execsql {DELETE FROM table1 WHERE f1>7} 179c5c4113dSnw141292 execsql {SELECT f1 FROM table1 ORDER BY f1} 180c5c4113dSnw141292} {1 2 3 4 5 6 7} 181c5c4113dSnw141292do_test delete-6.6 { 182c5c4113dSnw141292 execsql {DELETE FROM table2 WHERE f1>7} 183c5c4113dSnw141292 execsql {SELECT f1 FROM table2 ORDER BY f1} 184c5c4113dSnw141292} {1 2 3 4 5 6 7} 185c5c4113dSnw141292do_test delete-6.7 { 186c5c4113dSnw141292 execsql {DELETE FROM table1} 187c5c4113dSnw141292 execsql {SELECT f1 FROM table1} 188c5c4113dSnw141292} {} 189c5c4113dSnw141292do_test delete-6.8 { 190c5c4113dSnw141292 execsql {INSERT INTO table1 VALUES(2,3)} 191c5c4113dSnw141292 execsql {SELECT f1 FROM table1} 192c5c4113dSnw141292} {2} 193c5c4113dSnw141292do_test delete-6.9 { 194c5c4113dSnw141292 execsql {DELETE FROM table2} 195c5c4113dSnw141292 execsql {SELECT f1 FROM table2} 196c5c4113dSnw141292} {} 197c5c4113dSnw141292do_test delete-6.10 { 198c5c4113dSnw141292 execsql {INSERT INTO table2 VALUES(2,3)} 199c5c4113dSnw141292 execsql {SELECT f1 FROM table2} 200c5c4113dSnw141292} {2} 201c5c4113dSnw141292integrity_check delete-6.11 202c5c4113dSnw141292 203c5c4113dSnw141292do_test delete-7.1 { 204c5c4113dSnw141292 execsql { 205c5c4113dSnw141292 CREATE TABLE t3(a); 206c5c4113dSnw141292 INSERT INTO t3 VALUES(1); 207c5c4113dSnw141292 INSERT INTO t3 SELECT a+1 FROM t3; 208c5c4113dSnw141292 INSERT INTO t3 SELECT a+2 FROM t3; 209c5c4113dSnw141292 SELECT * FROM t3; 210c5c4113dSnw141292 } 211c5c4113dSnw141292} {1 2 3 4} 212c5c4113dSnw141292do_test delete-7.2 { 213c5c4113dSnw141292 execsql { 214c5c4113dSnw141292 CREATE TABLE cnt(del); 215c5c4113dSnw141292 INSERT INTO cnt VALUES(0); 216c5c4113dSnw141292 CREATE TRIGGER r1 AFTER DELETE ON t3 FOR EACH ROW BEGIN 217c5c4113dSnw141292 UPDATE cnt SET del=del+1; 218c5c4113dSnw141292 END; 219c5c4113dSnw141292 DELETE FROM t3 WHERE a<2; 220c5c4113dSnw141292 SELECT * FROM t3; 221c5c4113dSnw141292 } 222c5c4113dSnw141292} {2 3 4} 223c5c4113dSnw141292do_test delete-7.3 { 224c5c4113dSnw141292 execsql { 225c5c4113dSnw141292 SELECT * FROM cnt; 226c5c4113dSnw141292 } 227c5c4113dSnw141292} {1} 228c5c4113dSnw141292do_test delete-7.4 { 229c5c4113dSnw141292 execsql { 230c5c4113dSnw141292 DELETE FROM t3; 231c5c4113dSnw141292 SELECT * FROM t3; 232c5c4113dSnw141292 } 233c5c4113dSnw141292} {} 234c5c4113dSnw141292do_test delete-7.5 { 235c5c4113dSnw141292 execsql { 236c5c4113dSnw141292 SELECT * FROM cnt; 237c5c4113dSnw141292 } 238c5c4113dSnw141292} {4} 239c5c4113dSnw141292do_test delete-7.6 { 240c5c4113dSnw141292 execsql { 241c5c4113dSnw141292 INSERT INTO t3 VALUES(1); 242c5c4113dSnw141292 INSERT INTO t3 SELECT a+1 FROM t3; 243c5c4113dSnw141292 INSERT INTO t3 SELECT a+2 FROM t3; 244c5c4113dSnw141292 CREATE TABLE t4 AS SELECT * FROM t3; 245c5c4113dSnw141292 PRAGMA count_changes=ON; 246c5c4113dSnw141292 DELETE FROM t3; 247c5c4113dSnw141292 DELETE FROM t4; 248c5c4113dSnw141292 } 249c5c4113dSnw141292} {4 4} 250c5c4113dSnw141292integrity_check delete-7.7 251c5c4113dSnw141292 252c5c4113dSnw141292# Make sure error messages are consistent when attempting to delete 253c5c4113dSnw141292# from a read-only database. Ticket #304. 254c5c4113dSnw141292# 255c5c4113dSnw141292do_test delete-8.0 { 256c5c4113dSnw141292 execsql { 257c5c4113dSnw141292 PRAGMA count_changes=OFF; 258c5c4113dSnw141292 INSERT INTO t3 VALUES(123); 259c5c4113dSnw141292 SELECT * FROM t3; 260c5c4113dSnw141292 } 261c5c4113dSnw141292} {123} 262c5c4113dSnw141292db close 263c5c4113dSnw141292catch {file attributes test.db -permissions 0444} 264c5c4113dSnw141292catch {file attributes test.db -readonly 1} 265c5c4113dSnw141292sqlite db test.db 266c5c4113dSnw141292do_test delete-8.1 { 267c5c4113dSnw141292 catchsql { 268c5c4113dSnw141292 DELETE FROM t3; 269c5c4113dSnw141292 } 270c5c4113dSnw141292} {1 {attempt to write a readonly database}} 271c5c4113dSnw141292do_test delete-8.2 { 272c5c4113dSnw141292 execsql {SELECT * FROM t3} 273c5c4113dSnw141292} {123} 274c5c4113dSnw141292do_test delete-8.3 { 275c5c4113dSnw141292 catchsql { 276c5c4113dSnw141292 DELETE FROM t3 WHERE 1; 277c5c4113dSnw141292 } 278c5c4113dSnw141292} {1 {attempt to write a readonly database}} 279c5c4113dSnw141292do_test delete-8.4 { 280c5c4113dSnw141292 execsql {SELECT * FROM t3} 281c5c4113dSnw141292} {123} 282c5c4113dSnw141292do_test delete-8.5 { 283c5c4113dSnw141292 catchsql { 284c5c4113dSnw141292 DELETE FROM t3 WHERE a<100; 285c5c4113dSnw141292 } 286c5c4113dSnw141292} {0 {}} 287c5c4113dSnw141292do_test delete-8.6 { 288c5c4113dSnw141292 execsql {SELECT * FROM t3} 289c5c4113dSnw141292} {123} 290c5c4113dSnw141292integrity_check delete-8.7 291c5c4113dSnw141292 292c5c4113dSnw141292finish_test 293