os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/triggerB.test
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
# 2008 April 15
sl@0
     2
#
sl@0
     3
# The author disclaims copyright to this source code.  In place of
sl@0
     4
# a legal notice', here is a blessing:
sl@0
     5
#
sl@0
     6
#    May you do good and not evil.
sl@0
     7
#    May you find forgiveness for yourself and forgive others.
sl@0
     8
#    May you share freely, never taking more than you give.
sl@0
     9
#
sl@0
    10
#***********************************************************************
sl@0
    11
# This file implements regression tests for SQLite library. Specifically,
sl@0
    12
# it tests updating tables with constraints within a trigger.  Ticket #3055.
sl@0
    13
#
sl@0
    14
sl@0
    15
set testdir [file dirname $argv0]
sl@0
    16
source $testdir/tester.tcl
sl@0
    17
ifcapable {!trigger} {
sl@0
    18
  finish_test
sl@0
    19
  return
sl@0
    20
}
sl@0
    21
sl@0
    22
# Create test tables with constraints.
sl@0
    23
#
sl@0
    24
do_test triggerB-1.1 {
sl@0
    25
  execsql {
sl@0
    26
    CREATE TABLE x(x INTEGER PRIMARY KEY, y INT NOT NULL);
sl@0
    27
    INSERT INTO x(y) VALUES(1);
sl@0
    28
    INSERT INTO x(y) VALUES(1);
sl@0
    29
    CREATE TEMP VIEW vx AS SELECT x, y, 0 AS yy FROM x;
sl@0
    30
    CREATE TEMP TRIGGER tx INSTEAD OF UPDATE OF y ON vx
sl@0
    31
    BEGIN
sl@0
    32
      UPDATE x SET y = new.y WHERE x = new.x;
sl@0
    33
    END;
sl@0
    34
    SELECT * FROM vx;
sl@0
    35
  }
sl@0
    36
} {1 1 0 2 1 0}
sl@0
    37
do_test triggerB-1.2 {
sl@0
    38
  execsql {
sl@0
    39
    UPDATE vx SET y = yy;
sl@0
    40
    SELECT * FROM vx;
sl@0
    41
  }
sl@0
    42
} {1 0 0 2 0 0}
sl@0
    43
sl@0
    44
# Added 2008-08-22:
sl@0
    45
#
sl@0
    46
# Name resolution within triggers.
sl@0
    47
#
sl@0
    48
do_test triggerB-2.1 {
sl@0
    49
  catchsql {
sl@0
    50
    CREATE TRIGGER ty AFTER INSERT ON x BEGIN
sl@0
    51
       SELECT wen.x; -- Unrecognized name
sl@0
    52
    END;
sl@0
    53
    INSERT INTO x VALUES(1,2);
sl@0
    54
  }
sl@0
    55
} {1 {no such column: wen.x}}
sl@0
    56
do_test triggerB-2.2 {
sl@0
    57
  catchsql {
sl@0
    58
    CREATE TRIGGER tz AFTER UPDATE ON x BEGIN
sl@0
    59
       SELECT dlo.x; -- Unrecognized name
sl@0
    60
    END;
sl@0
    61
    UPDATE x SET y=y+1;
sl@0
    62
  }
sl@0
    63
} {1 {no such column: dlo.x}}
sl@0
    64
sl@0
    65
do_test triggerB-2.3 {
sl@0
    66
  execsql {
sl@0
    67
    CREATE TABLE t2(a INTEGER PRIMARY KEY, b);
sl@0
    68
    INSERT INTO t2 VALUES(1,2);
sl@0
    69
    CREATE TABLE changes(x,y);
sl@0
    70
    CREATE TRIGGER r1t2 AFTER UPDATE ON t2 BEGIN
sl@0
    71
      INSERT INTO changes VALUES(new.a, new.b);
sl@0
    72
    END;
sl@0
    73
  }
sl@0
    74
  execsql {
sl@0
    75
    UPDATE t2 SET a=a+10;
sl@0
    76
    SELECT * FROM changes;
sl@0
    77
  }
sl@0
    78
} {11 2}
sl@0
    79
