1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
| --------------
SET AUTOCOMMIT = 0
--------------
--------------
START TRANSACTION
--------------
--------------
DROP DATABASE IF EXISTS `base`
--------------
--------------
CREATE DATABASE `base`
DEFAULT CHARACTER SET `latin1`
DEFAULT COLLATE `latin1_general_ci`
--------------
--------------
DROP TABLE IF EXISTS `test`
--------------
--------------
CREATE TABLE `test` (
`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY,
`texte` VARCHAR(255) NULL DEFAULT NULL
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `test` (`id`,`texte`) values (1, 'un'), (2, 'deux')
--------------
--------------
select * from test
--------------
+----+-------+
| id | texte |
+----+-------+
| 1 | un |
| 2 | deux |
+----+-------+
--------------
DROP TABLE IF EXISTS `mouchard`
--------------
--------------
CREATE TABLE `mouchard` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`heure` DATETIME DEFAULT current_timestamp,
`libelle` VARCHAR(255) NULL DEFAULT NULL
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
DROP PROCEDURE IF EXISTS `updateTable`
--------------
--------------
CREATE PROCEDURE `updateTable`
(
in input_table_name varchar(30),
in input_id int,
in input_col_name varchar(30),
in input_string_value varchar(30)
)
DETERMINISTIC
NO SQL
BEGIN
DECLARE msg VARCHAR(255);
DECLARE EXIT HANDLER FOR SQLSTATE '42S02'
BEGIN
SET msg = concat("Table inconnue \'", input_table_name, "\'");
INSERT INTO mouchard (libelle) VALUES (msg);
SIGNAL SQLSTATE VALUE '07001' SET MESSAGE_TEXT = msg, MYSQL_ERRNO = 7001;
end;
DECLARE EXIT HANDLER FOR SQLSTATE '42S22'
BEGIN
SET msg = concat("Colonne inconnue \'", input_col_name, "\' dans la table \'", input_table_name, "\'");
INSERT INTO mouchard (libelle) VALUES (msg);
SIGNAL SQLSTATE VALUE '07002' SET MESSAGE_TEXT = msg, MYSQL_ERRNO = 7002;
end;
SET @val=NULL;
SET @req1 = concat("select id into @val from ", input_table_name, " where id = ", input_id);
PREPARE stmt1 FROM @req1;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
if (input_id = @val) then
set @req2 = concat("update ", input_table_name, " set ", input_col_name, " = \'", input_string_value, "\' where id = ", input_id);
PREPARE stmt2 FROM @req2;
EXECUTE stmt2;
DEALLOCATE PREPARE stmt2;
INSERT INTO mouchard (libelle) VALUES (@req2);
ELSE
set @req3 = concat("insert into ", input_table_name, " (id, ", input_col_name, ") values (", input_id, ", \'", input_string_value, "\')");
PREPARE stmt3 FROM @req3;
EXECUTE stmt3;
DEALLOCATE PREPARE stmt3;
INSERT INTO mouchard (libelle) VALUES (@req3);
END IF;
END
--------------
--------------
call `updateTable` ('test', 1, 'texte', 'ca marche')
--------------
--------------
call `updateTable` ('test', 3, 'texte', 'bla bla bla')
--------------
--------------
call `updateTable` ('test', 1, 'col', 'colonne')
--------------
ERROR 7002 (07002) at line 121: Colonne inconnue 'col' dans la table 'test'
--------------
call `updateTable` ('essai', 1, 'col', 'colonne')
--------------
ERROR 7001 (07001) at line 122: Table inconnue 'essai'
--------------
select * from test
--------------
+----+-------------+
| id | texte |
+----+-------------+
| 1 | ca marche |
| 2 | deux |
| 3 | bla bla bla |
+----+-------------+
--------------
select * from mouchard
--------------
+----+---------------------+--------------------------------------------------------+
| id | heure | libelle |
+----+---------------------+--------------------------------------------------------+
| 1 | 2015-12-03 17:27:24 | update test set texte = 'ca marche' where id = 1 |
| 2 | 2015-12-03 17:27:24 | insert into test (id, texte) values (3, 'bla bla bla') |
| 3 | 2015-12-03 17:27:24 | Colonne inconnue 'col' dans la table 'test' |
| 4 | 2015-12-03 17:27:24 | Table inconnue 'essai' |
+----+---------------------+--------------------------------------------------------+
--------------
COMMIT
--------------
--------------
SET AUTOCOMMIT = 1
--------------
Appuyez sur une touche pour continuer... |
Partager