Skip to content

Instantly share code, notes, and snippets.

@igorsgm
Created December 6, 2016 12:20
Show Gist options
  • Save igorsgm/792e3c598de46025d7acb8a89e2dfe9e to your computer and use it in GitHub Desktop.
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.
/*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