Commit 2f0a610f authored by unknown's avatar unknown

Polishing (was the part of original patch for BUG#16899):

Changed trigger-handling code so that there will be the one
place for generate statement string for replication log
and for trigger file.


sql/sql_trigger.cc:
  Changed trigger-handling code so that there will be the one
  place for generate statement string for replication log
  and for trigger file.
sql/sql_trigger.h:
  Changed trigger-handling code so that there will be the one
  place for generate statement string for replication log
  and for trigger file.
parent 965a3970
...@@ -158,11 +158,13 @@ bool mysql_create_or_drop_trigger(THD *thd, TABLE_LIST *tables, bool create) ...@@ -158,11 +158,13 @@ bool mysql_create_or_drop_trigger(THD *thd, TABLE_LIST *tables, bool create)
{ {
TABLE *table; TABLE *table;
bool result= TRUE; bool result= TRUE;
LEX_STRING definer_user; String stmt_query;
LEX_STRING definer_host;
DBUG_ENTER("mysql_create_or_drop_trigger"); DBUG_ENTER("mysql_create_or_drop_trigger");
/* Charset of the buffer for statement must be system one. */
stmt_query.set_charset(system_charset_info);
/* /*
QQ: This function could be merged in mysql_alter_table() function QQ: This function could be merged in mysql_alter_table() function
But do we want this ? But do we want this ?
...@@ -264,8 +266,8 @@ bool mysql_create_or_drop_trigger(THD *thd, TABLE_LIST *tables, bool create) ...@@ -264,8 +266,8 @@ bool mysql_create_or_drop_trigger(THD *thd, TABLE_LIST *tables, bool create)
} }
result= (create ? result= (create ?
table->triggers->create_trigger(thd, tables, &definer_user, &definer_host): table->triggers->create_trigger(thd, tables, &stmt_query):
table->triggers->drop_trigger(thd, tables)); table->triggers->drop_trigger(thd, tables, &stmt_query));
end: end:
VOID(pthread_mutex_unlock(&LOCK_open)); VOID(pthread_mutex_unlock(&LOCK_open));
...@@ -277,32 +279,9 @@ end: ...@@ -277,32 +279,9 @@ end:
{ {
thd->clear_error(); thd->clear_error();
String log_query(thd->query, thd->query_length, system_charset_info);
if (create)
{
log_query.set((char *) 0, 0, system_charset_info); /* reset log_query */
log_query.append(STRING_WITH_LEN("CREATE "));
if (definer_user.str && definer_host.str)
{
/*
Append definer-clause if the trigger is SUID (a usual trigger in
new MySQL versions).
*/
append_definer(thd, &log_query, &definer_user, &definer_host);
}
log_query.append(thd->lex->stmt_definition_begin,
(char *)thd->lex->sphead->m_body_begin -
thd->lex->stmt_definition_begin +
thd->lex->sphead->m_body.length);
}
/* Such a statement can always go directly to binlog, no trans cache. */ /* Such a statement can always go directly to binlog, no trans cache. */
Query_log_event qinfo(thd, log_query.ptr(), log_query.length(), 0, FALSE); Query_log_event qinfo(thd, stmt_query.ptr(), stmt_query.length(), 0,
FALSE);
mysql_bin_log.write(&qinfo); mysql_bin_log.write(&qinfo);
} }
...@@ -322,22 +301,8 @@ end: ...@@ -322,22 +301,8 @@ end:
LEX) LEX)
tables - table list containing one open table for which the tables - table list containing one open table for which the
trigger is created. trigger is created.
definer_user - [out] after a call it points to 0-terminated string or stmt_query - [OUT] after successful return, this string contains
contains the NULL-string: well-formed statement for creation this trigger.
- 0-terminated is returned if the trigger is SUID. The
string contains user name part of the actual trigger
definer.
- NULL-string is returned if the trigger is non-SUID.
Anyway, the caller is responsible to provide memory for
storing LEX_STRING object.
definer_host - [out] after a call it points to 0-terminated string or
contains the NULL-string:
- 0-terminated string is returned if the trigger is
SUID. The string contains host name part of the
actual trigger definer.
- NULL-string is returned if the trigger is non-SUID.
Anyway, the caller is responsible to provide memory for
storing LEX_STRING object.
NOTE NOTE
- Assumes that trigger name is fully qualified. - Assumes that trigger name is fully qualified.
...@@ -352,8 +317,7 @@ end: ...@@ -352,8 +317,7 @@ end:
True - error True - error
*/ */
bool Table_triggers_list::create_trigger(THD *thd, TABLE_LIST *tables, bool Table_triggers_list::create_trigger(THD *thd, TABLE_LIST *tables,
LEX_STRING *definer_user, String *stmt_query)
LEX_STRING *definer_host)
{ {
LEX *lex= thd->lex; LEX *lex= thd->lex;
TABLE *table= tables->table; TABLE *table= tables->table;
...@@ -361,6 +325,8 @@ bool Table_triggers_list::create_trigger(THD *thd, TABLE_LIST *tables, ...@@ -361,6 +325,8 @@ bool Table_triggers_list::create_trigger(THD *thd, TABLE_LIST *tables,
trigname_path[FN_REFLEN]; trigname_path[FN_REFLEN];
LEX_STRING dir, file, trigname_file; LEX_STRING dir, file, trigname_file;
LEX_STRING *trg_def, *name; LEX_STRING *trg_def, *name;
LEX_STRING definer_user;
LEX_STRING definer_host;
ulonglong *trg_sql_mode; ulonglong *trg_sql_mode;
char trg_definer_holder[USER_HOST_BUFF_SIZE]; char trg_definer_holder[USER_HOST_BUFF_SIZE];
LEX_STRING *trg_definer; LEX_STRING *trg_definer;
...@@ -508,8 +474,6 @@ bool Table_triggers_list::create_trigger(THD *thd, TABLE_LIST *tables, ...@@ -508,8 +474,6 @@ bool Table_triggers_list::create_trigger(THD *thd, TABLE_LIST *tables,
definers_list.push_back(trg_definer, &table->mem_root)) definers_list.push_back(trg_definer, &table->mem_root))
goto err_with_cleanup; goto err_with_cleanup;
trg_def->str= thd->query;
trg_def->length= thd->query_length;
*trg_sql_mode= thd->variables.sql_mode; *trg_sql_mode= thd->variables.sql_mode;
#ifndef NO_EMBEDDED_ACCESS_CHECKS #ifndef NO_EMBEDDED_ACCESS_CHECKS
...@@ -529,27 +493,54 @@ bool Table_triggers_list::create_trigger(THD *thd, TABLE_LIST *tables, ...@@ -529,27 +493,54 @@ bool Table_triggers_list::create_trigger(THD *thd, TABLE_LIST *tables,
{ {
/* SUID trigger. */ /* SUID trigger. */
*definer_user= lex->definer->user; definer_user= lex->definer->user;
*definer_host= lex->definer->host; definer_host= lex->definer->host;
trg_definer->str= trg_definer_holder; trg_definer->str= trg_definer_holder;
trg_definer->length= strxmov(trg_definer->str, definer_user->str, "@", trg_definer->length= strxmov(trg_definer->str, definer_user.str, "@",
definer_host->str, NullS) - trg_definer->str; definer_host.str, NullS) - trg_definer->str;
} }
else else
{ {
/* non-SUID trigger. */ /* non-SUID trigger. */
definer_user->str= 0; definer_user.str= 0;
definer_user->length= 0; definer_user.length= 0;
definer_host->str= 0; definer_host.str= 0;
definer_host->length= 0; definer_host.length= 0;
trg_definer->str= (char*) ""; trg_definer->str= (char*) "";
trg_definer->length= 0; trg_definer->length= 0;
} }
/*
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 (trg_definer)
{
/*
Append definer-clause if the trigger is SUID (a usual trigger in
new MySQL versions).
*/
append_definer(thd, stmt_query, &definer_user, &definer_host);
}
stmt_query->append(thd->lex->stmt_definition_begin,
(char *) thd->lex->sphead->m_body_begin -
thd->lex->stmt_definition_begin +
thd->lex->sphead->m_body.length);
trg_def->str= stmt_query->c_ptr();
trg_def->length= stmt_query->length();
/* Create trigger definition file. */
if (!sql_create_definition_file(&dir, &file, &triggers_file_type, if (!sql_create_definition_file(&dir, &file, &triggers_file_type,
(gptr)this, triggers_file_parameters, 0)) (gptr)this, triggers_file_parameters, 0))
return 0; return 0;
...@@ -647,15 +638,19 @@ static bool save_trigger_file(Table_triggers_list *triggers, const char *db, ...@@ -647,15 +638,19 @@ static bool save_trigger_file(Table_triggers_list *triggers, const char *db,
SYNOPSIS SYNOPSIS
drop_trigger() drop_trigger()
thd - current thread context (including trigger definition in LEX) thd - current thread context
tables - table list containing one open table for which trigger is (including trigger definition in LEX)
dropped. tables - table list containing one open table for which trigger
is dropped.
stmt_query - [OUT] after successful return, this string contains
well-formed statement for creation this trigger.
RETURN VALUE RETURN VALUE
False - success False - success
True - error True - error
*/ */
bool Table_triggers_list::drop_trigger(THD *thd, TABLE_LIST *tables) bool Table_triggers_list::drop_trigger(THD *thd, TABLE_LIST *tables,
String *stmt_query)
{ {
LEX *lex= thd->lex; LEX *lex= thd->lex;
LEX_STRING *name; LEX_STRING *name;
...@@ -665,6 +660,8 @@ bool Table_triggers_list::drop_trigger(THD *thd, TABLE_LIST *tables) ...@@ -665,6 +660,8 @@ bool Table_triggers_list::drop_trigger(THD *thd, TABLE_LIST *tables)
List_iterator<LEX_STRING> it_definer(definers_list); List_iterator<LEX_STRING> it_definer(definers_list);
char path[FN_REFLEN]; char path[FN_REFLEN];
stmt_query->append(thd->query, thd->query_length);
while ((name= it_name++)) while ((name= it_name++))
{ {
it_def++; it_def++;
......
...@@ -92,10 +92,8 @@ public: ...@@ -92,10 +92,8 @@ public:
} }
~Table_triggers_list(); ~Table_triggers_list();
bool create_trigger(THD *thd, TABLE_LIST *table, bool create_trigger(THD *thd, TABLE_LIST *table, String *stmt_query);
LEX_STRING *definer_user, bool drop_trigger(THD *thd, TABLE_LIST *table, String *stmt_query);
LEX_STRING *definer_host);
bool drop_trigger(THD *thd, TABLE_LIST *table);
bool process_triggers(THD *thd, trg_event_type event, bool process_triggers(THD *thd, trg_event_type event,
trg_action_time_type time_type, trg_action_time_type time_type,
bool old_row_is_record1); bool old_row_is_record1);
......
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