Skip to content

Commit

Permalink
MDEV-5273 Prepared statement doesn't return metadata after prepare.
Browse files Browse the repository at this point in the history
  Fix for SHOW CREATE DATABASE.
  • Loading branch information
Alexey Botchkov committed Jan 28, 2016
1 parent 07e9762 commit f3926cd
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 26 deletions.
57 changes: 40 additions & 17 deletions sql/sql_prepare.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1812,6 +1812,14 @@ static bool mysql_test_create_table(Prepared_statement *stmt)
}


static bool send_stmt_metadata(THD *thd, Prepared_statement *stmt, List<Item> *fields)
{
return send_prep_stmt(stmt, fields->elements) ||
thd->protocol->send_result_set_metadata(fields, Protocol::SEND_EOF) ||
thd->protocol->flush();
}


/**
Validate and prepare for execution SHOW CREATE TABLE statement.
Expand All @@ -1824,7 +1832,7 @@ static bool mysql_test_create_table(Prepared_statement *stmt)
TRUE error, error message is set in THD
*/

static int mysql_test_show_create_table(Prepared_statement *stmt,
static bool mysql_test_show_create_table(Prepared_statement *stmt,
TABLE_LIST *tables)
{
DBUG_ENTER("mysql_test_show_create_table");
Expand All @@ -1833,22 +1841,31 @@ static int mysql_test_show_create_table(Prepared_statement *stmt,
char buff[2048];
String buffer(buff, sizeof(buff), system_charset_info);

if (mysqld_show_create_get_fields(thd, tables, &fields, &buffer))
goto err_exit;

if (!stmt->is_sql_prepare())
{
if (send_prep_stmt(stmt, fields.elements) ||
thd->protocol->send_result_set_metadata(&fields, Protocol::SEND_EOF) ||
thd->protocol->flush())
goto err_exit;
DBUG_RETURN(mysqld_show_create_get_fields(thd, tables, &fields, &buffer) ||
send_stmt_metadata(thd, stmt, &fields));
}

DBUG_RETURN(2);
}
DBUG_RETURN(0);

err_exit:
DBUG_RETURN(1);
/**
Validate and prepare for execution SHOW CREATE DATABASE statement.
@param stmt prepared statement
@retval
FALSE success
@retval
TRUE error, error message is set in THD
*/

static bool mysql_test_show_create_db(Prepared_statement *stmt)
{
DBUG_ENTER("mysql_test_show_create_db");
THD *thd= stmt->thd;
List<Item> fields;

mysqld_show_create_db_get_fields(thd, &fields);

DBUG_RETURN(send_stmt_metadata(thd, stmt, &fields));
}


Expand Down Expand Up @@ -2186,8 +2203,14 @@ static bool check_prepared_statement(Prepared_statement *stmt)
res= mysql_test_create_table(stmt);
break;
case SQLCOM_SHOW_CREATE:
res= mysql_test_show_create_table(stmt, tables);
if (res == 2)
if (!(res= mysql_test_show_create_table(stmt, tables)))
{
/* Statement and field info has already been sent */
DBUG_RETURN(FALSE);
}
break;
case SQLCOM_SHOW_CREATE_DB:
if (!(res= mysql_test_show_create_db(stmt)))
{
/* Statement and field info has already been sent */
DBUG_RETURN(FALSE);
Expand Down
24 changes: 16 additions & 8 deletions sql/sql_show.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1272,6 +1272,19 @@ mysqld_show_create(THD *thd, TABLE_LIST *table_list)
DBUG_RETURN(error);
}


void mysqld_show_create_db_get_fields(THD *thd, List<Item> *field_list)
{
MEM_ROOT *mem_root= thd->mem_root;
field_list->push_back(new (mem_root)
Item_empty_string(thd, "Database", NAME_CHAR_LEN),
mem_root);
field_list->push_back(new (mem_root)
Item_empty_string(thd, "Create Database", 1024),
mem_root);
}


bool mysqld_show_create_db(THD *thd, LEX_STRING *dbname,
LEX_STRING *orig_dbname,
const DDL_options_st &options)
Expand All @@ -1284,7 +1297,7 @@ bool mysqld_show_create_db(THD *thd, LEX_STRING *dbname,
#endif
Schema_specification_st create;
Protocol *protocol=thd->protocol;
MEM_ROOT *mem_root= thd->mem_root;
List<Item> field_list;
DBUG_ENTER("mysql_show_create_db");

#ifndef NO_EMBEDDED_ACCESS_CHECKS
Expand Down Expand Up @@ -1318,13 +1331,8 @@ bool mysqld_show_create_db(THD *thd, LEX_STRING *dbname,

load_db_opt_by_name(thd, dbname->str, &create);
}
List<Item> field_list;
field_list.push_back(new (mem_root)
Item_empty_string(thd, "Database", NAME_CHAR_LEN),
mem_root);
field_list.push_back(new (mem_root)
Item_empty_string(thd, "Create Database", 1024),
mem_root);

mysqld_show_create_db_get_fields(thd, &field_list);

if (protocol->send_result_set_metadata(&field_list,
Protocol::SEND_NUM_ROWS |
Expand Down
1 change: 1 addition & 0 deletions sql/sql_show.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ int mysqld_dump_create_info(THD *thd, TABLE_LIST *table_list, int fd);
bool mysqld_show_create_get_fields(THD *thd, TABLE_LIST *table_list,
List<Item> *field_list, String *buffer);
bool mysqld_show_create(THD *thd, TABLE_LIST *table_list);
void mysqld_show_create_db_get_fields(THD *thd, List<Item> *field_list);
bool mysqld_show_create_db(THD *thd, LEX_STRING *db_name,
LEX_STRING *orig_db_name,
const DDL_options_st &options);
Expand Down
7 changes: 6 additions & 1 deletion tests/mysql_client_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -426,9 +426,14 @@ static void test_prepare_simple()
strmov(query, "SHOW CREATE TABLE test_prepare_simple");
stmt= mysql_simple_prepare(mysql, query);
check_stmt(stmt);

DIE_UNLESS(mysql_stmt_field_count(stmt) == 2);
mysql_stmt_close(stmt);

/* show create database */
strmov(query, "SHOW CREATE DATABASE test");
stmt= mysql_simple_prepare(mysql, query);
check_stmt(stmt);
DIE_UNLESS(mysql_stmt_field_count(stmt) == 2);
mysql_stmt_close(stmt);

/* now fetch the results ..*/
Expand Down

0 comments on commit f3926cd

Please sign in to comment.