sl@0: # 2001 September 15 sl@0: # sl@0: # The author disclaims copyright to this source code. In place of sl@0: # a legal notice, here is a blessing: sl@0: # sl@0: # May you do good and not evil. sl@0: # May you find forgiveness for yourself and forgive others. sl@0: # May you share freely, never taking more than you give. sl@0: # sl@0: #*********************************************************************** sl@0: # This file implements regression tests for SQLite library. The sl@0: # focus of this file is testing the INSERT statement. sl@0: # sl@0: # $Id: insert.test,v 1.31 2007/04/05 11:25:59 drh Exp $ sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: sl@0: # Try to insert into a non-existant table. sl@0: # sl@0: do_test insert-1.1 { sl@0: set v [catch {execsql {INSERT INTO test1 VALUES(1,2,3)}} msg] sl@0: lappend v $msg sl@0: } {1 {no such table: test1}} sl@0: sl@0: # Try to insert into sqlite_master sl@0: # sl@0: do_test insert-1.2 { sl@0: set v [catch {execsql {INSERT INTO sqlite_master VALUES(1,2,3,4)}} msg] sl@0: lappend v $msg sl@0: } {1 {table sqlite_master may not be modified}} sl@0: sl@0: # Try to insert the wrong number of entries. sl@0: # sl@0: do_test insert-1.3 { sl@0: execsql {CREATE TABLE test1(one int, two int, three int)} sl@0: set v [catch {execsql {INSERT INTO test1 VALUES(1,2)}} msg] sl@0: lappend v $msg sl@0: } {1 {table test1 has 3 columns but 2 values were supplied}} sl@0: do_test insert-1.3b { sl@0: set v [catch {execsql {INSERT INTO test1 VALUES(1,2,3,4)}} msg] sl@0: lappend v $msg sl@0: } {1 {table test1 has 3 columns but 4 values were supplied}} sl@0: do_test insert-1.3c { sl@0: set v [catch {execsql {INSERT INTO test1(one,two) VALUES(1,2,3,4)}} msg] sl@0: lappend v $msg sl@0: } {1 {4 values for 2 columns}} sl@0: do_test insert-1.3d { sl@0: set v [catch {execsql {INSERT INTO test1(one,two) VALUES(1)}} msg] sl@0: lappend v $msg sl@0: } {1 {1 values for 2 columns}} sl@0: sl@0: # Try to insert into a non-existant column of a table. sl@0: # sl@0: do_test insert-1.4 { sl@0: set v [catch {execsql {INSERT INTO test1(one,four) VALUES(1,2)}} msg] sl@0: lappend v $msg sl@0: } {1 {table test1 has no column named four}} sl@0: sl@0: # Make sure the inserts actually happen sl@0: # sl@0: do_test insert-1.5 { sl@0: execsql {INSERT INTO test1 VALUES(1,2,3)} sl@0: execsql {SELECT * FROM test1} sl@0: } {1 2 3} sl@0: do_test insert-1.5b { sl@0: execsql {INSERT INTO test1 VALUES(4,5,6)} sl@0: execsql {SELECT * FROM test1 ORDER BY one} sl@0: } {1 2 3 4 5 6} sl@0: do_test insert-1.5c { sl@0: execsql {INSERT INTO test1 VALUES(7,8,9)} sl@0: execsql {SELECT * FROM test1 ORDER BY one} sl@0: } {1 2 3 4 5 6 7 8 9} sl@0: sl@0: do_test insert-1.6 { sl@0: execsql {DELETE FROM test1} sl@0: execsql {INSERT INTO test1(one,two) VALUES(1,2)} sl@0: execsql {SELECT * FROM test1 ORDER BY one} sl@0: } {1 2 {}} sl@0: do_test insert-1.6b { sl@0: execsql {INSERT INTO test1(two,three) VALUES(5,6)} sl@0: execsql {SELECT * FROM test1 ORDER BY one} sl@0: } {{} 5 6 1 2 {}} sl@0: do_test insert-1.6c { sl@0: execsql {INSERT INTO test1(three,one) VALUES(7,8)} sl@0: execsql {SELECT * FROM test1 ORDER BY one} sl@0: } {{} 5 6 1 2 {} 8 {} 7} sl@0: sl@0: # A table to use for testing default values sl@0: # sl@0: do_test insert-2.1 { sl@0: execsql { sl@0: CREATE TABLE test2( sl@0: f1 int default -111, sl@0: f2 real default +4.32, sl@0: f3 int default +222, sl@0: f4 int default 7.89 sl@0: ) sl@0: } sl@0: execsql {SELECT * from test2} sl@0: } {} sl@0: do_test insert-2.2 { sl@0: execsql {INSERT INTO test2(f1,f3) VALUES(+10,-10)} sl@0: execsql {SELECT * FROM test2} sl@0: } {10 4.32 -10 7.89} sl@0: do_test insert-2.3 { sl@0: execsql {INSERT INTO test2(f2,f4) VALUES(1.23,-3.45)} sl@0: execsql {SELECT * FROM test2 WHERE f1==-111} sl@0: } {-111 1.23 222 -3.45} sl@0: do_test insert-2.4 { sl@0: execsql {INSERT INTO test2(f1,f2,f4) VALUES(77,+1.23,3.45)} sl@0: execsql {SELECT * FROM test2 WHERE f1==77} sl@0: } {77 1.23 222 3.45} sl@0: do_test insert-2.10 { sl@0: execsql { sl@0: DROP TABLE test2; sl@0: CREATE TABLE test2( sl@0: f1 int default 111, sl@0: f2 real default -4.32, sl@0: f3 text default hi, sl@0: f4 text default 'abc-123', sl@0: f5 varchar(10) sl@0: ) sl@0: } sl@0: execsql {SELECT * from test2} sl@0: } {} sl@0: do_test insert-2.11 { sl@0: execsql {INSERT INTO test2(f2,f4) VALUES(-2.22,'hi!')} sl@0: execsql {SELECT * FROM test2} sl@0: } {111 -2.22 hi hi! {}} sl@0: do_test insert-2.12 { sl@0: execsql {INSERT INTO test2(f1,f5) VALUES(1,'xyzzy')} sl@0: execsql {SELECT * FROM test2 ORDER BY f1} sl@0: } {1 -4.32 hi abc-123 xyzzy 111 -2.22 hi hi! {}} sl@0: sl@0: # Do additional inserts with default values, but this time sl@0: # on a table that has indices. In particular we want to verify sl@0: # that the correct default values are inserted into the indices. sl@0: # sl@0: do_test insert-3.1 { sl@0: execsql { sl@0: DELETE FROM test2; sl@0: CREATE INDEX index9 ON test2(f1,f2); sl@0: CREATE INDEX indext ON test2(f4,f5); sl@0: SELECT * from test2; sl@0: } sl@0: } {} sl@0: sl@0: # Update for sqlite3 v3: sl@0: # Change the 111 to '111' in the following two test cases, because sl@0: # the default value is being inserted as a string. TODO: It shouldn't be. sl@0: do_test insert-3.2 { sl@0: execsql {INSERT INTO test2(f2,f4) VALUES(-3.33,'hum')} sl@0: execsql {SELECT * FROM test2 WHERE f1='111' AND f2=-3.33} sl@0: } {111 -3.33 hi hum {}} sl@0: do_test insert-3.3 { sl@0: execsql {INSERT INTO test2(f1,f2,f5) VALUES(22,-4.44,'wham')} sl@0: execsql {SELECT * FROM test2 WHERE f1='111' AND f2=-3.33} sl@0: } {111 -3.33 hi hum {}} sl@0: do_test insert-3.4 { sl@0: execsql {SELECT * FROM test2 WHERE f1=22 AND f2=-4.44} sl@0: } {22 -4.44 hi abc-123 wham} sl@0: ifcapable {reindex} { sl@0: do_test insert-3.5 { sl@0: execsql REINDEX sl@0: } {} sl@0: } sl@0: integrity_check insert-3.5 sl@0: sl@0: # Test of expressions in the VALUES clause sl@0: # sl@0: do_test insert-4.1 { sl@0: execsql { sl@0: CREATE TABLE t3(a,b,c); sl@0: INSERT INTO t3 VALUES(1+2+3,4,5); sl@0: SELECT * FROM t3; sl@0: } sl@0: } {6 4 5} sl@0: do_test insert-4.2 { sl@0: ifcapable subquery { sl@0: execsql {INSERT INTO t3 VALUES((SELECT max(a) FROM t3)+1,5,6);} sl@0: } else { sl@0: set maxa [execsql {SELECT max(a) FROM t3}] sl@0: execsql "INSERT INTO t3 VALUES($maxa+1,5,6);" sl@0: } sl@0: execsql { sl@0: SELECT * FROM t3 ORDER BY a; sl@0: } sl@0: } {6 4 5 7 5 6} sl@0: ifcapable subquery { sl@0: do_test insert-4.3 { sl@0: catchsql { sl@0: INSERT INTO t3 VALUES((SELECT max(a) FROM t3)+1,t3.a,6); sl@0: SELECT * FROM t3 ORDER BY a; sl@0: } sl@0: } {1 {no such column: t3.a}} sl@0: } sl@0: do_test insert-4.4 { sl@0: ifcapable subquery { sl@0: execsql {INSERT INTO t3 VALUES((SELECT b FROM t3 WHERE a=0),6,7);} sl@0: } else { sl@0: set b [execsql {SELECT b FROM t3 WHERE a = 0}] sl@0: if {$b==""} {set b NULL} sl@0: execsql "INSERT INTO t3 VALUES($b,6,7);" sl@0: } sl@0: execsql { sl@0: SELECT * FROM t3 ORDER BY a; sl@0: } sl@0: } {{} 6 7 6 4 5 7 5 6} sl@0: do_test insert-4.5 { sl@0: execsql { sl@0: SELECT b,c FROM t3 WHERE a IS NULL; sl@0: } sl@0: } {6 7} sl@0: do_test insert-4.6 { sl@0: catchsql { sl@0: INSERT INTO t3 VALUES(notafunc(2,3),2,3); sl@0: } sl@0: } {1 {no such function: notafunc}} sl@0: do_test insert-4.7 { sl@0: execsql { sl@0: INSERT INTO t3 VALUES(min(1,2,3),max(1,2,3),99); sl@0: SELECT * FROM t3 WHERE c=99; sl@0: } sl@0: } {1 3 99} sl@0: sl@0: # Test the ability to insert from a temporary table into itself. sl@0: # Ticket #275. sl@0: # sl@0: ifcapable tempdb { sl@0: do_test insert-5.1 { sl@0: execsql { sl@0: CREATE TEMP TABLE t4(x); sl@0: INSERT INTO t4 VALUES(1); sl@0: SELECT * FROM t4; sl@0: } sl@0: } {1} sl@0: do_test insert-5.2 { sl@0: execsql { sl@0: INSERT INTO t4 SELECT x+1 FROM t4; sl@0: SELECT * FROM t4; sl@0: } sl@0: } {1 2} sl@0: ifcapable {explain} { sl@0: do_test insert-5.3 { sl@0: # verify that a temporary table is used to copy t4 to t4 sl@0: set x [execsql { sl@0: EXPLAIN INSERT INTO t4 SELECT x+2 FROM t4; sl@0: }] sl@0: expr {[lsearch $x OpenEphemeral]>0} sl@0: } {1} sl@0: } sl@0: sl@0: do_test insert-5.4 { sl@0: # Verify that table "test1" begins on page 3. This should be the same sl@0: # page number used by "t4" above. sl@0: # sl@0: # Update for v3 - the first table now begins on page 2 of each file, not 3. sl@0: execsql { sl@0: SELECT rootpage FROM sqlite_master WHERE name='test1'; sl@0: } sl@0: } [expr $AUTOVACUUM?3:2] sl@0: do_test insert-5.5 { sl@0: # Verify that "t4" begins on page 3. sl@0: # sl@0: # Update for v3 - the first table now begins on page 2 of each file, not 3. sl@0: execsql { sl@0: SELECT rootpage FROM sqlite_temp_master WHERE name='t4'; sl@0: } sl@0: } {2} sl@0: do_test insert-5.6 { sl@0: # This should not use an intermediate temporary table. sl@0: execsql { sl@0: INSERT INTO t4 SELECT one FROM test1 WHERE three=7; sl@0: SELECT * FROM t4 sl@0: } sl@0: } {1 2 8} sl@0: ifcapable {explain} { sl@0: do_test insert-5.7 { sl@0: # verify that no temporary table is used to copy test1 to t4 sl@0: set x [execsql { sl@0: EXPLAIN INSERT INTO t4 SELECT one FROM test1; sl@0: }] sl@0: expr {[lsearch $x OpenTemp]>0} sl@0: } {0} sl@0: } sl@0: } sl@0: sl@0: # Ticket #334: REPLACE statement corrupting indices. sl@0: # sl@0: ifcapable conflict { sl@0: # The REPLACE command is not available if SQLITE_OMIT_CONFLICT is sl@0: # defined at compilation time. sl@0: do_test insert-6.1 { sl@0: execsql { sl@0: CREATE TABLE t1(a INTEGER PRIMARY KEY, b UNIQUE); sl@0: INSERT INTO t1 VALUES(1,2); sl@0: INSERT INTO t1 VALUES(2,3); sl@0: SELECT b FROM t1 WHERE b=2; sl@0: } sl@0: } {2} sl@0: do_test insert-6.2 { sl@0: execsql { sl@0: REPLACE INTO t1 VALUES(1,4); sl@0: SELECT b FROM t1 WHERE b=2; sl@0: } sl@0: } {} sl@0: do_test insert-6.3 { sl@0: execsql { sl@0: UPDATE OR REPLACE t1 SET a=2 WHERE b=4; sl@0: SELECT * FROM t1 WHERE b=4; sl@0: } sl@0: } {2 4} sl@0: do_test insert-6.4 { sl@0: execsql { sl@0: SELECT * FROM t1 WHERE b=3; sl@0: } sl@0: } {} sl@0: ifcapable {reindex} { sl@0: do_test insert-6.5 { sl@0: execsql REINDEX sl@0: } {} sl@0: } sl@0: do_test insert-6.6 { sl@0: execsql { sl@0: DROP TABLE t1; sl@0: } sl@0: } {} sl@0: } sl@0: sl@0: # Test that the special optimization for queries of the form sl@0: # "SELECT max(x) FROM tbl" where there is an index on tbl(x) works with sl@0: # INSERT statments. sl@0: do_test insert-7.1 { sl@0: execsql { sl@0: CREATE TABLE t1(a); sl@0: INSERT INTO t1 VALUES(1); sl@0: INSERT INTO t1 VALUES(2); sl@0: CREATE INDEX i1 ON t1(a); sl@0: } sl@0: } {} sl@0: do_test insert-7.2 { sl@0: execsql { sl@0: INSERT INTO t1 SELECT max(a) FROM t1; sl@0: } sl@0: } {} sl@0: do_test insert-7.3 { sl@0: execsql { sl@0: SELECT a FROM t1; sl@0: } sl@0: } {1 2 2} sl@0: sl@0: # Ticket #1140: Check for an infinite loop in the algorithm that tests sl@0: # to see if the right-hand side of an INSERT...SELECT references the left-hand sl@0: # side. sl@0: # sl@0: ifcapable subquery&&compound { sl@0: do_test insert-8.1 { sl@0: execsql { sl@0: INSERT INTO t3 SELECT * FROM (SELECT * FROM t3 UNION ALL SELECT 1,2,3) sl@0: } sl@0: } {} sl@0: } sl@0: sl@0: # Make sure the rowid cache in the VDBE is reset correctly when sl@0: # an explicit rowid is given. sl@0: # sl@0: do_test insert-9.1 { sl@0: execsql { sl@0: CREATE TABLE t5(x); sl@0: INSERT INTO t5 VALUES(1); sl@0: INSERT INTO t5 VALUES(2); sl@0: INSERT INTO t5 VALUES(3); sl@0: INSERT INTO t5(rowid, x) SELECT nullif(x*2+10,14), x+100 FROM t5; sl@0: SELECT rowid, x FROM t5; sl@0: } sl@0: } {1 1 2 2 3 3 12 101 13 102 16 103} sl@0: do_test insert-9.2 { sl@0: execsql { sl@0: CREATE TABLE t6(x INTEGER PRIMARY KEY, y); sl@0: INSERT INTO t6 VALUES(1,1); sl@0: INSERT INTO t6 VALUES(2,2); sl@0: INSERT INTO t6 VALUES(3,3); sl@0: INSERT INTO t6 SELECT nullif(y*2+10,14), y+100 FROM t6; sl@0: SELECT x, y FROM t6; sl@0: } sl@0: } {1 1 2 2 3 3 12 101 13 102 16 103} sl@0: sl@0: integrity_check insert-99.0 sl@0: sl@0: finish_test