diff options
| author | Alexander Barkov <bar@mariadb.org> | 2015-03-04 09:52:01 +0400 |
|---|---|---|
| committer | Alexander Barkov <bar@mariadb.org> | 2015-03-04 09:52:01 +0400 |
| commit | 87b0cc99123e5d6923c77ac3e08c4e9ed0d4d769 (patch) | |
| tree | 1dfc0f010060d53a1db9547e4464b662faf112d2 /sql | |
| parent | a7ed8523e35ff2e82701cd1f483c8f665f322f3b (diff) | |
| download | mariadb-git-87b0cc99123e5d6923c77ac3e08c4e9ed0d4d769.tar.gz | |
MDEV-7286 TRIGGER: CREATE OR REPLACE, CREATE IF NOT EXISTS
Based on the patch by Sriram Patil, made under terms of GSoC 2014.
Diffstat (limited to 'sql')
| -rw-r--r-- | sql/sql_trigger.cc | 164 | ||||
| -rw-r--r-- | sql/sql_yacc.yy | 45 |
2 files changed, 122 insertions, 87 deletions
diff --git a/sql/sql_trigger.cc b/sql/sql_trigger.cc index 4b208134723..41e0ffe2df6 100644 --- a/sql/sql_trigger.cc +++ b/sql/sql_trigger.cc @@ -608,6 +608,63 @@ end: DBUG_RETURN(result); } +/** + Build stmt_query to write it in the bin-log + and get the trigger definer. + + @param thd current thread context (including trigger definition in + LEX) + @param tables table list containing one open table for which the + trigger is created. + @param[out] stmt_query after successful return, this string contains + well-formed statement for creation this trigger. + + @param[out] trg_definer The triggger definer. + @param[out] trg_definer_holder Used as a buffer for definer. + + @note + - Assumes that trigger name is fully qualified. + - NULL-string means the following LEX_STRING instance: + { str = 0; length = 0 }. + - In other words, definer_user and definer_host should contain + simultaneously NULL-strings (non-SUID/old trigger) or valid strings + (SUID/new trigger). +*/ +static void build_trig_stmt_query(THD *thd, TABLE_LIST *tables, + String *stmt_query, + LEX_STRING *trg_definer, + char trg_definer_holder[]) +{ + LEX *lex= thd->lex; + + /* + Create a query with the full trigger definition. + The original query is not appropriate, as it can miss the DEFINER=XXX part. + */ + stmt_query->append(STRING_WITH_LEN("CREATE ")); + + if (lex->create_info.or_replace()) + stmt_query->append(STRING_WITH_LEN("OR REPLACE ")); + + if (lex->sphead->m_chistics->suid != SP_IS_NOT_SUID) + { + /* SUID trigger */ + lex->definer->set_lex_string(trg_definer, trg_definer_holder); + append_definer(thd, stmt_query, &lex->definer->user, &lex->definer->host); + } + else + { + *trg_definer= empty_lex_str; + } + + LEX_STRING stmt_definition; + stmt_definition.str= (char*) thd->lex->stmt_definition_begin; + stmt_definition.length= thd->lex->stmt_definition_end - + thd->lex->stmt_definition_begin; + trim_whitespace(thd->charset(), &stmt_definition); + stmt_query->append(stmt_definition.str, stmt_definition.length); +} + /** Create trigger for table. @@ -640,8 +697,6 @@ bool Table_triggers_list::create_trigger(THD *thd, TABLE_LIST *tables, char file_buff[FN_REFLEN], trigname_buff[FN_REFLEN]; LEX_STRING file, trigname_file; LEX_STRING *trg_def; - LEX_STRING definer_user; - LEX_STRING definer_host; ulonglong *trg_sql_mode; char trg_definer_holder[USER_HOST_BUFF_SIZE]; LEX_STRING *trg_definer; @@ -650,6 +705,8 @@ bool Table_triggers_list::create_trigger(THD *thd, TABLE_LIST *tables, LEX_STRING *trg_client_cs_name; LEX_STRING *trg_connection_cl_name; LEX_STRING *trg_db_cl_name; + sp_head *trg_body= bodies[lex->trg_chistics.event] + [lex->trg_chistics.action_time]; if (check_for_broken_triggers()) return true; @@ -659,20 +716,31 @@ bool Table_triggers_list::create_trigger(THD *thd, TABLE_LIST *tables, lex->spname->m_db.str)) { my_error(ER_TRG_IN_WRONG_SCHEMA, MYF(0)); - return 1; + return true; } - /* We don't allow creation of several triggers of the same type yet */ - if (bodies[lex->trg_chistics.event][lex->trg_chistics.action_time] != 0) + /* + We don't allow creation of several triggers of the same type yet. + If a trigger with the same type already exists: + a. Throw a ER_NOT_SUPPORTED_YET error, + if the old and the new trigger names are different; + b. Or continue, if the old and the new trigger names are the same: + - either to recreate the trigger on "CREATE OR REPLACE" + - or send a "already exists" warning on "CREATE IF NOT EXISTS" + - or send an "alredy exists" error on normal CREATE. + */ + if (trg_body != 0 && + my_strcasecmp(table_alias_charset, + trg_body->m_name.str, lex->spname->m_name.str)) { my_error(ER_NOT_SUPPORTED_YET, MYF(0), "multiple triggers with the same action time" " and event for one table"); - return 1; + return true; } if (sp_process_definer(thd)) - return 1; + return true; /* Let us check if all references to fields in old/new versions of row in @@ -701,7 +769,7 @@ bool Table_triggers_list::create_trigger(THD *thd, TABLE_LIST *tables, if (!trg_field->fixed && trg_field->fix_fields(thd, (Item **)0)) - return 1; + return true; } /* @@ -722,8 +790,29 @@ bool Table_triggers_list::create_trigger(THD *thd, TABLE_LIST *tables, /* Use the filesystem to enforce trigger namespace constraints. */ if (!access(trigname_buff, F_OK)) { - my_error(ER_TRG_ALREADY_EXISTS, MYF(0)); - return 1; + if (lex->create_info.or_replace()) + { + String drop_trg_query; + drop_trg_query.append("DROP TRIGGER "); + drop_trg_query.append(lex->spname->m_name.str); + if (drop_trigger(thd, tables, &drop_trg_query)) + return 1; + } + else if (lex->create_info.if_not_exists()) + { + push_warning_printf(thd, Sql_condition::WARN_LEVEL_NOTE, + ER_TRG_ALREADY_EXISTS, ER(ER_TRG_ALREADY_EXISTS), + trigname_buff); + LEX_STRING trg_definer_tmp; + build_trig_stmt_query(thd, tables, stmt_query, + &trg_definer_tmp, trg_definer_holder); + return false; + } + else + { + my_error(ER_TRG_ALREADY_EXISTS, MYF(0)); + return true; + } } trigname.trigger_table.str= tables->table_name; @@ -731,7 +820,7 @@ bool Table_triggers_list::create_trigger(THD *thd, TABLE_LIST *tables, if (sql_create_definition_file(NULL, &trigname_file, &trigname_file_type, (uchar*)&trigname, trigname_file_parameters)) - return 1; + return true; /* Soon we will invalidate table object and thus Table_triggers_list object @@ -764,29 +853,6 @@ bool Table_triggers_list::create_trigger(THD *thd, TABLE_LIST *tables, *trg_sql_mode= thd->variables.sql_mode; - if (lex->sphead->m_chistics->suid != SP_IS_NOT_SUID) - { - /* SUID trigger. */ - - definer_user= lex->definer->user; - definer_host= lex->definer->host; - - lex->definer->set_lex_string(trg_definer, trg_definer_holder); - } - else - { - /* non-SUID trigger. */ - - definer_user.str= 0; - definer_user.length= 0; - - definer_host.str= 0; - definer_host.length= 0; - - trg_definer->str= (char*) ""; - trg_definer->length= 0; - } - /* Fill character set information: - client character set contains charset info only; @@ -802,30 +868,8 @@ bool Table_triggers_list::create_trigger(THD *thd, TABLE_LIST *tables, lex_string_set(trg_db_cl_name, get_default_db_collation(thd, tables->db)->name); - /* - Create well-formed trigger definition query. Original query is not - appropriated, because definer-clause can be not truncated. - */ - - stmt_query->append(STRING_WITH_LEN("CREATE ")); - - if (lex->sphead->m_chistics->suid != SP_IS_NOT_SUID) - { - /* - Append definer-clause if the trigger is SUID (a usual trigger in - new MySQL versions). - */ - - append_definer(thd, stmt_query, &definer_user, &definer_host); - } - - LEX_STRING stmt_definition; - stmt_definition.str= (char*) thd->lex->stmt_definition_begin; - stmt_definition.length= thd->lex->stmt_definition_end - - thd->lex->stmt_definition_begin; - trim_whitespace(thd->charset(), & stmt_definition); - - stmt_query->append(stmt_definition.str, stmt_definition.length); + build_trig_stmt_query(thd, tables, stmt_query, + trg_definer, trg_definer_holder); trg_def->str= stmt_query->c_ptr_safe(); trg_def->length= stmt_query->length(); @@ -834,11 +878,11 @@ bool Table_triggers_list::create_trigger(THD *thd, TABLE_LIST *tables, if (!sql_create_definition_file(NULL, &file, &triggers_file_type, (uchar*)this, triggers_file_parameters)) - return 0; + return false; err_with_cleanup: mysql_file_delete(key_file_trn, trigname_buff, MYF(MY_WME)); - return 1; + return true; } diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy index f0b723b2d2a..ae6ba56c1e7 100644 --- a/sql/sql_yacc.yy +++ b/sql/sql_yacc.yy @@ -2560,20 +2560,6 @@ create: Lex->create_view_suid= TRUE; } view_or_trigger_or_sp_or_event - { - // TODO: remove this when "MDEV-5359 CREATE OR REPLACE..." is done - if ($1.or_replace() && - Lex->sql_command != SQLCOM_CREATE_EVENT && - Lex->sql_command != SQLCOM_CREATE_VIEW && - Lex->sql_command != SQLCOM_CREATE_FUNCTION && - Lex->sql_command != SQLCOM_CREATE_SPFUNCTION && - Lex->sql_command != SQLCOM_CREATE_PROCEDURE) - { - my_error(ER_WRONG_USAGE, MYF(0), "OR REPLACE", - "TRIGGERS / SP / EVENT"); - MYSQL_YYABORT; - } - } | create_or_replace USER opt_if_not_exists clear_privileges grant_list { if (Lex->set_command_with_check(SQLCOM_CREATE_USER, $1 | $3)) @@ -16159,23 +16145,28 @@ view_check_option: trigger_tail: TRIGGER_SYM remember_name + opt_if_not_exists + { + if (Lex->add_create_options_with_check($3)) + MYSQL_YYABORT; + } sp_name trg_action_time trg_event ON - remember_name /* $7 */ - { /* $8 */ + remember_name /* $9 */ + { /* $10 */ Lex->raw_trg_on_table_name_begin= YYLIP->get_tok_start(); } - table_ident /* $9 */ + table_ident /* $11 */ FOR_SYM - remember_name /* $11 */ - { /* $12 */ + remember_name /* $13 */ + { /* $14 */ Lex->raw_trg_on_table_name_end= YYLIP->get_tok_start(); } EACH_SYM ROW_SYM - { /* $15 */ + { /* $17 */ LEX *lex= thd->lex; Lex_input_stream *lip= YYLIP; @@ -16186,17 +16177,17 @@ trigger_tail: } lex->stmt_definition_begin= $2; - lex->ident.str= $7; - lex->ident.length= $11 - $7; - lex->spname= $3; + lex->ident.str= $9; + lex->ident.length= $13 - $9; + lex->spname= $5; - if (!make_sp_head(thd, $3, TYPE_ENUM_TRIGGER)) + if (!make_sp_head(thd, $5, TYPE_ENUM_TRIGGER)) MYSQL_YYABORT; lex->sphead->set_body_start(thd, lip->get_cpp_ptr()); } - sp_proc_stmt /* $16 */ - { /* $17 */ + sp_proc_stmt /* $18 */ + { /* $19 */ LEX *lex= Lex; sp_head *sp= lex->sphead; @@ -16212,7 +16203,7 @@ trigger_tail: sp_proc_stmt alternatives are not saving/restoring LEX, so lex->query_tables can be wiped out. */ - if (!lex->select_lex.add_table_to_list(thd, $9, + if (!lex->select_lex.add_table_to_list(thd, $11, (LEX_STRING*) 0, TL_OPTION_UPDATING, TL_READ_NO_INSERT, |
