-- @@@ 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
-- Success: Row/Statement trigger with a CALL triggered action

obey TEST_SPJC_STX_006(clean_up);
obey TEST_SPJC_STX_006(set_up);
LOG LOG_SPJC_STX_006 clear;
obey TEST_SPJC_STX_006(tests);
LOG;
obey TEST_SPJC_STX_006(clean_up);
exit;

?section clean_up
set schema CAT1.SCHM;
drop trigger TRIG01_SPJC_STX_006;
drop trigger TRIG02_SPJC_STX_006;

drop table TAB01_SPJC_STX_006;
drop table TAB02_SPJC_STX_006;

?section set_up
SET SCHEMA CAT1.SCHM;

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

create table TAB01_SPJC_STX_006(a int, b int, c int, d int);
create table TAB02_SPJC_STX_006(a int, b int, c int, d int);

INSERT INTO TAB01_SPJC_STX_006 VALUES (1,2,3,4),
			(2,3,4,5),
			(100,101,102,103);


?section tests

------------------------------------------------------------------
-- TEST CASE 01: Row trigger with a CALL triggered action
-- Inserts supplied values into TAB02_SPJC_STX_006 by calling a procedure
-- Result: Success
------------------------------------------------------------------

CREATE TRIGGER TRIG01_SPJC_STX_006 AFTER DELETE ON
TAB01_SPJC_STX_006
REFERENCING OLD ROW AS O
FOR EACH ROW
	CALL InsertFourCoulmns('CAT1.SCHM.TAB02_SPJC_STX_006',
		O.A, O.B, O.C, O.D);

delete from TAB01_SPJC_STX_006 where a = 100;

-- Result: TAB01_SPJC_STX_006 has only two rows and
-- TAB02_SPJC_STX_006 has a copy of the deleted row.

select * from TAB01_SPJC_STX_006;

select * from TAB02_SPJC_STX_006;

------------------------------------------------------------------
-- TEST CASE 02: Statement trigger with a CALL triggered action
-- Inserts SUM of each column from TAB01_SPJC_STX_006 into
-- corresponding columns of TAB02_SPJC_STX_006.
-- Result: Success
------------------------------------------------------------------
CREATE TRIGGER TRIG02_SPJC_STX_006 AFTER UPDATE ON
TAB01_SPJC_STX_006 REFERENCING NEW TABLE AS NT, OLD TABLE AS OT
FOR EACH STATEMENT
	CALL InsertSelectOfSumOfFourColumns('CAT1.SCHM.TAB01_SPJC_STX_006',
		'CAT1.SCHM.TAB02_SPJC_STX_006', 'A', 'B', 'C', 'D');

update TAB01_SPJC_STX_006 set a = a + 1;

-- Result: TAB01_SPJC_STX_006 has two rows ( A = {2, 3} instead
-- of {1, 2}), and TAB02_SPJC_STX_006 has one extra row (copy of the
-- old row with a = {5}.

select * from TAB01_SPJC_STX_006;

select * from TAB02_SPJC_STX_006;
------------------------------------------------------------------
