3 # The author disclaims copyright to this source code. In place of
4 # a legal notice', here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
12 # This file tests the effects of SQL variable references embedded in
13 # triggers. If the user attempts to create such a trigger, it is an
14 # error. However, if an existing trigger definition is read from
15 # the sqlite_master table, the variable reference always evaluates
19 set testdir [file dirname $argv0]
20 source $testdir/tester.tcl
21 ifcapable {!trigger} {
25 set testprefix triggerE
28 CREATE TABLE t1(a, b);
29 CREATE TABLE t2(c, d);
30 CREATE TABLE t3(e, f);
33 # forcedelete test.db2
34 # do_execsql_test 1.1 {
35 # ATTACH 'test.db2' AS aux;
36 # CREATE TABLE aux.t4(x);
37 # INSERT INTO aux.t4 VALUES(5);
39 # CREATE TRIGGER tr1 AFTER INSERT ON t1 WHEN new.a IN (SELECT x FROM aux.t4)
44 # do_execsql_test 1.2 { INSERT INTO t1 VALUES(1,1); }
45 # do_execsql_test 1.3 { INSERT INTO t1 VALUES(5,5); }
47 #-------------------------------------------------------------------------
48 # Attempt to create various triggers that use bound variables.
50 set errmsg "trigger cannot use variables"
52 1 { AFTER INSERT ON t1 WHEN new.a = ? BEGIN SELECT 1; END; }
53 2 { BEFORE DELETE ON t1 BEGIN SELECT ?; END; }
54 3 { BEFORE DELETE ON t1 BEGIN SELECT * FROM (SELECT * FROM (SELECT ?)); END; }
55 5 { BEFORE DELETE ON t1 BEGIN SELECT * FROM t2 GROUP BY ?; END; }
56 6 { BEFORE DELETE ON t1 BEGIN SELECT * FROM t2 LIMIT ?; END; }
57 7 { BEFORE DELETE ON t1 BEGIN SELECT * FROM t2 ORDER BY ?; END; }
58 8 { BEFORE UPDATE ON t1 BEGIN UPDATE t2 SET c = ?; END; }
59 9 { BEFORE UPDATE ON t1 BEGIN UPDATE t2 SET c = 1 WHERE d = ?; END; }
60 10 { AFTER INSERT ON t1 BEGIN SELECT * FROM pragma_stats(?); END; }
62 catchsql {drop trigger tr1}
63 do_catchsql_test 1.1.$tn "CREATE TRIGGER tr1 $defn" [list 1 $errmsg]
64 do_catchsql_test 1.2.$tn "CREATE TEMP TRIGGER tr1 $defn" [list 1 $errmsg]
67 #-------------------------------------------------------------------------
68 # Test that variable references within trigger definitions loaded from
69 # the sqlite_master table are automatically converted to NULL.
71 sqlite3_db_config db DEFENSIVE 0
73 PRAGMA writable_schema = 1;
74 INSERT INTO sqlite_master VALUES('trigger', 'tr1', 't1', 0,
75 'CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN
76 INSERT INTO t2 VALUES(?1, ?2);
80 INSERT INTO sqlite_master VALUES('trigger', 'tr2', 't3', 0,
81 'CREATE TRIGGER tr2 AFTER INSERT ON t3 WHEN ?1 IS NULL BEGIN
82 UPDATE t2 SET c=d WHERE c IS ?2;
89 do_execsql_test 2.2.1 {
90 INSERT INTO t1 VALUES(1, 2);
97 INSERT INTO t1 VALUES($one, ?1);
101 do_execsql_test 2.2.3 { SELECT * FROM t1 } {1 2 3 3}
103 do_execsql_test 2.3 {
105 INSERT INTO t2 VALUES('x', 'y');
106 INSERT INTO t2 VALUES(NULL, 'z');
107 INSERT INTO t3 VALUES(1, 2);