summaryrefslogtreecommitdiffstats
path: root/test/trigger9.test
blob: 47940de5778e9019700f58cf05fc85d8417a4465 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# 2008 January 1
#
# 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.
#
#***********************************************************************
# This file implements regression tests for SQLite library. Specifically,
# it tests some compiler optimizations for SQL statements featuring
# triggers:
#
#
#

# trigger9-1.* -   Test that if there are no references to OLD.* cols, or a
#                  reference to only OLD.rowid, the data is not loaded.
#
# trigger9-2.* -   Test that for NEW.* records populated by UPDATE 
#                  statements, unused fields are populated with NULL values. 
#
# trigger9-3.* -   Test that the temporary tables used for OLD.* references
#                  in "INSTEAD OF" triggers have NULL values in unused 
#                  fields.
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl
ifcapable {!trigger} {
  finish_test
  return
}
set ::testprefix trigger9

proc has_rowdata {sql} {
  expr {[lsearch [execsql "explain $sql"] RowData]>=0}
}

do_test trigger9-1.1 {
  execsql {
    PRAGMA page_size = 1024;
    CREATE TABLE t1(x, y, z);
    INSERT INTO t1 VALUES('1', randstr(10000,10000), '2');
    INSERT INTO t1 VALUES('2', randstr(10000,10000), '4');
    INSERT INTO t1 VALUES('3', randstr(10000,10000), '6');
    CREATE TABLE t2(x);
  }
} {}

do_test trigger9-1.2.1 {
  execsql {
    BEGIN;
      CREATE TRIGGER trig1 BEFORE DELETE ON t1 BEGIN
        INSERT INTO t2 VALUES(old.rowid);
      END;
      DELETE FROM t1;
      SELECT * FROM t2;
  }
} {1 2 3}
do_test trigger9-1.2.3 {
  has_rowdata {DELETE FROM t1}
} 0
do_test trigger9-1.2.4 { execsql { ROLLBACK } } {}

do_test trigger9-1.3.1 {
  execsql {
    BEGIN;
      CREATE TRIGGER trig1 BEFORE DELETE ON t1 BEGIN
        INSERT INTO t2 VALUES(old.x);
      END;
      DELETE FROM t1;
      SELECT * FROM t2;
  }
} {1 2 3}
do_test trigger9-1.3.2 {
  has_rowdata {DELETE FROM t1}
} 0
do_test trigger9-1.3.3 { execsql { ROLLBACK } } {}

do_test trigger9-1.4.1 {
  execsql {
    BEGIN;
      CREATE TRIGGER trig1 BEFORE DELETE ON t1 WHEN old.x='1' BEGIN
        INSERT INTO t2 VALUES(old.rowid);
      END;
      DELETE FROM t1;
      SELECT * FROM t2;
  }
} {1}
do_test trigger9-1.4.2 {
  has_rowdata {DELETE FROM t1}
} 0
do_test trigger9-1.4.3 { execsql { ROLLBACK } } {}

do_test trigger9-1.5.1 {
  execsql {
    BEGIN;
      CREATE TRIGGER trig1 BEFORE UPDATE ON t1 BEGIN
        INSERT INTO t2 VALUES(old.rowid);
      END;
      UPDATE t1 SET y = '';
      SELECT * FROM t2;
  }
} {1 2 3}
do_test trigger9-1.5.2 {
  has_rowdata {UPDATE t1 SET y = ''}
} 0
do_test trigger9-1.5.3 { execsql { ROLLBACK } } {}

do_test trigger9-1.6.1 {
  execsql {
    BEGIN;
      CREATE TRIGGER trig1 BEFORE UPDATE ON t1 BEGIN
        INSERT INTO t2 VALUES(old.x);
      END;
      UPDATE t1 SET y = '';
      SELECT * FROM t2;
  }
} {1 2 3}
do_test trigger9-1.6.2 {
  has_rowdata {UPDATE t1 SET y = ''}
} 0
do_test trigger9-1.6.3 { execsql { ROLLBACK } } {}

do_test trigger9-1.7.1 {
  execsql {
    BEGIN;
      CREATE TRIGGER trig1 BEFORE UPDATE ON t1 WHEN old.x>='2' BEGIN
        INSERT INTO t2 VALUES(old.x);
      END;
      UPDATE t1 SET y = '';
      SELECT * FROM t2;
  }
} {2 3}
do_test trigger9-1.7.2 {
  has_rowdata {UPDATE t1 SET y = ''}
} 0
do_test trigger9-1.7.3 { execsql { ROLLBACK } } {}