do_test triggerB-2.4 {
sl@0
    80
  execsql {
sl@0
    81
    CREATE TRIGGER r2t2 AFTER DELETE ON t2 BEGIN
sl@0
    82
      INSERT INTO changes VALUES(old.a, old.c);
sl@0
    83
    END;
sl@0
    84
  }
sl@0
    85
  catchsql {
sl@0
    86
    DELETE FROM t2;
sl@0
    87
  }
sl@0
    88
} {1 {no such column: old.c}}
sl@0
    89
sl@0
    90
# Triggers maintain a mask of columns from the invoking table that are
sl@0
    91
# used in the trigger body as NEW.column or OLD.column.  That mask is then
sl@0
    92
# used to reduce the amount of information that needs to be loaded into
sl@0
    93
# the NEW and OLD pseudo-tables at run-time.
sl@0
    94
#
sl@0
    95
# These tests cases check the logic for when there are many columns - more
sl@0
    96
# than will fit in a bitmask.
sl@0
    97
#
sl@0
    98
do_test triggerB-3.1 {
sl@0
    99
  execsql {
sl@0
   100
    CREATE TABLE t3(
sl@0
   101
       c0,  c1,  c2,  c3,  c4,  c5,  c6,  c7,  c8,  c9,
sl@0
   102
       c10, c11, c12, c13, c14, c15, c16, c17, c18, c19,
sl@0
   103
       c20, c21, c22, c23, c24, c25, c26, c27, c28, c29,
sl@0
   104
       c30, c31, c32, c33, c34, c35, c36, c37, c38, c39,
sl@0
   105
       c40, c41, c42, c43, c44, c45, c46, c47, c48, c49,
sl@0
   106
       c50, c51, c52, c53, c54, c55, c56, c57, c58, c59,
sl@0
   107
       c60, c61, c62, c63, c64, c65
sl@0
   108
    );
sl@0
   109
    CREATE TABLE t3_changes(colnum, oldval, newval);
sl@0
   110
    INSERT INTO t3 VALUES(
sl@0
   111
       'a0', 'a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9',
sl@0
   112
       'a10','a11','a12','a13','a14','a15','a16','a17','a18','a19',
sl@0
   113
       'a20','a21','a22','a23','a24','a25','a26','a27','a28','a29',
sl@0
   114
       'a30','a31','a32','a33','a34','a35','a36','a37','a38','a39',
sl@0
   115
       'a40','a41','a42','a43','a44','a45','a46','a47','a48','a49',
sl@0
   116
       'a50','a51','a52','a53','a54','a55','a56','a57','a58','a59',
sl@0
   117
       'a60','a61','a62','a63','a64','a65'
sl@0
   118
    );
sl@0
   119
  }
sl@0
   120
  for {set i 0} {$i<=65} {incr i} {
sl@0
   121
    set sql [subst {
sl@0
   122
      CREATE TRIGGER t3c$i AFTER UPDATE ON t3
sl@0
   123
         WHEN old.c$i!=new.c$i BEGIN
sl@0
   124
          INSERT INTO t3_changes VALUES($i, old.c$i, new.c$i);
sl@0
   125
      END
sl@0
   126
    }]
sl@0
   127
    db eval $sql
sl@0
   128
  }
sl@0
   129
  execsql {
sl@0
   130
    SELECT * FROM t3_changes
sl@0
   131
  }
sl@0
   132
} {}
sl@0
   133
for {set i 0} {$i<=64} {incr i} {
sl@0
   134
  do_test triggerB-3.2.$i.1 [subst {
sl@0
   135
    execsql {
sl@0
   136
      UPDATE t3 SET c$i='b$i';
sl@0
   137
      SELECT * FROM t3_changes ORDER BY rowid DESC LIMIT 1;
sl@0
   138
    }
sl@0
   139
  }] [subst {$i a$i b$i}]
sl@0
   140
  do_test triggerB-3.2.$i.2 [subst {
sl@0
   141
    execsql {
sl@0
   142
      SELECT count(*) FROM t3_changes
sl@0
   143
    }
sl@0
   144
  }] [expr {$i+1}]
sl@0
   145
  do_test triggerB-3.2.$i.2 [subst {
sl@0
   146
    execsql {
sl@0
   147
      SELECT * FROM t3_changes WHERE colnum=$i
sl@0
   148
    }
sl@0
   149
  }] [subst {$i a$i b$i}]
sl@0
   150
}
sl@0
   151
  
sl@0
   152
sl@0
   153
finish_test