From ff102adddf78d001d97f11735a7c1af67fcba07a Mon Sep 17 00:00:00 2001
From: "bell@sanja.is.com.ua" <>
Date: Sun, 26 Sep 2004 19:00:55 +0300
Subject: [PATCH] fix xml with view

---
 client/mysqldump.c            | 18 +++++++++--
 mysql-test/r/mysqldump.result | 57 +++++++++++++++++++++++++++++++++++
 mysql-test/t/mysqldump.test   |  9 ++++++
 sql/sql_base.cc               |  3 ++
 4 files changed, 84 insertions(+), 3 deletions(-)

diff --git a/client/mysqldump.c b/client/mysqldump.c
index f48f862a0e0..e6c33ffdaa9 100644
--- a/client/mysqldump.c
+++ b/client/mysqldump.c
@@ -1161,6 +1161,12 @@ static uint getTableStructure(char *table, char* db)
       sprintf(buff,"show keys from %s", result_table);
       if (mysql_query(sock, buff))
       {
+        if (mysql_errno(sock) == ER_WRONG_OBJECT)
+        {
+          /* it is VIEW */
+          fputs("\t\t<options Comment=\"view\" />\n", sql_file);
+          goto continue_xml;
+        }
         fprintf(stderr, "%s: Can't get keys for table %s (%s)\n",
 		my_progname, result_table, mysql_error(sock));
         if (path)
@@ -1268,6 +1274,7 @@ static uint getTableStructure(char *table, char* db)
         }
         mysql_free_result(tableRes);		/* Is always safe to free */
       }
+continue_xml:
       if (!opt_xml)
 	fputs(";\n", sql_file);
       else
@@ -2134,9 +2141,14 @@ static const char *check_if_ignore_table(const char *table_name)
       mysql_free_result(res);
     return 0;					/* assume table is ok */
   }
-  if (strcmp(row[1], (result= "MRG_MyISAM")) &&
-      strcmp(row[1], (result= "MRG_ISAM")))
-    result= 0;
+  if (!(row[1]))
+      result= "VIEW";
+  else
+  {
+    if (strcmp(row[1], (result= "MRG_MyISAM")) &&
+        strcmp(row[1], (result= "MRG_ISAM")))
+      result= 0;
+  }
   mysql_free_result(res);  
   return result;
 }
diff --git a/mysql-test/r/mysqldump.result b/mysql-test/r/mysqldump.result
index 7e69620394b..d10da6457ed 100644
--- a/mysql-test/r/mysqldump.result
+++ b/mysql-test/r/mysqldump.result
@@ -332,3 +332,60 @@ CREATE TABLE `t1` (
 2
 3
 drop table t1;
+create table t1(a int);
+create view v1 as select * from t1;
+-- MySQL dump 10.7
+--
+-- Host: localhost    Database: test
+-- ------------------------------------------------------
+-- Server version	5.0.2-alpha-valgrind-max-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 */;
+/*!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" */;
+
+--
+-- Table structure for table `t1`
+--
+
+DROP TABLE IF EXISTS `t1`;
+CREATE TABLE `t1` (
+  `a` int(11) default NULL
+) ENGINE=MyISAM DEFAULT CHARSET=latin1;
+
+--
+-- Dumping data for table `t1`
+--
+
+
+/*!40000 ALTER TABLE `t1` DISABLE KEYS */;
+LOCK TABLES `t1` WRITE;
+UNLOCK TABLES;
+/*!40000 ALTER TABLE `t1` ENABLE KEYS */;
+
+--
+-- Table structure for table `v1`
+--
+
+DROP TABLE IF EXISTS `v1`;
+
+--
+-- View structure for view `v1`
+--
+
+DROP VIEW IF EXISTS `v1`;
+CREATE 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 */;
+/*!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 */;
+
+drop view v1;
+drop table t1;
diff --git a/mysql-test/t/mysqldump.test b/mysql-test/t/mysqldump.test
index 89b3739f955..b9fdfb3188d 100644
--- a/mysql-test/t/mysqldump.test
+++ b/mysql-test/t/mysqldump.test
@@ -127,3 +127,12 @@ insert into t1 values (1),(2),(3);
 --exec rm $MYSQL_TEST_DIR/var/tmp/t1.sql
 --exec rm $MYSQL_TEST_DIR/var/tmp/t1.txt
 drop table t1;
+
+#
+# dump of view
+#
+create table t1(a int);
+create view v1 as select * from t1;
+--exec $MYSQL_DUMP test
+drop view v1;
+drop table t1;
diff --git a/sql/sql_base.cc b/sql/sql_base.cc
index e7c2cfe3534..d627bf670d5 100644
--- a/sql/sql_base.cc
+++ b/sql/sql_base.cc
@@ -913,6 +913,9 @@ TABLE *open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT *mem_root,
     */
     {
       char path[FN_REFLEN];
+      TABLE tab;
+      if (!table)
+        table= &tab;
       strxnmov(path, FN_REFLEN, mysql_data_home, "/", table_list->db, "/",
                table_list->real_name, reg_ext, NullS);
       (void) unpack_filename(path, path);
-- 
2.30.9