DELIMITER //
CREATE TRIGGER updateindex
AFTER INSERT ON `dominioprova_it@giacomo`
FOR EACH ROW
BEGIN
DECLARE tipomsg tinyint;
SET tipomsg = NEW.`IN`;
IF NOT EXISTS ( SELECT * FROM `index` WHERE email = 'giacomo' AND dominio = 'dominioprova.it' AND anno = DATE_FORMAT(NOW(),'%Y') AND mese = DATE_FORMAT(NOW(),'%m'))
THEN
IF (tipomsg = 1 ) THEN /* E' un messaggio in ingresso */
INSERT INTO `index` ( index_id, email, dominio, type, num, size, anno, mese )
VALUES ('', 'giacomo', 'dominioprova.it', 'IN',1, NEW.size,DATE_FORMAT(NOW(),'%Y'),DATE_FORMAT(NOW() ,'%m'));
INSERT INTO `index` ( index_id, email, dominio, type, num, size, anno, mese )
VALUES ('', 'giacomo', 'dominioprova.it', 'out', 0, 0, DATE_FORMAT(NOW(),'%Y'), DATE_FORMAT(NOW(),'%m'));
ELSE IF (tipomsg = 0 ) THEN
INSERT INTO `index` ( index_id, email, dominio, type, num, size, anno, mese )
VALUES ('', 'giacomo','dominioprova.it','in',0,0,DATE_FORMAT(N OW(),'%Y'),DATE_FORMAT(NOW(),'%m'));
INSERT INTO `index` ( index_id, email, dominio, type, num, size, anno, mese )
VALUES ('', 'giacomo_fusaro', 'dominioprova.it', 'out', 1, NEW.size, 2008, 01);
ELSE
SET tipomsg = NEW.`IN`;
IF (tipomsg = 1 ) THEN
UPDATE `index` SET num = num + 1, size = size + NEW.size WHERE email = 'giacomo';
ELSE
UPDATE `index` SET num = num + 1, size = size + NEW.size WHERE email = 'giacomo;
END IF;
END;
//