# 2020 July 14
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl
set testprefix triggerupfrom

do_execsql_test 1.0 {
  CREATE TABLE map(k, v);
  INSERT INTO map VALUES(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four');

  CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);

  CREATE TRIGGER tr AFTER INSERT ON t1 BEGIN
    UPDATE t1 SET c = v FROM map WHERE k=new.a AND a=new.a;
  END;
}

do_execsql_test 1.1 {
  INSERT INTO t1(a) VALUES(1);
}

do_execsql_test 1.2 {
  SELECT a, c FROM t1 ORDER BY a;
} {1 one}

do_execsql_test 1.3 {
  INSERT INTO t1(a) VALUES(2), (3), (4), (5);
  SELECT a, c FROM t1 ORDER BY a;
} {1 one 2 two 3 three 4 four 5 {}}

forcedelete test.db2
do_execsql_test 2.0 {
  ATTACH 'test.db2' AS aux;
  CREATE TABLE aux.t3(x, y);
  INSERT INTO aux.t3 VALUES('x', 'y');
}

do_catchsql_test 2.1 {
  CREATE TRIGGER tr2 AFTER INSERT ON t1 BEGIN
    UPDATE t1 SET b = y FROM aux.t3 WHERE k=new.a;
  END;
} {1 {trigger tr2 cannot reference objects in database aux}}

do_execsql_test 2.2 {
  CREATE TEMP TRIGGER tr2 AFTER INSERT ON t1 BEGIN
    UPDATE t1 SET b = y FROM aux.t3 WHERE a=new.a;
  END;
  INSERT INTO t1(a) VALUES(10), (20);
  SELECT * FROM t1;
} {
  1 {} one 
  2 {} two 
  3 {} three 
  4 {} four 
  5 {} {} 
  10 y {} 
  20 y {}
}

do_execsql_test 2.3 {
  CREATE TABLE link(f, t);
  INSERT INTO link VALUES(5, 2), (20, 10), (2, 1);
  CREATE TRIGGER tr3 BEFORE DELETE ON t1 BEGIN
    UPDATE t1 SET b=coalesce(old.b,old.c) FROM main.link WHERE a=t AND old.a=f;
  END;
  DELETE FROM t1 WHERE a=2;
  SELECT * FROM t1;
} {
  1 two one 
  3 {} three 
  4 {} four 
  5 {} {} 
  10 y {} 
  20 y {}
}

db close
sqlite3 db ""
do_catchsql_test 2.4 {
  ATTACH 'test.db' AS yyy;
  SELECT * FROM t1;
} {1 {malformed database schema (tr3) - trigger tr3 cannot reference objects in database main}}

#-------------------------------------------------------------------------
reset_db
forcedelete test.db2
do_execsql_test 3.0 {
  CREATE TABLE mmm(x, y);
  INSERT INTO mmm VALUES(1, 'one');
  INSERT INTO mmm VALUES(2, 'two');
  INSERT INTO mmm VALUES(3, 'three');

  ATTACH 'test.db2' AS aux;
  CREATE TABLE aux.t1(a, b);
  CREATE TABLE aux.mmm(x, y);
  INSERT INTO aux.mmm VALUES(1, 'ONE');
  INSERT INTO aux.mmm VALUES(2, 'TWO');
  INSERT INTO aux.mmm VALUES(3, 'THREE');

  CREATE TRIGGER aux.ttt AFTER INSERT ON t1 BEGIN
    UPDATE t1 SET b=y FROM mmm WHERE x=new.a AND a=new.a;
  END;
  
  INSERT INTO t1(a) VALUES (2);
  SELECT * FROM t1;
} {2 TWO}

#-------------------------------------------------------------------------
# Test that INSTEAD OF UPDATE triggers on views work with UPDATE...FROM
# statements. Including, if the library is built with ENABLE_HIDDEN_COLUMNS, 
# that they work correctly on views with hidden columns.
#
reset_db
do_execsql_test 4.0 {
  CREATE TABLE t1(k, a, b);
  INSERT INTO t1 VALUES('a', 1, 'one');
  INSERT INTO t1 VALUES('b', 2, 'two');
  INSERT INTO t1 VALUES('c', 3, 'three');
  INSERT INTO t1 VALUES('d', 4, 'four');

  CREATE TABLE log(x);
  CREATE VIEW v1 AS SELECT k, a, b AS __hidden__b FROM t1;
  CREATE TRIGGER tr1 INSTEAD OF UPDATE ON v1 BEGIN
    INSERT INTO log VALUES(
      '('||old.a||','||old.__hidden__b||')->('||new.a||','||new.__hidden__b||')'
    );
  END;
}

ifcapable hiddencolumns {
  do_execsql_test 4.1-hc-enabled {
    SELECT * FROM v1
  } {a 1 b 2 c 3 d 4}
} else {
  do_execsql_test 4.1-hc-disabled {
    SELECT * FROM v1
  } {a 1 one b 2 two c 3 three d 4 four}
}

do_execsql_test 4.2 {
  UPDATE v1 SET a='xyz' WHERE k IN ('a', 'c');
  SELECT * FROM log;
  DELETE FROM log;
} {
  (1,one)->(xyz,one)
  (3,three)->(xyz,three)
}

do_execsql_test 4.3 {
  CREATE TABLE map(k, v);
  INSERT INTO map VALUES('b', 'twelve');
  INSERT INTO map VALUES('d', 'fourteen');
  UPDATE v1 SET a=map.v FROM map WHERE v1.k=map.k;
  SELECT * FROM log;
  DELETE FROM log;
} {
  (2,two)->(twelve,two)
  (4,four)->(fourteen,four)
}



finish_test