19 #include "my_global.h"
23 #include "sql_trigger.h"
24 #include "sql_parse.h"
25 #include "parse_file.h"
29 #include "sql_table.h"
33 #include "sql_handler.h"
35 #include <mysys_err.h>
42 return (T *) alloc_root(m,
sizeof (T));
53 template ulonglong *alloc_type<ulonglong>(
MEM_ROOT *m);
57 return alloc_type<LEX_STRING>(m);
107 Trigger_creation_ctx::create(THD *thd,
118 bool invalid_creation_ctx= FALSE;
120 if (resolve_charset(client_cs_name->str,
121 thd->variables.character_set_client,
124 sql_print_warning(
"Trigger for table '%s'.'%s': "
125 "invalid character_set_client value (%s).",
126 (
const char *) db_name,
127 (
const char *) table_name,
128 (
const char *) client_cs_name->str);
130 invalid_creation_ctx= TRUE;
133 if (resolve_collation(connection_cl_name->str,
134 thd->variables.collation_connection,
137 sql_print_warning(
"Trigger for table '%s'.'%s': "
138 "invalid collation_connection value (%s).",
139 (
const char *) db_name,
140 (
const char *) table_name,
141 (
const char *) connection_cl_name->str);
143 invalid_creation_ctx= TRUE;
146 if (resolve_collation(db_cl_name->str, NULL, &db_cl))
148 sql_print_warning(
"Trigger for table '%s'.'%s': "
149 "invalid database_collation value (%s).",
150 (
const char *) db_name,
151 (
const char *) table_name,
152 (
const char *) db_cl_name->str);
154 invalid_creation_ctx= TRUE;
157 if (invalid_creation_ctx)
159 push_warning_printf(thd,
160 Sql_condition::WARN_LEVEL_WARN,
161 ER_TRG_INVALID_CREATION_CTX,
162 ER(ER_TRG_INVALID_CREATION_CTX),
163 (
const char *) db_name,
164 (
const char *) table_name);
173 db_cl= get_default_db_collation(thd, db_name);
181 { C_STRING_WITH_LEN(
"TRIGGERS") };
183 const char *
const TRG_EXT=
".TRG";
194 { C_STRING_WITH_LEN(
"triggers") },
199 { C_STRING_WITH_LEN(
"sql_modes") },
204 { C_STRING_WITH_LEN(
"definers") },
209 { C_STRING_WITH_LEN(
"client_cs_names") },
214 { C_STRING_WITH_LEN(
"connection_cl_names") },
219 { C_STRING_WITH_LEN(
"db_cl_names") },
223 { { 0, 0 }, 0, FILE_OPTIONS_STRING }
228 { C_STRING_WITH_LEN(
"sql_modes") },
239 static const int TRG_NUM_REQUIRED_PARAMETERS= 6;
252 { C_STRING_WITH_LEN(
"TRIGGERNAME") };
254 const char *
const TRN_EXT=
".TRN";
259 { C_STRING_WITH_LEN(
"trigger_table")},
263 { { 0, 0 }, 0, FILE_OPTIONS_STRING }
267 const LEX_STRING trg_action_time_type_names[]=
269 { C_STRING_WITH_LEN(
"BEFORE") },
270 { C_STRING_WITH_LEN(
"AFTER") }
275 { C_STRING_WITH_LEN(
"INSERT") },
276 { C_STRING_WITH_LEN(
"UPDATE") },
277 { C_STRING_WITH_LEN(
"DELETE") }
289 virtual bool process_unknown_string(
const char *&unknown_key, uchar* base,
290 MEM_ROOT *mem_root,
const char *end);
299 :path(file_path), trigger_table_value(trigger_table_arg)
301 virtual bool process_unknown_string(
const char *&unknown_key, uchar* base,
302 MEM_ROOT *mem_root,
const char *end);
322 char m_message[MYSQL_ERRMSG_SIZE];
329 virtual bool handle_condition(THD *thd,
331 const char* sqlstate,
332 Sql_condition::enum_warning_level
level,
336 if (sql_errno != EE_OUTOFMEMORY &&
337 sql_errno != ER_OUT_OF_RESOURCES)
340 m_trigger_name= &thd->lex->spname->m_name;
342 my_snprintf(m_message,
sizeof(m_message),
343 ER(ER_ERROR_IN_TRIGGER_BODY),
344 m_trigger_name->str, message);
346 my_snprintf(m_message,
sizeof(m_message),
347 ER(ER_ERROR_IN_UNKNOWN_TRIGGER_BODY), message);
353 LEX_STRING *get_trigger_name() {
return m_trigger_name; }
354 char *get_error_message() {
return m_message; }
381 bool mysql_create_or_drop_trigger(THD *thd,
TABLE_LIST *tables,
bool create)
393 bool lock_upgrade_done= FALSE;
395 Query_tables_list backup;
397 DBUG_ENTER(
"mysql_create_or_drop_trigger");
400 stmt_query.set_charset(system_charset_info);
412 if (!thd->lex->spname->m_db.length || (create && !tables->db_length))
414 my_error(ER_NO_DB_ERROR, MYF(0));
421 if (create && !my_strcasecmp(system_charset_info,
"mysql", tables->db))
423 my_error(ER_NO_TRIGGERS_ON_SYSTEM_SCHEMA, MYF(0));
437 if (!trust_function_creators && mysql_bin_log.is_open() &&
438 !(thd->security_ctx->master_access & SUPER_ACL))
440 my_error(ER_BINLOG_CREATE_ROUTINE_NEED_SUPER, MYF(0));
446 bool if_exists= thd->lex->drop_if_exists;
452 thd->lex->reset_n_backup_query_tables_list(&backup);
459 thd->lex->sql_command= backup.sql_command;
461 if (add_table_for_trigger(thd, thd->lex->spname, if_exists, & tables))
466 DBUG_ASSERT(if_exists);
477 stmt_query.append(thd->query(), thd->query_length());
487 TABLE_LIST **save_query_tables_own_last= thd->lex->query_tables_own_last;
488 thd->lex->query_tables_own_last= 0;
492 thd->lex->query_tables_own_last= save_query_tables_own_last;
499 DBUG_ASSERT(tables->next_global == 0);
504 my_error(ER_TRG_ON_VIEW_OR_TEMP_TABLE, MYF(0), tables->alias);
509 tables->required_type= FRMTYPE_TABLE;
517 mysql_ha_rm_tables(thd, tables);
519 if (thd->locked_tables_mode)
530 TL_READ_NO_INSERT, 0);
533 tables->table->use_all_columns();
535 table= tables->table;
538 mdl_ticket= table->mdl_ticket;
543 lock_upgrade_done= TRUE;
545 if (!table->triggers)
549 my_error(ER_TRG_DOES_NOT_EXIST, MYF(0));
559 table->triggers->
drop_trigger(thd, tables, &stmt_query));
570 thd->locked_tables_list.reopen_tables(thd);
576 sp_cache_invalidate();
582 thd->add_to_binlog_accessed_dbs(tables->db);
583 result= write_bin_log(thd, TRUE, stmt_query.ptr(), stmt_query.length());
591 if (thd->locked_tables_mode && tables && lock_upgrade_done)
596 thd->lex->restore_backup_query_tables_list(&backup);
632 TABLE *table= tables->table;
633 char file_buff[FN_REFLEN], trigname_buff[FN_REFLEN];
638 sql_mode_t *trg_sql_mode;
639 char trg_definer_holder[USER_HOST_BUFF_SIZE];
647 if (check_for_broken_triggers())
651 if (my_strcasecmp(table_alias_charset, table->s->db.str,
652 lex->spname->m_db.str))
654 my_error(ER_TRG_IN_WRONG_SCHEMA, MYF(0));
663 if (bodies[trg_event][trg_action_time] != NULL)
665 my_error(ER_NOT_SUPPORTED_YET, MYF(0),
666 "multiple triggers with the same action time"
667 " and event for one table");
689 if (!thd->slave_thread)
703 (strcmp(lex->definer->user.str, thd->security_ctx->priv_user) ||
704 my_strcasecmp(system_charset_info,
705 lex->definer->host.str,
706 thd->security_ctx->priv_host)))
710 my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0),
"SUPER");
729 old_field= new_field= table->field;
732 trg_field; trg_field= trg_field->next_trg_field)
738 trg_field->setup_field(thd, table, NULL);
740 if (!trg_field->fixed &&
741 trg_field->fix_fields(thd, (
Item **)0))
750 file.length= build_table_filename(file_buff, FN_REFLEN - 1,
751 tables->db, tables->table_name,
755 trigname_file.length= build_table_filename(trigname_buff, FN_REFLEN-1,
757 lex->spname->m_name.str,
758 TRN_EXT, 0, &was_truncated);
762 my_error(ER_IDENT_CAUSES_TOO_LONG_PATH, MYF(0),
sizeof(trigname_buff)-1,
766 trigname_file.str= trigname_buff;
770 if (!access(trigname_buff, F_OK))
772 my_error(ER_TRG_ALREADY_EXISTS, MYF(0));
776 trigname.trigger_table.str= tables->table_name;
777 trigname.trigger_table.length= tables->table_name_length;
780 (uchar*)&trigname, trigname_file_parameters))
791 if (!(trg_def= alloc_lex_string(&table->
mem_root)) ||
794 !(trg_sql_mode= alloc_type<sql_mode_t>(&table->
mem_root)) ||
797 !(trg_definer= alloc_lex_string(&table->
mem_root)) ||
798 definers_list.push_back(trg_definer, &table->
mem_root) ||
800 !(trg_client_cs_name= alloc_lex_string(&table->
mem_root)) ||
801 client_cs_names.push_back(trg_client_cs_name, &table->
mem_root) ||
803 !(trg_connection_cl_name= alloc_lex_string(&table->
mem_root)) ||
804 connection_cl_names.push_back(trg_connection_cl_name, &table->
mem_root) ||
806 !(trg_db_cl_name= alloc_lex_string(&table->
mem_root)) ||
807 db_cl_names.push_back(trg_db_cl_name, &table->
mem_root))
809 goto err_with_cleanup;
812 *trg_sql_mode= thd->variables.sql_mode;
814 #ifndef NO_EMBEDDED_ACCESS_CHECKS
815 if (lex->definer && !is_acl_user(lex->definer->host.str,
816 lex->definer->user.str))
818 push_warning_printf(thd,
819 Sql_condition::WARN_LEVEL_NOTE,
822 lex->definer->user.str,
823 lex->definer->host.str);
831 definer_user= lex->definer->user;
832 definer_host= lex->definer->host;
834 trg_definer->str= trg_definer_holder;
835 trg_definer->length= strxmov(trg_definer->str, definer_user.str,
"@",
836 definer_host.str, NullS) - trg_definer->str;
843 definer_user.length= 0;
846 definer_host.length= 0;
848 trg_definer->str= (
char*)
"";
849 trg_definer->length= 0;
859 lex_string_set(trg_client_cs_name, thd->charset()->csname);
861 lex_string_set(trg_connection_cl_name,
862 thd->variables.collation_connection->name);
864 lex_string_set(trg_db_cl_name,
865 get_default_db_collation(thd, tables->db)->name);
872 stmt_query->append(STRING_WITH_LEN(
"CREATE "));
881 append_definer(thd, stmt_query, &definer_user, &definer_host);
885 stmt_definition.str= (
char*) thd->lex->stmt_definition_begin;
886 stmt_definition.length= thd->lex->stmt_definition_end
887 - thd->lex->stmt_definition_begin;
888 trim_whitespace(thd->charset(), & stmt_definition);
890 stmt_query->append(stmt_definition.str, stmt_definition.length);
892 trg_def->str= stmt_query->c_ptr();
893 trg_def->length= stmt_query->length();
898 (uchar*)
this, triggers_file_parameters))
921 static bool rm_trigger_file(
char *path,
const char *db,
922 const char *table_name)
924 build_table_filename(path, FN_REFLEN-1, db, table_name, TRG_EXT, 0);
943 static bool rm_trigname_file(
char *path,
const char *db,
944 const char *trigger_name)
946 build_table_filename(path, FN_REFLEN - 1, db, trigger_name, TRN_EXT, 0);
965 const char *table_name)
967 char file_buff[FN_REFLEN];
970 file.length= build_table_filename(file_buff, FN_REFLEN - 1, db, table_name,
974 (uchar*)triggers, triggers_file_parameters);
1002 const char *
sp_name= thd->lex->spname->m_name.str;
1005 char path[FN_REFLEN];
1016 stmt_query->append(thd->query(), thd->query_length());
1018 while ((name= it_name++))
1023 it_client_cs_name++;
1024 it_connection_cl_name++;
1027 if (my_strcasecmp(table_alias_charset, sp_name, name->str) == 0)
1035 it_definer.remove();
1036 it_client_cs_name.remove();
1037 it_connection_cl_name.remove();
1038 it_db_cl_name.remove();
1048 if (rm_trigger_file(path, tables->db, tables->table_name))
1053 if (save_trigger_file(
this, tables->db, tables->table_name))
1057 if (rm_trigname_file(path, tables->db, sp_name))
1063 my_message(ER_TRG_DOES_NOT_EXIST, ER(ER_TRG_DOES_NOT_EXIST), MYF(0));
1068 Table_triggers_list::~Table_triggers_list()
1070 for (
int i= 0;
i < (int)TRG_EVENT_MAX;
i++)
1071 for (
int j= 0; j < (int)TRG_ACTION_MAX; j++)
1072 delete bodies[
i][j];
1075 for (
Field **fld_ptr= record1_field; *fld_ptr; fld_ptr++)
1090 bool Table_triggers_list::prepare_record1_accessors()
1092 Field **fld, **old_fld;
1099 for (fld=
trigger_table->field, old_fld= record1_field; *fld; fld++, old_fld++)
1108 (*old_fld)->move_field_offset((my_ptrdiff_t)(
trigger_table->record[1] -
1126 for (
Field **field= new_table->triggers->record1_field ; *field ; field++)
1128 (*field)->table= (*field)->orig_table= new_table;
1129 (*field)->table_name= &new_table->alias;
1157 const char *table_name,
TABLE *table,
1160 char path_buff[FN_REFLEN];
1164 PSI_statement_locker *parent_locker= thd->m_statement_psi;
1166 DBUG_ENTER(
"Table_triggers_list::check_n_load");
1168 path.length= build_table_filename(path_buff, FN_REFLEN - 1,
1169 db, table_name, TRG_EXT, 0);
1170 path.str= path_buff;
1173 if (access(path_buff, F_OK))
1184 if (is_equal(&triggers_file_type, parser->type()))
1201 triggers->definers_list.empty();
1202 triggers->client_cs_names.empty();
1203 triggers->connection_cl_names.empty();
1204 triggers->db_cl_names.empty();
1207 triggers_file_parameters,
1208 TRG_NUM_REQUIRED_PARAMETERS,
1214 sql_mode_t *trg_sql_mode;
1225 if (!(trg_sql_mode= alloc_type<sql_mode_t>(&table->
mem_root)))
1229 *trg_sql_mode= global_system_variables.
sql_mode;
1241 if (triggers->definers_list.is_empty() &&
1254 if (!(trg_definer= alloc_lex_string(&table->
mem_root)))
1257 trg_definer->str= (
char*)
"";
1258 trg_definer->length= 0;
1262 if (triggers->definers_list.push_back(trg_definer,
1273 (triggers->client_cs_names.is_empty() ||
1274 triggers->connection_cl_names.is_empty() ||
1275 triggers->db_cl_names.is_empty()))
1285 if (!triggers->client_cs_names.is_empty() ||
1286 !triggers->connection_cl_names.is_empty() ||
1287 !triggers->db_cl_names.is_empty())
1289 my_error(ER_TRG_CORRUPTED_FILE, MYF(0),
1291 (
const char *) table_name);
1296 push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
1297 ER_TRG_NO_CREATION_CTX,
1298 ER(ER_TRG_NO_CREATION_CTX),
1300 (
const char*) table_name);
1302 if (!(trg_client_cs_name= alloc_lex_string(&table->
mem_root)) ||
1303 !(trg_connection_cl_name= alloc_lex_string(&table->
mem_root)) ||
1304 !(trg_db_cl_name= alloc_lex_string(&table->
mem_root)))
1314 lex_string_set(trg_client_cs_name,
1315 thd->variables.character_set_client->csname);
1317 lex_string_set(trg_connection_cl_name,
1318 thd->variables.collation_connection->name);
1320 lex_string_set(trg_db_cl_name,
1321 thd->variables.collation_database->name);
1325 if (triggers->client_cs_names.push_back(trg_client_cs_name,
1328 triggers->connection_cl_names.push_back(trg_connection_cl_name,
1331 triggers->db_cl_names.push_back(trg_db_cl_name,
1343 DBUG_ASSERT(triggers->definers_list.elements ==
1345 DBUG_ASSERT(triggers->client_cs_names.elements ==
1347 DBUG_ASSERT(triggers->connection_cl_names.elements ==
1349 DBUG_ASSERT(triggers->db_cl_names.elements ==
1352 table->triggers= triggers;
1358 if (!names_only && triggers->prepare_record1_accessors())
1366 LEX *old_lex= thd->lex, lex;
1367 sp_rcontext *sp_runtime_ctx_saved= thd->sp_runtime_ctx;
1368 sql_mode_t save_sql_mode= thd->variables.sql_mode;
1373 save_db.str= thd->db;
1374 save_db.length= thd->db_length;
1375 thd->reset_db((
char*) db, strlen(db));
1376 while ((trg_create_str= it++))
1378 trg_sql_mode= itm++;
1381 thd->variables.sql_mode= *trg_sql_mode;
1383 Parser_state parser_state;
1384 if (parser_state.init(thd, trg_create_str->str, trg_create_str->length))
1385 goto err_with_lex_cleanup;
1388 Trigger_creation_ctx::create(thd,
1391 it_client_cs_name++,
1392 it_connection_cl_name++,
1396 thd->sp_runtime_ctx= NULL;
1399 thd->push_internal_handler(&error_handler);
1400 thd->m_statement_psi= NULL;
1401 bool parse_error=
parse_sql(thd, & parser_state, creation_ctx);
1402 thd->m_statement_psi= parent_locker;
1403 thd->pop_internal_handler();
1413 lex.set_trg_event_type_for_tables();
1417 if (!triggers->m_has_unparseable_trigger)
1420 DBUG_ASSERT(lex.sphead == NULL);
1421 if (error_handler.get_trigger_name())
1424 const LEX_STRING *orig_trigger_name= error_handler.get_trigger_name();
1426 if (!(trigger_name= alloc_lex_string(&table->
mem_root)) ||
1427 !(trigger_name->str= strmake_root(&table->
mem_root,
1428 orig_trigger_name->str,
1429 orig_trigger_name->length)))
1430 goto err_with_lex_cleanup;
1432 trigger_name->length= orig_trigger_name->length;
1434 if (triggers->names_list.push_back(trigger_name,
1436 goto err_with_lex_cleanup;
1450 goto err_with_lex_cleanup;
1452 empty->str=
const_cast<char*
>(
"");
1454 if (triggers->names_list.push_back(empty, &table->
mem_root))
1455 goto err_with_lex_cleanup;
1462 sp->set_info(0, 0, &lex.sp_chistics, *trg_sql_mode);
1468 triggers->bodies[trg_event][trg_action_time]= sp;
1471 sp->set_info(0, 0, &lex.sp_chistics, *trg_sql_mode);
1472 sp->set_creation_ctx(creation_ctx);
1474 if (!trg_definer->length)
1482 push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
1483 ER_TRG_NO_DEFINER, ER(ER_TRG_NO_DEFINER),
1485 (
const char*) sp->m_name.str);
1492 sp->set_definer((
char*)
"", 0);
1502 sp->set_definer(trg_definer->str, trg_definer->length);
1504 if (triggers->names_list.push_back(&sp->m_name, &table->
mem_root))
1505 goto err_with_lex_cleanup;
1507 if (!(on_table_name= alloc_lex_string(&table->
mem_root)))
1508 goto err_with_lex_cleanup;
1510 on_table_name->str= (
char*) lex.raw_trg_on_table_name_begin;
1511 on_table_name->length= lex.raw_trg_on_table_name_end
1512 - lex.raw_trg_on_table_name_begin;
1514 if (triggers->on_table_names_list.push_back(on_table_name, &table->
mem_root))
1515 goto err_with_lex_cleanup;
1528 char fname[NAME_LEN + 1];
1529 DBUG_ASSERT((!my_strcasecmp(table_alias_charset, lex.query_tables->db, db) ||
1530 (check_n_cut_mysql50_prefix(db, fname,
sizeof(fname)) &&
1531 !my_strcasecmp(table_alias_charset, lex.query_tables->db, fname))));
1532 DBUG_ASSERT((!my_strcasecmp(table_alias_charset, lex.query_tables->table_name, table_name) ||
1533 (check_n_cut_mysql50_prefix(table_name, fname,
sizeof(fname)) &&
1534 !my_strcasecmp(table_alias_charset, lex.query_tables->table_name, fname))));
1553 trg_field= trg_field->next_trg_field)
1555 trg_field->setup_field(thd, table,
1561 thd->reset_db(save_db.str, save_db.length);
1563 thd->sp_runtime_ctx= sp_runtime_ctx_saved;
1564 thd->variables.sql_mode= save_sql_mode;
1568 err_with_lex_cleanup:
1572 thd->sp_runtime_ctx= sp_runtime_ctx_saved;
1573 thd->variables.sql_mode= save_sql_mode;
1574 thd->reset_db(save_db.str, save_db.length);
1582 my_error(ER_WRONG_OBJECT, MYF(0),
1583 table_name, TRG_EXT + 1,
"TRIGGER");
1611 trg_action_time_type time_type,
1614 sql_mode_t *sql_mode,
1621 DBUG_ENTER(
"get_trigger_info");
1622 if ((body= bodies[event][time_type]))
1625 bodies[event][time_type]->get_creation_ctx();
1627 *trigger_name= body->m_name;
1628 *trigger_stmt= body->m_body_utf8;
1631 if (body->
m_chistics->suid == SP_IS_NOT_SUID)
1638 definer->length= strxmov(definer->str, body->m_definer_user.str,
"@",
1639 body->m_definer_host.str, NullS) - definer->str;
1642 lex_string_set(client_cs_name,
1643 creation_ctx->get_client_cs()->csname);
1645 lex_string_set(connection_cl_name,
1646 creation_ctx->get_connection_cl()->name);
1648 lex_string_set(db_cl_name,
1649 creation_ctx->get_db_cl()->name);
1660 sql_mode_t *sql_mode,
1673 for (
int i = 0;
i < trigger_idx; ++
i)
1675 it_trigger_name.next_fast();
1676 it_sql_mode.next_fast();
1677 it_sql_orig_stmt.next_fast();
1679 it_client_cs_name.next_fast();
1680 it_connection_cl_name.next_fast();
1681 it_db_cl_name.next_fast();
1684 *trigger_name= *(it_trigger_name++);
1685 *sql_mode= *(it_sql_mode++);
1686 *sql_original_stmt= *(it_sql_orig_stmt++);
1688 *client_cs_name= *(it_client_cs_name++);
1689 *connection_cl_name= *(it_connection_cl_name++);
1690 *db_cl_name= *(it_db_cl_name++);
1694 int Table_triggers_list::find_trigger_by_name(
const LEX_STRING *trg_name)
1698 for (
int i = 0; ; ++
i)
1705 if (strcmp(cur_name->str, trg_name->str) == 0)
1727 bool add_table_for_trigger(THD *thd,
1733 char trn_path_buff[FN_REFLEN];
1737 DBUG_ENTER(
"add_table_for_trigger");
1739 build_trn_path(thd, trg_name, &trn_path);
1741 if (check_trn_exists(&trn_path))
1745 push_warning_printf(thd,
1746 Sql_condition::WARN_LEVEL_NOTE,
1747 ER_TRG_DOES_NOT_EXIST,
1748 ER(ER_TRG_DOES_NOT_EXIST));
1755 my_error(ER_TRG_DOES_NOT_EXIST, MYF(0));
1759 if (load_table_name_for_trigger(thd, trg_name, &trn_path, &tbl_name))
1762 *table= sp_add_to_query_tables(thd, lex, trg_name->m_db.str,
1763 tbl_name.str, TL_IGNORE,
1764 MDL_SHARED_NO_WRITE);
1766 DBUG_RETURN(*table ? FALSE : TRUE);
1786 char path[FN_REFLEN];
1788 DBUG_ENTER(
"drop_all_triggers");
1790 memset(&table, 0,
sizeof(table));
1791 init_sql_alloc(&table.
mem_root, 8192, 0);
1803 while ((trigger= it_name++))
1810 if (trigger->length == 0)
1812 if (rm_trigname_file(path, db, trigger->str))
1823 if (rm_trigger_file(path, db, name))
1831 delete table.triggers;
1832 free_root(&table.
mem_root, MYF(0));
1833 DBUG_RETURN(result);
1854 Table_triggers_list::change_table_name_in_triggers(THD *thd,
1855 const char *old_db_name,
1856 const char *new_db_name,
1860 char path_buff[FN_REFLEN];
1862 sql_mode_t save_sql_mode= thd->variables.sql_mode;
1866 size_t on_q_table_name_len, before_on_len;
1872 while ((def= it_def++))
1874 on_table_name= it_on_table_name++;
1875 thd->variables.sql_mode= *(it_mode++);
1881 DBUG_ASSERT(on_table_name->str > def->str);
1882 DBUG_ASSERT(on_table_name->str < (def->str + def->length));
1883 before_on_len= on_table_name->str - def->str;
1885 buff.append(def->str, before_on_len);
1886 buff.append(STRING_WITH_LEN(
"ON "));
1887 append_identifier(thd, &buff, new_table_name->str, new_table_name->length);
1888 buff.append(STRING_WITH_LEN(
" "));
1889 on_q_table_name_len= buff.length() - before_on_len;
1890 buff.append(on_table_name->str + on_table_name->length,
1891 def->length - (before_on_len + on_table_name->length));
1898 new_def.length= buff.length();
1899 on_table_name->str= new_def.str + before_on_len;
1900 on_table_name->length= on_q_table_name_len;
1904 thd->variables.sql_mode= save_sql_mode;
1906 if (thd->is_fatal_error)
1909 if (save_trigger_file(
this, new_db_name, new_table_name->str))
1911 if (rm_trigger_file(path_buff, old_db_name, old_table_name->str))
1913 (void) rm_trigger_file(path_buff, new_db_name, new_table_name->str);
1938 Table_triggers_list::change_table_name_in_trignames(
const char *old_db_name,
1939 const char *new_db_name,
1943 char trigname_buff[FN_REFLEN];
1949 while ((trigger= it_name++) != stopper)
1951 trigname_file.length= build_table_filename(trigname_buff, FN_REFLEN-1,
1952 new_db_name, trigger->str,
1954 trigname_file.str= trigname_buff;
1956 trigname.trigger_table= *new_table_name;
1959 (uchar*)&trigname, trigname_file_parameters))
1965 if (rm_trigname_file(trigname_buff, old_db_name, trigger->str))
1967 (void) rm_trigname_file(trigname_buff, new_db_name, trigger->str);
1999 const char *old_alias,
2000 const char *old_table,
2002 const char *new_table)
2006 bool upgrading50to51= FALSE;
2008 DBUG_ENTER(
"change_table_name");
2010 memset(&table, 0,
sizeof(table));
2011 init_sql_alloc(&table.
mem_root, 8192, 0);
2017 DBUG_ASSERT(thd->mdl_context.is_lock_owner(MDL_key::TABLE, db, old_table,
2020 DBUG_ASSERT(my_strcasecmp(table_alias_charset, db, new_db) ||
2021 my_strcasecmp(table_alias_charset, old_alias, new_table));
2030 if (table.triggers->check_for_broken_triggers())
2035 LEX_STRING old_table_name= { (
char *) old_alias, strlen(old_alias) };
2036 LEX_STRING new_table_name= { (
char *) new_table, strlen(new_table) };
2047 if (my_strcasecmp(table_alias_charset, db, new_db))
2049 char dbname[NAME_LEN + 1];
2050 if (check_n_cut_mysql50_prefix(db, dbname,
sizeof(dbname)) &&
2051 !my_strcasecmp(table_alias_charset, dbname, new_db))
2053 upgrading50to51= TRUE;
2057 my_error(ER_TRG_IN_WRONG_SCHEMA, MYF(0));
2062 if (table.triggers->change_table_name_in_triggers(thd, db, new_db,
2069 if ((err_trigname= table.triggers->change_table_name_in_trignames(
2070 upgrading50to51 ? db : NULL,
2071 new_db, &new_table_name, 0)))
2079 (void) table.triggers->change_table_name_in_trignames(
2080 upgrading50to51 ? new_db : NULL, db,
2081 &old_table_name, err_trigname);
2082 (void) table.triggers->change_table_name_in_triggers(
2084 &new_table_name, &old_table_name);
2091 delete table.triggers;
2092 free_root(&table.
mem_root, MYF(0));
2093 DBUG_RETURN(result);
2114 trg_event_type
event,
2115 trg_action_time_type time_type,
2116 bool old_row_is_record1)
2119 Sub_statement_state statement_state;
2120 sp_head *sp_trigger= bodies[event][time_type];
2121 SELECT_LEX *save_current_select;
2123 if (check_for_broken_triggers())
2126 if (sp_trigger == NULL)
2129 if (old_row_is_record1)
2131 old_field= record1_field;
2136 new_field= record1_field;
2144 static_cast<uint>(1 << static_cast<int>(event)));
2146 thd->reset_sub_statement_state(&statement_state, SUB_STMT_TRIGGER);
2153 save_current_select= thd->lex->current_select;
2154 thd->lex->current_select= NULL;
2160 thd->lex->current_select= save_current_select;
2162 thd->restore_sub_statement_state(&statement_state);
2184 Query_tables_list *prelocking_ctx,
2187 DBUG_ASSERT(static_cast<int>(table_list->lock_type) >=
2188 static_cast<int>(TL_WRITE_ALLOW_WRITE));
2190 for (
int i= 0;
i < (int)TRG_EVENT_MAX;
i++)
2193 static_cast<uint8>(1 << static_cast<int>(
i)))
2195 for (
int j= 0; j < (int)TRG_ACTION_MAX; j++)
2198 sp_head *trigger= table_list->table->triggers->bodies[
i][j];
2202 MDL_key key(MDL_key::TRIGGER, trigger->m_db.str, trigger->m_name.str);
2204 if (sp_add_used_routine(prelocking_ctx, thd->stmt_arena,
2205 &key, table_list->belong_to_view))
2208 &prelocking_ctx->query_tables_last,
2209 table_list->belong_to_view);
2210 sp_update_stmt_used_routines(thd, prelocking_ctx,
2212 table_list->belong_to_view);
2213 trigger->propagate_attributes(prelocking_ctx);
2232 trg_event_type event_type,
2233 trg_action_time_type action_time)
2236 sp_head *sp= bodies[event_type][action_time];
2237 DBUG_ASSERT(used_fields->n_bits ==
trigger_table->s->fields);
2240 trg_field= trg_field->next_trg_field)
2243 if (trg_field->field_idx != (uint)-1)
2245 if (bitmap_is_set(used_fields, trg_field->field_idx) &&
2246 trg_field->get_settable_routine_parameter())
2271 for (action_time= 0; action_time < (int)TRG_ACTION_MAX; action_time++)
2273 sp_head *sp= bodies[event][action_time];
2279 trg_field= trg_field->next_trg_field)
2282 if (trg_field->field_idx != (uint)-1)
2284 bitmap_set_bit(
trigger_table->read_set, trg_field->field_idx);
2285 if (trg_field->get_settable_routine_parameter())
2286 bitmap_set_bit(
trigger_table->write_set, trg_field->field_idx);
2305 m_has_unparseable_trigger=
true;
2306 strcpy(m_parse_error_message, error_message);
2330 #define INVALID_SQL_MODES_LENGTH 13
2333 Handle_old_incorrect_sql_modes_hook::
2334 process_unknown_string(
const char *&unknown_key, uchar* base,
2335 MEM_ROOT *mem_root,
const char *end)
2337 DBUG_ENTER(
"Handle_old_incorrect_sql_modes_hook::process_unknown_string");
2338 DBUG_PRINT(
"info", (
"unknown key: %60s", unknown_key));
2340 if (unknown_key + INVALID_SQL_MODES_LENGTH + 1 < end &&
2341 unknown_key[INVALID_SQL_MODES_LENGTH] ==
'=' &&
2342 !memcmp(unknown_key, STRING_WITH_LEN(
"sql_modes")))
2344 const char *ptr= unknown_key + INVALID_SQL_MODES_LENGTH + 1;
2346 DBUG_PRINT(
"info", (
"sql_modes affected by BUG#14090 detected"));
2347 push_warning_printf(current_thd,
2348 Sql_condition::WARN_LEVEL_NOTE,
2350 ER(ER_OLD_FILE_FORMAT),
2351 (
char *)path,
"TRIGGER");
2353 &sql_modes_parameters, mem_root))
2367 #define INVALID_TRIGGER_TABLE_LENGTH 15
2376 MEM_ROOT *mem_root,
const char *end)
2378 DBUG_ENTER(
"Handle_old_incorrect_trigger_table_hook::process_unknown_string");
2379 DBUG_PRINT(
"info", (
"unknown key: %60s", unknown_key));
2381 if (unknown_key + INVALID_TRIGGER_TABLE_LENGTH + 1 < end &&
2382 unknown_key[INVALID_TRIGGER_TABLE_LENGTH] ==
'=' &&
2383 !memcmp(unknown_key, STRING_WITH_LEN(
"trigger_table")))
2385 const char *ptr= unknown_key + INVALID_TRIGGER_TABLE_LENGTH + 1;
2387 DBUG_PRINT(
"info", (
"trigger_table affected by BUG#15921 detected"));
2388 push_warning_printf(current_thd,
2389 Sql_condition::WARN_LEVEL_NOTE,
2391 ER(ER_OLD_FILE_FORMAT),
2392 (
char *)path,
"TRIGGER");
2396 my_error(ER_FPARSER_ERROR_IN_PARAMETER, MYF(0),
"trigger_table",
2420 trn_path->length= build_table_filename(trn_path->str,
2423 trg_name->m_name.str,
2437 bool check_trn_exists(
const LEX_STRING *trn_path)
2439 return access(trn_path->str, F_OK) != 0;
2456 bool load_table_name_for_trigger(THD *thd,
2466 &trn_data.trigger_table);
2468 DBUG_ENTER(
"load_table_name_for_trigger");
2475 if (!is_equal(&trigname_file_type, parser->type()))
2477 my_error(ER_WRONG_OBJECT, MYF(0),
2478 trg_name->m_name.str,
2485 if (parser->
parse((uchar*) &trn_data, thd->mem_root,
2486 trigname_file_parameters, 1,
2487 &trigger_table_hook))
2492 *tbl_name= trn_data.trigger_table;