os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/trigger9.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 January 1
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 some compiler optimizations for SQL statements featuring
sl@0
    13
# triggers:
sl@0
    14
#
sl@0
    15
#
sl@0
    16
#
sl@0
    17
sl@0
    18
# trigger9-1.* -   Test that if there are no references to OLD.* cols, or a
sl@0
    19
#                  reference to only OLD.rowid, the data is not loaded.
sl@0
    20
#
sl@0
    21
# trigger9-2.* -   Test that for NEW.* records populated by UPDATE 
sl@0
    22
#                  statements, unused fields are populated with NULL values. 
sl@0
    23
#
sl@0
    24
# trigger9-3.* -   Test that the temporary tables used for OLD.* references
sl@0
    25
#                  in "INSTEAD OF" triggers have NULL values in unused 
sl@0
    26
#                  fields.
sl@0
    27
#
sl@0
    28
sl@0
    29
set testdir [file dirname $argv0]
sl@0
    30
source $testdir/tester.tcl
sl@0
    31
ifcapable {!trigger} {
sl@0
    32
  finish_test
sl@0
    33
  return
sl@0
    34
}
sl@0
    35
sl@0
    36
proc has_rowdata {sql} {
sl@0
    37
  expr {[lsearch [execsql "explain $sql"] RowData]>=0}
sl@0
    38
}
sl@0
    39
sl@0
    40
do_test trigger9-1.1 {
sl@0
    41
  execsql {
sl@0
    42
    PRAGMA page_size = 1024;
sl@0
    43
    CREATE TABLE t1(x, y, z);
sl@0
    44
    INSERT INTO t1 VALUES('1', randstr(10000,10000), '2');
sl@0
    45
    INSERT INTO t1 VALUES('2', randstr(10000,10000), '4');
sl@0
    46
    INSERT INTO t1 VALUES('3', randstr(10000,10000), '6');
sl@0
    47
    CREATE TABLE t2(x);
sl@0
    48
  }
sl@0
    49
} {}
sl@0
    50
sl@0
    51
do_test trigger9-1.2.1 {
sl@0
    52
  execsql {
sl@0
    53
    BEGIN;
sl@0
    54
      CREATE TRIGGER trig1 BEFORE DELETE ON t1 BEGIN
sl@0
    55
        INSERT INTO t2 VALUES(old.rowid);
sl@0
    56
      END;
sl@0
    57
      DELETE FROM t1;
sl@0
    58
      SELECT * FROM t2;
sl@0
    59
  }
sl@0
    60
} {1 2 3}
sl@0
    61
do_test trigger9-1.2.3 {
sl@0
    62
  has_rowdata {DELETE FROM t1}
sl@0
    63
} 0
sl@0
    64
do_test trigger9-1.2.4 { execsql { ROLLBACK } } {}
sl@0
    65
sl@0
    66
do_test trigger9-1.3.1 {
sl@0
    67
  execsql {
sl@0
    68
    BEGIN;
sl@0
    69
      CREATE TRIGGER trig1 BEFORE DELETE ON t1 BEGIN
sl@0
    70
        INSERT INTO t2 VALUES(old.x);
sl@0
    71
      END;
sl@0
    72
      DELETE FROM t1;
sl@0
    73
      SELECT * FROM t2;
sl@0
    74
  }
sl@0
    75
} {1 2 3}
sl@0
    76
do_test trigger9-1.3.2 {
sl@0
    77
  has_rowdata {DELETE FROM t1}
sl@0
    78
} 1
sl@0
    79
do_test trigger9-1.3.3 { execsql { ROLLBACK } } {}
sl@0
    80
sl@0
    81
do_test trigger9-1.4.1 {
sl@0
    82
  execsql {
sl@0
    83
    BEGIN;
sl@0
    84
      CREATE TRIGGER trig1 BEFORE DELETE ON t1 WHEN old.x='1' BEGIN
sl@0
    85
        INSERT INTO t2 VALUES(old.rowid);
sl@0
    86
      END;
sl@0
    87
      DELETE FROM t1;
sl@0
    88
      SELECT * FROM t2;
sl@0
    89
  }
sl@0
    90
} {1}
sl@0
    91
