-- @@@ 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
-- CS with more than one Insert/Update/Delete statement.
-- Result: Success

obey TEST_SPJC_CSTA_005(clean_up);
obey TEST_SPJC_CSTA_005(set_up);
log  LOG_SPJC_CSTA_005 clear;
obey TEST_SPJC_CSTA_005(tests);
LOG;
obey TEST_SPJC_CSTA_005(clean_up);
exit;

?section clean_up
set schema CAT1.SCHM;


drop trigger TRIG01_SPJC_CSTA_005;
drop trigger TRIG02_SPJC_CSTA_005;

drop table TAB01_SPJC_CSTA_005;
drop table TAB02_SPJC_CSTA_005;

?section set_up
set schema CAT1.SCHM;

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

CREATE TABLE TAB01_SPJC_CSTA_005(a int, b int, c int, d int);
CREATE TABLE TAB02_SPJC_CSTA_005(a int, b int, c int, d int);

INSERT INTO TAB01_SPJC_CSTA_005 VALUES(10, 11, 12, 13),
		(20, 21, 22, 23),
		(30, 31, 32, 33);

?section tests

------------------------------------------------------------------
-- TEST CASE 01: CS with multiple Insert/Update/Delete statements (row
-- trigger)
-- Result: Success
------------------------------------------------------------------

CREATE TRIGGER TRIG01_SPJC_CSTA_005 AFTER UPDATE ON
TAB01_SPJC_CSTA_005
REFERENCING OLD ROW AS O, NEW ROW AS N
FOR EACH ROW
BEGIN ATOMIC
	DELETE FROM TAB02_SPJC_CSTA_005 WHERE A = O.A;

	INSERT INTO TAB02_SPJC_CSTA_005 VALUES
			(N.A, N.B + N.A, N.C + N.A, N.D + N.A);
END;
------------------------------------------------------------------


------------------------------------------------------------------
-- TEST CASE 02: CS with multiple Insert/Update/Delete statements (
-- statement trigger)
-- Result: Success
------------------------------------------------------------------

CREATE TRIGGER TRIG02_SPJC_CSTA_005 AFTER DELETE ON
TAB01_SPJC_CSTA_005
REFERENCING OLD TABLE AS OT
FOR EACH STATEMENT
BEGIN ATOMIC
	INSERT INTO TAB02_SPJC_CSTA_005
		SELECT SUM(A), SUM(B), SUM(C), SUM(D)
		FROM TAB01_SPJC_CSTA_005;

	INSERT INTO TAB02_SPJC_CSTA_005
		SELECT SUM(A) + 100, SUM(B) + 100, SUM(C) + 100, SUM(D) + 100
			FROM TAB01_SPJC_CSTA_005;
END;
------------------------------------------------------------------

-- case 1:
UPDATE TAB01_SPJC_CSTA_005 SET A = A + 100 WHERE A = 10;

select * from TAB01_SPJC_CSTA_005;

select * from TAB02_SPJC_CSTA_005;

-- now update TAB01_SPJC_CSTA_005 once again
UPDATE TAB01_SPJC_CSTA_005 SET A = A + 100 WHERE A = 110;


select * from TAB01_SPJC_CSTA_005;

-- Result: TAB02_SPJC_CSTA_005 has one row with new A and new A added to
-- other columns
select * from TAB02_SPJC_CSTA_005;

-- case 2:
DELETE FROM TAB01_SPJC_CSTA_005 WHERE A = 210;

select * from TAB02_SPJC_CSTA_005;
------------------------------------------------------------------
