Commit 6105040a authored by anozdrin@mysql.com's avatar anozdrin@mysql.com

Fix for BUG#13095: Cannot create VIEWs in prepared statements

  - allow CREATE VIEW as well as DROP VIEW to use in prepared statements;
  - fix CREATE VIEW implementation to make it work in prepared statements.
parent 0647d280
...@@ -3532,4 +3532,47 @@ select @a| ...@@ -3532,4 +3532,47 @@ select @a|
Local Local
drop function bug13941| drop function bug13941|
drop procedure bug13941| drop procedure bug13941|
DROP PROCEDURE IF EXISTS bug13095;
DROP TABLE IF EXISTS bug13095_t1;
DROP VIEW IF EXISTS bug13095_v1;
CREATE PROCEDURE bug13095(tbl_name varchar(32))
BEGIN
SET @str =
CONCAT("CREATE TABLE ", tbl_name, "(stuff char(15))");
SELECT @str;
PREPARE stmt FROM @str;
EXECUTE stmt;
SET @str =
CONCAT("INSERT INTO ", tbl_name, " VALUES('row1'),('row2'),('row3')" );
SELECT @str;
PREPARE stmt FROM @str;
EXECUTE stmt;
SET @str =
CONCAT("CREATE VIEW bug13095_v1(c1) AS SELECT stuff FROM ", tbl_name);
SELECT @str;
PREPARE stmt FROM @str;
EXECUTE stmt;
SELECT * FROM bug13095_v1;
SET @str =
"DROP VIEW bug13095_v1";
SELECT @str;
PREPARE stmt FROM @str;
EXECUTE stmt;
END|
CALL bug13095('bug13095_t1');
@str
CREATE TABLE bug13095_t1(stuff char(15))
@str
INSERT INTO bug13095_t1 VALUES('row1'),('row2'),('row3')
@str
CREATE VIEW bug13095_v1(c1) AS SELECT stuff FROM bug13095_t1
c1
row1
row2
row3
@str
DROP VIEW bug13095_v1
DROP PROCEDURE IF EXISTS bug13095;
DROP VIEW IF EXISTS bug13095_v1;
DROP TABLE IF EXISTS bug13095_t1;
drop table t1,t2; drop table t1,t2;
...@@ -4433,6 +4433,62 @@ drop function bug13941| ...@@ -4433,6 +4433,62 @@ drop function bug13941|
drop procedure bug13941| drop procedure bug13941|
#
# BUG#13095: Cannot create VIEWs in prepared statements
#
delimiter ;|
--disable_warnings
DROP PROCEDURE IF EXISTS bug13095;
DROP TABLE IF EXISTS bug13095_t1;
DROP VIEW IF EXISTS bug13095_v1;
--enable_warnings
delimiter |;
CREATE PROCEDURE bug13095(tbl_name varchar(32))
BEGIN
SET @str =
CONCAT("CREATE TABLE ", tbl_name, "(stuff char(15))");
SELECT @str;
PREPARE stmt FROM @str;
EXECUTE stmt;
SET @str =
CONCAT("INSERT INTO ", tbl_name, " VALUES('row1'),('row2'),('row3')" );
SELECT @str;
PREPARE stmt FROM @str;
EXECUTE stmt;
SET @str =
CONCAT("CREATE VIEW bug13095_v1(c1) AS SELECT stuff FROM ", tbl_name);
SELECT @str;
PREPARE stmt FROM @str;
EXECUTE stmt;
SELECT * FROM bug13095_v1;
SET @str =
"DROP VIEW bug13095_v1";
SELECT @str;
PREPARE stmt FROM @str;
EXECUTE stmt;
END|
delimiter ;|
CALL bug13095('bug13095_t1');
--disable_warnings
DROP PROCEDURE IF EXISTS bug13095;
DROP VIEW IF EXISTS bug13095_v1;
DROP TABLE IF EXISTS bug13095_t1;
--enable_warnings
delimiter |;
# #
# BUG#NNNN: New bug synopsis # BUG#NNNN: New bug synopsis
# #
......
...@@ -738,6 +738,7 @@ typedef struct st_lex ...@@ -738,6 +738,7 @@ typedef struct st_lex
/* store original leaf_tables for INSERT SELECT and PS/SP */ /* store original leaf_tables for INSERT SELECT and PS/SP */
TABLE_LIST *leaf_tables_insert; TABLE_LIST *leaf_tables_insert;
st_lex_user *create_view_definer; st_lex_user *create_view_definer;
char *create_view_start;
char *create_view_select_start; char *create_view_select_start;
List<key_part_spec> col_list; List<key_part_spec> col_list;
......
...@@ -1742,6 +1742,8 @@ static bool check_prepared_statement(Prepared_statement *stmt, ...@@ -1742,6 +1742,8 @@ static bool check_prepared_statement(Prepared_statement *stmt,
case SQLCOM_ROLLBACK: case SQLCOM_ROLLBACK:
case SQLCOM_TRUNCATE: case SQLCOM_TRUNCATE:
case SQLCOM_CALL: case SQLCOM_CALL:
case SQLCOM_CREATE_VIEW:
case SQLCOM_DROP_VIEW:
break; break;
default: default:
......
...@@ -643,7 +643,8 @@ static int mysql_register_view(THD *thd, TABLE_LIST *view, ...@@ -643,7 +643,8 @@ static int mysql_register_view(THD *thd, TABLE_LIST *view,
view->query.length= str.length()-1; // we do not need last \0 view->query.length= str.length()-1; // we do not need last \0
view->source.str= thd->lex->create_view_select_start; view->source.str= thd->lex->create_view_select_start;
view->source.length= (thd->query_length - view->source.length= (thd->query_length -
(thd->lex->create_view_select_start - thd->query)); (thd->lex->create_view_select_start -
thd->lex->create_view_start));
view->file_version= 1; view->file_version= 1;
view->calc_md5(md5); view->calc_md5(md5);
view->md5.str= md5; view->md5.str= md5;
......
...@@ -1263,6 +1263,7 @@ create: ...@@ -1263,6 +1263,7 @@ create:
THD *thd= YYTHD; THD *thd= YYTHD;
LEX *lex= thd->lex; LEX *lex= thd->lex;
lex->sql_command= SQLCOM_CREATE_VIEW; lex->sql_command= SQLCOM_CREATE_VIEW;
lex->create_view_start= thd->query;
/* first table in list is target VIEW name */ /* first table in list is target VIEW name */
if (!lex->select_lex.add_table_to_list(thd, $7, NULL, 0)) if (!lex->select_lex.add_table_to_list(thd, $7, NULL, 0))
YYABORT; YYABORT;
...@@ -3425,6 +3426,7 @@ alter: ...@@ -3425,6 +3426,7 @@ alter:
THD *thd= YYTHD; THD *thd= YYTHD;
LEX *lex= thd->lex; LEX *lex= thd->lex;
lex->sql_command= SQLCOM_CREATE_VIEW; lex->sql_command= SQLCOM_CREATE_VIEW;
lex->create_view_start= thd->query;
lex->create_view_mode= VIEW_ALTER; lex->create_view_mode= VIEW_ALTER;
/* first table in list is target VIEW name */ /* first table in list is target VIEW name */
lex->select_lex.add_table_to_list(thd, $6, NULL, 0); lex->select_lex.add_table_to_list(thd, $6, NULL, 0);
......
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