Skip to content

Commit b9ac987

Browse files
committed
Merge branch 'mysql-5.6' into mysql-5.7
2 parents 3218235 + 88301e5 commit b9ac987

File tree

1 file changed

+40
-6
lines changed

1 file changed

+40
-6
lines changed

sql/sp_instr.cc

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,35 @@ static bool subst_spvars(THD *thd, sp_instr *instr, LEX_STRING *query_str)
261261
///////////////////////////////////////////////////////////////////////////
262262

263263

264+
class SP_instr_error_handler : public Internal_error_handler
265+
{
266+
public:
267+
SP_instr_error_handler()
268+
: cts_table_exists_error(false)
269+
{}
270+
271+
virtual bool handle_condition(THD *thd,
272+
uint sql_errno,
273+
const char*,
274+
Sql_condition::enum_severity_level*,
275+
const char*)
276+
{
277+
/*
278+
Check if the "table exists" error or warning reported for the
279+
CREATE TABLE ... SELECT statement.
280+
*/
281+
if (thd->lex && thd->lex->sql_command == SQLCOM_CREATE_TABLE &&
282+
thd->lex->select_lex && thd->lex->select_lex->item_list.elements > 0 &&
283+
sql_errno == ER_TABLE_EXISTS_ERROR)
284+
cts_table_exists_error= true;
285+
286+
return false;
287+
}
288+
289+
bool cts_table_exists_error;
290+
};
291+
292+
264293
bool sp_lex_instr::reset_lex_and_exec_core(THD *thd,
265294
uint *nextp,
266295
bool open_tables)
@@ -336,6 +365,9 @@ bool sp_lex_instr::reset_lex_and_exec_core(THD *thd,
336365
thd->lex->safe_to_cache_query= 0;
337366
#endif
338367

368+
SP_instr_error_handler sp_instr_error_handler;
369+
thd->push_internal_handler(&sp_instr_error_handler);
370+
339371
/* Open tables if needed. */
340372

341373
if (!error)
@@ -413,6 +445,9 @@ bool sp_lex_instr::reset_lex_and_exec_core(THD *thd,
413445
}
414446
}
415447

448+
// Pop SP_instr_error_handler error handler.
449+
thd->pop_internal_handler();
450+
416451
if (m_lex->query_tables_own_last)
417452
{
418453
/*
@@ -451,7 +486,8 @@ bool sp_lex_instr::reset_lex_and_exec_core(THD *thd,
451486
See Query_arena->state definition for explanation.
452487
453488
Some special handling of CREATE TABLE .... SELECT in an SP is required. The
454-
state is always set to STMT_INITIALIZED_FOR_SP in such a case.
489+
state is set to STMT_INITIALIZED_FOR_SP even in case of "table exists"
490+
error situation.
455491
456492
Why is this necessary? A useful pointer would be to note how
457493
PREPARE/EXECUTE uses functions like select_like_stmt_test to implement
@@ -467,12 +503,10 @@ bool sp_lex_instr::reset_lex_and_exec_core(THD *thd,
467503
*/
468504

469505
bool reprepare_error=
470-
error && thd->get_stmt_da()->mysql_errno() == ER_NEED_REPREPARE;
471-
bool is_create_table_select=
472-
thd->lex && thd->lex->sql_command == SQLCOM_CREATE_TABLE &&
473-
thd->lex->select_lex && thd->lex->select_lex->item_list.elements > 0;
506+
error && thd->is_error() &&
507+
thd->get_stmt_da()->mysql_errno() == ER_NEED_REPREPARE;
474508

475-
if (reprepare_error || is_create_table_select)
509+
if (reprepare_error || sp_instr_error_handler.cts_table_exists_error)
476510
thd->stmt_arena->state= Query_arena::STMT_INITIALIZED_FOR_SP;
477511
else if (!error || !thd->is_error() ||
478512
(thd->get_stmt_da()->mysql_errno() != ER_CANT_REOPEN_TABLE &&

0 commit comments

Comments
 (0)