OLD | NEW |
(Empty) | |
| 1 # 2009 December 29 |
| 2 # |
| 3 # The author disclaims copyright to this source code. In place of |
| 4 # a legal notice', here is a blessing: |
| 5 # |
| 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. |
| 9 # |
| 10 #*********************************************************************** |
| 11 # |
| 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 |
| 16 # to NULL. |
| 17 # |
| 18 |
| 19 set testdir [file dirname $argv0] |
| 20 source $testdir/tester.tcl |
| 21 ifcapable {!trigger} { |
| 22 finish_test |
| 23 return |
| 24 } |
| 25 set testprefix triggerE |
| 26 |
| 27 do_execsql_test 1.0 { |
| 28 CREATE TABLE t1(a, b); |
| 29 CREATE TABLE t2(c, d); |
| 30 CREATE TABLE t3(e, f); |
| 31 } |
| 32 |
| 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); |
| 38 # |
| 39 # CREATE TRIGGER tr1 AFTER INSERT ON t1 WHEN new.a IN (SELECT x FROM aux.t4) |
| 40 # BEGIN |
| 41 # SELECT 1; |
| 42 # END; |
| 43 # } |
| 44 # do_execsql_test 1.2 { INSERT INTO t1 VALUES(1,1); } |
| 45 # do_execsql_test 1.3 { INSERT INTO t1 VALUES(5,5); } |
| 46 |
| 47 #------------------------------------------------------------------------- |
| 48 # Attempt to create various triggers that use bound variables. |
| 49 # |
| 50 set errmsg "trigger cannot use variables" |
| 51 foreach {tn defn} { |
| 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 } { |
| 61 catchsql {drop trigger tr1} |
| 62 do_catchsql_test 1.1.$tn "CREATE TRIGGER tr1 $defn" [list 1 $errmsg] |
| 63 do_catchsql_test 1.2.$tn "CREATE TEMP TRIGGER tr1 $defn" [list 1 $errmsg] |
| 64 } |
| 65 |
| 66 #------------------------------------------------------------------------- |
| 67 # Test that variable references within trigger definitions loaded from |
| 68 # the sqlite_master table are automatically converted to NULL. |
| 69 # |
| 70 do_execsql_test 2.1 { |
| 71 PRAGMA writable_schema = 1; |
| 72 INSERT INTO sqlite_master VALUES('trigger', 'tr1', 't1', 0, |
| 73 'CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN |
| 74 INSERT INTO t2 VALUES(?1, ?2); |
| 75 END' |
| 76 ); |
| 77 |
| 78 INSERT INTO sqlite_master VALUES('trigger', 'tr2', 't3', 0, |
| 79 'CREATE TRIGGER tr2 AFTER INSERT ON t3 WHEN ?1 IS NULL BEGIN |
| 80 UPDATE t2 SET c=d WHERE c IS ?2; |
| 81 END' |
| 82 ); |
| 83 } |
| 84 db close |
| 85 sqlite3 db test.db |
| 86 |
| 87 do_execsql_test 2.2.1 { |
| 88 INSERT INTO t1 VALUES(1, 2); |
| 89 SELECT * FROM t2; |
| 90 } {{} {}} |
| 91 do_test 2.2.2 { |
| 92 set one 3 |
| 93 execsql { |
| 94 DELETE FROM t2; |
| 95 INSERT INTO t1 VALUES($one, ?1); |
| 96 SELECT * FROM t2; |
| 97 } |
| 98 } {{} {}} |
| 99 do_execsql_test 2.2.3 { SELECT * FROM t1 } {1 2 3 3} |
| 100 |
| 101 do_execsql_test 2.3 { |
| 102 DELETE FROM t2; |
| 103 INSERT INTO t2 VALUES('x', 'y'); |
| 104 INSERT INTO t2 VALUES(NULL, 'z'); |
| 105 INSERT INTO t3 VALUES(1, 2); |
| 106 SELECT * FROM t3; |
| 107 SELECT * FROM t2; |
| 108 } {1 2 x y z z} |
| 109 |
| 110 finish_test |
| 111 |
| 112 |
OLD | NEW |