Skip to content

Commit

Permalink
Moving the code from *.yy to new methods to LEX and sp_context
Browse files Browse the repository at this point in the history
Adding:
  LEX::sp_variable_declarations_init()
  LEX::sp_variable_declarations_finalize()
  LEX::sp_handler_declaration_init()
  LEX::sp_handler_declaration_finalize()
  LEX::sp_declare_cursor()
  sp_context::declare_condition()
  • Loading branch information
Alexander Barkov committed Apr 5, 2017
1 parent 0281757 commit 36b80ca
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 276 deletions.
6 changes: 3 additions & 3 deletions sql/sp_pcontext.cc
Expand Up @@ -249,7 +249,7 @@ bool sp_pcontext::add_condition(THD *thd,
}


sp_condition_value *sp_pcontext::find_condition(LEX_STRING name,
sp_condition_value *sp_pcontext::find_condition(const LEX_STRING name,
bool current_scope_only) const
{
uint i= m_conditions.elements();
Expand Down Expand Up @@ -415,7 +415,7 @@ sp_pcontext::find_handler(const char *sql_state,
}


bool sp_pcontext::add_cursor(LEX_STRING name)
bool sp_pcontext::add_cursor(const LEX_STRING name)
{
if (m_cursors.elements() == m_max_cursor_index)
++m_max_cursor_index;
Expand All @@ -424,7 +424,7 @@ bool sp_pcontext::add_cursor(LEX_STRING name)
}


bool sp_pcontext::find_cursor(LEX_STRING name,
bool sp_pcontext::find_cursor(const LEX_STRING name,
uint *poff,
bool current_scope_only) const
{
Expand Down
22 changes: 17 additions & 5 deletions sql/sp_pcontext.h
Expand Up @@ -192,7 +192,7 @@ class sp_condition : public Sql_alloc
sp_condition_value *value;

public:
sp_condition(LEX_STRING _name, sp_condition_value *_value)
sp_condition(const LEX_STRING _name, sp_condition_value *_value)
:Sql_alloc(),
name(_name),
value(_value)
Expand Down Expand Up @@ -422,11 +422,22 @@ class sp_pcontext : public Sql_alloc
// Conditions.
/////////////////////////////////////////////////////////////////////////

bool add_condition(THD *thd, LEX_STRING name, sp_condition_value *value);
bool add_condition(THD *thd, const LEX_STRING name,
sp_condition_value *value);

/// See comment for find_variable() above.
sp_condition_value *find_condition(LEX_STRING name,
sp_condition_value *find_condition(const LEX_STRING name,
bool current_scope_only) const;
bool declare_condition(THD *thd, const LEX_STRING name,
sp_condition_value *val)
{
if (find_condition(name, true))
{
my_error(ER_SP_DUP_COND, MYF(0), name.str);
return true;
}
return add_condition(thd, name, val);
}

/////////////////////////////////////////////////////////////////////////
// Handlers.
Expand Down Expand Up @@ -467,10 +478,11 @@ class sp_pcontext : public Sql_alloc
// Cursors.
/////////////////////////////////////////////////////////////////////////

bool add_cursor(LEX_STRING name);
bool add_cursor(const LEX_STRING name);

/// See comment for find_variable() above.
bool find_cursor(LEX_STRING name, uint *poff, bool current_scope_only) const;
bool find_cursor(const LEX_STRING name,
uint *poff, bool current_scope_only) const;

/// Find cursor by offset (for debugging only).
const LEX_STRING *find_cursor(uint offset) const;
Expand Down
123 changes: 123 additions & 0 deletions sql/sql_lex.cc
Expand Up @@ -5185,6 +5185,129 @@ bool LEX::init_default_internal_variable(struct sys_var_with_base *variable,
return false;
}

void LEX::sp_variable_declarations_init(THD *thd, int nvars)
{
// get the last variable:
uint num_vars= spcont->context_var_count();
uint var_idx= spcont->var_context2runtime(num_vars - 1);
sp_variable *spvar= spcont->find_variable(var_idx);

sphead->reset_lex(thd);
spcont->declare_var_boundary(nvars);
thd->lex->init_last_field(&spvar->field_def, spvar->name.str,
thd->variables.collation_database);
}

bool LEX::sp_variable_declarations_finalize(THD *thd, int nvars,
const Lex_field_type_st &type,
Item *dflt_value_item)
{
uint num_vars= spcont->context_var_count();

if (!dflt_value_item &&
!(dflt_value_item= new (thd->mem_root) Item_null(thd)))
return true;
/* QQ Set to the var_type with null_value? */

for (uint i= num_vars - nvars ; i < num_vars ; i++)
{
uint var_idx= spcont->var_context2runtime(i);
sp_variable *spvar= spcont->find_variable(var_idx);
bool last= i == num_vars - 1;

if (!spvar)
return true;

if (!last)
spvar->field_def= *last_field;

spvar->default_value= dflt_value_item;
spvar->field_def.field_name= spvar->name.str;

if (sphead->fill_field_definition(thd, &spvar->field_def))
return true;

spvar->field_def.pack_flag |= FIELDFLAG_MAYBE_NULL;

/* The last instruction is responsible for freeing LEX. */
sp_instr_set *is= new (this->thd->mem_root)
sp_instr_set(sphead->instructions(),
spcont, var_idx, dflt_value_item,
type.field_type(), this, last);
if (is == NULL || sphead->add_instr(is))
return true;
}

spcont->declare_var_boundary(0);
return sphead->restore_lex(thd);
}


bool LEX::sp_declare_cursor(THD *thd, const LEX_STRING name, LEX *cursor_stmt)
{
uint offp;
sp_instr_cpush *i;

if (spcont->find_cursor(name, &offp, true))
{
my_error(ER_SP_DUP_CURS, MYF(0), name.str);
return true;
}
i= new (thd->mem_root)
sp_instr_cpush(sphead->instructions(), spcont, cursor_stmt,
spcont->current_cursor_count());
return i == NULL || sphead->add_instr(i) || spcont->add_cursor(name);
}


bool LEX::sp_handler_declaration_init(THD *thd, int type)
{
sp_handler *h= spcont->add_handler(thd, (sp_handler::enum_type) type);

spcont= spcont->push_context(thd, sp_pcontext::HANDLER_SCOPE);

sp_instr_hpush_jump *i=
new (thd->mem_root) sp_instr_hpush_jump(sphead->instructions(), spcont, h);

if (i == NULL || sphead->add_instr(i))
return true;

/* For continue handlers, mark end of handler scope. */
if (type == sp_handler::CONTINUE &&
sphead->push_backpatch(thd, i, spcont->last_label()))
return true;

if (sphead->push_backpatch(thd, i, spcont->push_label(thd, empty_lex_str, 0)))
return true;

return false;
}


bool LEX::sp_handler_declaration_finalize(THD *thd, int type)
{
sp_label *hlab= spcont->pop_label(); /* After this hdlr */
sp_instr_hreturn *i;

if (type == sp_handler::CONTINUE)
{
i= new (thd->mem_root) sp_instr_hreturn(sphead->instructions(), spcont);
if (i == NULL ||
sphead->add_instr(i))
return true;
}
else
{ /* EXIT or UNDO handler, just jump to the end of the block */
i= new (thd->mem_root) sp_instr_hreturn(sphead->instructions(), spcont);
if (i == NULL ||
sphead->add_instr(i) ||
sphead->push_backpatch(thd, i, spcont->last_label())) /* Block end */
return true;
}
sphead->backpatch(hlab);
spcont= spcont->pop_context();
return false;
}

#ifdef MYSQL_SERVER
uint binlog_unsafe_map[256];
Expand Down
8 changes: 8 additions & 0 deletions sql/sql_lex.h
Expand Up @@ -3071,6 +3071,14 @@ struct LEX: public Query_tables_list
LEX_STRING dbname, LEX_STRING name);
bool init_default_internal_variable(struct sys_var_with_base *variable,
LEX_STRING name);
void sp_variable_declarations_init(THD *thd, int nvars);
bool sp_variable_declarations_finalize(THD *thd, int nvars,
const Lex_field_type_st &type,
Item *def);
bool sp_handler_declaration_init(THD *thd, int type);
bool sp_handler_declaration_finalize(THD *thd, int type);

bool sp_declare_cursor(THD *thd, const LEX_STRING name, LEX *cursor_stmt);
bool set_local_variable(sp_variable *spv, Item *val);
Item_splocal *create_item_for_sp_var(LEX_STRING name, sp_variable *spvar,
const char *start_in_q,
Expand Down

0 comments on commit 36b80ca

Please sign in to comment.