From 36f250a2400a24f2d9594b9ea40df9d0d957d802 Mon Sep 17 00:00:00 2001 From: "bell@sanja.is.com.ua" <> Date: Fri, 29 Jul 2005 23:39:08 +0300 Subject: [PATCH] Added sql_mode saving to mysql_dump (BUG#5891, part 2) --- client/client_priv.h | 3 +- client/mysqldump.c | 35 ++++++++------ mysql-test/r/mysqldump.result | 86 +++++++++++++++++++++++------------ mysql-test/r/sp.result | 8 ++-- mysql-test/t/mysqldump.test | 4 ++ mysql-test/t/sp.test | 2 +- sql/set_var.cc | 11 +++-- 7 files changed, 97 insertions(+), 52 deletions(-) diff --git a/client/client_priv.h b/client/client_priv.h index c8aa7385276..27e0f838995 100644 --- a/client/client_priv.h +++ b/client/client_priv.h @@ -49,5 +49,6 @@ enum options_client #ifdef HAVE_NDBCLUSTER_DB OPT_NDBCLUSTER, OPT_NDB_CONNECTSTRING, #endif - OPT_IGNORE_TABLE,OPT_INSERT_IGNORE,OPT_SHOW_WARNINGS,OPT_DROP_DATABASE + OPT_IGNORE_TABLE, OPT_INSERT_IGNORE, OPT_SHOW_WARNINGS, OPT_DROP_DATABASE, + OPT_TRIGGER }; diff --git a/client/mysqldump.c b/client/mysqldump.c index be9eb5ef58b..48e5d8c52dd 100644 --- a/client/mysqldump.c +++ b/client/mysqldump.c @@ -372,7 +372,7 @@ static struct my_option my_long_options[] = (gptr*) &path, (gptr*) &path, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0}, {"tables", OPT_TABLES, "Overrides option --databases (-B).", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}, - {"triggers", '/0', "Dump triggers for each dumped table", + {"triggers", OPT_TRIGGER, "Dump triggers for each dumped table", (gptr*) &opt_dump_triggers, (gptr*) &opt_dump_triggers, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0, 0, 0}, #ifndef DONT_ALLOW_USER_CHANGE @@ -1296,10 +1296,11 @@ static uint get_table_structure(char *table, char *db) row= mysql_fetch_row(tableRes); if (opt_drop) - fprintf(sql_file, "DROP VIEW IF EXISTS %s;\n",opt_quoted_table); + fprintf(sql_file, "/*!50001 DROP VIEW IF EXISTS %s*/;\n", + opt_quoted_table); /* Print CREATE statement but remove TEMPORARY */ - fprintf(sql_file, "CREATE %s;\n", row[1]+17); + fprintf(sql_file, "/*!50001 CREATE %s*/;\n", row[1]+17); check_io(sql_file); mysql_free_result(tableRes); @@ -1335,19 +1336,23 @@ static uint get_table_structure(char *table, char *db) DBUG_RETURN(0); } if (mysql_num_rows(tableRes)) - fprintf(sql_file, "\nDELIMITER //;\n"); + fprintf(sql_file, "\n/*!50003 SET @OLD_SQL_MODE=@@SQL_MODE*/;\n\ +DELIMITER //;\n"); while ((row=mysql_fetch_row(tableRes))) { - fprintf(sql_file, "CREATE TRIGGER %s %s %s ON %s\n" - "FOR EACH ROW%s//\n\n", - quote_name(row[0], name_buff, 0), - row[4], - row[1], + fprintf(sql_file, "/*!50003 SET SESSION SQL_MODE=\"%s\"*/ //\n\ +/*!50003 CREATE TRIGGER %s %s %s ON %s FOR EACH ROW%s*/ //\n\n", + row[6], /* sql_mode */ + quote_name(row[0], name_buff, 0), /* Trigger */ + row[4], /* Timing */ + row[1], /* Event */ result_table, - row[3]); + row[3] /* Statement */); } if (mysql_num_rows(tableRes)) - fprintf(sql_file, "DELIMITER ;//"); + fprintf(sql_file, + "DELIMITER ;//\n\ +/*!50003 SET SESSION SQL_MODE=@OLD_SQL_MODE*/;"); mysql_free_result(tableRes); } } @@ -2957,13 +2962,15 @@ static my_bool get_view_structure(char *table, char* db) } if (opt_drop) { - fprintf(sql_file, "DROP TABLE IF EXISTS %s;\n", opt_quoted_table); - fprintf(sql_file, "DROP VIEW IF EXISTS %s;\n", opt_quoted_table); + fprintf(sql_file, "/*!50001 DROP TABLE IF EXISTS %s*/;\n", + opt_quoted_table); + fprintf(sql_file, "/*!50001 DROP VIEW IF EXISTS %s*/;\n", + opt_quoted_table); check_io(sql_file); } row= mysql_fetch_row(table_res); - fprintf(sql_file, "%s;\n", row[1]); + fprintf(sql_file, "/*!50001 %s*/;\n", row[1]); check_io(sql_file); mysql_free_result(table_res); diff --git a/mysql-test/r/mysqldump.result b/mysql-test/r/mysqldump.result index bf783402921..8d6febdef64 100644 --- a/mysql-test/r/mysqldump.result +++ b/mysql-test/r/mysqldump.result @@ -379,13 +379,13 @@ LOCK TABLES `t1` WRITE; UNLOCK TABLES; /*!40000 ALTER TABLE `t1` ENABLE KEYS */; DROP TABLE IF EXISTS `v1`; -DROP VIEW IF EXISTS `v1`; -CREATE TABLE `v1` ( +/*!50001 DROP VIEW IF EXISTS `v1`*/; +/*!50001 CREATE TABLE `v1` ( `a` int(11) default NULL -) ENGINE=MyISAM DEFAULT CHARSET=latin1; -DROP TABLE IF EXISTS `v1`; -DROP VIEW IF EXISTS `v1`; -CREATE ALGORITHM=UNDEFINED VIEW `test`.`v1` AS select `test`.`t1`.`a` AS `a` from `test`.`t1`; +) ENGINE=MyISAM DEFAULT CHARSET=latin1*/; +/*!50001 DROP TABLE IF EXISTS `v1`*/; +/*!50001 DROP VIEW IF EXISTS `v1`*/; +/*!50001 CREATE ALGORITHM=UNDEFINED VIEW `test`.`v1` AS select `test`.`t1`.`a` AS `a` from `test`.`t1`*/; /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; @@ -1462,13 +1462,13 @@ INSERT INTO `t2` VALUES ('alfred'),('angie'),('bingo'),('waffle'),('lemon'); UNLOCK TABLES; /*!40000 ALTER TABLE `t2` ENABLE KEYS */; DROP TABLE IF EXISTS `v2`; -DROP VIEW IF EXISTS `v2`; -CREATE TABLE `v2` ( +/*!50001 DROP VIEW IF EXISTS `v2`*/; +/*!50001 CREATE TABLE `v2` ( `a` varchar(30) default NULL -) ENGINE=MyISAM DEFAULT CHARSET=latin1; -DROP TABLE IF EXISTS `v2`; -DROP VIEW IF EXISTS `v2`; -CREATE ALGORITHM=UNDEFINED VIEW `db1`.`v2` AS select `db1`.`t2`.`a` AS `a` from `db1`.`t2` where (`db1`.`t2`.`a` like _latin1'a%') WITH CASCADED CHECK OPTION; +) ENGINE=MyISAM DEFAULT CHARSET=latin1*/; +/*!50001 DROP TABLE IF EXISTS `v2`*/; +/*!50001 DROP VIEW IF EXISTS `v2`*/; +/*!50001 CREATE ALGORITHM=UNDEFINED VIEW `db1`.`v2` AS select `db1`.`t2`.`a` AS `a` from `db1`.`t2` where (`db1`.`t2`.`a` like _latin1'a%') WITH CASCADED CHECK OPTION*/; /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; @@ -1685,6 +1685,7 @@ end| create trigger trg2 before update on t1 for each row begin if old.a % 2 = 0 then set new.b := 12; end if; end| +set sql_mode="traditional"| create trigger trg3 after update on t1 for each row begin if new.a = -1 then @@ -1697,24 +1698,25 @@ if new.a > 10 then set @fired:= "No"; end if; end| +set sql_mode=default| show triggers like "t1"; -Trigger Event Table Statement Timing Created +Trigger Event Table Statement Timing Created sql_mode trg1 INSERT t1 begin if new.a > 10 then set new.a := 10; set new.a := 11; end if; -end BEFORE 0000-00-00 00:00:00 +end BEFORE 0000-00-00 00:00:00 trg2 UPDATE t1 begin if old.a % 2 = 0 then set new.b := 12; end if; -end BEFORE 0000-00-00 00:00:00 +end BEFORE 0000-00-00 00:00:00 trg3 UPDATE t1 begin if new.a = -1 then set @fired:= "Yes"; end if; -end AFTER 0000-00-00 00:00:00 +end AFTER 0000-00-00 00:00:00 STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,TRADITIONAL,NO_AUTO_CREATE_USER INSERT INTO t1 (a) VALUES (1),(2),(3),(22); update t1 set a = 4 where a=3; @@ -1736,30 +1738,32 @@ CREATE TABLE `t1` ( `b` bigint(20) default NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1; +/*!50003 SET @OLD_SQL_MODE=@@SQL_MODE*/; DELIMITER //; -CREATE TRIGGER `trg1` BEFORE INSERT ON `t1` -FOR EACH ROW +/*!50003 SET SESSION SQL_MODE=""*/ // +/*!50003 CREATE TRIGGER `trg1` BEFORE INSERT ON `t1` FOR EACH ROW begin if new.a > 10 then set new.a := 10; set new.a := 11; end if; -end// +end*/ // -CREATE TRIGGER `trg2` BEFORE UPDATE ON `t1` -FOR EACH ROW begin +/*!50003 SET SESSION SQL_MODE=""*/ // +/*!50003 CREATE TRIGGER `trg2` BEFORE UPDATE ON `t1` FOR EACH ROW begin if old.a % 2 = 0 then set new.b := 12; end if; -end// +end*/ // -CREATE TRIGGER `trg3` AFTER UPDATE ON `t1` -FOR EACH ROW +/*!50003 SET SESSION SQL_MODE="STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,TRADITIONAL,NO_AUTO_CREATE_USER"*/ // +/*!50003 CREATE TRIGGER `trg3` AFTER UPDATE ON `t1` FOR EACH ROW begin if new.a = -1 then set @fired:= "Yes"; end if; -end// +end*/ // DELIMITER ;// +/*!50003 SET SESSION SQL_MODE=@OLD_SQL_MODE*/; /*!40000 ALTER TABLE `t1` DISABLE KEYS */; LOCK TABLES `t1` WRITE; @@ -1771,16 +1775,18 @@ CREATE TABLE `t2` ( `a` int(11) default NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1; +/*!50003 SET @OLD_SQL_MODE=@@SQL_MODE*/; DELIMITER //; -CREATE TRIGGER `trg4` BEFORE INSERT ON `t2` -FOR EACH ROW +/*!50003 SET SESSION SQL_MODE="STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,TRADITIONAL,NO_AUTO_CREATE_USER"*/ // +/*!50003 CREATE TRIGGER `trg4` BEFORE INSERT ON `t2` FOR EACH ROW begin if new.a > 10 then set @fired:= "No"; end if; -end// +end*/ // DELIMITER ;// +/*!50003 SET SESSION SQL_MODE=@OLD_SQL_MODE*/; /*!40000 ALTER TABLE `t2` DISABLE KEYS */; LOCK TABLES `t2` WRITE; @@ -1844,4 +1850,28 @@ show tables; Tables_in_test t1 t2 +show triggers; +Trigger Event Table Statement Timing Created sql_mode +trg1 INSERT t1 +begin +if new.a > 10 then +set new.a := 10; +set new.a := 11; +end if; +end BEFORE # +trg2 UPDATE t1 begin +if old.a % 2 = 0 then set new.b := 12; end if; +end BEFORE # +trg3 UPDATE t1 +begin +if new.a = -1 then +set @fired:= "Yes"; +end if; +end AFTER # STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,TRADITIONAL,NO_AUTO_CREATE_USER +trg4 INSERT t2 +begin +if new.a > 10 then +set @fired:= "No"; +end if; +end BEFORE # STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,TRADITIONAL,NO_AUTO_CREATE_USER DROP TABLE t1, t2; diff --git a/mysql-test/r/sp.result b/mysql-test/r/sp.result index 69900edf3d9..04d49bf4b3b 100644 --- a/mysql-test/r/sp.result +++ b/mysql-test/r/sp.result @@ -2658,20 +2658,20 @@ call avg ()| drop procedure avg| drop procedure if exists bug6129| set @old_mode= @@sql_mode; -set @@sql_mode= ""; +set @@sql_mode= "ERROR_FOR_DIVISION_BY_ZERO"; create procedure bug6129() select @@sql_mode| call bug6129()| @@sql_mode - +ERROR_FOR_DIVISION_BY_ZERO set @@sql_mode= "NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO"| call bug6129()| @@sql_mode -NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO +ERROR_FOR_DIVISION_BY_ZERO set @@sql_mode= "NO_ZERO_IN_DATE"| call bug6129()| @@sql_mode -NO_ZERO_IN_DATE +ERROR_FOR_DIVISION_BY_ZERO set @@sql_mode=@old_mode; drop procedure bug6129| drop procedure if exists bug9856| diff --git a/mysql-test/t/mysqldump.test b/mysql-test/t/mysqldump.test index 81000c17d98..0fe5b473b23 100644 --- a/mysql-test/t/mysqldump.test +++ b/mysql-test/t/mysqldump.test @@ -726,6 +726,7 @@ end| create trigger trg2 before update on t1 for each row begin if old.a % 2 = 0 then set new.b := 12; end if; end| +set sql_mode="traditional"| create trigger trg3 after update on t1 for each row begin if new.a = -1 then @@ -738,6 +739,7 @@ begin set @fired:= "No"; end if; end| +set sql_mode=default| delimiter ;| --replace_column 6 '0000-00-00 00:00:00' show triggers like "t1"; @@ -753,4 +755,6 @@ drop table t1; --exec $MYSQL test < var/tmp/mysqldump.sql # Check that tables have been reloaded show tables; +--replace_column 6 # +show triggers; DROP TABLE t1, t2; diff --git a/mysql-test/t/sp.test b/mysql-test/t/sp.test index 1a35df40a17..ecc9c3c12a2 100644 --- a/mysql-test/t/sp.test +++ b/mysql-test/t/sp.test @@ -3378,7 +3378,7 @@ drop procedure avg| drop procedure if exists bug6129| --enable_warnings set @old_mode= @@sql_mode; -set @@sql_mode= ""; +set @@sql_mode= "ERROR_FOR_DIVISION_BY_ZERO"; create procedure bug6129() select @@sql_mode| call bug6129()| diff --git a/sql/set_var.cc b/sql/set_var.cc index d3f3f62c259..20d383d0025 100644 --- a/sql/set_var.cc +++ b/sql/set_var.cc @@ -3199,10 +3199,13 @@ bool sys_var_thd_table_type::update(THD *thd, set_var *var) /* Make string representation of mode - SINOPSYS - thd thread handler - val sql_mode value - len pointer on length of string + SYNOPSIS + thd in thread handler + val in sql_mode value + len out pointer on length of string + + RETURN + pointer to string with sql_mode representation */ byte *sys_var_thd_sql_mode::symbolic_mode_representation(THD *thd, ulong val,