Commit 122ffa22 authored by Aleksey Midenkov's avatar Aleksey Midenkov

SQL(misc): Query_arena_stmt RAII

parent 1e8a81de
...@@ -7425,3 +7425,16 @@ bool Discrete_intervals_list::append(Discrete_interval *new_interval) ...@@ -7425,3 +7425,16 @@ bool Discrete_intervals_list::append(Discrete_interval *new_interval)
} }
#endif /* !defined(MYSQL_CLIENT) */ #endif /* !defined(MYSQL_CLIENT) */
Query_arena_stmt::Query_arena_stmt(THD *_thd) :
thd(_thd)
{
arena= thd->activate_stmt_arena_if_needed(&backup);
}
Query_arena_stmt::~Query_arena_stmt()
{
if (arena)
thd->restore_active_arena(arena, &backup);
}
...@@ -1024,6 +1024,22 @@ class Query_arena_memroot: public Query_arena, public Sql_alloc ...@@ -1024,6 +1024,22 @@ class Query_arena_memroot: public Query_arena, public Sql_alloc
}; };
class Query_arena_stmt
{
THD *thd;
Query_arena backup;
Query_arena *arena;
public:
Query_arena_stmt(THD *_thd);
~Query_arena_stmt();
bool arena_replaced()
{
return arena != NULL;
}
};
class Server_side_cursor; class Server_side_cursor;
/** /**
......
...@@ -723,14 +723,11 @@ bool mysql_derived_prepare(THD *thd, LEX *lex, TABLE_LIST *derived) ...@@ -723,14 +723,11 @@ bool mysql_derived_prepare(THD *thd, LEX *lex, TABLE_LIST *derived)
TABLE_SHARE *s= tl->table->s; TABLE_SHARE *s= tl->table->s;
const char *db= tl->db; const char *db= tl->db;
const char *alias= tl->alias; const char *alias= tl->alias;
Query_arena backup; Query_arena_stmt on_stmt_arena(thd);
Query_arena *arena= thd->activate_stmt_arena_if_needed(&backup);
sl->item_list.push_back(new (thd->mem_root) Item_field( sl->item_list.push_back(new (thd->mem_root) Item_field(
thd, &sl->context, db, alias, s->vers_start_field()->field_name)); thd, &sl->context, db, alias, s->vers_start_field()->field_name));
sl->item_list.push_back(new (thd->mem_root) Item_field( sl->item_list.push_back(new (thd->mem_root) Item_field(
thd, &sl->context, db, alias, s->vers_end_field()->field_name)); thd, &sl->context, db, alias, s->vers_end_field()->field_name));
if (arena)
thd->restore_active_arena(arena, &backup);
} }
} }
} }
......
...@@ -695,7 +695,6 @@ int vers_setup_select(THD *thd, TABLE_LIST *tables, COND **where_expr, ...@@ -695,7 +695,6 @@ int vers_setup_select(THD *thd, TABLE_LIST *tables, COND **where_expr,
TABLE_LIST *table; TABLE_LIST *table;
int versioned_tables= 0; int versioned_tables= 0;
int slex_conds_used= 0; int slex_conds_used= 0;
Query_arena *arena= 0, backup;
if (!thd->stmt_arena->is_conventional() && if (!thd->stmt_arena->is_conventional() &&
!thd->stmt_arena->is_stmt_prepare() && !thd->stmt_arena->is_sp_execute()) !thd->stmt_arena->is_stmt_prepare() && !thd->stmt_arena->is_sp_execute())
...@@ -727,7 +726,7 @@ int vers_setup_select(THD *thd, TABLE_LIST *tables, COND **where_expr, ...@@ -727,7 +726,7 @@ int vers_setup_select(THD *thd, TABLE_LIST *tables, COND **where_expr,
/* For prepared statements we create items on statement arena, /* For prepared statements we create items on statement arena,
because they must outlive execution phase for multiple executions. */ because they must outlive execution phase for multiple executions. */
arena= thd->activate_stmt_arena_if_needed(&backup); Query_arena_stmt on_stmt_arena(thd);
if (slex->saved_where) if (slex->saved_where)
{ {
...@@ -798,8 +797,6 @@ int vers_setup_select(THD *thd, TABLE_LIST *tables, COND **where_expr, ...@@ -798,8 +797,6 @@ int vers_setup_select(THD *thd, TABLE_LIST *tables, COND **where_expr,
case TL_WRITE: case TL_WRITE:
case TL_WRITE_ONLY: case TL_WRITE_ONLY:
my_error(ER_VERS_WRONG_PARAMS, MYF(0), "FOR SYSTEM_TIME query", "write-locking of historic rows"); my_error(ER_VERS_WRONG_PARAMS, MYF(0), "FOR SYSTEM_TIME query", "write-locking of historic rows");
if (arena)
thd->restore_active_arena(arena, &backup);
DBUG_RETURN(-1); DBUG_RETURN(-1);
default: default:
break; break;
...@@ -969,7 +966,7 @@ int vers_setup_select(THD *thd, TABLE_LIST *tables, COND **where_expr, ...@@ -969,7 +966,7 @@ int vers_setup_select(THD *thd, TABLE_LIST *tables, COND **where_expr,
cond2, cond2,
cond1)); cond1));
if (arena) if (on_stmt_arena.arena_replaced())
*dst_cond= cond1; *dst_cond= cond1;
else else
thd->change_item_tree(dst_cond, cond1); thd->change_item_tree(dst_cond, cond1);
...@@ -977,9 +974,6 @@ int vers_setup_select(THD *thd, TABLE_LIST *tables, COND **where_expr, ...@@ -977,9 +974,6 @@ int vers_setup_select(THD *thd, TABLE_LIST *tables, COND **where_expr,
} // if (... table->table->versioned()) } // if (... table->table->versioned())
} // for (table= tables; ...) } // for (table= tables; ...)
if (arena)
thd->restore_active_arena(arena, &backup);
if (!slex_conds_used && slex->vers_conditions) if (!slex_conds_used && slex->vers_conditions)
{ {
my_error(ER_VERS_WRONG_QUERY, MYF(0), "unused `QUERY FOR SYSTEM_TIME` clause!"); my_error(ER_VERS_WRONG_QUERY, MYF(0), "unused `QUERY FOR SYSTEM_TIME` clause!");
......
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