mirror of
https://github.com/MariaDB/server.git
synced 2025-07-30 16:24:05 +03:00
Fix for bug #5888 "Triggers with nonexistent columns cause packets
out of order". (final version) Now instead of binding Item_trigger_field to TABLE objects during trigger definition parsing at table open, we perform pass through special list of all such objects in trigger. This allows easily check all references to fields in old/new version of row in trigger during execution of CREATE TRIGGER statement (this is more courtesy for users since we can't check everything anyway). We also report that such reference is bad by returning error from Item_trigger_field::fix_fields() method (instead of setup_field()) This means that if trigger is broken we will bark during trigger execution instead of trigger definition parsing at table open. (i.e. now we allow to open tables with broken triggers). mysql-test/r/trigger.result: Added test which attempts to create trigger for table referencing to field which does not exist in this table. mysql-test/t/trigger.test: Added test which attempts to create trigger for table referencing to field which does not exist in this table. sql/item.cc: Item_trigger_field::setup_field() now returns void. If any error will occur we will report it at fix_fields() stage. sql/item.h: Item_trigger_field: - Added next_trg_field member for linking all such objects in trigger in one list. - Also setup_field() now returns void. If any error will occur we will report it at fix_fields() stage. sql/mysql_priv.h: Added SQL_LIST::push_back() method which allows to add another SQL_LIST to the end of this SQL_LIST. sql/sp_head.cc: sp_head::init()/reset_lex()/restore_lex(): In order to fill global LEX::trg_table_fields (list of all Item_trigger_field objects for trigger) we should init the same list in LEX of substatement before its parsing and merge it to global list after parsing. sql/sp_head.h: sp_instr_trigger_field: Made trigger_field member public to be able to add it more easily to global list of all Item_trigger_field objects in trigger. sql/sql_lex.cc: LEX::trg_table was removed. sql/sql_lex.h: Now we are binding Item_trigger_field's to TABLE object by passing through specially constructed list of all such objects in this trigger instead of doing this during trigger definition parsing at table open. So we no longer need LEX::trg_table, we use LEX::trg_table_fields list instead. sql/sql_parse.cc: mysql_execute_command(): Since now we use trigger body for some checks in mysql_create_or_drop_trigger() we should destroy it only after calling this function. sql/sql_trigger.cc: Now instead of binding Item_trigger_field to TABLE objects during trigger definition parsing at table open, we perform pass through special list of all such objects in trigger. This allows easily check all references to fields in old/new version of row in trigger during execution of CREATE TRIGGER statement (this is more courtesy for users since we can't check everything anyway). We also report that such reference is bad by returning error from Item_trigger_field::fix_fields() method (instead of setup_field()) This means that if trigger is broken we will bark during trigger execution instead of trigger definition parsing at table open. (i.e. now we allow to open tables with broken triggers). Table_triggers_list::prepare_old_row_accessors() method was added to be able to reuse code creating Field objects referencing TABLE::record[1] buffer instead of TABLE::record[0]. sql/sql_trigger.h: Added Table_triggers_list::prepare_old_row_accessors() method to be able to reuse code creating Field objects referencing to TABLE::record[1] instead of record[0]. sql/sql_yacc.yy: Now instead of performing binding of Item_trigger_field objects to TABLE object during trigger definition parsing at table open, we perform this binding by passing through specially constructed list of all such items in trigger. We also check value returned from memory allocation functions.
This commit is contained in:
@ -136,6 +136,7 @@ bool Table_triggers_list::create_trigger(THD *thd, TABLE_LIST *tables)
|
||||
char dir_buff[FN_REFLEN], file_buff[FN_REFLEN];
|
||||
LEX_STRING dir, file;
|
||||
LEX_STRING *trg_def, *name;
|
||||
Item_trigger_field *trg_field;
|
||||
List_iterator_fast<LEX_STRING> it(names_list);
|
||||
|
||||
/* We don't allow creation of several triggers of the same type yet */
|
||||
@ -156,6 +157,31 @@ bool Table_triggers_list::create_trigger(THD *thd, TABLE_LIST *tables)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Let us check if all references to fields in old/new versions of row in
|
||||
this trigger are ok.
|
||||
|
||||
NOTE: We do it here more from ease of use standpoint. We still have to
|
||||
do some checks on each execution. E.g. we can catch privilege changes
|
||||
only during execution. Also in near future, when we will allow access
|
||||
to other tables from trigger we won't be able to catch changes in other
|
||||
tables...
|
||||
|
||||
To simplify code a bit we have to create Fields for accessing to old row
|
||||
values if we have ON UPDATE trigger.
|
||||
*/
|
||||
if (!old_field && lex->trg_chistics.event == TRG_EVENT_UPDATE &&
|
||||
prepare_old_row_accessors(table))
|
||||
return 1;
|
||||
|
||||
for (trg_field= (Item_trigger_field *)(lex->trg_table_fields.first);
|
||||
trg_field; trg_field= trg_field->next_trg_field)
|
||||
{
|
||||
trg_field->setup_field(thd, table, lex->trg_chistics.event);
|
||||
if (trg_field->fix_fields(thd, (TABLE_LIST *)0, (Item **)0))
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
Here we are creating file with triggers and save all triggers in it.
|
||||
sql_create_definition_file() files handles renaming and backup of older
|
||||
@ -274,6 +300,44 @@ Table_triggers_list::~Table_triggers_list()
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Prepare array of Field objects which will represent OLD.* row values in
|
||||
ON UPDATE trigger (by referencing to record[1] instead of record[0]).
|
||||
|
||||
SYNOPSIS
|
||||
prepare_old_row_accessors()
|
||||
table - pointer to TABLE object for which we are creating fields.
|
||||
|
||||
RETURN VALUE
|
||||
False - success
|
||||
True - error
|
||||
*/
|
||||
bool Table_triggers_list::prepare_old_row_accessors(TABLE *table)
|
||||
{
|
||||
Field **fld, **old_fld;
|
||||
|
||||
if (!(old_field= (Field **)alloc_root(&table->mem_root,
|
||||
(table->fields + 1) *
|
||||
sizeof(Field*))))
|
||||
return 1;
|
||||
|
||||
for (fld= table->field, old_fld= old_field; *fld; fld++, old_fld++)
|
||||
{
|
||||
/*
|
||||
QQ: it is supposed that it is ok to use this function for field
|
||||
cloning...
|
||||
*/
|
||||
if (!(*old_fld= (*fld)->new_field(&table->mem_root, table)))
|
||||
return 1;
|
||||
(*old_fld)->move_field((my_ptrdiff_t)(table->record[1] -
|
||||
table->record[0]));
|
||||
}
|
||||
*old_fld= 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Check whenever .TRG file for table exist and load all triggers it contains.
|
||||
|
||||
@ -317,7 +381,6 @@ bool Table_triggers_list::check_n_load(THD *thd, const char *db,
|
||||
if (!strncmp(triggers_file_type.str, parser->type()->str,
|
||||
parser->type()->length))
|
||||
{
|
||||
Field **fld, **old_fld;
|
||||
Table_triggers_list *triggers=
|
||||
new (&table->mem_root) Table_triggers_list();
|
||||
|
||||
@ -330,31 +393,10 @@ bool Table_triggers_list::check_n_load(THD *thd, const char *db,
|
||||
|
||||
table->triggers= triggers;
|
||||
|
||||
/*
|
||||
We have to prepare array of Field objects which will represent OLD.*
|
||||
row values by referencing to record[1] instead of record[0]
|
||||
|
||||
TODO: This could be avoided if there is no ON UPDATE trigger.
|
||||
*/
|
||||
if (!(triggers->old_field=
|
||||
(Field **)alloc_root(&table->mem_root, (table->fields + 1) *
|
||||
sizeof(Field*))))
|
||||
/* TODO: This could be avoided if there is no ON UPDATE trigger. */
|
||||
if (triggers->prepare_old_row_accessors(table))
|
||||
DBUG_RETURN(1);
|
||||
|
||||
for (fld= table->field, old_fld= triggers->old_field; *fld;
|
||||
fld++, old_fld++)
|
||||
{
|
||||
/*
|
||||
QQ: it is supposed that it is ok to use this function for field
|
||||
cloning...
|
||||
*/
|
||||
if (!(*old_fld= (*fld)->new_field(&table->mem_root, table)))
|
||||
DBUG_RETURN(1);
|
||||
(*old_fld)->move_field((my_ptrdiff_t)(table->record[1] -
|
||||
table->record[0]));
|
||||
}
|
||||
*old_fld= 0;
|
||||
|
||||
List_iterator_fast<LEX_STRING> it(triggers->definitions_list);
|
||||
LEX_STRING *trg_create_str, *trg_name_str;
|
||||
char *trg_name_buff;
|
||||
@ -365,7 +407,7 @@ bool Table_triggers_list::check_n_load(THD *thd, const char *db,
|
||||
while ((trg_create_str= it++))
|
||||
{
|
||||
lex_start(thd, (uchar*)trg_create_str->str, trg_create_str->length);
|
||||
lex.trg_table= table;
|
||||
|
||||
if (yyparse((void *)thd) || thd->is_fatal_error)
|
||||
{
|
||||
/*
|
||||
@ -400,6 +442,21 @@ bool Table_triggers_list::check_n_load(THD *thd, const char *db,
|
||||
if (triggers->names_list.push_back(trg_name_str, &table->mem_root))
|
||||
goto err_with_lex_cleanup;
|
||||
|
||||
/*
|
||||
Let us bind Item_trigger_field objects representing access to fields
|
||||
in old/new versions of row in trigger to Field objects in table being
|
||||
opened.
|
||||
|
||||
We ignore errors here, because if even something is wrong we still will
|
||||
be willing to open table to perform some operations (e.g. SELECT)...
|
||||
Anyway some things can be checked only during trigger execution.
|
||||
*/
|
||||
for (Item_trigger_field *trg_field=
|
||||
(Item_trigger_field *)(lex.trg_table_fields.first);
|
||||
trg_field;
|
||||
trg_field= trg_field->next_trg_field)
|
||||
trg_field->setup_field(thd, table, lex.trg_chistics.event);
|
||||
|
||||
lex_end(&lex);
|
||||
}
|
||||
thd->lex= old_lex;
|
||||
|
Reference in New Issue
Block a user