MySQL 9.1.0
Source Code Documentation
trigger.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2013, 2024, 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 TRIGGER_H_INCLUDED
26#define TRIGGER_H_INCLUDED
27
28#include <stdint.h>
29#include <string.h>
30
31#include "my_config.h"
32#ifdef HAVE_SYS_TIME_H
33#include <sys/time.h>
34#endif
35#include <sys/types.h>
36
37#include "lex_string.h"
38#include "my_inttypes.h"
39#include "mysql_com.h"
40#include "sql/table.h" // GRANT_INFO
41#include "sql/trigger_def.h" // enum_trigger_event_type
42
44class String;
45class THD;
46class sp_head;
47struct MEM_ROOT;
48
49using sql_mode_t = uint64_t;
50
51/**
52 This class represents a trigger object.
53 Trigger can be created, initialized, parsed, and executed.
54
55 Trigger attributes are usually stored on the memory root
56 of the subject table TABLE object or its TABLE_SHARE
57 (depending on whether it is something specific to the TABLE
58 instance (e.g. sp_head), or static metadata that can be shared by
59 all TABLE instances (e.g. subject table name).
60
61 A Trigger object however can exist when the subject table does not.
62 In this case, trigger attributes are stored on a separate memory root.
63
64 @note We create separate sets of Trigger objects for TABLE_SHARE and
65 TABLE instances. The set for the former is used to store static
66 information about table's triggers and is directly associated
67 with TABLE_SHARE object. The set for the latter is used primarily
68 for trigger execution, and is asssociated with TABLE object with
69 the help of Table_triggers_dispatcher class. Attributes representing
70 static properties in Trigger instances of the latter type reference
71 attributes/memory belonging to attributes of Trigger objects
72 associated with the TABLE_SHARE.
73
74 Trigger objects are created in two ways:
75
76 1. loading from Data Dictionary (by Trigger_loader)
77
78 In this case Trigger object is initialized from the data
79 which is directly available in data dictionary;
80
81 @see Trigger::create_from_dd().
82
83 2. creating a new Trigger object that represents the trigger object being
84 created by CREATE TRIGGER statement (by Table_trigger_dispatcher).
85
86 In this case Trigger object is created temporarily.
87
88 @see Trigger::create_from_parser().
89*/
90class Trigger {
91 public:
92 static Trigger *create_from_parser(THD *thd, TABLE *subject_table,
93 String *binlog_create_trigger_stmt);
94
95 static Trigger *create_from_dd(
96 MEM_ROOT *mem_root, const LEX_CSTRING &trigger_name,
97 const LEX_CSTRING &db_name, const LEX_CSTRING &subject_table_name,
98 const LEX_CSTRING &definition, const LEX_CSTRING &definition_utf8,
99 sql_mode_t sql_mode, const LEX_CSTRING &definer_user,
100 const LEX_CSTRING &definer_host, const LEX_CSTRING &client_cs_name,
101 const LEX_CSTRING &connection_cl_name, const LEX_CSTRING &db_cl_name,
102 enum_trigger_event_type trg_event_type,
103 enum_trigger_action_time_type trg_time_type, uint action_order,
104 my_timeval created_timestamp);
105
107
108 /**
109 Constructs CREATE TRIGGER statement taking into account a value of
110 the DEFINER clause.
111
112 The point of this method is to create canonical forms of CREATE TRIGGER
113 statement for writing into the binlog.
114
115 @note
116 A statement for the binlog form must preserve FOLLOWS/PRECEDES clause
117 if it was in the original statement. The reason for that difference is this:
118
119 - the Data Dictionary preserves the trigger execution order
120 (action_order), thus FOLLOWS/PRECEDES clause is not needed.
121
122 - moreover, FOLLOWS/PRECEDES clause usually makes problem in mysqldump,
123 because CREATE TRIGGER statement will have a reference to
124 not-yet-existing trigger (which is about to be created right after this
125 one).
126
127 - thus, FOLLOWS/PRECEDES must not be stored in the Data Dictionary.
128
129 - on the other hand, the binlog contains statements in the user order (as
130 the user executes them). Thus, it is important to preserve
131 FOLLOWS/PRECEDES clause if the user has specified it so that the trigger
132 execution order on master and slave will be the same.
133
134 @param thd thread context
135 @param[out] binlog_query well-formed CREATE TRIGGER statement for putting
136 into binlog (after successful execution)
137 @param def_user user part of a definer value
138 @param def_host host part of a definer value
139
140 @return Operation status.
141 @retval false Success
142 @retval true Failure
143 */
145 THD *thd, String *binlog_query, const LEX_CSTRING &def_user,
146 const LEX_CSTRING &def_host);
147
148 public:
149 bool execute(THD *thd);
150
151 bool parse(THD *thd, bool is_upgrade);
152
153 void add_tables_and_routines(THD *thd, Query_tables_list *prelocking_ctx,
154 Table_ref *table_list);
155
156 void print_upgrade_warning(THD *thd);
157
158 public:
159 /************************************************************************
160 * Attribute accessors.
161 ***********************************************************************/
162
163 const LEX_CSTRING &get_db_name() const { return m_db_name; }
164
167 }
168
169 const LEX_CSTRING &get_trigger_name() const { return m_trigger_name; }
170
171 const LEX_CSTRING &get_definition() const { return m_definition; }
172
174
176
177 const LEX_CSTRING &get_definer() const { return m_definer; }
178
179 const LEX_CSTRING &get_definer_user() const { return m_definer_user; }
180
181 const LEX_CSTRING &get_definer_host() const { return m_definer_host; }
182
184
187 }
188
189 const LEX_CSTRING &get_db_cl_name() const { return m_db_cl_name; }
190
192
193 const LEX_CSTRING &get_event_as_string() const;
194
196 return m_action_time;
197 }
198
200
202 return m_created_timestamp.m_tv_sec == 0 &&
204 }
205
207
209
210 void set_action_order(ulonglong action_order) {
211 m_action_order = action_order;
212 }
213
214 sp_head *get_sp() { return m_sp; }
215
217
218 bool has_parse_error() const { return m_parse_error_message; }
219
220 const char *get_parse_error_message() const { return m_parse_error_message; }
221
222 /**
223 Construct a full CREATE TRIGGER statement from Trigger's data members.
224
225 @param [in] thd Thread context
226 @param [out] full_trigger_definition Place where a CREATE TRIGGER
227 statement be stored.
228
229 @return Operation status
230 @retval true Failure
231 @retval false Success
232 */
233
234 bool create_full_trigger_definition(const THD *thd,
235 String *full_trigger_definition) const;
236
237 private:
238 Trigger(const LEX_CSTRING &trigger_name, MEM_ROOT *mem_root,
240 const LEX_CSTRING &definition, const LEX_CSTRING &definition_utf8,
241 sql_mode_t sql_mode, const LEX_CSTRING &definer_user,
242 const LEX_CSTRING &definer_host, const LEX_CSTRING &client_cs_name,
243 const LEX_CSTRING &connection_cl_name, const LEX_CSTRING &db_cl_name,
244 enum_trigger_event_type event_type,
245 enum_trigger_action_time_type action_time, uint action_order,
246 my_timeval created_timestamp);
247
248 public:
249 ~Trigger();
250
251 private:
252 void set_trigger_name(const LEX_CSTRING &trigger_name) {
253 m_trigger_name = trigger_name;
254 }
255
256 void set_trigger_def(const LEX_CSTRING &trigger_def) {
257 m_definition = trigger_def;
258 }
259
260 void set_trigger_def_utf8(const LEX_CSTRING &trigger_def_utf8) {
261 m_definition_utf8 = trigger_def_utf8;
262 }
263
264 void set_parse_error_message(const char *error_message);
265
266 /**
267 Memory root to store all data of this Trigger object.
268
269 This can be a pointer to the subject table share memory root (if this
270 Trigger object is associated with TABLE_SHARE), table memory root
271 (if this Trigger object is associated with TABLE object), or it can be a
272 pointer to a dedicated memory root if subject table does not exist.
273 */
275
276 /**
277 Full trigger definition reconstructed from a data loaded from the table
278 mysql.trigger.
279 */
281
282 private:
283 /************************************************************************
284 * Mandatory trigger attributes loaded from data dictionary.
285 * All these strings are allocated on TABLE_SHARE's memory root
286 * (for both cases when Trigger object is bound to TABLE_SHARE object
287 * and to TABLE object) or dedicated memory root pointed by m_mem_root.
288 ***********************************************************************/
289
290 /// Database name.
292
293 /// Table name.
295
296 /// Trigger definition to save in DD.
298
299 /// Trigger definition in UTF8 to save in DD.
301
302 /// Trigger sql-mode.
304
305 /// Trigger definer.
307
308 /// Trigger definer (user part).
310
311 /// Trigger definer (host part).
313
314 /// Character set context, used for parsing and executing trigger.
316
317 /// Collation name of the connection within one a trigger are created.
319
320 /// Default database collation.
322
323 /// Trigger event.
325
326 /// Trigger action time.
328
329 /**
330 Current time when the trigger was created (measured in milliseconds since
331 since 0 hours, 0 minutes, 0 seconds, January 1, 1970, UTC). This is the
332 value of CREATED attribute.
333
334 There is special value -- zero means CREATED is not set (NULL).
335 */
337
338 /**
339 Action_order value for the trigger. Action_order is the ordinal position
340 of the trigger in the list of triggers with the same EVENT_MANIPULATION,
341 CONDITION_TIMING, and ACTION_ORIENTATION.
342 */
344
345 private:
346 /************************************************************************
347 * All these strings are allocated on the TABLE_SHARE's mem-root.
348 ***********************************************************************/
349
350 /// Trigger name.
352
353 private:
354 /************************************************************************
355 * Other attributes. Allocated on m_mem_root if necessary.
356 ***********************************************************************/
357
358 /// Grant information for the trigger.
360
361 /// Pointer to the sp_head corresponding to the trigger.
363
364 /**
365 Parse error message for trigger if it has one, nullptr otherwise.
366
367 This error will be displayed when the user tries to manipulate or invoke
368 triggers on a table that has broken triggers. It will get set only once
369 per statement and thus will contain the first parse error encountered in
370 the trigger file.
371 */
373};
374
375///////////////////////////////////////////////////////////////////////////
376
377#endif // TRIGGER_H_INCLUDED
Definition: sql_lex.h:2618
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:2900
This class represents a trigger object.
Definition: trigger.h:90
sp_head * m_sp
Pointer to the sp_head corresponding to the trigger.
Definition: trigger.h:362
GRANT_INFO * get_subject_table_grant()
Definition: trigger.h:216
bool has_parse_error() const
Definition: trigger.h:218
enum_trigger_event_type m_event
Trigger event.
Definition: trigger.h:324
LEX_CSTRING m_client_cs_name
Character set context, used for parsing and executing trigger.
Definition: trigger.h:315
ulonglong get_action_order() const
Definition: trigger.h:208
LEX_CSTRING m_db_name
Database name.
Definition: trigger.h:291
LEX_CSTRING m_connection_cl_name
Collation name of the connection within one a trigger are created.
Definition: trigger.h:318
enum_trigger_action_time_type get_action_time() const
Definition: trigger.h:195
const LEX_CSTRING & get_definer_user() const
Definition: trigger.h:179
~Trigger()
Destroy associated SP (if any).
Definition: trigger.cc:376
LEX_CSTRING m_definition_utf8
Trigger definition in UTF8 to save in DD.
Definition: trigger.h:300
my_timeval m_created_timestamp
Current time when the trigger was created (measured in milliseconds since since 0 hours,...
Definition: trigger.h:336
static Trigger * create_from_dd(MEM_ROOT *mem_root, const LEX_CSTRING &trigger_name, const LEX_CSTRING &db_name, const LEX_CSTRING &subject_table_name, const LEX_CSTRING &definition, const LEX_CSTRING &definition_utf8, sql_mode_t sql_mode, const LEX_CSTRING &definer_user, const LEX_CSTRING &definer_host, const LEX_CSTRING &client_cs_name, const LEX_CSTRING &connection_cl_name, const LEX_CSTRING &db_cl_name, enum_trigger_event_type trg_event_type, enum_trigger_action_time_type trg_time_type, uint action_order, my_timeval created_timestamp)
Creates a new Trigger-instance with the state loaded from the Data Dictionary.
Definition: trigger.cc:306
const LEX_CSTRING & get_action_time_as_string() const
Definition: trigger.cc:154
void set_action_order(ulonglong action_order)
Definition: trigger.h:210
bool parse(THD *thd, bool is_upgrade)
Parse CREATE TRIGGER statement.
Definition: trigger.cc:448
bool create_full_trigger_definition(const THD *thd, String *full_trigger_definition) const
Construct a full CREATE TRIGGER statement from Trigger's data members.
Definition: trigger.cc:413
ulonglong m_action_order
Action_order value for the trigger.
Definition: trigger.h:343
my_timeval get_created_timestamp() const
Definition: trigger.h:206
const char * m_parse_error_message
Parse error message for trigger if it has one, nullptr otherwise.
Definition: trigger.h:372
const LEX_CSTRING & get_trigger_name() const
Definition: trigger.h:169
const char * get_parse_error_message() const
Definition: trigger.h:220
const LEX_CSTRING & get_definition_utf8() const
Definition: trigger.h:173
LEX_CSTRING m_trigger_name
Trigger name.
Definition: trigger.h:351
LEX_CSTRING m_db_cl_name
Default database collation.
Definition: trigger.h:321
const LEX_CSTRING & get_definer() const
Definition: trigger.h:177
sql_mode_t m_sql_mode
Trigger sql-mode.
Definition: trigger.h:303
LEX_CSTRING m_definition
Trigger definition to save in DD.
Definition: trigger.h:297
bool execute(THD *thd)
Execute trigger's body.
Definition: trigger.cc:388
MEM_ROOT * m_mem_root
Memory root to store all data of this Trigger object.
Definition: trigger.h:274
const LEX_CSTRING & get_db_cl_name() const
Definition: trigger.h:189
enum_trigger_event_type get_event() const
Definition: trigger.h:191
void set_trigger_def_utf8(const LEX_CSTRING &trigger_def_utf8)
Definition: trigger.h:260
void add_tables_and_routines(THD *thd, Query_tables_list *prelocking_ctx, Table_ref *table_list)
Add tables and routines used by trigger to the set of elements used by statement.
Definition: trigger.cc:639
LEX_CSTRING m_definer
Trigger definer.
Definition: trigger.h:306
const LEX_CSTRING & get_definition() const
Definition: trigger.h:171
bool is_created_timestamp_null() const
Definition: trigger.h:201
LEX_CSTRING m_subject_table_name
Table name.
Definition: trigger.h:294
void set_parse_error_message(const char *error_message)
Mark trigger as having a parse error and remember the message for future use.
Definition: trigger.cc:695
sql_mode_t get_sql_mode() const
Definition: trigger.h:175
LEX_CSTRING m_definer_user
Trigger definer (user part).
Definition: trigger.h:309
const LEX_CSTRING & get_connection_cl_name() const
Definition: trigger.h:185
Trigger(const LEX_CSTRING &trigger_name, MEM_ROOT *mem_root, const LEX_CSTRING &db_name, const LEX_CSTRING &table_name, const LEX_CSTRING &definition, const LEX_CSTRING &definition_utf8, sql_mode_t sql_mode, const LEX_CSTRING &definer_user, const LEX_CSTRING &definer_host, const LEX_CSTRING &client_cs_name, const LEX_CSTRING &connection_cl_name, const LEX_CSTRING &db_cl_name, enum_trigger_event_type event_type, enum_trigger_action_time_type action_time, uint action_order, my_timeval created_timestamp)
Trigger constructor.
Definition: trigger.cc:342
Trigger * clone_shallow(MEM_ROOT *mem_root) const
Create a new Trigger object as a shallow clone of existing Trigger object.
Definition: trigger.cc:329
LEX_CSTRING m_definer_host
Trigger definer (host part).
Definition: trigger.h:312
void set_trigger_def(const LEX_CSTRING &trigger_def)
Definition: trigger.h:256
GRANT_INFO m_subject_table_grant
Grant information for the trigger.
Definition: trigger.h:359
const LEX_CSTRING & get_subject_table_name() const
Definition: trigger.h:165
const LEX_CSTRING & get_event_as_string() const
Definition: trigger.cc:158
const LEX_CSTRING & get_db_name() const
Definition: trigger.h:163
static bool construct_create_trigger_stmt_with_definer(THD *thd, String *binlog_query, const LEX_CSTRING &def_user, const LEX_CSTRING &def_host)
Constructs CREATE TRIGGER statement taking into account a value of the DEFINER clause.
Definition: trigger.cc:128
const LEX_CSTRING & get_definer_host() const
Definition: trigger.h:181
const LEX_CSTRING & get_client_cs_name() const
Definition: trigger.h:183
void print_upgrade_warning(THD *thd)
Print upgrade warnings (if any).
Definition: trigger.cc:682
LEX_CSTRING m_full_trigger_definition
Full trigger definition reconstructed from a data loaded from the table mysql.trigger.
Definition: trigger.h:280
enum_trigger_action_time_type m_action_time
Trigger action time.
Definition: trigger.h:327
sp_head * get_sp()
Definition: trigger.h:214
void set_trigger_name(const LEX_CSTRING &trigger_name)
Definition: trigger.h:252
static Trigger * create_from_parser(THD *thd, TABLE *subject_table, String *binlog_create_trigger_stmt)
Creates a new Trigger-instance with the state from the parser.
Definition: trigger.cc:182
sp_head represents one instance of a stored program.
Definition: sp_head.h:383
static MEM_ROOT mem_root
Definition: client_plugin.cc:114
uint64_t sql_mode_t
Definition: dd_event.h:39
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:56
Common definition between mysql server & client.
static const char * sql_mode
Definition: mysqlslap.cc:199
const char * table_name
Definition: rules_table_service.cc:56
const char * db_name
Definition: rules_table_service.cc:55
The current state of the privilege checking process for the current user, SQL statement and SQL objec...
Definition: table.h:372
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:40
Definition: table.h:1421
Replacement of system's struct timeval to ensure we can carry 64 bit values even on a platform which ...
Definition: my_time_t.h:45
int64_t m_tv_sec
Definition: my_time_t.h:46
int64_t m_tv_usec
Definition: my_time_t.h:47
Include file for Sun RPC to compile out of the box.
This file defines all base public constants related to triggers in MySQL.
enum_trigger_event_type
Constants to enumerate possible event types on which triggers can be fired.
Definition: trigger_def.h:42
enum_trigger_action_time_type
Constants to enumerate possible timings when triggers can be fired.
Definition: trigger_def.h:52