MySQL 9.3.0
Source Code Documentation
table_trigger_dispatcher.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2013, 2025, 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 designed to work 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 either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License, version 2.0, for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
24
25#ifndef TABLE_TRIGGER_DISPATCHER_H_INCLUDED
26#define TABLE_TRIGGER_DISPATCHER_H_INCLUDED
27
28///////////////////////////////////////////////////////////////////////////
29
30#include <assert.h>
31#include <string.h>
32
33#include "lex_string.h"
34#include "my_inttypes.h"
35#include "my_sys.h"
36#include "mysql_com.h" // MYSQL_ERRMSG_SIZE
37#include "mysqld_error.h" // ER_PARSE_ERROR
38#include "sql/sql_list.h" // List
39#include "sql/table_trigger_field_support.h" // Table_trigger_field_support
40#include "sql/trigger_def.h" // enum_trigger_action_time_type
41
42class Field;
44class String;
45class THD;
46class Trigger;
47class Trigger_chain;
48struct MEM_ROOT;
49
50namespace dd {
51class Table;
52} // namespace dd
53struct TABLE;
54class Table_ref;
55template <class T>
56class List;
57
59class Mock_share;
60}
61
62///////////////////////////////////////////////////////////////////////////
63
64/**
65 This class holds all information about triggers of a table.
66*/
67
69 public:
70 static Table_trigger_dispatcher *create(TABLE *subject_table);
71
72 bool finalize_load(THD *thd);
73
74 private:
75 Table_trigger_dispatcher(TABLE *subject_table);
76
78
79 public:
81
82 /**
83 Checks if there is a broken trigger for this table.
84
85 @retval false if all triggers are Ok.
86 @retval true in case there is at least one broken trigger (a trigger which
87 SQL-definition can not be parsed) for this table.
88 */
91 my_message(ER_PARSE_ERROR, m_parse_error_message, MYF(0));
92 return true;
93 }
94 return false;
95 }
96
97 /**
98 Create trigger for table.
99
100 @param thd Thread context
101 @param[out] binlog_create_trigger_stmt
102 Well-formed CREATE TRIGGER statement for putting into
103 binlog (after successful execution)
104 @param if_not_exists
105 True if 'IF NOT EXISTS' clause was specified
106 @param[out] already_exists
107 Set to true if trigger already exists on the same table
108
109 @note
110 - Assumes that trigger name is fully qualified.
111 - NULL-string means the following LEX_STRING instance:
112 { str = 0; length = 0 }.
113 - In other words, definer_user and definer_host should contain
114 simultaneously NULL-strings (non-SUID/old trigger) or valid strings
115 (SUID/new trigger).
116
117 @return Operation status.
118 @retval false Success
119 @retval true Failure
120 */
121 bool create_trigger(THD *thd, String *binlog_create_trigger_stmt,
122 bool if_not_exists, bool &already_exists);
123
126 bool old_row_is_record1);
127
128 Trigger_chain *get_triggers(int event, int action_time) {
129 assert(0 <= event && event < TRG_EVENT_MAX);
130 assert(0 <= action_time && action_time < TRG_ACTION_MAX);
131 return m_trigger_map[event][action_time];
132 }
133
134 const Trigger_chain *get_triggers(int event, int action_time) const {
135 assert(0 <= event && event < TRG_EVENT_MAX);
136 assert(0 <= action_time && action_time < TRG_ACTION_MAX);
137 return m_trigger_map[event][action_time];
138 }
139
140 Trigger *find_trigger(const LEX_STRING &trigger_name);
141
143 enum_trigger_action_time_type action_time) const {
144 return get_triggers(event, action_time) != nullptr;
145 }
146
147 bool has_update_triggers() const {
150 }
151
152 bool has_delete_triggers() const {
155 }
156
158
160 Query_tables_list *prelocking_ctx,
161 Table_ref *table_list);
162
165 void reset_field_nulls();
166
167 void print_upgrade_warnings(THD *thd);
168
169 void parse_triggers(THD *thd, List<Trigger> *triggers, bool is_upgrade);
170
171 /**
172 Check whether we have finalized loading of triggers for the table
173 by parsing their bodies, creating sp_head objects and preparing
174 row-accessors.
175 */
177
178 private:
182
184
185 /**
186 Remember a parse error that occurred while parsing trigger definitions
187 loaded from the Data Dictionary. This makes the Table_trigger_dispatcher
188 enter the error state flagged by m_parse_error_message != nullptr . The
189 error message will be used whenever a statement invoking or manipulating
190 triggers is issued against the Table_trigger_dispatcher's table.
191
192 @param error_message The error message thrown by the parser.
193 */
194 void set_parse_error_message(const char *error_message);
195
196 private:
197 /************************************************************************
198 * Table_trigger_field_support interface implementation.
199 ***********************************************************************/
200
202
204 int field_index) override {
205 return (v == TRG_OLD_ROW) ? m_old_field[field_index]
206 : m_new_field[field_index];
207 }
208
209 private:
210 /**
211 TABLE instance for which this triggers list object was created.
212 */
214
215 /// Triggers grouped by event, action_time.
217
218 /**
219 Copy of TABLE::Field array with field pointers set to TABLE::record[1]
220 buffer instead of TABLE::record[0] (used for OLD values in on UPDATE
221 trigger and DELETE trigger when it is called for REPLACE).
222 */
224
225 /**
226 During execution of trigger m_new_field and m_old_field should point to the
227 array of fields representing new or old version of row correspondingly
228 (so it can point to TABLE::field or to
229 Table_trigger_dispatcher::m_record1_field).
230 */
233
234 /**
235 Error which occurred while parsing one of the triggers for the table;
236 nullptr if there was no error for any of its triggers.
237
238 Non-nullptr value indicates that as a precaution the object has entered
239 the state where all trigger operations result in errors (referencing
240 this error message saved) until all the table triggers are dropped. It is
241 not safe to add triggers since it is unknown if the broken trigger has the
242 same name or event type. Nor is it safe to invoke any trigger. The only
243 safe operations are drop_trigger() and drop_all_triggers().
244
245 @see Table_trigger_dispatcher::set_parse_error()
246 */
248
249 /** Indicates whether we have finalized loading of triggers for the table. */
251};
252
253///////////////////////////////////////////////////////////////////////////
254
255#endif // TABLE_TRIGGER_DISPATCHER_H_INCLUDED
Definition: field.h:573
Definition: sql_list.h:494
Definition: sql_lex.h:2753
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:167
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
Definition: table.h:2904
This class holds all information about triggers of a table.
Definition: table_trigger_dispatcher.h:68
Table_trigger_dispatcher(TABLE *subject_table)
Private form of Table_trigger_dispatcher constructor.
Definition: table_trigger_dispatcher.cc:90
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:384
bool has_update_triggers() const
Definition: table_trigger_dispatcher.h:147
void disable_fields_temporary_nullability()
Reset "temporary nullable" flag from trigger fields.
Definition: table_trigger_dispatcher.cc:628
bool has_triggers(enum_trigger_event_type event, enum_trigger_action_time_type action_time) const
Definition: table_trigger_dispatcher.h:142
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:529
void print_upgrade_warnings(THD *thd)
Iterate along triggers and print necessary upgrade warnings.
Definition: table_trigger_dispatcher.cc:653
const char * m_parse_error_message
Error which occurred while parsing one of the triggers for the table; nullptr if there was no error f...
Definition: table_trigger_dispatcher.h:247
const Trigger_chain * get_triggers(int event, int action_time) const
Definition: table_trigger_dispatcher.h:134
void reset_field_nulls()
Reset the temporary null values set to the field for triggers.
Definition: table_trigger_dispatcher.cc:638
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:231
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.cc:701
bool m_load_finalized
Indicates whether we have finalized loading of triggers for the table.
Definition: table_trigger_dispatcher.h:250
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:223
Trigger * find_trigger(const LEX_STRING &trigger_name)
Get trigger object by trigger name.
Definition: table_trigger_dispatcher.cc:409
friend class table_cache_unittest::Mock_share
Definition: table_trigger_dispatcher.h:77
bool check_for_broken_triggers()
Checks if there is a broken trigger for this table.
Definition: table_trigger_dispatcher.h:89
Field ** m_old_field
Definition: table_trigger_dispatcher.h:232
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:684
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:600
bool finalize_load(THD *thd)
Finalize load of triggers for the table by creating Trigger objects to be associated with TABLE/Table...
Definition: table_trigger_dispatcher.cc:314
bool has_delete_triggers() const
Definition: table_trigger_dispatcher.h:152
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:116
Field * get_trigger_variable_field(enum_trigger_variable_type v, int field_index) override
Definition: table_trigger_dispatcher.h:203
Trigger_chain * get_triggers(int event, int action_time)
Definition: table_trigger_dispatcher.h:128
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:80
bool add_tables_and_routines_for_triggers(THD *thd, Query_tables_list *prelocking_ctx, Table_ref *table_list)
Add triggers for table to the set of routines used by statement.
Definition: table_trigger_dispatcher.cc:576
bool has_load_been_finalized()
Check whether we have finalized loading of triggers for the table by parsing their bodies,...
Definition: table_trigger_dispatcher.h:176
TABLE * get_subject_table() override
Definition: table_trigger_dispatcher.h:201
TABLE * m_subject_table
TABLE instance for which this triggers list object was created.
Definition: table_trigger_dispatcher.h:213
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:276
~Table_trigger_dispatcher() override
Definition: table_trigger_dispatcher.cc:100
Trigger_chain * m_trigger_map[TRG_EVENT_MAX][TRG_ACTION_MAX]
Triggers grouped by event, action_time.
Definition: table_trigger_dispatcher.h:216
void parse_triggers(THD *thd, List< Trigger > *triggers, bool is_upgrade)
Parse trigger definition statements (CREATE TRIGGER).
Definition: table_trigger_dispatcher.cc:444
This is an interface to be used from Item_trigger_field to access information about table trigger fie...
Definition: table_trigger_field_support.h:44
Definition: trigger_chain.h:41
This class represents a trigger object.
Definition: trigger.h:90
static MEM_ROOT mem_root
Definition: client_plugin.cc:114
void my_message(uint my_err, const char *str, myf MyFlags)
Print an error message.
Definition: my_error.cc:311
Some integer typedefs for easier portability.
#define MYF(v)
Definition: my_inttypes.h:97
Common header for many mysys elements.
Common definition between mysql server & client.
The version of the current data dictionary table definitions.
Definition: dictionary_client.h:43
entry::Table Table
Definition: select.h:51
Definition: table_trigger_dispatcher.h:58
required string event
Definition: replication_group_member_actions.proto:32
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:83
Definition: mysql_lex_string.h:35
Definition: table.h:1425
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:73
@ TRG_OLD_ROW
Definition: trigger_def.h:73
enum_trigger_event_type
Constants to enumerate possible event types on which triggers can be fired.
Definition: trigger_def.h:42
@ TRG_EVENT_UPDATE
Definition: trigger_def.h:44
@ TRG_EVENT_MAX
Definition: trigger_def.h:46
@ TRG_EVENT_DELETE
Definition: trigger_def.h:45
enum_trigger_action_time_type
Constants to enumerate possible timings when triggers can be fired.
Definition: trigger_def.h:52
@ TRG_ACTION_BEFORE
Definition: trigger_def.h:53
@ TRG_ACTION_MAX
Definition: trigger_def.h:55
@ TRG_ACTION_AFTER
Definition: trigger_def.h:54