MySQL 8.0.30
Source Code Documentation
table_trigger_dispatcher.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2013, 2022, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is also distributed with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have included with MySQL.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef TABLE_TRIGGER_DISPATCHER_H_INCLUDED
25#define TABLE_TRIGGER_DISPATCHER_H_INCLUDED
26
27///////////////////////////////////////////////////////////////////////////
28
29#include <assert.h>
30#include <string.h>
31
32#include "lex_string.h"
33#include "my_inttypes.h"
34#include "my_sys.h"
35#include "mysql_com.h" // MYSQL_ERRMSG_SIZE
36#include "mysqld_error.h" // ER_PARSE_ERROR
37#include "sql/table_trigger_field_support.h" // Table_trigger_field_support
38#include "sql/trigger_def.h" // enum_trigger_action_time_type
39
40class Field;
42class String;
43class THD;
44class Trigger;
45class Trigger_chain;
46struct MEM_ROOT;
47
48namespace dd {
49class Table;
50} // namespace dd
51struct TABLE;
52struct TABLE_LIST;
53template <class T>
54class List;
55
56///////////////////////////////////////////////////////////////////////////
57
58/**
59 This class holds all information about triggers of a table.
60*/
61
63 public:
64 static Table_trigger_dispatcher *create(TABLE *subject_table);
65
66 bool check_n_load(THD *thd, const dd::Table &table);
67
68 /*
69 During upgrade from 5.7, we need to use the trigger chains to fix
70 the trigger order.
71 */
73
74 private:
75 Table_trigger_dispatcher(TABLE *subject_table);
76
77 public:
79
80 /**
81 Checks if there is a broken trigger for this table.
82
83 @retval false if all triggers are Ok.
84 @retval true in case there is at least one broken trigger (a trigger which
85 SQL-definition can not be parsed) for this table.
86 */
89 my_message(ER_PARSE_ERROR, m_parse_error_message, MYF(0));
90 return true;
91 }
92 return false;
93 }
94
95 /**
96 Create trigger for table.
97
98 @param thd Thread context
99 @param[out] binlog_create_trigger_stmt
100 Well-formed CREATE TRIGGER statement for putting into
101 binlog (after successful execution)
102 @param if_not_exists
103 True if 'IF NOT EXISTS' clause was specified
104 @param[out] already_exists
105 Set to true if trigger already exists on the same table
106
107 @note
108 - Assumes that trigger name is fully qualified.
109 - NULL-string means the following LEX_STRING instance:
110 { str = 0; length = 0 }.
111 - In other words, definer_user and definer_host should contain
112 simultaneously NULL-strings (non-SUID/old trigger) or valid strings
113 (SUID/new trigger).
114
115 @return Operation status.
116 @retval false Success
117 @retval true Failure
118 */
119 bool create_trigger(THD *thd, String *binlog_create_trigger_stmt,
120 bool if_not_exists, bool &already_exists);
121
124 bool old_row_is_record1);
125
126 Trigger_chain *get_triggers(int event, int action_time) {
127 assert(0 <= event && event < TRG_EVENT_MAX);
128 assert(0 <= action_time && action_time < TRG_ACTION_MAX);
129 return m_trigger_map[event][action_time];
130 }
131
132 const Trigger_chain *get_triggers(int event, int action_time) const {
133 assert(0 <= event && event < TRG_EVENT_MAX);
134 assert(0 <= action_time && action_time < TRG_ACTION_MAX);
135 return m_trigger_map[event][action_time];
136 }
137
138 Trigger *find_trigger(const LEX_STRING &trigger_name);
139
141 enum_trigger_action_time_type action_time) const {
142 return get_triggers(event, action_time) != nullptr;
143 }
144
145 bool has_update_triggers() const {
148 }
149
150 bool has_delete_triggers() const {
153 }
154
156
158 Query_tables_list *prelocking_ctx,
159 TABLE_LIST *table_list);
160
163
164 void print_upgrade_warnings(THD *thd);
165
166 void parse_triggers(THD *thd, List<Trigger> *triggers, bool is_upgrade);
167
168 private:
172
174
175 /**
176 Remember a parse error that occurred while parsing trigger definitions
177 loaded from the Data Dictionary. This makes the Table_trigger_dispatcher
178 enter the error state flagged by m_has_unparseable_trigger == true. The
179 error message will be used whenever a statement invoking or manipulating
180 triggers is issued against the Table_trigger_dispatcher's table.
181
182 @param error_message The error message thrown by the parser.
183 */
184 void set_parse_error_message(const char *error_message) {
187 snprintf(m_parse_error_message, sizeof(m_parse_error_message), "%s",
188 error_message);
189 }
190 }
191
192 private:
193 /************************************************************************
194 * Table_trigger_field_support interface implementation.
195 ***********************************************************************/
196
198
200 int field_index) override {
201 return (v == TRG_OLD_ROW) ? m_old_field[field_index]
202 : m_new_field[field_index];
203 }
204
205 private:
206 /**
207 TABLE instance for which this triggers list object was created.
208 */
210
211 /// Triggers grouped by event, action_time.
213
214 /**
215 Copy of TABLE::Field array with field pointers set to TABLE::record[1]
216 buffer instead of TABLE::record[0] (used for OLD values in on UPDATE
217 trigger and DELETE trigger when it is called for REPLACE).
218 */
220
221 /**
222 During execution of trigger m_new_field and m_old_field should point to the
223 array of fields representing new or old version of row correspondingly
224 (so it can point to TABLE::field or to
225 Table_trigger_dispatcher::m_record1_field).
226 */
229
230 /**
231 This flag indicates that one of the triggers was not parsed successfully,
232 and as a precaution the object has entered the state where all trigger
233 operations result in errors until all the table triggers are dropped. It is
234 not safe to add triggers since it is unknown if the broken trigger has the
235 same name or event type. Nor is it safe to invoke any trigger. The only
236 safe operations are drop_trigger() and drop_all_triggers().
237
238 We can't use the value of m_parse_error_message as a flag to inform that
239 a trigger has a parse error since for multi-byte locale the first byte of
240 message can be 0 but the message still be meaningful. It means that just a
241 comparison against m_parse_error_message[0] can not be done safely.
242
243 @see Table_trigger_dispatcher::set_parse_error()
244 */
246
247 /**
248 This error will be displayed when the user tries to manipulate or invoke
249 triggers on a table that has broken triggers. It is set once per statement
250 and thus will contain the first parse error encountered in the trigger file.
251 */
253};
254
255///////////////////////////////////////////////////////////////////////////
256
257#endif // TABLE_TRIGGER_DISPATCHER_H_INCLUDED
Definition: field.h:573
Definition: sql_list.h:433
Definition: sql_lex.h:2465
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:166
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:922
This class holds all information about triggers of a table.
Definition: table_trigger_dispatcher.h:62
Table_trigger_dispatcher(TABLE *subject_table)
Private form of Table_trigger_dispatcher constructor.
Definition: table_trigger_dispatcher.cc:86
bool reorder_57_list(MEM_ROOT *mem_root, List< Trigger > *triggers)
Definition: table.cc:106
Trigger_chain * create_trigger_chain(MEM_ROOT *mem_root, enum_trigger_event_type event, enum_trigger_action_time_type action_time)
Make sure there is a chain for the specified event and action time.
Definition: table_trigger_dispatcher.cc:390
bool has_update_triggers() const
Definition: table_trigger_dispatcher.h:145
void disable_fields_temporary_nullability()
Reset "temporary nullable" flag from trigger fields.
Definition: table_trigger_dispatcher.cc:634
bool m_has_unparseable_trigger
This flag indicates that one of the triggers was not parsed successfully, and as a precaution the obj...
Definition: table_trigger_dispatcher.h:245
bool has_triggers(enum_trigger_event_type event, enum_trigger_action_time_type action_time) const
Definition: table_trigger_dispatcher.h:140
bool process_triggers(THD *thd, enum_trigger_event_type event, enum_trigger_action_time_type action_time, bool old_row_is_record1)
Execute trigger for given (event, time) pair.
Definition: table_trigger_dispatcher.cc:535
void print_upgrade_warnings(THD *thd)
Iterate along triggers and print necessary upgrade warnings.
Definition: table_trigger_dispatcher.cc:649
const Trigger_chain * get_triggers(int event, int action_time) const
Definition: table_trigger_dispatcher.h:132
Field ** m_new_field
During execution of trigger m_new_field and m_old_field should point to the array of fields represent...
Definition: table_trigger_dispatcher.h:227
void set_parse_error_message(const char *error_message)
Remember a parse error that occurred while parsing trigger definitions loaded from the Data Dictionar...
Definition: table_trigger_dispatcher.h:184
Field ** m_record1_field
Copy of TABLE::Field array with field pointers set to TABLE::record[1] buffer instead of TABLE::recor...
Definition: table_trigger_dispatcher.h:219
Trigger * find_trigger(const LEX_STRING &trigger_name)
Get trigger object by trigger name.
Definition: table_trigger_dispatcher.cc:415
bool check_for_broken_triggers()
Checks if there is a broken trigger for this table.
Definition: table_trigger_dispatcher.h:87
Field ** m_old_field
Definition: table_trigger_dispatcher.h:228
bool mark_fields(enum_trigger_event_type event)
Mark fields of subject table which we read/set in its triggers as such.
Definition: table_trigger_dispatcher.cc:680
void enable_fields_temporary_nullability(THD *thd)
Mark all trigger fields as "temporary nullable" and remember the current THD::check_for_truncated_fie...
Definition: table_trigger_dispatcher.cc:606
bool has_delete_triggers() const
Definition: table_trigger_dispatcher.h:150
bool create_trigger(THD *thd, String *binlog_create_trigger_stmt, bool if_not_exists, bool &already_exists)
Create trigger for table.
Definition: table_trigger_dispatcher.cc:109
Field * get_trigger_variable_field(enum_trigger_variable_type v, int field_index) override
Definition: table_trigger_dispatcher.h:199
Trigger_chain * get_triggers(int event, int action_time)
Definition: table_trigger_dispatcher.h:126
static Table_trigger_dispatcher * create(TABLE *subject_table)
Create an instance of Table_trigger_dispatcher for the given subject table.
Definition: table_trigger_dispatcher.cc:76
TABLE * get_subject_table() override
Definition: table_trigger_dispatcher.h:197
TABLE * m_subject_table
TABLE instance for which this triggers list object was created.
Definition: table_trigger_dispatcher.h:209
bool prepare_record1_accessors()
Prepare array of Field objects referencing to TABLE::record[1] instead of record<a href="they will re...
Definition: table_trigger_dispatcher.cc:293
char m_parse_error_message[MYSQL_ERRMSG_SIZE]
This error will be displayed when the user tries to manipulate or invoke triggers on a table that has...
Definition: table_trigger_dispatcher.h:252
~Table_trigger_dispatcher() override
Definition: table_trigger_dispatcher.cc:96
bool add_tables_and_routines_for_triggers(THD *thd, Query_tables_list *prelocking_ctx, TABLE_LIST *table_list)
Add triggers for table to the set of routines used by statement.
Definition: table_trigger_dispatcher.cc:582
Trigger_chain * m_trigger_map[TRG_EVENT_MAX][TRG_ACTION_MAX]
Triggers grouped by event, action_time.
Definition: table_trigger_dispatcher.h:212
bool check_n_load(THD *thd, const dd::Table &table)
Load triggers for the table.
Definition: table_trigger_dispatcher.cc:329
void parse_triggers(THD *thd, List< Trigger > *triggers, bool is_upgrade)
Parse trigger definition statements (CREATE TRIGGER).
Definition: table_trigger_dispatcher.cc:450
This is an interface to be used from Item_trigger_field to access information about table trigger fie...
Definition: table_trigger_field_support.h:43
Definition: trigger_chain.h:40
This class represents a trigger object.
Definition: trigger.h:73
Definition: table.h:46
static MEM_ROOT mem_root
Definition: client_plugin.cc:109
void my_message(uint my_err, const char *str, myf MyFlags)
Print an error message.
Definition: my_error.cc:310
Some integer typedefs for easier portability.
#define MYF(v)
Definition: my_inttypes.h:96
Common header for many mysys elements.
Common definition between mysql server & client.
#define MYSQL_ERRMSG_SIZE
Max length of a error message.
Definition: mysql_com.h:877
The version of the current data dictionary table definitions.
Definition: dictionary_client.h:42
required string event
Definition: replication_group_member_actions.proto:31
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:82
Definition: mysql_lex_string.h:34
Definition: table.h:2684
Definition: table.h:1394
This file defines all base public constants related to triggers in MySQL.
enum_trigger_variable_type
Enum constants to designate NEW and OLD trigger pseudo-variables.
Definition: trigger_def.h:72
@ TRG_OLD_ROW
Definition: trigger_def.h:72
enum_trigger_event_type
Constants to enumerate possible event types on which triggers can be fired.
Definition: trigger_def.h:41
@ TRG_EVENT_UPDATE
Definition: trigger_def.h:43
@ TRG_EVENT_MAX
Definition: trigger_def.h:45
@ TRG_EVENT_DELETE
Definition: trigger_def.h:44
enum_trigger_action_time_type
Constants to enumerate possible timings when triggers can be fired.
Definition: trigger_def.h:51
@ TRG_ACTION_BEFORE
Definition: trigger_def.h:52
@ TRG_ACTION_MAX
Definition: trigger_def.h:54
@ TRG_ACTION_AFTER
Definition: trigger_def.h:53