MySQL  8.0.21
Source Code Documentation
table_trigger_dispatcher.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2013, 2019, 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 TABLE_TRIGGER_DISPATCHER_H_INCLUDED
25 #define TABLE_TRIGGER_DISPATCHER_H_INCLUDED
26 
27 ///////////////////////////////////////////////////////////////////////////
28 
29 #include <string.h>
30 
31 #include "lex_string.h"
32 #include "my_dbug.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 
40 class Field;
41 class Query_tables_list;
42 class String;
43 class THD;
44 class Trigger;
45 class Trigger_chain;
46 struct MEM_ROOT;
47 
48 namespace dd {
49 class Table;
50 } // namespace dd
51 struct TABLE;
52 struct TABLE_LIST;
53 template <class T>
54 class 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  */
72  bool reorder_57_list(MEM_ROOT *mem_root, List<Trigger> *triggers);
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  */
88  if (m_has_unparseable_trigger) {
89  my_message(ER_PARSE_ERROR, m_parse_error_message, MYF(0));
90  return true;
91  }
92  return false;
93  }
94 
95  bool create_trigger(THD *thd, String *binlog_create_trigger_stmt);
96 
97  bool process_triggers(THD *thd, enum_trigger_event_type event,
99  bool old_row_is_record1);
100 
101  Trigger_chain *get_triggers(int event, int action_time) {
102  DBUG_ASSERT(0 <= event && event < TRG_EVENT_MAX);
103  DBUG_ASSERT(0 <= action_time && action_time < TRG_ACTION_MAX);
104  return m_trigger_map[event][action_time];
105  }
106 
107  const Trigger_chain *get_triggers(int event, int action_time) const {
108  DBUG_ASSERT(0 <= event && event < TRG_EVENT_MAX);
109  DBUG_ASSERT(0 <= action_time && action_time < TRG_ACTION_MAX);
110  return m_trigger_map[event][action_time];
111  }
112 
113  Trigger *find_trigger(const LEX_STRING &trigger_name);
114 
116  enum_trigger_action_time_type action_time) const {
117  return get_triggers(event, action_time) != nullptr;
118  }
119 
120  bool has_update_triggers() const {
121  return get_triggers(TRG_EVENT_UPDATE, TRG_ACTION_BEFORE) ||
122  get_triggers(TRG_EVENT_UPDATE, TRG_ACTION_AFTER);
123  }
124 
125  bool has_delete_triggers() const {
126  return get_triggers(TRG_EVENT_DELETE, TRG_ACTION_BEFORE) ||
127  get_triggers(TRG_EVENT_DELETE, TRG_ACTION_AFTER);
128  }
129 
130  bool mark_fields(enum_trigger_event_type event);
131 
132  bool add_tables_and_routines_for_triggers(THD *thd,
133  Query_tables_list *prelocking_ctx,
134  TABLE_LIST *table_list);
135 
136  void enable_fields_temporary_nullability(THD *thd);
137  void disable_fields_temporary_nullability();
138 
139  void print_upgrade_warnings(THD *thd);
140 
141  void parse_triggers(THD *thd, List<Trigger> *triggers, bool is_upgrade);
142 
143  private:
144  Trigger_chain *create_trigger_chain(
145  MEM_ROOT *mem_root, enum_trigger_event_type event,
146  enum_trigger_action_time_type action_time);
147 
148  bool prepare_record1_accessors();
149 
150  /**
151  Remember a parse error that occurred while parsing trigger definitions
152  loaded from the Data Dictionary. This makes the Table_trigger_dispatcher
153  enter the error state flagged by m_has_unparseable_trigger == true. The
154  error message will be used whenever a statement invoking or manipulating
155  triggers is issued against the Table_trigger_dispatcher's table.
156 
157  @param error_message The error message thrown by the parser.
158  */
159  void set_parse_error_message(const char *error_message) {
160  if (!m_has_unparseable_trigger) {
161  m_has_unparseable_trigger = true;
162  strncpy(m_parse_error_message, error_message,
163  sizeof(m_parse_error_message));
164  }
165  }
166 
167  private:
168  /************************************************************************
169  * Table_trigger_field_support interface implementation.
170  ***********************************************************************/
171 
172  virtual TABLE *get_subject_table() { return m_subject_table; }
173 
175  int field_index) {
176  return (v == TRG_OLD_ROW) ? m_old_field[field_index]
177  : m_new_field[field_index];
178  }
179 
180  private:
181  /**
182  TABLE instance for which this triggers list object was created.
183  */
185 
186  /// Triggers grouped by event, action_time.
188 
189  /**
190  Copy of TABLE::Field array with field pointers set to TABLE::record[1]
191  buffer instead of TABLE::record[0] (used for OLD values in on UPDATE
192  trigger and DELETE trigger when it is called for REPLACE).
193  */
195 
196  /**
197  During execution of trigger m_new_field and m_old_field should point to the
198  array of fields representing new or old version of row correspondingly
199  (so it can point to TABLE::field or to
200  Table_trigger_dispatcher::m_record1_field).
201  */
204 
205  /**
206  This flag indicates that one of the triggers was not parsed successfully,
207  and as a precaution the object has entered the state where all trigger
208  operations result in errors until all the table triggers are dropped. It is
209  not safe to add triggers since it is unknown if the broken trigger has the
210  same name or event type. Nor is it safe to invoke any trigger. The only
211  safe operations are drop_trigger() and drop_all_triggers().
212 
213  We can't use the value of m_parse_error_message as a flag to inform that
214  a trigger has a parse error since for multi-byte locale the first byte of
215  message can be 0 but the message still be meaningful. It means that just a
216  comparison against m_parse_error_message[0] can not be done safely.
217 
218  @see Table_trigger_dispatcher::set_parse_error()
219  */
221 
222  /**
223  This error will be displayed when the user tries to manipulate or invoke
224  triggers on a table that has broken triggers. It is set once per statement
225  and thus will contain the first parse error encountered in the trigger file.
226  */
227  char m_parse_error_message[MYSQL_ERRMSG_SIZE];
228 };
229 
230 ///////////////////////////////////////////////////////////////////////////
231 
232 #endif // TABLE_TRIGGER_DISPATCHER_H_INCLUDED
This file defines all base public constants related to triggers in MySQL.
Definition: mysql_lex_string.h:34
This class holds all information about triggers of a table.
Definition: table_trigger_dispatcher.h:62
Definition: sql_lex.h:2267
virtual Field * get_trigger_variable_field(enum_trigger_variable_type v, int field_index)
Definition: table_trigger_dispatcher.h:174
Definition: trigger_def.h:44
Definition: trigger_chain.h:40
Some integer typedefs for easier portability.
Trigger_chain * get_triggers(int event, int action_time)
Definition: table_trigger_dispatcher.h:101
Definition: field.h:694
bool check_for_broken_triggers()
Checks if there is a broken trigger for this table.
Definition: table_trigger_dispatcher.h:87
TABLE * m_subject_table
TABLE instance for which this triggers list object was created.
Definition: table_trigger_dispatcher.h:184
Definition: trigger_def.h:45
Definition: trigger_def.h:43
virtual TABLE * get_subject_table()
Definition: table_trigger_dispatcher.h:172
enum_trigger_variable_type
Enum constants to designate NEW and OLD trigger pseudo-variables.
Definition: trigger_def.h:72
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:202
Definition: trigger_def.h:53
Using this class is fraught with peril, and you need to be very careful when doing so...
Definition: sql_string.h:164
Definition: table.h:1313
Common definition between mysql server & client.
#define DBUG_ASSERT(A)
Definition: my_dbug.h:199
bool has_triggers(enum_trigger_event_type event, enum_trigger_action_time_type action_time) const
Definition: table_trigger_dispatcher.h:115
#define MYF(v)
Definition: aggregate_check.h:523
dberr_t create() noexcept
Create the dblwr data structures in the system tablespace.
Definition: buf0dblwr.cc:1835
bool has_delete_triggers() const
Definition: table_trigger_dispatcher.h:125
#define MYSQL_ERRMSG_SIZE
Max length of a error message.
Definition: mysql_com.h:833
void my_message(uint my_err, const char *str, myf MyFlags)
Print an error message.
Definition: my_error.cc:310
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:194
Common header for many mysys elements.
bool has_update_triggers() const
Definition: table_trigger_dispatcher.h:120
static MEM_ROOT mem_root
Definition: client_plugin.cc:109
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:159
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:220
Field ** m_old_field
Definition: table_trigger_dispatcher.h:203
Definition: trigger_def.h:72
bool create_trigger(THD *thd, const ::Trigger *new_trigger, enum_trigger_order_type ordering_clause, const LEX_CSTRING &referenced_trigger_name)
Create new trigger in the data dictionary.
Definition: dd_trigger.cc:177
Definition: test_sql_stmt.cc:150
Definition: trigger_def.h:52
The version of the current data dictionary table definitions.
Definition: dictionary_client.h:39
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:77
Definition: table.h:2494
enum_trigger_event_type
Constants to enumerate possible event types on which triggers can be fired.
Definition: trigger_def.h:41
This class represents a trigger object.
Definition: trigger.h:73
Definition: trigger_def.h:54
Definition: table.h:46
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
enum_trigger_action_time_type
Constants to enumerate possible timings when triggers can be fired.
Definition: trigger_def.h:51
const Trigger_chain * get_triggers(int event, int action_time) const
Definition: table_trigger_dispatcher.h:107
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:799