MySQL  8.0.18
Source Code Documentation
trigger.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
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 TRIGGER_H_INCLUDED
25 #define TRIGGER_H_INCLUDED
26 
27 #include "my_config.h"
28 
29 #include <string.h>
30 #ifdef HAVE_SYS_TIME_H
31 #include <sys/time.h>
32 #endif
33 #include <sys/types.h>
34 
35 #include "lex_string.h"
36 #include "my_inttypes.h"
37 #include "mysql_com.h"
38 #include "sql/table.h" // GRANT_INFO
39 #include "sql/trigger_def.h" // enum_trigger_event_type
40 
41 class Query_tables_list;
42 class String;
43 class THD;
44 class sp_head;
45 struct MEM_ROOT;
46 
47 typedef ulonglong sql_mode_t;
48 
49 /**
50  This class represents a trigger object.
51  Trigger can be created, initialized, parsed and executed.
52 
53  Trigger attributes are usually stored on the memory root of the subject table.
54  Trigger object however can exist when the subject table does not. In this
55  case, trigger attributes are stored on a separate memory root.
56 
57  Trigger objects are created in two ways:
58 
59  1. loading from Data Dictionary (by Trigger_loader)
60 
61  In this case Trigger object is initialized from the data
62  which is directly available in data dictionary;
63 
64  @see Trigger::create_from_dd().
65 
66  2. creating a new Trigger object that represents the trigger object being
67  created by CREATE TRIGGER statement (by Table_trigger_dispatcher).
68 
69  In this case Trigger object is created temporarily.
70 
71  @see Trigger::create_from_parser().
72 */
73 class Trigger {
74  public:
75  static Trigger *create_from_parser(THD *thd, TABLE *subject_table,
76  String *binlog_create_trigger_stmt);
77 
78  static Trigger *create_from_dd(
79  MEM_ROOT *mem_root, const LEX_CSTRING &trigger_name,
80  const LEX_CSTRING &db_name, const LEX_CSTRING &subject_table_name,
81  const LEX_CSTRING &definition, const LEX_CSTRING &definition_utf8,
82  sql_mode_t sql_mode, const LEX_CSTRING &definer_user,
83  const LEX_CSTRING &definer_host, const LEX_CSTRING &client_cs_name,
84  const LEX_CSTRING &connection_cl_name, const LEX_CSTRING &db_cl_name,
85  enum_trigger_event_type trg_event_type,
86  enum_trigger_action_time_type trg_time_type, uint action_order,
87  timeval created_timestamp);
88 
89  public:
90  bool execute(THD *thd);
91 
92  bool parse(THD *thd, bool is_upgrade);
93 
94  void add_tables_and_routines(THD *thd, Query_tables_list *prelocking_ctx,
95  TABLE_LIST *table_list);
96 
97  void print_upgrade_warning(THD *thd);
98 
99  public:
100  /************************************************************************
101  * Attribute accessors.
102  ***********************************************************************/
103 
104  const LEX_CSTRING &get_db_name() const { return m_db_name; }
105 
107  return m_subject_table_name;
108  }
109 
110  const LEX_CSTRING &get_trigger_name() const { return m_trigger_name; }
111 
112  const LEX_CSTRING &get_definition() const { return m_definition; }
113 
115 
116  sql_mode_t get_sql_mode() const { return m_sql_mode; }
117 
118  const LEX_CSTRING &get_definer() const { return m_definer; }
119 
120  const LEX_CSTRING &get_definer_user() const { return m_definer_user; }
121 
122  const LEX_CSTRING &get_definer_host() const { return m_definer_host; }
123 
125 
127  return m_connection_cl_name;
128  }
129 
130  const LEX_CSTRING &get_db_cl_name() const { return m_db_cl_name; }
131 
133 
134  const LEX_CSTRING &get_event_as_string() const;
135 
137  return m_action_time;
138  }
139 
140  const LEX_CSTRING &get_action_time_as_string() const;
141 
143  return m_created_timestamp.tv_sec == 0 && m_created_timestamp.tv_usec == 0;
144  }
145 
146  timeval get_created_timestamp() const { return m_created_timestamp; }
147 
149 
150  void set_action_order(ulonglong action_order) {
151  m_action_order = action_order;
152  }
153 
154  sp_head *get_sp() { return m_sp; }
155 
157 
158  bool has_parse_error() const { return m_has_parse_error; }
159 
160  const char *get_parse_error_message() const { return m_parse_error_message; }
161 
162  /**
163  Construct a full CREATE TRIGGER statement from Trigger's data members.
164 
165  @param [in] thd Thread context
166  @param [out] full_trigger_definition Place where a CREATE TRIGGER
167  statement be stored.
168 
169  @return Operation status
170  @retval true Failure
171  @retval false Success
172  */
173 
174  bool create_full_trigger_definition(const THD *thd,
175  String *full_trigger_definition) const;
176 
177  private:
178  Trigger(const LEX_CSTRING &trigger_name, MEM_ROOT *mem_root,
179  const LEX_CSTRING &db_name, const LEX_CSTRING &table_name,
180  const LEX_CSTRING &definition, const LEX_CSTRING &definition_utf8,
181  sql_mode_t sql_mode, const LEX_CSTRING &definer_user,
182  const LEX_CSTRING &definer_host, const LEX_CSTRING &client_cs_name,
183  const LEX_CSTRING &connection_cl_name, const LEX_CSTRING &db_cl_name,
185  enum_trigger_action_time_type action_time, uint action_order,
186  timeval created_timestamp);
187 
188  public:
189  ~Trigger();
190 
191  private:
192  void set_trigger_name(const LEX_CSTRING &trigger_name) {
193  m_trigger_name = trigger_name;
194  }
195 
196  void set_trigger_def(const LEX_CSTRING &trigger_def) {
197  m_definition = trigger_def;
198  }
199 
200  void set_trigger_def_utf8(const LEX_CSTRING &trigger_def_utf8) {
201  m_definition_utf8 = trigger_def_utf8;
202  }
203 
204  void set_parse_error_message(const char *error_message) {
205  m_has_parse_error = true;
206  strncpy(m_parse_error_message, error_message,
207  sizeof(m_parse_error_message));
208  }
209 
210  /**
211  Memory root to store all data of this Trigger object.
212 
213  This can be a pointer to the subject table memory root, or it can be a
214  pointer to a dedicated memory root if subject table does not exist.
215  */
217 
218  /**
219  Full trigger definition reconstructed from a data loaded from the table
220  mysql.trigger.
221  */
223 
224  private:
225  /************************************************************************
226  * Mandatory trigger attributes loaded from data dictionary.
227  * All these strings are allocated on m_mem_root.
228  ***********************************************************************/
229 
230  /// Database name.
232 
233  /// Table name.
235 
236  /// Trigger definition to save in DD.
238 
239  /// Trigger definition in UTF8 to save in DD.
241 
242  /// Trigger sql-mode.
244 
245  /// Trigger definer.
247 
248  /// Trigger definer (user part).
250 
251  /// Trigger definer (host part).
253 
254  /// Character set context, used for parsing and executing trigger.
256 
257  /// Collation name of the connection within one a trigger are created.
259 
260  /// Default database collation.
262 
263  /// Trigger event.
265 
266  /// Trigger action time.
268 
269  /**
270  Current time when the trigger was created (measured in milliseconds since
271  since 0 hours, 0 minutes, 0 seconds, January 1, 1970, UTC). This is the
272  value of CREATED attribute.
273 
274  There is special value -- zero means CREATED is not set (NULL).
275  */
277 
278  /**
279  Action_order value for the trigger. Action_order is the ordinal position
280  of the trigger in the list of triggers with the same EVENT_MANIPULATION,
281  CONDITION_TIMING, and ACTION_ORIENTATION.
282  */
284 
285  private:
286  /************************************************************************
287  * All these strings are allocated on the trigger table's mem-root.
288  ***********************************************************************/
289 
290  /// Trigger name.
292 
293  private:
294  /************************************************************************
295  * Other attributes.
296  ***********************************************************************/
297 
298  /// Grant information for the trigger.
300 
301  /// Pointer to the sp_head corresponding to the trigger.
303 
304  /// This flags specifies whether the trigger has parse error or not.
306 
307  /**
308  This error will be displayed when the user tries to manipulate or invoke
309  triggers on a table that has broken triggers. It will get set only once
310  per statement and thus will contain the first parse error encountered in
311  the trigger file.
312  */
314 };
315 
316 ///////////////////////////////////////////////////////////////////////////
317 
318 #endif // TRIGGER_H_INCLUDED
sp_head * m_sp
Pointer to the sp_head corresponding to the trigger.
Definition: trigger.h:302
unsigned long long int ulonglong
Definition: my_inttypes.h:55
void set_trigger_def(const LEX_CSTRING &trigger_def)
Definition: trigger.h:196
timeval m_created_timestamp
Current time when the trigger was created (measured in milliseconds since since 0 hours...
Definition: trigger.h:276
This file defines all base public constants related to triggers in MySQL.
const char * db_name
Definition: rules_table_service.cc:54
LEX_CSTRING m_definition
Trigger definition to save in DD.
Definition: trigger.h:237
const LEX_CSTRING & get_definer_user() const
Definition: trigger.h:120
const LEX_CSTRING & get_client_cs_name() const
Definition: trigger.h:124
Definition: sql_lex.h:2062
const LEX_CSTRING & get_db_cl_name() const
Definition: trigger.h:130
Include file for Sun RPC to compile out of the box.
Some integer typedefs for easier portability.
const LEX_CSTRING & get_db_name() const
Definition: trigger.h:104
timeval get_created_timestamp() const
Definition: trigger.h:146
const LEX_CSTRING & get_subject_table_name() const
Definition: trigger.h:106
Definition: mysql_lex_string.h:39
~Trigger()
Destroy associated SP (if any).
Definition: trigger.cc:393
LEX_CSTRING m_definition_utf8
Trigger definition in UTF8 to save in DD.
Definition: trigger.h:240
bool create_full_trigger_definition(const THD *thd, String *full_trigger_definition) const
Construct a full CREATE TRIGGER statement from Trigger&#39;s data members.
Definition: trigger.cc:430
LEX_CSTRING m_full_trigger_definition
Full trigger definition reconstructed from a data loaded from the table mysql.trigger.
Definition: trigger.h:222
const LEX_CSTRING & get_event_as_string() const
Definition: trigger.cc:190
Using this class is fraught with peril, and you need to be very careful when doing so...
Definition: sql_string.h:161
Definition: table.h:1301
Common definition between mysql server & client.
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, timeval created_timestamp)
Trigger constructor.
Definition: trigger.cc:356
LEX_CSTRING m_db_name
Database name.
Definition: trigger.h:231
const LEX_CSTRING & get_definer() const
Definition: trigger.h:118
enum_trigger_event_type m_event
Trigger event.
Definition: trigger.h:264
sql_mode_t m_sql_mode
Trigger sql-mode.
Definition: trigger.h:243
void set_trigger_def_utf8(const LEX_CSTRING &trigger_def_utf8)
Definition: trigger.h:200
ulonglong m_action_order
Action_order value for the trigger.
Definition: trigger.h:283
void print_upgrade_warning(THD *thd)
Print upgrade warnings (if any).
Definition: trigger.cc:699
const LEX_CSTRING & get_action_time_as_string() const
Definition: trigger.cc:186
GRANT_INFO m_subject_table_grant
Grant information for the trigger.
Definition: trigger.h:299
void set_action_order(ulonglong action_order)
Definition: trigger.h:150
LEX_CSTRING m_subject_table_name
Table name.
Definition: trigger.h:234
LEX_CSTRING m_db_cl_name
Default database collation.
Definition: trigger.h:261
bool has_parse_error() const
Definition: trigger.h:158
#define MYSQL_ERRMSG_SIZE
Max length of a error message.
Definition: mysql_com.h:833
unsigned int uint
Definition: uca-dump.cc:29
bool is_created_timestamp_null() const
Definition: trigger.h:142
LEX_CSTRING m_trigger_name
Trigger name.
Definition: trigger.h:291
bool m_has_parse_error
This flags specifies whether the trigger has parse error or not.
Definition: trigger.h:305
GRANT_INFO * get_subject_table_grant()
Definition: trigger.h:156
void set_parse_error_message(const char *error_message)
Definition: trigger.h:204
LEX_CSTRING m_client_cs_name
Character set context, used for parsing and executing trigger.
Definition: trigger.h:255
const char * get_parse_error_message() const
Definition: trigger.h:160
enum_trigger_action_time_type get_action_time() const
Definition: trigger.h:136
MEM_ROOT * m_mem_root
Memory root to store all data of this Trigger object.
Definition: trigger.h:216
void set_trigger_name(const LEX_CSTRING &trigger_name)
Definition: trigger.h:192
bool parse(THD *thd, bool is_upgrade)
Parse CREATE TRIGGER statement.
Definition: trigger.cc:465
sp_head represents one instance of a stored program.
Definition: sp_head.h:405
sql_mode_t get_sql_mode() const
Definition: trigger.h:116
static MEM_ROOT mem_root
Definition: client_plugin.cc:107
const LEX_CSTRING & get_connection_cl_name() const
Definition: trigger.h:126
const LEX_CSTRING & get_definition_utf8() const
Definition: trigger.h:114
const LEX_CSTRING & get_trigger_name() const
Definition: trigger.h:110
bool execute(THD *thd)
Execute trigger&#39;s body.
Definition: trigger.cc:405
ulonglong get_action_order() const
Definition: trigger.h:148
LEX_CSTRING m_definer_user
Trigger definer (user part).
Definition: trigger.h:249
LEX_CSTRING m_connection_cl_name
Collation name of the connection within one a trigger are created.
Definition: trigger.h:258
LEX_CSTRING m_definer_host
Trigger definer (host part).
Definition: trigger.h:252
The current state of the privilege checking process for the current user, SQL statement and SQL objec...
Definition: table.h:340
ulonglong sql_mode_t
Definition: dd_event.h:36
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:214
const LEX_CSTRING & get_definition() const
Definition: trigger.h:112
const LEX_CSTRING & get_definer_host() const
Definition: trigger.h:122
enum_trigger_event_type get_event() const
Definition: trigger.h:132
static const char * sql_mode
Definition: mysqlslap.cc:193
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:77
Definition: table.h:2468
enum_trigger_event_type
Constants to enumerate possible event types on which triggers can be fired.
Definition: trigger_def.h:41
event_type
Definition: test_pfs_resource_group.cc:70
This class represents a trigger object.
Definition: trigger.h:73
sp_head * get_sp()
Definition: trigger.h:154
void add_tables_and_routines(THD *thd, Query_tables_list *prelocking_ctx, TABLE_LIST *table_list)
Add tables and routines used by trigger to the set of elements used by statement. ...
Definition: trigger.cc:656
LEX_CSTRING m_definer
Trigger definer.
Definition: trigger.h:246
enum_trigger_action_time_type
Constants to enumerate possible timings when triggers can be fired.
Definition: trigger_def.h:51
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, timeval created_timestamp)
Creates a new Trigger-instance with the state loaded from the Data Dictionary.
Definition: trigger.cc:336
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:778
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: trigger.h:313
ulonglong sql_mode_t
Definition: trigger.h:45
enum_trigger_action_time_type m_action_time
Trigger action time.
Definition: trigger.h:267
const char * table_name
Definition: rules_table_service.cc:55