Created
December 6, 2016 12:20
-
-
Save igorsgm/792e3c598de46025d7acb8a89e2dfe9e to your computer and use it in GitHub Desktop.
Exemplos de Triggers que irão popular a tabela `anmt_restful_extsender_logs` de acordo com o método (INSERT, UPDATE, DELETE) de alteração nas tabelas que estão sendo monitoradas, neste caso, a tabela `anmt_associados`. Infelizmente o MySQL não aceita Triggers para múltiplos métodos, por isso é necessário criar uma para cada função de CRUD.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*Trigger para INSERT */ | |
DELIMITER $$ | |
CREATE TRIGGER onINSERTanmt_associados | |
AFTER UPDATE ON anmt_associados | |
FOR EACH ROW | |
BEGIN | |
IF ((SELECT `state` FROM anmt_restful_external_senders WHERE `table` = 'anmt_associados') = 1) THEN | |
INSERT INTO anmt_restful_extsender_logs (resource, id_resource_element, change_time, method, request_sent) | |
VALUES ('anmt_associados', NEW.id, NOW(), 'INSERT', 0); | |
END IF; | |
END $$ | |
DELIMITER $$ | |
CREATE TRIGGER onUPDATEanmt_associados | |
AFTER UPDATE ON anmt_associados | |
FOR EACH ROW | |
BEGIN | |
IF ((SELECT `state` FROM anmt_restful_external_senders WHERE `table` = 'anmt_associados') = 1) THEN | |
INSERT INTO anmt_restful_extsender_logs (resource, id_resource_element, change_time, method, request_sent) | |
VALUES ('anmt_associados', NEW.id, NOW(), 'UPDATE', 0); | |
END IF; | |
END $$ | |
DELIMITER $$ | |
CREATE TRIGGER onUPDATEanmt_associados | |
AFTER UPDATE ON anmt_associados | |
FOR EACH ROW | |
BEGIN | |
IF ((SELECT `state` FROM anmt_restful_external_senders WHERE `table` = 'anmt_associados') = 1) THEN | |
INSERT INTO anmt_restful_extsender_logs (resource, id_resource_element, change_time, method, request_sent) | |
VALUES ('anmt_associados', OLD.id, NOW(), 'DELETE', 0); | |
END IF; | |
END $$ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment