forked from alibaba/AliSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrigger_impl.cc
More file actions
265 lines (210 loc) · 10.1 KB
/
trigger_impl.cc
File metadata and controls
265 lines (210 loc) · 10.1 KB
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
255
256
257
258
259
260
261
262
263
264
265
/* Copyright (c) 2016, 2025, 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 */
#include "sql/dd/impl/types/trigger_impl.h"
#include <sstream>
#include <string>
#include "lex_string.h"
#include "my_sys.h"
#include "my_user.h" // parse_user
#include "mysql_com.h"
#include "mysqld_error.h" // ER_*
#include "sql/dd/dd_utility.h" // normalize_string
#include "sql/dd/impl/raw/raw_record.h" // Raw_record
#include "sql/dd/impl/tables/schemata.h" // Schemata::name_collation
#include "sql/dd/impl/tables/triggers.h" // Triggers
#include "sql/dd/impl/transaction_impl.h" // Open_dictionary_tables_ctx
#include "sql/dd/impl/utils.h" // is_string_in_lowercase
#include "sql/dd/string_type.h" // dd::String_type
#include "sql/dd/types/object_table.h"
#include "sql/dd/types/weak_object.h"
using dd::tables::Triggers;
namespace dd {
class Table;
///////////////////////////////////////////////////////////////////////////
// Trigger_impl implementation.
///////////////////////////////////////////////////////////////////////////
Trigger_impl::Trigger_impl()
: m_event_type(enum_event_type::ET_INSERT),
m_action_timing(enum_action_timing::AT_BEFORE),
m_ordinal_position(0),
m_action_order(0),
m_sql_mode(0),
m_table(nullptr),
m_client_collation_id(INVALID_OBJECT_ID),
m_connection_collation_id(INVALID_OBJECT_ID),
m_schema_collation_id(INVALID_OBJECT_ID) {}
Trigger_impl::Trigger_impl(Table_impl *table)
: m_event_type(enum_event_type::ET_INSERT),
m_action_timing(enum_action_timing::AT_BEFORE),
m_ordinal_position(0),
m_sql_mode(0),
m_table(table),
m_client_collation_id(INVALID_OBJECT_ID),
m_connection_collation_id(INVALID_OBJECT_ID),
m_schema_collation_id(INVALID_OBJECT_ID) {}
///////////////////////////////////////////////////////////////////////////
const Table &Trigger_impl::table() const { return *m_table; }
Table &Trigger_impl::table() { return *m_table; }
///////////////////////////////////////////////////////////////////////////
bool Trigger_impl::validate() const {
if (m_table == nullptr) {
my_error(ER_INVALID_DD_OBJECT, MYF(0), DD_table::instance().name().c_str(),
"Table object for trigger is not set");
return true;
}
return false;
}
/////////////////////////////////////////////////////////////////////////
bool Trigger_impl::restore_attributes(const Raw_record &r) {
if (check_parent_consistency(m_table,
r.read_ref_id(Triggers::FIELD_TABLE_ID)))
return true;
// Read id and name.
restore_id(r, Triggers::FIELD_ID);
restore_name(r, Triggers::FIELD_NAME);
// Read enums
m_event_type =
static_cast<enum_event_type>(r.read_int(Triggers::FIELD_EVENT_TYPE));
m_action_timing = static_cast<enum_action_timing>(
r.read_int(Triggers::FIELD_ACTION_TIMING));
m_sql_mode = r.read_int(Triggers::FIELD_SQL_MODE);
// Read numerics
m_action_order = r.read_uint(Triggers::FIELD_ACTION_ORDER);
m_created = r.read_timestamp(Triggers::FIELD_CREATED);
m_last_altered = r.read_timestamp(Triggers::FIELD_LAST_ALTERED);
// Read references
m_client_collation_id = r.read_ref_id(Triggers::FIELD_CLIENT_COLLATION_ID);
m_connection_collation_id =
r.read_ref_id(Triggers::FIELD_CONNECTION_COLLATION_ID);
m_schema_collation_id = r.read_ref_id(Triggers::FIELD_SCHEMA_COLLATION_ID);
// Read strings
m_action_statement = r.read_str(Triggers::FIELD_ACTION_STATEMENT);
m_action_statement_utf8 = r.read_str(Triggers::FIELD_ACTION_STATEMENT_UTF8);
// Read definer user/host
{
String_type definer = r.read_str(Triggers::FIELD_DEFINER);
char user_name_holder[USERNAME_LENGTH + 1];
LEX_STRING user_name = {user_name_holder, USERNAME_LENGTH};
char host_name_holder[HOSTNAME_LENGTH + 1];
LEX_STRING host_name = {host_name_holder, HOSTNAME_LENGTH};
parse_user(definer.c_str(), definer.length(), user_name.str,
&user_name.length, host_name.str, &host_name.length);
m_definer_user.assign(user_name.str, user_name.length);
m_definer_host.assign(host_name.str, host_name.length);
}
return false;
}
///////////////////////////////////////////////////////////////////////////
bool Trigger_impl::store_attributes(Raw_record *r) {
dd::Stringstream_type definer;
definer << m_definer_user << '@' << m_definer_host;
return store_id(r, Triggers::FIELD_ID) ||
store_name(r, Triggers::FIELD_NAME) ||
r->store(Triggers::FIELD_TABLE_ID, m_table->id()) ||
r->store(Triggers::FIELD_SCHEMA_ID, schema_id()) ||
r->store(Triggers::FIELD_EVENT_TYPE, (uint)m_event_type) ||
r->store(Triggers::FIELD_ACTION_TIMING, (uint)m_action_timing) ||
r->store(Triggers::FIELD_ACTION_ORDER, m_action_order) ||
r->store(Triggers::FIELD_ACTION_STATEMENT, m_action_statement) ||
r->store(Triggers::FIELD_ACTION_STATEMENT_UTF8,
m_action_statement_utf8) ||
r->store(Triggers::FIELD_DEFINER, definer.str()) ||
r->store(Triggers::FIELD_SQL_MODE, m_sql_mode) ||
r->store(Triggers::FIELD_CLIENT_COLLATION_ID, m_client_collation_id) ||
r->store(Triggers::FIELD_CONNECTION_COLLATION_ID,
m_connection_collation_id) ||
r->store(Triggers::FIELD_SCHEMA_COLLATION_ID, m_schema_collation_id) ||
r->store_timestamp(Triggers::FIELD_CREATED, m_created) ||
r->store_timestamp(Triggers::FIELD_LAST_ALTERED, m_last_altered);
}
///////////////////////////////////////////////////////////////////////////
void Trigger_impl::debug_print(String_type &outb) const {
dd::Stringstream_type ss;
ss << "TRIGGER OBJECT: { "
<< "id: {OID: " << id() << "}; "
<< "m_name: " << name() << "; "
<< "m_schema_id: {OID: " << schema_id() << "}; "
<< "m_table_id: {OID: " << m_table->id() << "}; "
<< "m_event_type: " << (uint)m_event_type << "; "
<< "m_action_timing: " << (uint)m_action_timing << "; "
<< "m_action_order: " << m_action_order << "; "
<< "m_action_statement: " << m_action_statement << "; "
<< "m_action_statement_utf8: " << m_action_statement_utf8 << "; "
<< "m_created: " << m_created.m_tv_sec << "; "
<< "m_last_altered: " << m_last_altered.m_tv_sec << "; "
<< "m_sql_mode: " << m_sql_mode << "; "
<< "m_definer_user: " << m_definer_user << "; "
<< "m_definer_host: " << m_definer_host << "; "
<< "m_client_collation_id: " << m_client_collation_id << "; "
<< "m_connection_collation_id: " << m_connection_collation_id << "; "
<< "m_schema_collation_id: " << m_schema_collation_id << "; }";
outb = ss.str();
}
///////////////////////////////////////////////////////////////////////////
Trigger_impl::Trigger_impl(const Trigger_impl &src, Table_impl *parent)
: Weak_object(src),
Entity_object_impl(src),
m_event_type(src.m_event_type),
m_action_timing(src.m_action_timing),
m_ordinal_position(src.m_ordinal_position),
m_action_order(src.m_action_order),
m_sql_mode(src.m_sql_mode),
m_created(src.m_created),
m_last_altered(src.m_last_altered),
m_action_statement_utf8(src.m_action_statement_utf8),
m_action_statement(src.m_action_statement),
m_definer_user(src.m_definer_user),
m_definer_host(src.m_definer_host),
m_table(parent),
m_client_collation_id(src.m_client_collation_id),
m_connection_collation_id(src.m_connection_collation_id),
m_schema_collation_id(src.m_schema_collation_id) {}
///////////////////////////////////////////////////////////////////////////
const Object_table &Trigger_impl::object_table() const {
return DD_table::instance();
}
///////////////////////////////////////////////////////////////////////////
void Trigger_impl::register_tables(Open_dictionary_tables_ctx *otx) {
otx->add_table<Triggers>();
}
///////////////////////////////////////////////////////////////////////////
void Trigger::create_mdl_key(const String_type &schema_name,
const String_type &name, MDL_key *mdl_key) {
#ifndef DEBUG_OFF
// Make sure schema name is lowercased when lower_case_table_names == 2.
if (lower_case_table_names == 2)
assert(is_string_in_lowercase(schema_name,
tables::Schemata::name_collation()));
#endif
/*
Normalize the trigger name so that key comparison for case and accent
insensitive trigger names yields the correct result.
*/
char normalized_name[NAME_CHAR_LEN * 2];
size_t len = normalize_string(DD_table::name_collation(), name,
normalized_name, sizeof(normalized_name));
mdl_key->mdl_key_init(MDL_key::TRIGGER, schema_name.c_str(), normalized_name,
len, name.c_str());
}
///////////////////////////////////////////////////////////////////////////
const CHARSET_INFO *Trigger::name_collation() {
return DD_table::name_collation();
}
///////////////////////////////////////////////////////////////////////////
} // namespace dd