Commit 1f86b3f1 authored by grog@mysql.com's avatar grog@mysql.com

BUG#17201: Improve handling of views.

parent c21e6498
...@@ -129,8 +129,8 @@ static const char *mysql_universal_client_charset= ...@@ -129,8 +129,8 @@ static const char *mysql_universal_client_charset=
static char *default_charset; static char *default_charset;
static CHARSET_INFO *charset_info= &my_charset_latin1; static CHARSET_INFO *charset_info= &my_charset_latin1;
const char *default_dbug_option="d:t:o,/tmp/mysqldump.trace"; const char *default_dbug_option="d:t:o,/tmp/mysqldump.trace";
/* do we met VIEWs during tables scaning */ /* have we seen any VIEWs during table scanning? */
my_bool was_views= 0; my_bool seen_views= 0;
const char *compatible_mode_names[]= const char *compatible_mode_names[]=
{ {
...@@ -1388,7 +1388,7 @@ static uint dump_routines_for_db(char *db) ...@@ -1388,7 +1388,7 @@ static uint dump_routines_for_db(char *db)
ARGS ARGS
table - table name table - table name
db - db name db - db name
table_type - table type ie "InnoDB" table_type - table type, e.g. "MyISAM" or "InnoDB", but also "VIEW"
ignore_flag - what we must particularly ignore - see IGNORE_ defines above ignore_flag - what we must particularly ignore - see IGNORE_ defines above
RETURN RETURN
...@@ -1480,13 +1480,22 @@ static uint get_table_structure(char *table, char *db, char *table_type, ...@@ -1480,13 +1480,22 @@ static uint get_table_structure(char *table, char *db, char *table_type,
} }
if (!opt_xml && opt_comments) if (!opt_xml && opt_comments)
{ {
if (strcmp (table_type, "VIEW") == 0) /* view */
fprintf(sql_file, "\n--\n-- Temporary table structure for view %s\n--\n\n",
result_table);
else
fprintf(sql_file, "\n--\n-- Table structure for table %s\n--\n\n", fprintf(sql_file, "\n--\n-- Table structure for table %s\n--\n\n",
result_table); result_table);
check_io(sql_file); check_io(sql_file);
} }
if (opt_drop) if (opt_drop)
{ {
fprintf(sql_file, "DROP TABLE IF EXISTS %s;\n", opt_quoted_table); /*
Even if the "table" is a view, we do a DROP TABLE here. The
view-specific code below fills in the DROP VIEW.
*/
fprintf(sql_file, "DROP TABLE IF EXISTS %s;\n",
opt_quoted_table);
check_io(sql_file); check_io(sql_file);
} }
...@@ -1524,10 +1533,13 @@ static uint get_table_structure(char *table, char *db, char *table_type, ...@@ -1524,10 +1533,13 @@ static uint get_table_structure(char *table, char *db, char *table_type,
{ {
if (opt_drop) if (opt_drop)
{ {
/*
We have already dropped any table of the same name
above, so here we just drop the view.
*/
fprintf(sql_file, "/*!50001 DROP VIEW IF EXISTS %s*/;\n", fprintf(sql_file, "/*!50001 DROP VIEW IF EXISTS %s*/;\n",
opt_quoted_table); opt_quoted_table);
fprintf(sql_file, "/*!50001 DROP TABLE IF EXISTS %s*/;\n",
opt_quoted_table);
check_io(sql_file); check_io(sql_file);
} }
...@@ -1554,7 +1566,7 @@ static uint get_table_structure(char *table, char *db, char *table_type, ...@@ -1554,7 +1566,7 @@ static uint get_table_structure(char *table, char *db, char *table_type,
} }
mysql_free_result(result); mysql_free_result(result);
was_views= 1; seen_views= 1;
DBUG_RETURN(0); DBUG_RETURN(0);
} }
...@@ -2053,6 +2065,12 @@ static void dump_table(char *table, char *db) ...@@ -2053,6 +2065,12 @@ static void dump_table(char *table, char *db)
*/ */
num_fields= get_table_structure(table, db, table_type, &ignore_flag); num_fields= get_table_structure(table, db, table_type, &ignore_flag);
/*
The "table" could be a view. If so, we don't do anything here.
*/
if (strcmp (table_type, "VIEW") == 0)
return;
/* Check --no-data flag */ /* Check --no-data flag */
if (dFlag) if (dFlag)
{ {
...@@ -2538,7 +2556,7 @@ static int dump_all_databases() ...@@ -2538,7 +2556,7 @@ static int dump_all_databases()
if (dump_all_tables_in_db(row[0])) if (dump_all_tables_in_db(row[0]))
result=1; result=1;
} }
if (was_views) if (seen_views)
{ {
if (mysql_query(sock, "SHOW DATABASES") || if (mysql_query(sock, "SHOW DATABASES") ||
!(tableres = mysql_store_result(sock))) !(tableres = mysql_store_result(sock)))
...@@ -2567,7 +2585,7 @@ static int dump_databases(char **db_names) ...@@ -2567,7 +2585,7 @@ static int dump_databases(char **db_names)
if (dump_all_tables_in_db(*db)) if (dump_all_tables_in_db(*db))
result=1; result=1;
} }
if (!result && was_views) if (!result && seen_views)
{ {
for (db= db_names ; *db ; db++) for (db= db_names ; *db ; db++)
{ {
...@@ -2741,8 +2759,6 @@ static my_bool dump_all_views_in_db(char *database) ...@@ -2741,8 +2759,6 @@ static my_bool dump_all_views_in_db(char *database)
uint numrows; uint numrows;
char table_buff[NAME_LEN*2+3]; char table_buff[NAME_LEN*2+3];
if (init_dumping(database))
return 1;
if (opt_xml) if (opt_xml)
print_xml_tag1(md_result_file, "", "database name=", database, "\n"); print_xml_tag1(md_result_file, "", "database name=", database, "\n");
if (lock_tables) if (lock_tables)
...@@ -2908,7 +2924,7 @@ static int dump_selected_tables(char *db, char **table_names, int tables) ...@@ -2908,7 +2924,7 @@ static int dump_selected_tables(char *db, char **table_names, int tables)
} }
/* Dump each selected view */ /* Dump each selected view */
if (was_views) if (seen_views)
{ {
for(i=0; i < dump_tables.records; i++) for(i=0; i < dump_tables.records; i++)
{ {
...@@ -3369,7 +3385,7 @@ static my_bool get_view_structure(char *table, char* db) ...@@ -3369,7 +3385,7 @@ static my_bool get_view_structure(char *table, char* db)
if (!opt_xml && opt_comments) if (!opt_xml && opt_comments)
{ {
fprintf(sql_file, "\n--\n-- View structure for view %s\n--\n\n", fprintf(sql_file, "\n--\n-- Final view structure for view %s\n--\n\n",
result_table); result_table);
check_io(sql_file); check_io(sql_file);
} }
......
...@@ -1458,7 +1458,6 @@ UNLOCK TABLES; ...@@ -1458,7 +1458,6 @@ UNLOCK TABLES;
/*!40000 ALTER TABLE `t2` ENABLE KEYS */; /*!40000 ALTER TABLE `t2` ENABLE KEYS */;
DROP TABLE IF EXISTS `v2`; DROP TABLE IF EXISTS `v2`;
/*!50001 DROP VIEW IF EXISTS `v2`*/; /*!50001 DROP VIEW IF EXISTS `v2`*/;
/*!50001 DROP TABLE IF EXISTS `v2`*/;
/*!50001 CREATE TABLE `v2` ( /*!50001 CREATE TABLE `v2` (
`a` varchar(30) `a` varchar(30)
) */; ) */;
...@@ -1763,7 +1762,6 @@ UNLOCK TABLES; ...@@ -1763,7 +1762,6 @@ UNLOCK TABLES;
/*!40000 ALTER TABLE `t1` ENABLE KEYS */; /*!40000 ALTER TABLE `t1` ENABLE KEYS */;
DROP TABLE IF EXISTS `v1`; DROP TABLE IF EXISTS `v1`;
/*!50001 DROP VIEW IF EXISTS `v1`*/; /*!50001 DROP VIEW IF EXISTS `v1`*/;
/*!50001 DROP TABLE IF EXISTS `v1`*/;
/*!50001 CREATE TABLE `v1` ( /*!50001 CREATE TABLE `v1` (
`a` int(11) `a` int(11)
) */; ) */;
...@@ -1821,7 +1819,6 @@ UNLOCK TABLES; ...@@ -1821,7 +1819,6 @@ UNLOCK TABLES;
/*!40000 ALTER TABLE `t2` ENABLE KEYS */; /*!40000 ALTER TABLE `t2` ENABLE KEYS */;
DROP TABLE IF EXISTS `v2`; DROP TABLE IF EXISTS `v2`;
/*!50001 DROP VIEW IF EXISTS `v2`*/; /*!50001 DROP VIEW IF EXISTS `v2`*/;
/*!50001 DROP TABLE IF EXISTS `v2`*/;
/*!50001 CREATE TABLE `v2` ( /*!50001 CREATE TABLE `v2` (
`a` varchar(30) `a` varchar(30)
) */; ) */;
...@@ -1914,7 +1911,6 @@ UNLOCK TABLES; ...@@ -1914,7 +1911,6 @@ UNLOCK TABLES;
/*!40000 ALTER TABLE `t1` ENABLE KEYS */; /*!40000 ALTER TABLE `t1` ENABLE KEYS */;
DROP TABLE IF EXISTS `v1`; DROP TABLE IF EXISTS `v1`;
/*!50001 DROP VIEW IF EXISTS `v1`*/; /*!50001 DROP VIEW IF EXISTS `v1`*/;
/*!50001 DROP TABLE IF EXISTS `v1`*/;
/*!50001 CREATE TABLE `v1` ( /*!50001 CREATE TABLE `v1` (
`a` int(11), `a` int(11),
`b` int(11), `b` int(11),
...@@ -1922,13 +1918,11 @@ DROP TABLE IF EXISTS `v1`; ...@@ -1922,13 +1918,11 @@ DROP TABLE IF EXISTS `v1`;
) */; ) */;
DROP TABLE IF EXISTS `v2`; DROP TABLE IF EXISTS `v2`;
/*!50001 DROP VIEW IF EXISTS `v2`*/; /*!50001 DROP VIEW IF EXISTS `v2`*/;
/*!50001 DROP TABLE IF EXISTS `v2`*/;
/*!50001 CREATE TABLE `v2` ( /*!50001 CREATE TABLE `v2` (
`a` int(11) `a` int(11)
) */; ) */;
DROP TABLE IF EXISTS `v3`; DROP TABLE IF EXISTS `v3`;
/*!50001 DROP VIEW IF EXISTS `v3`*/; /*!50001 DROP VIEW IF EXISTS `v3`*/;
/*!50001 DROP TABLE IF EXISTS `v3`*/;
/*!50001 CREATE TABLE `v3` ( /*!50001 CREATE TABLE `v3` (
`a` int(11), `a` int(11),
`b` int(11), `b` int(11),
...@@ -2489,7 +2483,6 @@ UNLOCK TABLES; ...@@ -2489,7 +2483,6 @@ UNLOCK TABLES;
/*!40000 ALTER TABLE `t1` ENABLE KEYS */; /*!40000 ALTER TABLE `t1` ENABLE KEYS */;
DROP TABLE IF EXISTS `v0`; DROP TABLE IF EXISTS `v0`;
/*!50001 DROP VIEW IF EXISTS `v0`*/; /*!50001 DROP VIEW IF EXISTS `v0`*/;
/*!50001 DROP TABLE IF EXISTS `v0`*/;
/*!50001 CREATE TABLE `v0` ( /*!50001 CREATE TABLE `v0` (
`a` int(11), `a` int(11),
`b` varchar(32), `b` varchar(32),
...@@ -2497,7 +2490,6 @@ DROP TABLE IF EXISTS `v0`; ...@@ -2497,7 +2490,6 @@ DROP TABLE IF EXISTS `v0`;
) */; ) */;
DROP TABLE IF EXISTS `v1`; DROP TABLE IF EXISTS `v1`;
/*!50001 DROP VIEW IF EXISTS `v1`*/; /*!50001 DROP VIEW IF EXISTS `v1`*/;
/*!50001 DROP TABLE IF EXISTS `v1`*/;
/*!50001 CREATE TABLE `v1` ( /*!50001 CREATE TABLE `v1` (
`a` int(11), `a` int(11),
`b` varchar(32), `b` varchar(32),
...@@ -2505,16 +2497,11 @@ DROP TABLE IF EXISTS `v1`; ...@@ -2505,16 +2497,11 @@ DROP TABLE IF EXISTS `v1`;
) */; ) */;
DROP TABLE IF EXISTS `v2`; DROP TABLE IF EXISTS `v2`;
/*!50001 DROP VIEW IF EXISTS `v2`*/; /*!50001 DROP VIEW IF EXISTS `v2`*/;
/*!50001 DROP TABLE IF EXISTS `v2`*/;
/*!50001 CREATE TABLE `v2` ( /*!50001 CREATE TABLE `v2` (
`a` int(11), `a` int(11),
`b` varchar(32), `b` varchar(32),
`c` varchar(32) `c` varchar(32)
) */; ) */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `test` /*!40100 DEFAULT CHARACTER SET latin1 */;
USE `test`;
/*!50001 DROP TABLE IF EXISTS `v0`*/; /*!50001 DROP TABLE IF EXISTS `v0`*/;
/*!50001 DROP VIEW IF EXISTS `v0`*/; /*!50001 DROP VIEW IF EXISTS `v0`*/;
/*!50001 CREATE ALGORITHM=UNDEFINED */ /*!50001 CREATE ALGORITHM=UNDEFINED */
...@@ -2731,3 +2718,88 @@ p CREATE DEFINER=`root`@`localhost` PROCEDURE `p`() ...@@ -2731,3 +2718,88 @@ p CREATE DEFINER=`root`@`localhost` PROCEDURE `p`()
select 42 select 42
drop function f; drop function f;
drop procedure p; drop procedure p;
drop database if exists test;
create database test;
use test;
create table t1 (id int);
create view v1 as select * from t1;
insert into t1 values (1232131);
insert into t1 values (4711);
insert into t1 values (3231);
insert into t1 values (0815);
-- MySQL dump 10.10
--
-- Host: localhost Database: test
-- ------------------------------------------------------
-- Server version 5.0.22-debug-log
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Current Database: `test`
--
/*!40000 DROP DATABASE IF EXISTS `test`*/;
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `test` /*!40100 DEFAULT CHARACTER SET latin1 */;
USE `test`;
--
-- Table structure for table `t1`
--
DROP TABLE IF EXISTS `t1`;
CREATE TABLE `t1` (
`id` int(11) default NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Dumping data for table `t1`
--
/*!40000 ALTER TABLE `t1` DISABLE KEYS */;
LOCK TABLES `t1` WRITE;
INSERT INTO `t1` VALUES (1232131),(4711),(3231),(815);
UNLOCK TABLES;
/*!40000 ALTER TABLE `t1` ENABLE KEYS */;
--
-- Temporary table structure for view `v1`
--
DROP TABLE IF EXISTS `v1`;
/*!50001 DROP VIEW IF EXISTS `v1`*/;
/*!50001 CREATE TABLE `v1` (
`id` int(11)
) */;
--
-- Final view structure for view `v1`
--
/*!50001 DROP TABLE IF EXISTS `v1`*/;
/*!50001 DROP VIEW IF EXISTS `v1`*/;
/*!50001 CREATE ALGORITHM=UNDEFINED */
/*!50013 DEFINER=`root`@`localhost` SQL SECURITY DEFINER */
/*!50001 VIEW `v1` AS select `t1`.`id` AS `id` from `t1` */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
...@@ -1143,3 +1143,18 @@ show create procedure p; ...@@ -1143,3 +1143,18 @@ show create procedure p;
drop function f; drop function f;
drop procedure p; drop procedure p;
#
# BUG#17201 Spurious 'DROP DATABASE' in output,
# also confusion between tables and views.
# Example code from Markus Popp
drop database if exists test;
create database test;
use test;
create table t1 (id int);
create view v1 as select * from t1;
insert into t1 values (1232131);
insert into t1 values (4711);
insert into t1 values (3231);
insert into t1 values (0815);
--exec $MYSQL_DUMP --add-drop-database --databases test
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment