-- @@@ 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
-- BEFORE trigger with a compound statement as a triggered
-- action. The compound statement has a statement other than SET, CALL,
-- or SIGNAL.
-- Result: Error

obey TEST_SPJC_STX_012(clean_up);
obey TEST_SPJC_STX_012(set_up);
LOG LOG_SPJC_STX_012 clear;
obey TEST_SPJC_STX_012(tests);
LOG;
obey TEST_SPJC_STX_012(clean_up);
exit;

?section clean_up
set schema CAT1.SCHM;

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

drop trigger TRIG01_SPJC_STX_012;
drop trigger TRIG02_SPJC_STX_012;

drop table TAB01_SPJC_STX_012;
drop table TAB02_SPJC_STX_012;

?section set_up
set schema CAT1.SCHM;

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

create table TAB01_SPJC_STX_012(a int, b int, c int, d int);
create table TAB02_SPJC_STX_012(a int, b int, c int, d int);

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

?section tests

------------------------------------------------------------------
-- TEST CASE 01: BEFORE trigger with a compound statement as a triggered
-- action. The compound statement has a statement other than SET, CALL,
-- or SIGNAL.
-- Result: Error (BEFORE Trigger does not allow a Compound statement
-- at present)
------------------------------------------------------------------

CREATE TRIGGER TRIG01_SPJC_STX_012 BEFORE UPDATE ON
TAB01_SPJC_STX_012
REFERENCING OLD ROW AS O, NEW ROW AS N
FOR EACH ROW
	BEGIN ATOMIC
		INSERT INTO TAB02_SPJC_STX_012 VALUES (O.A, O.B, O.C, O.D);
		IF (N.A > O.B) THEN
			SET N.A = N.A + 1;
			INSERT INTO TAB02_SPJC_STX_012 VALUES (N.A, N.B, N.C, N.D);
		ELSEIF (N.A < O.B) THEN
			CALL InsertFourCoulmns('CAT1.SCHM.TAB02_SPJC_STX_012',
					O.A, O.B, O.C, O.D);
		ELSE
			INSERT INTO TAB02_SPJC_STX_012 VALUES
					(O.A + N.A, O.B + N.B, O.C + N.C, O.D + N.D);
			SIGNAL SQLSTATE 'S0213'('TRIG01_SPJC_STX_012');
		END IF;
	END;
------------------------------------------------------------------
