Skip to content

Commit

Permalink
MDEV-11347 Move add_create_index_prepare(), add_key_to_list(), set_tr…
Browse files Browse the repository at this point in the history
…igger_new_row(), set_local_variable(), set_system_variable(), create_item_for_sp_var() as methods to LEX

Moving another banch of functions implemented in sql_yacc.yy as methods to LEX,
to be able to reuse them between sql_yacc.yy and sql_yacc_ora.yy easier.
The list of functions:
- add_create_index_prepare()
- add_key_to_list()
- set_trigger_new_row()
- set_local_variable()
- set_system_variable()
- create_item_for_sp_var()
  • Loading branch information
Alexander Barkov committed Dec 6, 2016
1 parent ebd8710 commit 7ae95bd
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 72 deletions.
27 changes: 27 additions & 0 deletions sql/sql_lex.h
Expand Up @@ -41,6 +41,7 @@ class sp_head;
class sp_name;
class sp_instr;
class sp_pcontext;
class sp_variable;
class st_alter_tablespace;
class partition_info;
class Event_parse_data;
Expand Down Expand Up @@ -3022,6 +3023,13 @@ struct LEX: public Query_tables_list
int case_stmt_action_then();
bool add_select_to_union_list(bool is_union_distinct, bool is_top_level);
bool setup_select_in_parentheses();
bool set_trigger_new_row(LEX_STRING *name, Item *val);
bool set_system_variable(struct sys_var_with_base *tmp,
enum enum_var_type var_type, Item *val);
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,
const char *end_in_q);

// Check if "KEY IF NOT EXISTS name" used outside of ALTER context
bool check_add_key(DDL_options_st ddl)
Expand Down Expand Up @@ -3053,6 +3061,25 @@ struct LEX: public Query_tables_list
alter_info.key_list.push_back(last_key);
return false;
}
bool add_create_index_prepare(Table_ident *table)
{
sql_command= SQLCOM_CREATE_INDEX;
if (!current_select->add_table_to_list(thd, table, NULL,
TL_OPTION_UPDATING,
TL_READ_NO_INSERT,
MDL_SHARED_UPGRADABLE))
return true;
alter_info.reset();
alter_info.flags= Alter_info::ALTER_ADD_INDEX;
option_list= NULL;
return false;
}
/*
Add an UNIQUE or PRIMARY key which is a part of a column definition:
CREATE TABLE t1 (a INT PRIMARY KEY);
*/
void add_key_to_list(LEX_STRING *field_name,
enum Key::Keytype type, bool check_exists);
// Add a constraint as a part of CREATE TABLE or ALTER TABLE
bool add_constraint(LEX_STRING *name, Virtual_column_info *constr,
bool if_not_exists)
Expand Down
118 changes: 46 additions & 72 deletions sql/sql_yacc.yy
Expand Up @@ -399,24 +399,22 @@ find_sys_var_null_base(THD *thd, struct sys_var_with_base *tmp)
Helper action for a SET statement.
Used to push a system variable into the assignment list.

@param thd the current thread
@param tmp the system variable with base name
@param var_type the scope of the variable
@param val the value being assigned to the variable

@return TRUE if error, FALSE otherwise.
*/

static bool
set_system_variable(THD *thd, struct sys_var_with_base *tmp,
enum enum_var_type var_type, Item *val)
bool
LEX::set_system_variable(struct sys_var_with_base *tmp,
enum enum_var_type var_type, Item *val)
{
set_var *var;
LEX *lex= thd->lex;

/* No AUTOCOMMIT from a stored function or trigger. */
if (lex->spcont && tmp->var == Sys_autocommit_ptr)
lex->sphead->m_flags|= sp_head::HAS_SET_AUTOCOMMIT_STMT;
if (spcont && tmp->var == Sys_autocommit_ptr)
sphead->m_flags|= sp_head::HAS_SET_AUTOCOMMIT_STMT;

if (val && val->type() == Item::FIELD_ITEM &&
((Item_field*)val)->table_name)
Expand All @@ -429,26 +427,24 @@ set_system_variable(THD *thd, struct sys_var_with_base *tmp,
set_var(thd, var_type, tmp->var, &tmp->base_name, val)))
return TRUE;

return lex->var_list.push_back(var, thd->mem_root);
return var_list.push_back(var, thd->mem_root);
}


/**
Helper action for a SET statement.
Used to push a SP local variable into the assignment list.

@param thd the current thread
@param var_type the SP local variable
@param val the value being assigned to the variable

@return TRUE if error, FALSE otherwise.
*/

static bool
set_local_variable(THD *thd, sp_variable *spv, Item *val)
bool
LEX::set_local_variable(sp_variable *spv, Item *val)
{
Item *it;
LEX *lex= thd->lex;
sp_instr_set *sp_set;

if (val)
Expand All @@ -463,52 +459,48 @@ set_local_variable(THD *thd, sp_variable *spv, Item *val)
}

sp_set= new (thd->mem_root)
sp_instr_set(lex->sphead->instructions(), lex->spcont,
spv->offset, it, spv->sql_type(),
lex, TRUE);
sp_instr_set(sphead->instructions(), spcont,
spv->offset, it, spv->sql_type(),
this, true);

