-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathtable_trigger_dispatcher.h
254 lines (200 loc) · 8.66 KB
/
table_trigger_dispatcher.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
Copyright (c) 2013, 2024, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#ifndef TABLE_TRIGGER_DISPATCHER_H_INCLUDED
#define TABLE_TRIGGER_DISPATCHER_H_INCLUDED
///////////////////////////////////////////////////////////////////////////
#include <assert.h>
#include <string.h>
#include "lex_string.h"
#include "my_inttypes.h"
#include "my_sys.h"
#include "mysql_com.h" // MYSQL_ERRMSG_SIZE
#include "mysqld_error.h" // ER_PARSE_ERROR
#include "sql/sql_list.h" // List
#include "sql/table_trigger_field_support.h" // Table_trigger_field_support
#include "sql/trigger_def.h" // enum_trigger_action_time_type
class Field;
class Query_tables_list;
class String;
class THD;
class Trigger;
class Trigger_chain;
struct MEM_ROOT;
namespace dd {
class Table;
} // namespace dd
struct TABLE;
class Table_ref;
template <class T>
class List;
namespace table_cache_unittest {
class Mock_share;
}
///////////////////////////////////////////////////////////////////////////
/**
This class holds all information about triggers of a table.
*/
class Table_trigger_dispatcher : public Table_trigger_field_support {
public:
static Table_trigger_dispatcher *create(TABLE *subject_table);
bool finalize_load(THD *thd);
private:
Table_trigger_dispatcher(TABLE *subject_table);
friend class table_cache_unittest::Mock_share;
public:
~Table_trigger_dispatcher() override;
/**
Checks if there is a broken trigger for this table.
@retval false if all triggers are Ok.
@retval true in case there is at least one broken trigger (a trigger which
SQL-definition can not be parsed) for this table.
*/
bool check_for_broken_triggers() {
if (m_parse_error_message) {
my_message(ER_PARSE_ERROR, m_parse_error_message, MYF(0));
return true;
}
return false;
}
/**
Create trigger for table.
@param thd Thread context
@param[out] binlog_create_trigger_stmt
Well-formed CREATE TRIGGER statement for putting into
binlog (after successful execution)
@param if_not_exists
True if 'IF NOT EXISTS' clause was specified
@param[out] already_exists
Set to true if trigger already exists on the same table
@note
- Assumes that trigger name is fully qualified.
- NULL-string means the following LEX_STRING instance:
{ str = 0; length = 0 }.
- In other words, definer_user and definer_host should contain
simultaneously NULL-strings (non-SUID/old trigger) or valid strings
(SUID/new trigger).
@return Operation status.
@retval false Success
@retval true Failure
*/
bool create_trigger(THD *thd, String *binlog_create_trigger_stmt,
bool if_not_exists, bool &already_exists);
bool process_triggers(THD *thd, enum_trigger_event_type event,
enum_trigger_action_time_type action_time,
bool old_row_is_record1);
Trigger_chain *get_triggers(int event, int action_time) {
assert(0 <= event && event < TRG_EVENT_MAX);
assert(0 <= action_time && action_time < TRG_ACTION_MAX);
return m_trigger_map[event][action_time];
}
const Trigger_chain *get_triggers(int event, int action_time) const {
assert(0 <= event && event < TRG_EVENT_MAX);
assert(0 <= action_time && action_time < TRG_ACTION_MAX);
return m_trigger_map[event][action_time];
}
Trigger *find_trigger(const LEX_STRING &trigger_name);
bool has_triggers(enum_trigger_event_type event,
enum_trigger_action_time_type action_time) const {
return get_triggers(event, action_time) != nullptr;
}
bool has_update_triggers() const {
return get_triggers(TRG_EVENT_UPDATE, TRG_ACTION_BEFORE) ||
get_triggers(TRG_EVENT_UPDATE, TRG_ACTION_AFTER);
}
bool has_delete_triggers() const {
return get_triggers(TRG_EVENT_DELETE, TRG_ACTION_BEFORE) ||
get_triggers(TRG_EVENT_DELETE, TRG_ACTION_AFTER);
}
bool mark_fields(enum_trigger_event_type event);
bool add_tables_and_routines_for_triggers(THD *thd,
Query_tables_list *prelocking_ctx,
Table_ref *table_list);
void enable_fields_temporary_nullability(THD *thd);
void disable_fields_temporary_nullability();
void print_upgrade_warnings(THD *thd);
void parse_triggers(THD *thd, List<Trigger> *triggers, bool is_upgrade);
/**
Check whether we have finalized loading of triggers for the table
by parsing their bodies, creating sp_head objects and preparing
row-accessors.
*/
bool has_load_been_finalized() { return m_load_finalized; }
private:
Trigger_chain *create_trigger_chain(
MEM_ROOT *mem_root, enum_trigger_event_type event,
enum_trigger_action_time_type action_time);
bool prepare_record1_accessors();
/**
Remember a parse error that occurred while parsing trigger definitions
loaded from the Data Dictionary. This makes the Table_trigger_dispatcher
enter the error state flagged by m_parse_error_message != nullptr . The
error message will be used whenever a statement invoking or manipulating
triggers is issued against the Table_trigger_dispatcher's table.
@param error_message The error message thrown by the parser.
*/
void set_parse_error_message(const char *error_message);
private:
/************************************************************************
* Table_trigger_field_support interface implementation.
***********************************************************************/
TABLE *get_subject_table() override { return m_subject_table; }
Field *get_trigger_variable_field(enum_trigger_variable_type v,
int field_index) override {
return (v == TRG_OLD_ROW) ? m_old_field[field_index]
: m_new_field[field_index];
}
private:
/**
TABLE instance for which this triggers list object was created.
*/
TABLE *m_subject_table;
/// Triggers grouped by event, action_time.
Trigger_chain *m_trigger_map[TRG_EVENT_MAX][TRG_ACTION_MAX];
/**
Copy of TABLE::Field array with field pointers set to TABLE::record[1]
buffer instead of TABLE::record[0] (used for OLD values in on UPDATE
trigger and DELETE trigger when it is called for REPLACE).
*/
Field **m_record1_field;
/**
During execution of trigger m_new_field and m_old_field should point to the
array of fields representing new or old version of row correspondingly
(so it can point to TABLE::field or to
Table_trigger_dispatcher::m_record1_field).
*/
Field **m_new_field;
Field **m_old_field;
/**
Error which occurred while parsing one of the triggers for the table;
nullptr if there was no error for any of its triggers.
Non-nullptr value indicates that as a precaution the object has entered
the state where all trigger operations result in errors (referencing
this error message saved) until all the table triggers are dropped. It is
not safe to add triggers since it is unknown if the broken trigger has the
same name or event type. Nor is it safe to invoke any trigger. The only
safe operations are drop_trigger() and drop_all_triggers().
@see Table_trigger_dispatcher::set_parse_error()
*/
const char *m_parse_error_message;
/** Indicates whether we have finalized loading of triggers for the table. */
bool m_load_finalized;
};
///////////////////////////////////////////////////////////////////////////
#endif // TABLE_TRIGGER_DISPATCHER_H_INCLUDED