From f499311a163d979ad616dd46c9b825da0b109d17 Mon Sep 17 00:00:00 2001 From: alexandre Date: Wed, 4 Jan 2023 08:36:55 +0100 Subject: [PATCH 1/3] refs #4951 changed triggers and added checkLength --- db/changes/230201/00-triggersXDiario.sql | 83 ++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 db/changes/230201/00-triggersXDiario.sql diff --git a/db/changes/230201/00-triggersXDiario.sql b/db/changes/230201/00-triggersXDiario.sql new file mode 100644 index 000000000..3efed0f76 --- /dev/null +++ b/db/changes/230201/00-triggersXDiario.sql @@ -0,0 +1,83 @@ +DROP TRIGGER IF EXISTS vn.XDiario_beforeUpdate; +USE vn; + +DELIMITER $$ +$$ +CREATE DEFINER=`root`@`localhost` TRIGGER `vn`.`XDiario_beforeUpdate` + BEFORE UPDATE ON `XDiario` + FOR EACH ROW +BEGIN + IF NOT NEW.SUBCTA <=> OLD.SUBCTA THEN + IF NOT util.checkLength(NEW.SUBCTA, 10) THEN + CALL util.throw('INVALID_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'); + END IF; + END IF; + IF NOT NEW.FECHA <=> OLD.FECHA THEN + CALL XDiario_checkDate(NEW.FECHA); + END IF; + IF NOT NEW.FECHA_EX <=> OLD.FECHA_EX THEN + CALL XDiario_checkDate(NEW.FECHA_EX); + END IF; + IF NOT NEW.FECHA_OP <=> OLD.FECHA_OP THEN + CALL XDiario_checkDate(NEW.FECHA_OP); + END IF; + IF NOT NEW.FECHA_RT <=> OLD.FECHA_RT THEN + CALL XDiario_checkDate(NEW.FECHA_RT); + END IF; + IF NOT NEW.FECREGCON <=> OLD.FECREGCON THEN + CALL XDiario_checkDate(NEW.FECREGCON); + END IF; +END$$ +DELIMITER ; + + +DROP TRIGGER IF EXISTS vn.XDiario_beforeInsert; +USE vn; + +DELIMITER $$ +$$ +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'); + END IF; + IF NOT util.checkLength(NEW.CONTRA, 10) THEN + CALL util.throw('INVALID_LENGTH'); + END IF; + CALL XDiario_checkDate(NEW.FECHA); + CALL XDiario_checkDate(NEW.FECHA_EX); + CALL XDiario_checkDate(NEW.FECHA_OP); + CALL XDiario_checkDate(NEW.FECHA_RT); + CALL XDiario_checkDate(NEW.FECREGCON); +END$$ +DELIMITER ; + + +DROP FUNCTION IF EXISTS `util`.`checkLength`; + +DELIMITER $$ +$$ +CREATE DEFINER=`root`@`localhost` FUNCTION `util`.`checkLength`(vString VARCHAR(10), vLength INT(11)) RETURNS tinyint(1) + DETERMINISTIC +BEGIN +/** + * Comprueba la longitud de un string + * + * @param vString String a comprobar + * @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; +END$$ +DELIMITER ; + From a4e591c5f5dc8d5e76606670072691980b72017a Mon Sep 17 00:00:00 2001 From: alexandre Date: Wed, 4 Jan 2023 10:37:59 +0100 Subject: [PATCH 2/3] 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 ; From a42723210f81f14463c78fa552504875460570b7 Mon Sep 17 00:00:00 2001 From: alexandre Date: Wed, 11 Jan 2023 09:13:18 +0100 Subject: [PATCH 3/3] refs #4951 delete function --- db/changes/230201/00-triggersXDiario.sql | 27 ++++-------------------- 1 file changed, 4 insertions(+), 23 deletions(-) diff --git a/db/changes/230201/00-triggersXDiario.sql b/db/changes/230201/00-triggersXDiario.sql index b31157a3f..5cf0b6253 100644 --- a/db/changes/230201/00-triggersXDiario.sql +++ b/db/changes/230201/00-triggersXDiario.sql @@ -11,7 +11,7 @@ BEGIN IF NEW.SUBCTA <=> '' THEN SET NEW.SUBCTA = NULL; END IF; - IF NEW.SUBCTA IS NOT NULL AND NOT util.checkStringLength(NEW.SUBCTA, 10) THEN + IF NEW.SUBCTA IS NOT NULL AND NOT LENGTH(NEW.SUBCTA) <=> 10 THEN CALL util.throw('INVALID_STRING_LENGTH'); END IF; END IF; @@ -19,7 +19,7 @@ BEGIN IF NEW.CONTRA <=> '' THEN SET NEW.CONTRA = NULL; END IF; - IF NEW.CONTRA IS NOT NULL AND NOT util.checkStringLength(NEW.CONTRA, 10) THEN + IF NEW.CONTRA IS NOT NULL AND NOT LENGTH(NEW.CONTRA) <=> 10 THEN CALL util.throw('INVALID_STRING_LENGTH'); END IF; END IF; @@ -54,13 +54,13 @@ BEGIN IF NEW.SUBCTA <=> '' THEN SET NEW.SUBCTA = NULL; END IF; - IF NEW.SUBCTA IS NOT NULL AND NOT util.checkStringLength(NEW.SUBCTA, 10) THEN + IF NEW.SUBCTA IS NOT NULL AND NOT LENGTH(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 + IF NEW.CONTRA IS NOT NULL AND NOT LENGTH(NEW.CONTRA) <=> 10 THEN CALL util.throw('INVALID_STRING_LENGTH'); END IF; CALL XDiario_checkDate(NEW.FECHA); @@ -71,22 +71,3 @@ BEGIN END$$ DELIMITER ; - -DROP FUNCTION IF EXISTS `util`.`checkStringLength`; - -DELIMITER $$ -$$ -CREATE DEFINER=`root`@`localhost` FUNCTION `util`.`checkStringLength`(vString VARCHAR(255), vLength INT(3)) RETURNS tinyint(1) - DETERMINISTIC -BEGIN -/** - * Comprueba la longitud de un string - * - * @param vString String a comprobar - * @param vLength Longitud que debe tener - * @return Devuelve TRUE/FALSE en caso de que tenga la longitud correcta o no - */ - RETURN LENGTH(vString) <=> vLength; -END$$ -DELIMITER ; -