-- @@@ 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 @@@
-- HL_R2_Triggers_Spec.doc: SPJ enhancements to Triggers
-- AFTER trigger with a CALL triggered action with NO SQL, READ SQL DATA,
-- and MODIFIES SQL DATA access modes.
-- Result: Success

obey TEST_SPJC_PCTA_001(clean_up);
obey TEST_SPJC_PCTA_001(set_up);
log  LOG_SPJC_PCTA_001 clear;
obey TEST_SPJC_PCTA_001(tests);
LOG;
obey TEST_SPJC_PCTA_001(clean_up);
exit;

?section clean_up
set schema CAT1.SCHM;

sh rm -f sum_of_ints1.txt
sh rm -f sum_of_ints2.txt
sh rm -f sum_of_1col1.txt

?section set_up
set schema CAT1.SCHM;

SET ENVVAR ALLOW_CS_IN_SQLCI 1;
CONTROL QUERY DEFAULT POS 'OFF';

create table TAB01_SPJC_PCTA_001(a int, b int, c int, d int);
create table TAB02_SPJC_PCTA_001(a int, b int, c int, d int);

INSERT INTO TAB01_SPJC_PCTA_001 VALUES (1, 2, 3, 4),
				(8, 6, 4, 2),
				(3, 6, 9, 12);

?section tests

------------------------------------------------------------------
-- TEST CASE 01: Row trigger on insert.
-- Result: Success
------------------------------------------------------------------

CREATE TRIGGER TRIG01_SPJC_PCTA_001 AFTER INSERT ON
TAB01_SPJC_PCTA_001
REFERENCING NEW ROW AS N
FOR EACH ROW
	CALL WriteSumOfFourIntsToFile('sum_of_ints1.txt',
		N.A, N.B, N.C, N.D);

------------------------------------------------------------------

------------------------------------------------------------------
-- TEST CASE 02: Row trigger on update.
-- Result: Success
------------------------------------------------------------------

CREATE TRIGGER TRIG02_SPJC_PCTA_001 AFTER UPDATE ON
TAB01_SPJC_PCTA_001
REFERENCING OLD ROW AS O
FOR EACH ROW
	CALL WriteSumOfOneColumnToFile('sum_of_1col1.txt',
		'A', 'CAT1.SCHM.TAB01_SPJC_PCTA_001');

------------------------------------------------------------------

------------------------------------------------------------------
-- TEST CASE 03: Row trigger on delete.
-- Result: Success
------------------------------------------------------------------

CREATE TRIGGER TRIG03_SPJC_PCTA_001 AFTER DELETE ON
TAB01_SPJC_PCTA_001
REFERENCING OLD ROW AS O
FOR EACH ROW
BEGIN ATOMIC
	CALL InsertFourCoulmns('CAT1.SCHM.TAB02_SPJC_PCTA_001',
			O.A, O.B, O.C, O.D);
END;

------------------------------------------------------------------

-- The tests

--	triggers a call to a procedure returning sum of inputs
INSERT INTO TAB01_SPJC_PCTA_001 VALUES(101, 201, 301, 401);

--	Result: 1004
sh cat sum_of_ints1.txt >> LOG_SPJC_PCTA_001;

--	triggers a call to procedure returning SUM of A of TAB01_SPJC_PCTA_001
UPDATE TAB01_SPJC_PCTA_001 set a = a + 1000, b = b + 1000,
		c = c + 1000, d = d + 1000 where a = 3;

--	Result: 1113
sh cat sum_of_1col1.txt >> LOG_SPJC_PCTA_001;

--	triggers a call to a procedure that inserts deleted row into
--	TAB02_SPJC_PCTA_001
DELETE FROM TAB01_SPJC_PCTA_001 where a = 8;

-- Result: old row in TAB02_SPJC_PCTA_001
select * from TAB02_SPJC_PCTA_001;
------------------------------------------------------------------