do_test trigger9-3.1 {
  execsql {
    CREATE TABLE t3(a, b);
    INSERT INTO t3 VALUES(1, 'one');
    INSERT INTO t3 VALUES(2, 'two');
    INSERT INTO t3 VALUES(3, 'three');
  }
} {}
do_test trigger9-3.2 {
  execsql {
    BEGIN;
      CREATE VIEW v1 AS SELECT * FROM t3;
      CREATE TRIGGER trig1 INSTEAD OF UPDATE ON v1 BEGIN
        INSERT INTO t2 VALUES(old.a);
      END;
      UPDATE v1 SET b = 'hello';
      SELECT * FROM t2;
    ROLLBACK;
  }
} {1 2 3}
do_test trigger9-3.3 {
  # In this test the 'c' column of the view is not required by
  # the INSTEAD OF trigger, but the expression is reused internally as
  # part of the view's WHERE clause. Check that this does not cause
  # a problem.
  #
  execsql {
    BEGIN;
      CREATE VIEW v1 AS SELECT a, b AS c FROM t3 WHERE c > 'one';
      CREATE TRIGGER trig1 INSTEAD OF UPDATE ON v1 BEGIN
        INSERT INTO t2 VALUES(old.a);
      END;
      UPDATE v1 SET c = 'hello';
      SELECT * FROM t2;
    ROLLBACK;
  }
} {2 3}
do_test trigger9-3.4 {
  execsql {
    BEGIN;
      INSERT INTO t3 VALUES(3, 'three');
      INSERT INTO t3 VALUES(3, 'four');
      CREATE VIEW v1 AS SELECT DISTINCT a, b FROM t3;
      CREATE TRIGGER trig1 INSTEAD OF UPDATE ON v1 BEGIN
        INSERT INTO t2 VALUES(old.a);
      END;
      UPDATE v1 SET b = 'hello';
      SELECT * FROM t2;
    ROLLBACK;
  }
} {1 2 3 3}

ifcapable compound {
  do_test trigger9-3.5 {
    execsql {
      BEGIN;
        INSERT INTO t3 VALUES(1, 'uno');
        CREATE VIEW v1 AS SELECT a, b FROM t3 EXCEPT SELECT 1, 'one';
        CREATE TRIGGER trig1 INSTEAD OF UPDATE ON v1 BEGIN
          INSERT INTO t2 VALUES(old.a);
        END;
        UPDATE v1 SET b = 'hello';
        SELECT * FROM t2;
      ROLLBACK;
    }
  } {1 2 3}
  do_test trigger9-3.6 {
    execsql {
      BEGIN;
        INSERT INTO t3 VALUES(1, 'zero');
        CREATE VIEW v1 AS 
          SELECT sum(a) AS a, max(b) AS b FROM t3 GROUP BY t3.a HAVING b>'two';
        CREATE TRIGGER trig1 INSTEAD OF UPDATE ON v1 BEGIN
          INSERT INTO t2 VALUES(old.a);
        END;
        UPDATE v1 SET b = 'hello';
        SELECT * FROM t2;
      ROLLBACK;
    }
  } {2}
}

reset_db
do_execsql_test 4.1 {
  CREATE TABLE t1(a, b);
  CREATE TABLE log(x);
  INSERT INTO t1 VALUES(1, 2);
  INSERT INTO t1 VALUES(3, 4);
  CREATE VIEW v1 AS SELECT a, b FROM t1;

  CREATE TRIGGER tr1 INSTEAD OF DELETE ON v1 BEGIN
    INSERT INTO log VALUES('delete');
  END;

  CREATE TRIGGER tr2 INSTEAD OF UPDATE ON v1 BEGIN
    INSERT INTO log VALUES('update');
  END;

  CREATE TRIGGER tr3 INSTEAD OF INSERT ON v1 BEGIN
    INSERT INTO log VALUES('insert');
  END;
}

ifcapable !allow_rowid_in_view {
  do_catchsql_test 4.2 {
    DELETE FROM v1 WHERE rowid=1;
  } {1 {no such column: rowid}}

  do_catchsql_test 4.3 {
    UPDATE v1 SET a=b WHERE rowid=2;
  } {1 {no such column: rowid}}
} else {
  do_execsql_test 4.2a {
    DELETE FROM log;
  }
  do_catchsql_test 4.2 {
    DELETE FROM v1 WHERE rowid=1;
  } {0 {}}
  do_catchsql_test 4.3 {
    UPDATE v1 SET a=b WHERE rowid=2;
  } {0 {}}
  do_execsql_test 4.3b {
    SELECT * FROM log;
  }
}

finish_test