summaryrefslogtreecommitdiffstats
path: root/test/triggerF.test
blob: 2e3e35e3b2dcb1bc74607bfd2f2d8e90190c82ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 2017 January 4
#
# 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 triggerF
ifcapable {!trigger} {
  finish_test
  return
}


foreach {tn sql log} {
  1 {} {}

  2 { 
    CREATE TRIGGER trd AFTER DELETE ON t1 BEGIN
      INSERT INTO log VALUES(old.a || old.b || (SELECT count(*) FROM t1));
    END;
  } {1one2 2two1 3three1}

  3 { 
    CREATE TRIGGER trd BEFORE DELETE ON t1 BEGIN
      INSERT INTO log VALUES(old.a || old.b || (SELECT count(*) FROM t1));
    END;
  } {1one3 2two2 3three2}

  4 { 
    CREATE TRIGGER tr1 AFTER DELETE ON t1 BEGIN
      INSERT INTO log VALUES(old.a || old.b || (SELECT count(*) FROM t1));
    END;
    CREATE TRIGGER tr2 BEFORE DELETE ON t1 BEGIN
      INSERT INTO log VALUES(old.a || old.b || (SELECT count(*) FROM t1));
    END;
  } {1one3 1one2 2two2 2two1 3three2 3three1}

} {
  reset_db
  do_execsql_test 1.$tn.0 {
    PRAGMA recursive_triggers = on;
    CREATE TABLE t1(a INT PRIMARY KEY, b) WITHOUT ROWID;
    CREATE TABLE log(t);
  }
  
  execsql $sql

  do_execsql_test 1.$tn.1 {
    INSERT INTO t1 VALUES(1, 'one');
    INSERT INTO t1 VALUES(2, 'two');
    INSERT INTO t1 VALUES(3, 'three');

    DELETE FROM t1 WHERE a=1;
    INSERT OR REPLACE INTO t1 VALUES(2, 'three');
    UPDATE OR REPLACE t1 SET a=3 WHERE a=2;
  }

  do_execsql_test 1.$tn.2 {
    SELECT * FROM log ORDER BY rowid;
  } $log
}

finish_test