do_test trigger9-1.4.2 {
sl@0
    92
  has_rowdata {DELETE FROM t1}
sl@0
    93
} 1
sl@0
    94
do_test trigger9-1.4.3 { execsql { ROLLBACK } } {}
sl@0
    95
sl@0
    96
do_test trigger9-1.5.1 {
sl@0
    97
  execsql {
sl@0
    98
    BEGIN;
sl@0
    99
      CREATE TRIGGER trig1 BEFORE UPDATE ON t1 BEGIN
sl@0
   100
        INSERT INTO t2 VALUES(old.rowid);
sl@0
   101
      END;
sl@0
   102
      UPDATE t1 SET y = '';
sl@0
   103
      SELECT * FROM t2;
sl@0
   104
  }
sl@0
   105
} {1 2 3}
sl@0
   106
do_test trigger9-1.5.2 {
sl@0
   107
  has_rowdata {UPDATE t1 SET y = ''}
sl@0
   108
} 0
sl@0
   109
do_test trigger9-1.5.3 { execsql { ROLLBACK } } {}
sl@0
   110
sl@0
   111
do_test trigger9-1.6.1 {
sl@0
   112
  execsql {
sl@0
   113
    BEGIN;
sl@0
   114
      CREATE TRIGGER trig1 BEFORE UPDATE ON t1 BEGIN
sl@0
   115
        INSERT INTO t2 VALUES(old.x);
sl@0
   116
      END;
sl@0
   117
      UPDATE t1 SET y = '';
sl@0
   118
      SELECT * FROM t2;
sl@0
   119
  }
sl@0
   120
} {1 2 3}
sl@0
   121
do_test trigger9-1.6.2 {
sl@0
   122
  has_rowdata {UPDATE t1 SET y = ''}
sl@0
   123
} 1
sl@0
   124
do_test trigger9-1.6.3 { execsql { ROLLBACK } } {}
sl@0
   125
sl@0
   126
do_test trigger9-1.7.1 {
sl@0
   127
  execsql {
sl@0
   128
    BEGIN;
sl@0
   129
      CREATE TRIGGER trig1 BEFORE UPDATE ON t1 WHEN old.x>='2' BEGIN
sl@0
   130
        INSERT INTO t2 VALUES(old.x);
sl@0
   131
      END;
sl@0
   132
      UPDATE t1 SET y = '';
sl@0
   133
      SELECT * FROM t2;
sl@0
   134
  }
sl@0
   135
} {2 3}
sl@0
   136
do_test trigger9-1.7.2 {
sl@0
   137
  has_rowdata {UPDATE t1 SET y = ''}
sl@0
   138
} 1
sl@0
   139
do_test trigger9-1.7.3 { execsql { ROLLBACK } } {}
sl@0
   140
sl@0
   141
do_test trigger9-3.1 {
sl@0
   142
  execsql {
sl@0
   143
    CREATE TABLE t3(a, b);
sl@0
   144
    INSERT INTO t3 VALUES(1, 'one');
sl@0
   145
    INSERT INTO t3 VALUES(2, 'two');
sl@0
   146
    INSERT INTO t3 VALUES(3, 'three');
sl@0
   147
  }
sl@0
   148
} {}
sl@0
   149
do_test trigger9-3.2 {
sl@0
   150
  execsql {
sl@0
   151
    BEGIN;
sl@0
   152
      CREATE VIEW v1 AS SELECT * FROM t3;
sl@0
   153
      CREATE TRIGGER trig1 INSTEAD OF UPDATE ON v1 BEGIN
sl@0
   154
        INSERT INTO t2 VALUES(old.a);
sl@0
   155
      END;
sl@0
   156
      UPDATE v1 SET b = 'hello';
sl@0
   157
      SELECT * FROM t2;
sl@0
   158
    ROLLBACK;
sl@0
   159
  }
sl@0
   160
} {1 2 3}
sl@0
   161
