sl@0: # 2005 January 13 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 corner cases of the INSERT statement. sl@0: # sl@0: # $Id: insert3.test,v 1.7 2007/09/12 17:01:45 danielk1977 Exp $ sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: sl@0: # All the tests in this file require trigger support sl@0: # sl@0: ifcapable {trigger} { sl@0: sl@0: # Create a table and a corresponding insert trigger. Do a self-insert sl@0: # into the table. sl@0: # sl@0: do_test insert3-1.0 { sl@0: execsql { sl@0: CREATE TABLE t1(a,b); sl@0: CREATE TABLE log(x UNIQUE, y); sl@0: CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN sl@0: UPDATE log SET y=y+1 WHERE x=new.a; sl@0: INSERT OR IGNORE INTO log VALUES(new.a, 1); sl@0: END; sl@0: INSERT INTO t1 VALUES('hello','world'); sl@0: INSERT INTO t1 VALUES(5,10); sl@0: SELECT * FROM log ORDER BY x; sl@0: } sl@0: } {5 1 hello 1} sl@0: do_test insert3-1.1 { sl@0: execsql { sl@0: INSERT INTO t1 SELECT a, b+10 FROM t1; sl@0: SELECT * FROM log ORDER BY x; sl@0: } sl@0: } {5 2 hello 2} sl@0: do_test insert3-1.2 { sl@0: execsql { sl@0: CREATE TABLE log2(x PRIMARY KEY,y); sl@0: CREATE TRIGGER r2 BEFORE INSERT ON t1 BEGIN sl@0: UPDATE log2 SET y=y+1 WHERE x=new.b; sl@0: INSERT OR IGNORE INTO log2 VALUES(new.b,1); sl@0: END; sl@0: INSERT INTO t1 VALUES(453,'hi'); sl@0: SELECT * FROM log ORDER BY x; sl@0: } sl@0: } {5 2 453 1 hello 2} sl@0: do_test insert3-1.3 { sl@0: execsql { sl@0: SELECT * FROM log2 ORDER BY x; sl@0: } sl@0: } {hi 1} sl@0: ifcapable compound { sl@0: do_test insert3-1.4.1 { sl@0: execsql { sl@0: INSERT INTO t1 SELECT * FROM t1; sl@0: SELECT 'a:', x, y FROM log UNION ALL sl@0: SELECT 'b:', x, y FROM log2 ORDER BY x; sl@0: } sl@0: } {a: 5 4 b: 10 2 b: 20 1 a: 453 2 a: hello 4 b: hi 2 b: world 1} sl@0: do_test insert3-1.4.2 { sl@0: execsql { sl@0: SELECT 'a:', x, y FROM log UNION ALL sl@0: SELECT 'b:', x, y FROM log2 ORDER BY x, y; sl@0: } sl@0: } {a: 5 4 b: 10 2 b: 20 1 a: 453 2 a: hello 4 b: hi 2 b: world 1} sl@0: do_test insert3-1.5 { sl@0: execsql { sl@0: INSERT INTO t1(a) VALUES('xyz'); sl@0: SELECT * FROM log ORDER BY x; sl@0: } sl@0: } {5 4 453 2 hello 4 xyz 1} sl@0: } sl@0: sl@0: do_test insert3-2.1 { sl@0: execsql { sl@0: CREATE TABLE t2( sl@0: a INTEGER PRIMARY KEY, sl@0: b DEFAULT 'b', sl@0: c DEFAULT 'c' sl@0: ); sl@0: CREATE TABLE t2dup(a,b,c); sl@0: CREATE TRIGGER t2r1 BEFORE INSERT ON t2 BEGIN sl@0: INSERT INTO t2dup(a,b,c) VALUES(new.a,new.b,new.c); sl@0: END; sl@0: INSERT INTO t2(a) VALUES(123); sl@0: INSERT INTO t2(b) VALUES(234); sl@0: INSERT INTO t2(c) VALUES(345); sl@0: SELECT * FROM t2dup; sl@0: } sl@0: } {123 b c -1 234 c -1 b 345} sl@0: do_test insert3-2.2 { sl@0: execsql { sl@0: DELETE FROM t2dup; sl@0: INSERT INTO t2(a) SELECT 1 FROM t1 LIMIT 1; sl@0: INSERT INTO t2(b) SELECT 987 FROM t1 LIMIT 1; sl@0: INSERT INTO t2(c) SELECT 876 FROM t1 LIMIT 1; sl@0: SELECT * FROM t2dup; sl@0: } sl@0: } {1 b c -1 987 c -1 b 876} sl@0: sl@0: # Test for proper detection of malformed WHEN clauses on INSERT triggers. sl@0: # sl@0: do_test insert3-3.1 { sl@0: execsql { sl@0: CREATE TABLE t3(a,b,c); sl@0: CREATE TRIGGER t3r1 BEFORE INSERT on t3 WHEN nosuchcol BEGIN sl@0: SELECT 'illegal WHEN clause'; sl@0: END; sl@0: } sl@0: } {} sl@0: do_test insert3-3.2 { sl@0: catchsql { sl@0: INSERT INTO t3 VALUES(1,2,3) sl@0: } sl@0: } {1 {no such column: nosuchcol}} sl@0: do_test insert3-3.3 { sl@0: execsql { sl@0: CREATE TABLE t4(a,b,c); sl@0: CREATE TRIGGER t4r1 AFTER INSERT on t4 WHEN nosuchcol BEGIN sl@0: SELECT 'illegal WHEN clause'; sl@0: END; sl@0: } sl@0: } {} sl@0: do_test insert3-3.4 { sl@0: catchsql { sl@0: INSERT INTO t4 VALUES(1,2,3) sl@0: } sl@0: } {1 {no such column: nosuchcol}} sl@0: sl@0: } ;# ifcapable {trigger} sl@0: sl@0: # Tests for the INSERT INTO ... DEFAULT VALUES construct sl@0: # sl@0: do_test insert3-3.5 { sl@0: execsql { sl@0: CREATE TABLE t5( sl@0: a INTEGER PRIMARY KEY, sl@0: b DEFAULT 'xyz' sl@0: ); sl@0: INSERT INTO t5 DEFAULT VALUES; sl@0: SELECT * FROM t5; sl@0: } sl@0: } {1 xyz} sl@0: do_test insert3-3.6 { sl@0: execsql { sl@0: INSERT INTO t5 DEFAULT VALUES; sl@0: SELECT * FROM t5; sl@0: } sl@0: } {1 xyz 2 xyz} sl@0: sl@0: ifcapable bloblit { sl@0: do_test insert3-3.7 { sl@0: execsql { sl@0: CREATE TABLE t6(x,y DEFAULT 4.3, z DEFAULT x'6869'); sl@0: INSERT INTO t6 DEFAULT VALUES; sl@0: SELECT * FROM t6; sl@0: } sl@0: } {{} 4.3 hi} sl@0: } sl@0: db close sl@0: sl@0: finish_test