1 # 2004 December 07 2 # 3 # The author disclaims copyright to this source code. In place of 4 # a legal notice, here is a blessing: 5 # 6 # May you do good and not evil. 7 # May you find forgiveness for yourself and forgive others. 8 # May you share freely, never taking more than you give. 9 # 10 #*********************************************************************** 11 # This file implements regression tests for SQLite library. 12 # 13 # This file implements tests to make sure expression of an INSERT 14 # and UPDATE statement are only evaluated once. See ticket #980. 15 # If an expression uses a function that has side-effects or which 16 # is not deterministic (ex: random()) then we want to make sure 17 # that the same evaluation occurs for the actual INSERT/UPDATE and 18 # for the NEW.* fields of any triggers that fire. 19 # 20 # $Id: trigger6.test,v 1.2 2005/05/05 11:04:50 drh Exp $ 21 22 set testdir [file dirname $argv0] 23 source $testdir/tester.tcl 24 ifcapable {!trigger} { 25 finish_test 26 return 27 } 28 29 do_test trigger6-1.1 { 30 execsql { 31 CREATE TABLE t1(x, y); 32 CREATE TABLE log(a, b, c); 33 CREATE TRIGGER r1 BEFORE INSERT ON t1 BEGIN 34 INSERT INTO log VALUES(1, new.x, new.y); 35 END; 36 CREATE TRIGGER r2 BEFORE UPDATE ON t1 BEGIN 37 INSERT INTO log VALUES(2, new.x, new.y); 38 END; 39 } 40 set ::trigger6_cnt 0 41 proc trigger6_counter {args} { 42 incr ::trigger6_cnt 43 return $::trigger6_cnt 44 } 45 db function counter trigger6_counter 46 execsql { 47 INSERT INTO t1 VALUES(1,counter()); 48 SELECT * FROM t1; 49 } 50 } {1 1} 51 do_test trigger6-1.2 { 52 execsql { 53 SELECT * FROM log; 54 } 55 } {1 1 1} 56 do_test trigger6-1.3 { 57 execsql { 58 DELETE FROM t1; 59 DELETE FROM log; 60 INSERT INTO t1 VALUES(2,counter(2,3)+4); 61 SELECT * FROM t1; 62 } 63 } {2 6} 64 do_test trigger6-1.4 { 65 execsql { 66 SELECT * FROM log; 67 } 68 } {1 2 6} 69 do_test trigger6-1.5 { 70 execsql { 71 DELETE FROM log; 72 UPDATE t1 SET y=counter(5); 73 SELECT * FROM t1; 74 } 75 } {2 3} 76 do_test trigger6-1.6 { 77 execsql { 78 SELECT * FROM log; 79 } 80 } {2 2 3} 81 82 finish_test 83