do_test trigger9-3.3 {
sl@0
   162
  # In this test the 'c' column of the view is not required by
sl@0
   163
  # the INSTEAD OF trigger, but the expression is reused internally as
sl@0
   164
  # part of the view's WHERE clause. Check that this does not cause
sl@0
   165
  # a problem.
sl@0
   166
  #
sl@0
   167
  execsql {
sl@0
   168
    BEGIN;
sl@0
   169
      CREATE VIEW v1 AS SELECT a, b AS c FROM t3 WHERE c > 'one';
sl@0
   170
      CREATE TRIGGER trig1 INSTEAD OF UPDATE ON v1 BEGIN
sl@0
   171
        INSERT INTO t2 VALUES(old.a);
sl@0
   172
      END;
sl@0
   173
      UPDATE v1 SET c = 'hello';
sl@0
   174
      SELECT * FROM t2;
sl@0
   175
    ROLLBACK;
sl@0
   176
  }
sl@0
   177
} {2 3}
sl@0
   178
do_test trigger9-3.4 {
sl@0
   179
  execsql {
sl@0
   180
    BEGIN;
sl@0
   181
      INSERT INTO t3 VALUES(3, 'three');
sl@0
   182
      INSERT INTO t3 VALUES(3, 'four');
sl@0
   183
      CREATE VIEW v1 AS SELECT DISTINCT a, b FROM t3;
sl@0
   184
      CREATE TRIGGER trig1 INSTEAD OF UPDATE ON v1 BEGIN
sl@0
   185
        INSERT INTO t2 VALUES(old.a);
sl@0
   186
      END;
sl@0
   187
      UPDATE v1 SET b = 'hello';
sl@0
   188
      SELECT * FROM t2;
sl@0
   189
    ROLLBACK;
sl@0
   190
  }
sl@0
   191
} {1 2 3 3}
sl@0
   192
sl@0
   193
ifcapable compound {
sl@0
   194
  do_test trigger9-3.5 {
sl@0
   195
    execsql {
sl@0
   196
      BEGIN;
sl@0
   197
        INSERT INTO t3 VALUES(1, 'uno');
sl@0
   198
        CREATE VIEW v1 AS SELECT a, b FROM t3 EXCEPT SELECT 1, 'one';
sl@0
   199
        CREATE TRIGGER trig1 INSTEAD OF UPDATE ON v1 BEGIN
sl@0
   200
          INSERT INTO t2 VALUES(old.a);
sl@0
   201
        END;
sl@0
   202
        UPDATE v1 SET b = 'hello';
sl@0
   203
        SELECT * FROM t2;
sl@0
   204
      ROLLBACK;
sl@0
   205
    }
sl@0
   206
  } {1 2 3}
sl@0
   207
  do_test trigger9-3.6 {
sl@0
   208
    execsql {
sl@0
   209
      BEGIN;
sl@0
   210
        INSERT INTO t3 VALUES(1, 'zero');
sl@0
   211
        CREATE VIEW v1 AS 
sl@0
   212
          SELECT sum(a) AS a, max(b) AS b FROM t3 GROUP BY t3.a HAVING b>'two';
sl@0
   213
        CREATE TRIGGER trig1 INSTEAD OF UPDATE ON v1 BEGIN
sl@0
   214
          INSERT INTO t2 VALUES(old.a);
sl@0
   215
        END;
sl@0
   216
        UPDATE v1 SET b = 'hello';
sl@0
   217
        SELECT * FROM t2;
sl@0
   218
      ROLLBACK;
sl@0
   219
    }
sl@0
   220
  } {2}
sl@0
   221
}
sl@0
   222
sl@0
   223
finish_test