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 #***********************************************************************
13 set testdir [file dirname $argv0]
14 source $testdir/tester.tcl
15 set testprefix triggerupfrom
18 CREATE TABLE map(k, v);
19 INSERT INTO map VALUES(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four');
21 CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
23 CREATE TRIGGER tr AFTER INSERT ON t1 BEGIN
24 UPDATE t1 SET c = v FROM map WHERE k=new.a AND a=new.a;
29 INSERT INTO t1(a) VALUES(1);
33 SELECT a, c FROM t1 ORDER BY a;
37 INSERT INTO t1(a) VALUES(2), (3), (4), (5);
38 SELECT a, c FROM t1 ORDER BY a;
39 } {1 one 2 two 3 three 4 four 5 {}}
43 ATTACH 'test.db2' AS aux;
44 CREATE TABLE aux.t3(x, y);
45 INSERT INTO aux.t3 VALUES('x', 'y');
48 do_catchsql_test 2.1 {
49 CREATE TRIGGER tr2 AFTER INSERT ON t1 BEGIN
50 UPDATE t1 SET b = y FROM aux.t3 WHERE k=new.a;
52 } {1 {trigger tr2 cannot reference objects in database aux}}
55 CREATE TEMP TRIGGER tr2 AFTER INSERT ON t1 BEGIN
56 UPDATE t1 SET b = y FROM aux.t3 WHERE a=new.a;
58 INSERT INTO t1(a) VALUES(10), (20);
71 CREATE TABLE link(f, t);
72 INSERT INTO link VALUES(5, 2), (20, 10), (2, 1);
73 CREATE TRIGGER tr3 BEFORE DELETE ON t1 BEGIN
74 UPDATE t1 SET b=coalesce(old.b,old.c) FROM main.link WHERE a=t AND old.a=f;
76 DELETE FROM t1 WHERE a=2;
89 do_catchsql_test 2.4 {
90 ATTACH 'test.db' AS yyy;
92 } {1 {malformed database schema (tr3) - trigger tr3 cannot reference objects in database main}}
94 #-------------------------------------------------------------------------
98 CREATE TABLE mmm(x, y);
99 INSERT INTO mmm VALUES(1, 'one');
100 INSERT INTO mmm VALUES(2, 'two');
101 INSERT INTO mmm VALUES(3, 'three');
103 ATTACH 'test.db2' AS aux;
104 CREATE TABLE aux.t1(a, b);
105 CREATE TABLE aux.mmm(x, y);
106 INSERT INTO aux.mmm VALUES(1, 'ONE');
107 INSERT INTO aux.mmm VALUES(2, 'TWO');
108 INSERT INTO aux.mmm VALUES(3, 'THREE');
110 CREATE TRIGGER aux.ttt AFTER INSERT ON t1 BEGIN
111 UPDATE t1 SET b=y FROM mmm WHERE x=new.a AND a=new.a;
114 INSERT INTO t1(a) VALUES (2);
118 #-------------------------------------------------------------------------
119 # Test that INSTEAD OF UPDATE triggers on views work with UPDATE...FROM
120 # statements. Including, if the library is built with ENABLE_HIDDEN_COLUMNS,
121 # that they work correctly on views with hidden columns.
124 do_execsql_test 4.0 {
125 CREATE TABLE t1(k, a, b);
126 INSERT INTO t1 VALUES('a', 1, 'one');
127 INSERT INTO t1 VALUES('b', 2, 'two');
128 INSERT INTO t1 VALUES('c', 3, 'three');
129 INSERT INTO t1 VALUES('d', 4, 'four');
132 CREATE VIEW v1 AS SELECT k, a, b AS __hidden__b FROM t1;
133 CREATE TRIGGER tr1 INSTEAD OF UPDATE ON v1 BEGIN
134 INSERT INTO log VALUES(
135 '('||old.a||','||old.__hidden__b||')->('||new.a||','||new.__hidden__b||')'
140 ifcapable hiddencolumns {
141 do_execsql_test 4.1-hc-enabled {
145 do_execsql_test 4.1-hc-disabled {
147 } {a 1 one b 2 two c 3 three d 4 four}
150 do_execsql_test 4.2 {
151 UPDATE v1 SET a='xyz' WHERE k IN ('a', 'c');
156 (3,three)->(xyz,three)
159 do_execsql_test 4.3 {
160 CREATE TABLE map(k, v);
161 INSERT INTO map VALUES('b', 'twelve');
162 INSERT INTO map VALUES('d', 'fourteen');
163 UPDATE v1 SET a=map.v FROM map WHERE v1.k=map.k;
167 (2,two)->(twelve,two)
168 (4,four)->(fourteen,four)