-- @@@ 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 @@@
-- tentative execution of before triggers
-- promoting an RI by a before trigger

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

----------------------------------
-- DEFINE CONSTRAINT for_key_c
---------------------------------


obey TEST_4_3_1(clean_up);
log   LOG_4_3_1 clear;
obey TEST_4_3_1(set_up);
obey TEST_4_3_1(tests);
log;
obey TEST_4_3_1(clean_up);
exit;

?section clean_up
set schema cat1.schm;
ALTER TABLE tab1A DROP CONSTRAINT for_key_c;
ALTER TABLE tab1A DROP CONSTRAINT for_key_d;
DROP TRIGGER btrig5;
------------------------------------------------------------------
obey clearTables;

?section set_up
ALTER TABLE tab1A ADD CONSTRAINT for_key_c FOREIGN KEY (c) REFERENCES tab1B;

----------------------------------
-- DEFINE CONSTRAINT for_key_d
---------------------------------

ALTER TABLE tab1A ADD CONSTRAINT for_key_d FOREIGN KEY (d) REFERENCES cat2.schm.tab2A;

----------------------------------
-- INSERT INTO TABLES
----------------------------------
INSERT INTO tab1B VALUES (10,10);
INSERT INTO tab1B VALUES (20,20);
INSERT INTO cat2.schm.tab2A VALUES (30,30);
INSERT INTO tab1A VALUES (1,2,10,30);

---------------------------------
-- DEFINE TRIGGER btrig5
---------------------------------

CREATE TRIGGER btrig5
BEFORE UPDATE OF (b)
ON tab1A
REFERENCING NEW AS newR 
FOR EACH ROW
set newR.d= newR.b+20;

?section tests

------------------------------------------------------------------
--          TEST CASE
------------------------------------------------------------------

UPDATE tab1A SET b=10, C=20;

SELECT * FROM tab1A; -- check result

UPDATE tab1A SET b=11, C=20;

SELECT * FROM tab1A; -- check result

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