return (sp_set == NULL || lex->sphead->add_instr(sp_set));
return (sp_set == NULL || sphead->add_instr(sp_set));
}


/**
Helper action for a SET statement.
Used to SET a field of NEW row.

@param thd the current thread
@param name the field name
@param val the value being assigned to the row

@return TRUE if error, FALSE otherwise.
*/

static bool
set_trigger_new_row(THD *thd, LEX_STRING *name, Item *val)
bool LEX::set_trigger_new_row(LEX_STRING *name, Item *val)
{
LEX *lex= thd->lex;
Item_trigger_field *trg_fld;
sp_instr_set_trigger_field *sp_fld;

/* QQ: Shouldn't this be field's default value ? */
if (! val)
val= new (thd->mem_root) Item_null(thd);

DBUG_ASSERT(lex->trg_chistics.action_time == TRG_ACTION_BEFORE &&
(lex->trg_chistics.event == TRG_EVENT_INSERT ||
lex->trg_chistics.event == TRG_EVENT_UPDATE));
DBUG_ASSERT(trg_chistics.action_time == TRG_ACTION_BEFORE &&
(trg_chistics.event == TRG_EVENT_INSERT ||
trg_chistics.event == TRG_EVENT_UPDATE));

trg_fld= new (thd->mem_root)
Item_trigger_field(thd, lex->current_context(),
Item_trigger_field(thd, current_context(),
Item_trigger_field::NEW_ROW,
name->str, UPDATE_ACL, FALSE);

if (trg_fld == NULL)
return TRUE;

sp_fld= new (thd->mem_root)
sp_instr_set_trigger_field(lex->sphead->instructions(),
lex->spcont, trg_fld, val,
lex);
sp_instr_set_trigger_field(sphead->instructions(),
spcont, trg_fld, val, this);

if (sp_fld == NULL)
return TRUE;
Expand All @@ -517,16 +509,15 @@ set_trigger_new_row(THD *thd, LEX_STRING *name, Item *val)
Let us add this item to list of all Item_trigger_field
objects in trigger.
*/
lex->trg_table_fields.link_in_list(trg_fld, &trg_fld->next_trg_field);
trg_table_fields.link_in_list(trg_fld, &trg_fld->next_trg_field);

return lex->sphead->add_instr(sp_fld);
return sphead->add_instr(sp_fld);
}


/**
Create an object to represent a SP variable in the Item-hierarchy.

@param thd The current thread.
@param name The SP variable name.
@param spvar The SP variable (optional).
@param start_in_q Start position of the SP variable name in the query.
Expand All @@ -538,29 +529,27 @@ set_trigger_new_row(THD *thd, LEX_STRING *name, Item *val)

@return An Item_splocal object representing the SP variable, or NULL on error.
*/
static Item_splocal*
create_item_for_sp_var(THD *thd, LEX_STRING name, sp_variable *spvar,
const char *start_in_q, const char *end_in_q)
Item_splocal*
LEX::create_item_for_sp_var(LEX_STRING name, sp_variable *spvar,
const char *start_in_q, const char *end_in_q)
{
Item_splocal *item;
LEX *lex= thd->lex;
uint pos_in_q, len_in_q;
sp_pcontext *spc = lex->spcont;

/* If necessary, look for the variable. */
if (spc && !spvar)
spvar= spc->find_variable(name, false);
if (spcont && !spvar)
spvar= spcont->find_variable(name, false);

if (!spvar)
{
my_error(ER_SP_UNDECLARED_VAR, MYF(0), name.str);
return NULL;
}

DBUG_ASSERT(spc && spvar);
DBUG_ASSERT(spcont && spvar);

/* Position and length of the SP variable name in the query. */
pos_in_q= start_in_q - lex->sphead->m_tmp_query;
pos_in_q= start_in_q - sphead->m_tmp_query;
len_in_q= end_in_q - start_in_q;

item= new (thd->mem_root)
Expand All @@ -569,7 +558,7 @@ create_item_for_sp_var(THD *thd, LEX_STRING name, sp_variable *spvar,

#ifndef DBUG_OFF
if (item)
item->m_sp= lex->sphead;
item->m_sp= sphead;
#endif

return item;
Expand Down Expand Up @@ -709,20 +698,6 @@ bool LEX::add_select_to_union_list(bool is_union_distinct,
}


static bool add_create_index_prepare(LEX *lex, Table_ident *table)
{
lex->sql_command= SQLCOM_CREATE_INDEX;
if (!lex->current_select->add_table_to_list(lex->thd, table, NULL,
TL_OPTION_UPDATING,
TL_READ_NO_INSERT,
MDL_SHARED_UPGRADABLE))
return TRUE;
lex->alter_info.reset();
lex->alter_info.flags= Alter_info::ALTER_ADD_INDEX;
lex->option_list= NULL;
return FALSE;
}


/**
Create a separate LEX for each assignment if in SP.
Expand Down Expand Up @@ -833,20 +808,19 @@ static bool sp_create_assignment_instr(THD *thd, bool no_lookahead)
return false;
}


static void add_key_to_list(LEX *lex, LEX_STRING *field_name,
enum Key::Keytype type, bool check_exists)
void LEX::add_key_to_list(LEX_STRING *field_name,
enum Key::Keytype type, bool check_exists)
{
Key *key;
MEM_ROOT *mem_root= lex->thd->mem_root;
MEM_ROOT *mem_root= thd->mem_root;
key= new (mem_root)
Key(type, null_lex_str, HA_KEY_ALG_UNDEF, false,
DDL_options(check_exists ?
DDL_options::OPT_IF_NOT_EXISTS :
DDL_options::OPT_NONE));
key->columns.push_back(new (mem_root) Key_part_spec(*field_name, 0),
mem_root);
lex->alter_info.key_list.push_back(key, mem_root);
alter_info.key_list.push_back(key, mem_root);
}

void LEX::init_last_field(Column_definition *field, const char *field_name,
Expand Down Expand Up @@ -2578,7 +2552,7 @@ create:
opt_key_algorithm_clause
ON table_ident
{
if (add_create_index_prepare(Lex, $8))
if (Lex->add_create_index_prepare($8))
MYSQL_YYABORT;
if (Lex->add_create_index($2, $5, $6, $1 | $4))
MYSQL_YYABORT;
Expand All @@ -2588,7 +2562,7 @@ create:
| create_or_replace fulltext INDEX_SYM opt_if_not_exists ident
ON table_ident
{
if (add_create_index_prepare(Lex, $7))
if (Lex->add_create_index_prepare($7))
MYSQL_YYABORT;
if (Lex->add_create_index($2, $5, HA_KEY_ALG_UNDEF, $1 | $4))
MYSQL_YYABORT;
Expand All @@ -2598,7 +2572,7 @@ create:
| create_or_replace spatial INDEX_SYM opt_if_not_exists ident
ON table_ident
{
if (add_create_index_prepare(Lex, $7))
if (Lex->add_create_index_prepare($7))
MYSQL_YYABORT;
if (Lex->add_create_index($2, $5, HA_KEY_ALG_UNDEF, $1 | $4))
MYSQL_YYABORT;
Expand Down Expand Up @@ -3564,9 +3538,9 @@ simple_target_specification:
ident
{
Lex_input_stream *lip= &thd->m_parser_state->m_lip;
$$= create_item_for_sp_var(thd, $1, NULL,
lip->get_tok_start(), lip->get_ptr());

$$= thd->lex->create_item_for_sp_var($1, NULL,
lip->get_tok_start(),
lip->get_ptr());
if ($$ == NULL)
MYSQL_YYABORT;
}
Expand Down Expand Up @@ -6162,9 +6136,9 @@ field_spec:

$$->create_if_not_exists= Lex->check_exists;
if ($$->flags & PRI_KEY_FLAG)
add_key_to_list(lex, &$1, Key::PRIMARY, Lex->check_exists);
lex->add_key_to_list(&$1, Key::PRIMARY, lex->check_exists);
else if ($$->flags & UNIQUE_KEY_FLAG)
add_key_to_list(lex, &$1, Key::UNIQUE, Lex->check_exists);
lex->add_key_to_list(&$1, Key::UNIQUE, lex->check_exists);
}
;

Expand Down Expand Up @@ -15085,7 +15059,7 @@ option_value_following_option_type:
if ($1.var && $1.var != trg_new_row_fake_var)
{
/* It is a system variable. */
if (set_system_variable(thd, &$1, lex->option_type, $3))
if (lex->set_system_variable(&$1, lex->option_type, $3))
MYSQL_YYABORT;
}
else
Expand All @@ -15109,13 +15083,13 @@ option_value_no_option_type:
if ($1.var == trg_new_row_fake_var)
{
/* We are in trigger and assigning value to field of new row */
if (set_trigger_new_row(thd, &$1.base_name, $3))
if (lex->set_trigger_new_row(&$1.base_name, $3))
MYSQL_YYABORT;
}
else if ($1.var)
{
/* It is a system variable. */
if (set_system_variable(thd, &$1, lex->option_type, $3))
if (lex->set_system_variable(&$1, lex->option_type, $3))
MYSQL_YYABORT;
}
else
Expand All @@ -15124,7 +15098,7 @@ option_value_no_option_type:
sp_variable *spv= spc->find_variable($1.base_name, false);

/* It is a local variable. */
if (set_local_variable(thd, spv, $3))
if (lex->set_local_variable(spv, $3))
MYSQL_YYABORT;
}
}
Expand All @@ -15148,7 +15122,7 @@ option_value_no_option_type:
if (find_sys_var_null_base(thd, &tmp))
MYSQL_YYABORT;
}
if (set_system_variable(thd, &tmp, $3, $6))
if (Lex->set_system_variable(&tmp, $3, $6))
MYSQL_YYABORT;
}
| charset old_or_new_charset_name_or_default
Expand Down

0 comments on commit 7ae95bd

Please sign in to comment.