/*
 * Trigger declarations for hpr_hpr
 *
 */

--
-- Table 'log' to hold details of certain background activities invoked by
-- stored procedures and triggers
--
DROP TABLE IF EXISTS log;
CREATE TABLE log (
    id int(5) NOT NULL AUTO_INCREMENT,
    stamp timestamp DEFAULT now(),
    message text NOT NULL,
    PRIMARY KEY (id)
);

--
-- When an episode is added to the eps table check whether there is an
-- entry in the reservations table with the same id. If there is, delete
-- it. Log the deletion in the log table.
--
DROP TRIGGER IF EXISTS check_reservations;
DELIMITER $$
CREATE TRIGGER check_reservations BEFORE INSERT ON eps
FOR EACH ROW
BEGIN
    IF EXISTS(SELECT id FROM reservations WHERE id = NEW.id) THEN
	DELETE FROM reservations WHERE id = NEW.id;
	INSERT INTO log (message) VALUES(concat('DELETE FROM reservations WHERE id = ',NEW.id));
    END IF;
END;
$$
DELIMITER ;

/*
vim: syntax=sql ai tw=75:
*/
