-- @@@ START COPYRIGHT @@@
--
-- Licensed to the Apache Software Foundation (ASF) under one
-- or more contributor license agreements.  See the NOTICE file
-- distributed with this work for additional information
-- regarding copyright ownership.  The ASF licenses this file
-- to you under the Apache License, Version 2.0 (the
-- "License"); you may not use this file except in compliance
-- with the License.  You may obtain a copy of the License at
--
--   http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing,
-- software distributed under the License is distributed on an
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-- KIND, either express or implied.  See the License for the
-- specific language governing permissions and limitations
-- under the License.
--
-- @@@ END COPYRIGHT @@@
-- Test for VEG infinite loop bug #34

------------------------------------------------------------------
-- SET ENVIRONMENT
------------------------------------------------------------------


obey TEST_13_3(clean_up);
log   LOG_13_3 clear;
obey TEST_13_3(set_up);
obey TEST_13_3(tests);
LOG;
obey TEST_13_3(clean_up);
exit;

?section clean_up
set schema CAT1.SCHM;
DROP TRIGGER tr133_1;
DROP TRIGGER tr133_2;
DROP TRIGGER tr133_3;
DROP TABLE   t133;

DELETE FROM T1;
DELETE FROM T2;

------------------------------------------------------------------
obey clearTables;

?section set_up

INSERT INTO T1 VALUES (1,2), (101, 102), (201, 202), (301, 302);
INSERT INTO T2 VALUES (1,2), (101, 102), (201, 202), (301, 302);

CREATE TABLE T133
	(op CHAR(10),
	 a  INT,
	 b  INT);

-- Before Trigger on T1
CREATE TRIGGER tr133_1 BEFORE UPDATE ON T1
	REFERENCING OLD AS MY_OLD, NEW AS MY_NEW
	FOR EACH ROW
	SET  MY_NEW.a = MY_NEW.a+50;

-- Row Trigger on T1, write to T2
CREATE TRIGGER tr133_2 AFTER UPDATE ON T1
	REFERENCING OLD AS MY_OLD, NEW AS MY_NEW
	FOR EACH ROW
	UPDATE T2
		SET   a = MY_NEW.a
		WHERE a = MY_OLD.a;

-- Statement trigger on T2, write to T133
CREATE TRIGGER tr133_3 AFTER UPDATE ON T2
	REFERENCING OLD AS MY_OLD, NEW AS MY_NEW
	FOR EACH STATEMENT
	INSERT INTO T133 VALUES ('Update', 
                             (SELECT MAX(a) FROM MY_OLD), 
                             (SELECT MAX(a) FROM MY_NEW));

?section tests
------------------------------------------------------------------
--          TEST CASE
------------------------------------------------------------------

UPDATE T1 SET A=1 WHERE A>100;

SELECT * FROM T1;
SELECT * FROM T2;
SELECT * FROM T133;

------------------------------------------------------------------
--           END
------------------------------------------------------------------
