From a4e591c5f5dc8d5e76606670072691980b72017a Mon Sep 17 00:00:00 2001 From: alexandre Date: Wed, 4 Jan 2023 10:37:59 +0100 Subject: [PATCH] refs #4951 triggers modified --- db/changes/230201/00-triggersXDiario.sql | 37 +++++++++++++++--------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/db/changes/230201/00-triggersXDiario.sql b/db/changes/230201/00-triggersXDiario.sql index 3efed0f76..b31157a3f 100644 --- a/db/changes/230201/00-triggersXDiario.sql +++ b/db/changes/230201/00-triggersXDiario.sql @@ -8,13 +8,19 @@ CREATE DEFINER=`root`@`localhost` TRIGGER `vn`.`XDiario_beforeUpdate` FOR EACH ROW BEGIN IF NOT NEW.SUBCTA <=> OLD.SUBCTA THEN - IF NOT util.checkLength(NEW.SUBCTA, 10) THEN - CALL util.throw('INVALID_LENGTH'); + IF NEW.SUBCTA <=> '' THEN + SET NEW.SUBCTA = NULL; + END IF; + IF NEW.SUBCTA IS NOT NULL AND NOT util.checkStringLength(NEW.SUBCTA, 10) THEN + CALL util.throw('INVALID_STRING_LENGTH'); END IF; END IF; IF NOT NEW.CONTRA <=> OLD.CONTRA THEN - IF NOT util.checkLength(NEW.CONTRA, 10) THEN - CALL util.throw('INVALID_LENGTH'); + IF NEW.CONTRA <=> '' THEN + SET NEW.CONTRA = NULL; + END IF; + IF NEW.CONTRA IS NOT NULL AND NOT util.checkStringLength(NEW.CONTRA, 10) THEN + CALL util.throw('INVALID_STRING_LENGTH'); END IF; END IF; IF NOT NEW.FECHA <=> OLD.FECHA THEN @@ -45,11 +51,17 @@ CREATE DEFINER=`root`@`localhost` TRIGGER `vn`.`XDiario_beforeInsert` BEFORE INSERT ON `XDiario` FOR EACH ROW BEGIN - IF NOT util.checkLength(NEW.SUBCTA, 10) THEN - CALL util.throw('INVALID_LENGTH'); + IF NEW.SUBCTA <=> '' THEN + SET NEW.SUBCTA = NULL; END IF; - IF NOT util.checkLength(NEW.CONTRA, 10) THEN - CALL util.throw('INVALID_LENGTH'); + IF NEW.SUBCTA IS NOT NULL AND NOT util.checkStringLength(NEW.SUBCTA, 10) THEN + CALL util.throw('INVALID_STRING_LENGTH'); + END IF; + IF NEW.CONTRA <=> '' THEN + SET NEW.CONTRA = NULL; + END IF; + IF NEW.CONTRA IS NOT NULL AND NOT util.checkStringLength(NEW.CONTRA, 10) THEN + CALL util.throw('INVALID_STRING_LENGTH'); END IF; CALL XDiario_checkDate(NEW.FECHA); CALL XDiario_checkDate(NEW.FECHA_EX); @@ -60,11 +72,11 @@ END$$ DELIMITER ; -DROP FUNCTION IF EXISTS `util`.`checkLength`; +DROP FUNCTION IF EXISTS `util`.`checkStringLength`; DELIMITER $$ $$ -CREATE DEFINER=`root`@`localhost` FUNCTION `util`.`checkLength`(vString VARCHAR(10), vLength INT(11)) RETURNS tinyint(1) +CREATE DEFINER=`root`@`localhost` FUNCTION `util`.`checkStringLength`(vString VARCHAR(255), vLength INT(3)) RETURNS tinyint(1) DETERMINISTIC BEGIN /** @@ -74,10 +86,7 @@ BEGIN * @param vLength Longitud que debe tener * @return Devuelve TRUE/FALSE en caso de que tenga la longitud correcta o no */ - IF LENGTH(vString) <=> vLength THEN - RETURN TRUE; - END IF; - RETURN FALSE; + RETURN LENGTH(vString) <=> vLength; END$$ DELIMITER ;