-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathdd_event.cc
407 lines (343 loc) · 13.1 KB
/
dd_event.cc
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/* Copyright (c) 2016, 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 */
#include "sql/dd/dd_event.h"
#include <memory>
#include <string>
#include "lex_string.h"
#include "my_dbug.h"
#include "mysql/components/services/log_builtins.h"
#include "mysql/components/services/log_shared.h"
#include "mysql/my_loglevel.h"
#include "mysqld_error.h"
#include "sql/dd/cache/dictionary_client.h" // dd::cache::Dictionary_client
#include "sql/dd/impl/utils.h"
#include "sql/dd/types/schema.h"
#include "sql/event_parse_data.h" // Event_parse_data
#include "sql/log.h"
#include "sql/sql_class.h" // THD
#include "sql/sql_connect.h"
#include "sql/sql_db.h" // get_default_db_collation
#include "sql/sql_lex.h"
#include "sql/system_variables.h"
#include "sql/tztime.h" // Time_zone
#include "sql_string.h"
struct CHARSET_INFO;
namespace dd {
static const char *failsafe_object = "Event status option";
int get_old_status(Event::enum_event_status event_status) {
switch (event_status) {
case Event::ES_ENABLED:
return Event_parse_data::ENABLED;
case Event::ES_DISABLED:
return Event_parse_data::DISABLED;
case Event::ES_REPLICA_SIDE_DISABLED:
return Event_parse_data::REPLICA_SIDE_DISABLED;
}
/* purecov: begin deadcode */
LogErr(ERROR_LEVEL, ER_DD_FAILSAFE, failsafe_object);
assert(false);
return Event_parse_data::DISABLED;
/* purecov: end deadcode */
}
/**
Convert legacy event status to dd::Event::enum_event_status.
@param event_status Legacy event_status
@returns dd::Event::enum_event_status value corresponding to
legacy event_status.
*/
static Event::enum_event_status get_enum_event_status(int event_status) {
switch (event_status) {
case Event_parse_data::ENABLED:
return Event::ES_ENABLED;
case Event_parse_data::DISABLED:
return Event::ES_DISABLED;
case Event_parse_data::REPLICA_SIDE_DISABLED:
return Event::ES_REPLICA_SIDE_DISABLED;
}
/* purecov: begin deadcode */
LogErr(ERROR_LEVEL, ER_DD_FAILSAFE, failsafe_object);
assert(false);
return Event::ES_DISABLED;
/* purecov: end deadcode */
}
int get_old_on_completion(Event::enum_on_completion on_completion) {
switch (on_completion) {
case Event::OC_DROP:
return Event_parse_data::ON_COMPLETION_DROP;
case Event::OC_PRESERVE:
return Event_parse_data::ON_COMPLETION_PRESERVE;
}
/* purecov: begin deadcode */
LogErr(ERROR_LEVEL, ER_DD_FAILSAFE, failsafe_object);
assert(false);
return Event_parse_data::ON_COMPLETION_DROP;
/* purecov: end deadcode */
}
/**
Convert legacy event on completion behavior to dd::Event::enum_on_compeltion.
@param on_completion Legacy on completion behaviour value
@returns dd::Event::enum_on_compeltion corresponding to legacy
event on completion value.
*/
static Event::enum_on_completion get_on_completion(
Event_parse_data::enum_on_completion on_completion) {
switch (on_completion) {
case Event_parse_data::ON_COMPLETION_DEFAULT:
case Event_parse_data::ON_COMPLETION_DROP:
return Event::OC_DROP;
case Event_parse_data::ON_COMPLETION_PRESERVE:
return Event::OC_PRESERVE;
}
/* purecov: begin deadcode */
LogErr(ERROR_LEVEL, ER_DD_FAILSAFE, failsafe_object);
assert(false);
return Event::OC_DROP;
/* purecov: end deadcode */
}
interval_type get_old_interval_type(Event::enum_interval_field interval_field) {
switch (interval_field) {
case Event::IF_YEAR:
return INTERVAL_YEAR;
case Event::IF_QUARTER:
return INTERVAL_QUARTER;
case Event::IF_MONTH:
return INTERVAL_MONTH;
case Event::IF_WEEK:
return INTERVAL_WEEK;
case Event::IF_DAY:
return INTERVAL_DAY;
case Event::IF_HOUR:
return INTERVAL_HOUR;
case Event::IF_MINUTE:
return INTERVAL_MINUTE;
case Event::IF_SECOND:
return INTERVAL_SECOND;
case Event::IF_MICROSECOND:
return INTERVAL_MICROSECOND;
case Event::IF_YEAR_MONTH:
return INTERVAL_YEAR_MONTH;
case Event::IF_DAY_HOUR:
return INTERVAL_DAY_HOUR;
case Event::IF_DAY_MINUTE:
return INTERVAL_DAY_MINUTE;
case Event::IF_DAY_SECOND:
return INTERVAL_DAY_SECOND;
case Event::IF_HOUR_MINUTE:
return INTERVAL_HOUR_MINUTE;
case Event::IF_HOUR_SECOND:
return INTERVAL_HOUR_SECOND;
case Event::IF_MINUTE_SECOND:
return INTERVAL_MINUTE_SECOND;
case Event::IF_DAY_MICROSECOND:
return INTERVAL_DAY_MICROSECOND;
case Event::IF_HOUR_MICROSECOND:
return INTERVAL_HOUR_MICROSECOND;
case Event::IF_MINUTE_MICROSECOND:
return INTERVAL_MINUTE_MICROSECOND;
case Event::IF_SECOND_MICROSECOND:
return INTERVAL_SECOND_MICROSECOND;
}
/* purecov: begin deadcode */
LogErr(ERROR_LEVEL, ER_DD_FAILSAFE, failsafe_object);
assert(false);
return INTERVAL_YEAR;
/* purecov: end deadcode */
}
/**
Convert legacy interval type value to dd::Event::enum_interval_field.
@param interval_type_val Interval type value.
@returns dd::Event::enum_interval_field corresponding to legacy
interval type value.
*/
static Event::enum_interval_field get_enum_interval_field(
interval_type interval_type_val) {
switch (interval_type_val) {
case INTERVAL_YEAR:
return Event::IF_YEAR;
case INTERVAL_QUARTER:
return Event::IF_QUARTER;
case INTERVAL_MONTH:
return Event::IF_MONTH;
case INTERVAL_WEEK:
return Event::IF_WEEK;
case INTERVAL_DAY:
return Event::IF_DAY;
case INTERVAL_HOUR:
return Event::IF_HOUR;
case INTERVAL_MINUTE:
return Event::IF_MINUTE;
case INTERVAL_SECOND:
return Event::IF_SECOND;
case INTERVAL_MICROSECOND:
return Event::IF_MICROSECOND;
case INTERVAL_YEAR_MONTH:
return Event::IF_YEAR_MONTH;
case INTERVAL_DAY_HOUR:
return Event::IF_DAY_HOUR;
case INTERVAL_DAY_MINUTE:
return Event::IF_DAY_MINUTE;
case INTERVAL_DAY_SECOND:
return Event::IF_DAY_SECOND;
case INTERVAL_HOUR_MINUTE:
return Event::IF_HOUR_MINUTE;
case INTERVAL_HOUR_SECOND:
return Event::IF_HOUR_SECOND;
case INTERVAL_MINUTE_SECOND:
return Event::IF_MINUTE_SECOND;
case INTERVAL_DAY_MICROSECOND:
return Event::IF_DAY_MICROSECOND;
case INTERVAL_HOUR_MICROSECOND:
return Event::IF_HOUR_MICROSECOND;
case INTERVAL_MINUTE_MICROSECOND:
return Event::IF_MINUTE_MICROSECOND;
case INTERVAL_SECOND_MICROSECOND:
return Event::IF_SECOND_MICROSECOND;
case INTERVAL_LAST:
assert(false);
}
/* purecov: begin deadcode */
LogErr(ERROR_LEVEL, ER_DD_FAILSAFE, failsafe_object);
assert(false);
return Event::IF_YEAR;
/* purecov: end deadcode */
}
/**
Set Event attributes.
@param thd THD context.
@param schema Schema containing the event.
@param event Pointer to Event Object.
@param event_name Event name.
@param event_body Event body.
@param event_body_utf8 Event body utf8.
@param definer Definer of event.
@param event_data Parsed Event Data.
@param is_update true if existing Event objects attributes set
else false.
*/
static void set_event_attributes(THD *thd, const dd::Schema &schema,
Event *event, const String_type &event_name,
const String_type &event_body,
const String_type &event_body_utf8,
const LEX_USER *definer,
Event_parse_data *event_data, bool is_update) {
// Set Event name and definer.
event->set_name(event_name);
event->set_definer(definer->user.str, definer->host.str);
// Set last altered time.
event->set_last_altered(
dd::my_time_t_to_ull_datetime(thd->query_start_in_secs()));
// Set Event on completion and status.
event->set_on_completion(get_on_completion(event_data->on_completion));
if (!is_update || event_data->status_changed)
event->set_event_status(get_enum_event_status(event_data->status));
event->set_originator(event_data->originator);
// Set Event definition attributes.
if (event_data->body_changed) {
event->set_sql_mode(thd->variables.sql_mode);
event->set_definition_utf8(event_body_utf8);
event->set_definition(event_body);
}
// Set Event scheduling attributes.
if (event_data->expression) {
const String *tz_name = thd->variables.time_zone->get_name();
if (!is_update || !event_data->starts_null)
event->set_time_zone(tz_name->ptr());
event->set_interval_value_null(false);
event->set_interval_value(event_data->expression);
event->set_interval_field_null(false);
event->set_interval_field(get_enum_interval_field(event_data->interval));
event->set_execute_at_null(true);
if (!event_data->starts_null) {
event->set_starts_null(false);
event->set_starts(event_data->starts);
} else
event->set_starts_null(true);
if (!event_data->ends_null) {
event->set_ends_null(false);
event->set_ends(event_data->ends);
} else
event->set_ends_null(true);
} else if (event_data->execute_at) {
const String *tz_name = thd->variables.time_zone->get_name();
event->set_time_zone(tz_name->ptr());
event->set_interval_value_null(true);
event->set_interval_field_null(true);
event->set_starts_null(true);
event->set_ends_null(true);
event->set_execute_at_null(false);
event->set_execute_at(event_data->execute_at);
} else
assert(is_update);
if (event_data->comment.str != nullptr)
event->set_comment(String_type(event_data->comment.str));
// Set collation relate attributes.
event->set_client_collation_id(thd->variables.character_set_client->number);
event->set_connection_collation_id(
thd->variables.collation_connection->number);
const CHARSET_INFO *db_cl = nullptr;
if (get_default_db_collation(schema, &db_cl)) {
DBUG_PRINT("error", ("get_default_db_collation failed."));
// Obtain collation from thd and proceed.
thd->clear_error();
}
db_cl = db_cl ? db_cl : thd->collation();
event->set_schema_collation_id(db_cl->number);
}
bool create_event(THD *thd, const Schema &schema, const String_type &event_name,
const String_type &event_body,
const String_type &event_body_utf8, const LEX_USER *definer,
Event_parse_data *event_data) {
DBUG_TRACE;
std::unique_ptr<dd::Event> event(schema.create_event(thd));
// Set Event attributes.
set_event_attributes(thd, schema, event.get(), event_name, event_body,
event_body_utf8, definer, event_data, false);
return thd->dd_client()->store(event.get());
}
bool update_event(THD *thd, Event *event, const dd::Schema &schema,
const dd::Schema *new_schema,
const String_type &new_event_name,
const String_type &new_event_body,
const String_type &new_event_body_utf8,
const LEX_USER *definer, Event_parse_data *event_data) {
DBUG_TRACE;
// Check whether alter event was given dates that are in the past.
if (event_data->check_dates(thd,
static_cast<Event_parse_data::enum_on_completion>(
event->on_completion())))
return true;
// Update Schema Id if there is a dbname change.
if (new_schema != nullptr) event->set_schema_id(new_schema->id());
// Set the altered event attributes.
set_event_attributes(
thd, (new_schema != nullptr) ? *new_schema : schema, event,
new_event_name != "" ? new_event_name : event->name(), new_event_body,
new_event_body_utf8, definer, event_data, true);
return thd->dd_client()->update(event);
}
bool update_event_time_and_status(THD *thd, Event *event,
my_time_t last_executed, ulonglong status) {
DBUG_TRACE;
event->set_event_status_null(false);
event->set_event_status(get_enum_event_status(status));
event->set_last_executed_null(false);
event->set_last_executed(last_executed);
return thd->dd_client()->update(event);
}
} // namespace dd