Commit 5d2fc533 authored by Sergey Glukhov's avatar Sergey Glukhov

Bug#40345 MySQLDump prefixes view name with database name when view references other db

print compact view name if the view belongs to the current database


mysql-test/r/information_schema_db.result:
  result fix
mysql-test/r/mysqldump.result:
  result fix
mysql-test/r/view_grant.result:
  result fix
sql/sql_show.cc:
  print compact view name if the view belongs to the current database
parent 2bc4ad4f
...@@ -188,7 +188,7 @@ Field Type Null Key Default Extra ...@@ -188,7 +188,7 @@ Field Type Null Key Default Extra
f1 char(4) YES NULL f1 char(4) YES NULL
show create view v2; show create view v2;
View Create View View Create View
v2 CREATE ALGORITHM=UNDEFINED DEFINER=`testdb_2`@`localhost` SQL SECURITY DEFINER VIEW `test`.`v2` AS select `v1`.`f1` AS `f1` from `testdb_1`.`v1` v2 CREATE ALGORITHM=UNDEFINED DEFINER=`testdb_2`@`localhost` SQL SECURITY DEFINER VIEW `v2` AS select `v1`.`f1` AS `f1` from `testdb_1`.`v1`
show create view testdb_1.v1; show create view testdb_1.v1;
ERROR 42000: SHOW VIEW command denied to user 'testdb_2'@'localhost' for table 'v1' ERROR 42000: SHOW VIEW command denied to user 'testdb_2'@'localhost' for table 'v1'
select table_name from information_schema.columns a select table_name from information_schema.columns a
......
...@@ -3246,7 +3246,7 @@ USE `mysqldump_views`; ...@@ -3246,7 +3246,7 @@ USE `mysqldump_views`;
/*!50001 DROP TABLE `nasishnasifu`*/; /*!50001 DROP TABLE `nasishnasifu`*/;
/*!50001 CREATE ALGORITHM=UNDEFINED */ /*!50001 CREATE ALGORITHM=UNDEFINED */
/*!50013 DEFINER=`root`@`localhost` SQL SECURITY DEFINER */ /*!50013 DEFINER=`root`@`localhost` SQL SECURITY DEFINER */
/*!50001 VIEW `mysqldump_views`.`nasishnasifu` AS select `mysqldump_tables`.`basetable`.`id` AS `id` from `mysqldump_tables`.`basetable` */; /*!50001 VIEW `nasishnasifu` AS select `mysqldump_tables`.`basetable`.`id` AS `id` from `mysqldump_tables`.`basetable` */;
drop view nasishnasifu; drop view nasishnasifu;
drop database mysqldump_views; drop database mysqldump_views;
drop table mysqldump_tables.basetable; drop table mysqldump_tables.basetable;
......
...@@ -26,7 +26,7 @@ create view v2 as select * from mysqltest.t2; ...@@ -26,7 +26,7 @@ create view v2 as select * from mysqltest.t2;
ERROR 42000: ANY command denied to user 'mysqltest_1'@'localhost' for table 't2' ERROR 42000: ANY command denied to user 'mysqltest_1'@'localhost' for table 't2'
show create view v1; show create view v1;
View Create View View Create View
v1 CREATE ALGORITHM=UNDEFINED DEFINER=`mysqltest_1`@`localhost` SQL SECURITY DEFINER VIEW `test`.`v1` AS select `mysqltest`.`t1`.`a` AS `a`,`mysqltest`.`t1`.`b` AS `b` from `mysqltest`.`t1` v1 CREATE ALGORITHM=UNDEFINED DEFINER=`mysqltest_1`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select `mysqltest`.`t1`.`a` AS `a`,`mysqltest`.`t1`.`b` AS `b` from `mysqltest`.`t1`
grant create view,drop,select on test.* to mysqltest_1@localhost; grant create view,drop,select on test.* to mysqltest_1@localhost;
use test; use test;
alter view v1 as select * from mysqltest.t1; alter view v1 as select * from mysqltest.t1;
...@@ -307,7 +307,7 @@ grant create view,select on test.* to mysqltest_1@localhost; ...@@ -307,7 +307,7 @@ grant create view,select on test.* to mysqltest_1@localhost;
create view v1 as select * from mysqltest.t1; create view v1 as select * from mysqltest.t1;
show create view v1; show create view v1;
View Create View View Create View
v1 CREATE ALGORITHM=UNDEFINED DEFINER=`mysqltest_1`@`localhost` SQL SECURITY DEFINER VIEW `test`.`v1` AS select `mysqltest`.`t1`.`a` AS `a`,`mysqltest`.`t1`.`b` AS `b` from `mysqltest`.`t1` v1 CREATE ALGORITHM=UNDEFINED DEFINER=`mysqltest_1`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select `mysqltest`.`t1`.`a` AS `a`,`mysqltest`.`t1`.`b` AS `b` from `mysqltest`.`t1`
revoke select on mysqltest.t1 from mysqltest_1@localhost; revoke select on mysqltest.t1 from mysqltest_1@localhost;
select * from v1; select * from v1;
ERROR HY000: View 'test.v1' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them ERROR HY000: View 'test.v1' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them
......
...@@ -1232,21 +1232,25 @@ void append_definer(THD *thd, String *buffer, const LEX_STRING *definer_user, ...@@ -1232,21 +1232,25 @@ void append_definer(THD *thd, String *buffer, const LEX_STRING *definer_user,
static int static int
view_store_create_info(THD *thd, TABLE_LIST *table, String *buff) view_store_create_info(THD *thd, TABLE_LIST *table, String *buff)
{ {
my_bool compact_view_name= TRUE;
my_bool foreign_db_mode= (thd->variables.sql_mode & (MODE_POSTGRESQL | my_bool foreign_db_mode= (thd->variables.sql_mode & (MODE_POSTGRESQL |
MODE_ORACLE | MODE_ORACLE |
MODE_MSSQL | MODE_MSSQL |
MODE_DB2 | MODE_DB2 |
MODE_MAXDB | MODE_MAXDB |
MODE_ANSI)) != 0; MODE_ANSI)) != 0;
if (!thd->db || strcmp(thd->db, table->view_db.str))
/* /*
Compact output format for view can be used print compact view name if the view belongs to the current database
- if user has db of this view as current db
- if this view only references table inside it's own db
*/ */
if (!thd->db || strcmp(thd->db, table->view_db.str)) compact_view_name= table->compact_view_format= FALSE;
table->compact_view_format= FALSE;
else else
{ {
/*
Compact output format for view body can be used
if this view only references table inside it's own db
*/
TABLE_LIST *tbl; TABLE_LIST *tbl;
table->compact_view_format= TRUE; table->compact_view_format= TRUE;
for (tbl= thd->lex->query_tables; for (tbl= thd->lex->query_tables;
...@@ -1267,7 +1271,7 @@ view_store_create_info(THD *thd, TABLE_LIST *table, String *buff) ...@@ -1267,7 +1271,7 @@ view_store_create_info(THD *thd, TABLE_LIST *table, String *buff)
view_store_options(thd, table, buff); view_store_options(thd, table, buff);
} }
buff->append(STRING_WITH_LEN("VIEW ")); buff->append(STRING_WITH_LEN("VIEW "));
if (!table->compact_view_format) if (!compact_view_name)
{ {
append_identifier(thd, buff, table->view_db.str, table->view_db.length); append_identifier(thd, buff, table->view_db.str, table->view_db.length);
buff->append('.'); buff->append('.');
......
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