forked from trdtnguyen/mysql-plnvm
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathevent_data_objects.cc
1583 lines (1309 loc) · 44 KB
/
event_data_objects.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
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 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 "event_data_objects.h"
#include "sql_parse.h" // parse_sql
#include "strfunc.h" // find_string_in_array
#include "sql_db.h" // get_default_db_collation
#include "sql_time.h" // interval_type_to_name,
// date_add_interval,
// calc_time_diff
#include "tztime.h" // my_tz_find, my_tz_OFFSET0, struct Time_zone
#include "auth_common.h" // EVENT_ACL, SUPER_ACL
#include "sp.h" // load_charset, load_collation
#include "events.h"
#include "event_db_repository.h"
#include "event_parse_data.h"
#include "sp_head.h"
#include "sql_show.h" // append_definer, append_identifier
#include "log.h"
#include "mysql/psi/mysql_sp.h"
/**
@addtogroup Event_Scheduler
@{
*/
#ifdef HAVE_PSI_INTERFACE
void init_scheduler_psi_keys()
{
const char *category= "scheduler";
PSI_server->register_statement(category, & Event_queue_element_for_exec::psi_info, 1);
}
PSI_statement_info Event_queue_element_for_exec::psi_info=
{ 0, "event", 0};
#endif
/*************************************************************************/
/**
Event_creation_ctx -- creation context of events.
*/
class Event_creation_ctx :public Stored_program_creation_ctx,
public Sql_alloc
{
public:
static bool load_from_db(THD *thd,
MEM_ROOT *event_mem_root,
const char *db_name,
const char *event_name,
TABLE *event_tbl,
Stored_program_creation_ctx **ctx);
public:
virtual Stored_program_creation_ctx *clone(MEM_ROOT *mem_root)
{
return new (mem_root)
Event_creation_ctx(m_client_cs, m_connection_cl, m_db_cl);
}
protected:
virtual Object_creation_ctx *create_backup_ctx(THD *thd) const
{
/*
We can avoid usual backup/restore employed in stored programs since we
know that this is a top level statement and the worker thread is
allocated exclusively to execute this event.
*/
return NULL;
}
private:
Event_creation_ctx(const CHARSET_INFO *client_cs,
const CHARSET_INFO *connection_cl,
const CHARSET_INFO *db_cl)
: Stored_program_creation_ctx(client_cs, connection_cl, db_cl)
{ }
};
/**************************************************************************
Event_creation_ctx implementation.
**************************************************************************/
bool
Event_creation_ctx::load_from_db(THD *thd,
MEM_ROOT *event_mem_root,
const char *db_name,
const char *event_name,
TABLE *event_tbl,
Stored_program_creation_ctx **ctx)
{
/* Load character set/collation attributes. */
const CHARSET_INFO *client_cs;
const CHARSET_INFO *connection_cl;
const CHARSET_INFO *db_cl;
bool invalid_creation_ctx= FALSE;
if (load_charset(event_mem_root,
event_tbl->field[ET_FIELD_CHARACTER_SET_CLIENT],
thd->variables.character_set_client,
&client_cs))
{
sql_print_warning("Event '%s'.'%s': invalid value "
"in column mysql.event.character_set_client.",
db_name,
event_name);
invalid_creation_ctx= TRUE;
}
if (load_collation(event_mem_root,
event_tbl->field[ET_FIELD_COLLATION_CONNECTION],
thd->variables.collation_connection,
&connection_cl))
{
sql_print_warning("Event '%s'.'%s': invalid value "
"in column mysql.event.collation_connection.",
db_name,
event_name);
invalid_creation_ctx= TRUE;
}
if (load_collation(event_mem_root,
event_tbl->field[ET_FIELD_DB_COLLATION],
NULL,
&db_cl))
{
sql_print_warning("Event '%s'.'%s': invalid value "
"in column mysql.event.db_collation.",
db_name,
event_name);
invalid_creation_ctx= TRUE;
}
/*
If we failed to resolve the database collation, load the default one
from the disk.
*/
if (!db_cl)
db_cl= get_default_db_collation(thd, db_name);
/* Create the context. */
*ctx= new Event_creation_ctx(client_cs, connection_cl, db_cl);
return invalid_creation_ctx;
}
/*************************************************************************/
/*
Initiliazes dbname and name of an Event_queue_element_for_exec
object
SYNOPSIS
Event_queue_element_for_exec::init()
RETURN VALUE
FALSE OK
TRUE Error (OOM)
*/
bool
Event_queue_element_for_exec::init(LEX_STRING db, LEX_STRING n)
{
if (!(dbname.str= my_strndup(key_memory_Event_queue_element_for_exec_names,
db.str, dbname.length= db.length, MYF(MY_WME))))
return TRUE;
if (!(name.str= my_strndup(key_memory_Event_queue_element_for_exec_names,
n.str, name.length= n.length, MYF(MY_WME))))
{
my_free(dbname.str);
return TRUE;
}
return FALSE;
}
void Event_queue_element_for_exec::claim_memory_ownership()
{
my_claim(dbname.str);
my_claim(name.str);
}
/*
Destructor
SYNOPSIS
Event_queue_element_for_exec::~Event_queue_element_for_exec()
*/
Event_queue_element_for_exec::~Event_queue_element_for_exec()
{
my_free(dbname.str);
my_free(name.str);
}
/*
Constructor
SYNOPSIS
Event_basic::Event_basic()
*/
Event_basic::Event_basic()
{
DBUG_ENTER("Event_basic::Event_basic");
/* init memory root */
init_sql_alloc(key_memory_event_basic_root, &mem_root, 256, 512);
dbname.str= name.str= NULL;
dbname.length= name.length= 0;
time_zone= NULL;
DBUG_VOID_RETURN;
}
/*
Destructor
SYNOPSIS
Event_basic::Event_basic()
*/
Event_basic::~Event_basic()
{
DBUG_ENTER("Event_basic::~Event_basic");
free_root(&mem_root, MYF(0));
DBUG_VOID_RETURN;
}
/*
Short function to load a char column into a LEX_STRING
SYNOPSIS
Event_basic::load_string_field()
field_name The field( enum_events_table_field is not actually used
because it's unknown in event_data_objects.h)
fields The Field array
field_value The value
*/
bool
Event_basic::load_string_fields(Field **fields, ...)
{
bool ret= FALSE;
va_list args;
enum enum_events_table_field field_name;
LEX_STRING *field_value;
DBUG_ENTER("Event_basic::load_string_fields");
va_start(args, fields);
field_name= (enum enum_events_table_field) va_arg(args, int);
while (field_name < ET_FIELD_COUNT)
{
field_value= va_arg(args, LEX_STRING *);
if ((field_value->str= get_field(&mem_root, fields[field_name])) == NullS)
{
ret= TRUE;
break;
}
field_value->length= strlen(field_value->str);
field_name= (enum enum_events_table_field) va_arg(args, int);
}
va_end(args);
DBUG_RETURN(ret);
}
bool
Event_basic::load_time_zone(THD *thd, const LEX_STRING tz_name)
{
String str(tz_name.str, &my_charset_latin1);
time_zone= my_tz_find(thd, &str);
return (time_zone == NULL);
}
/*
Constructor
SYNOPSIS
Event_queue_element::Event_queue_element()
*/
Event_queue_element::Event_queue_element():
on_completion(Event_parse_data::ON_COMPLETION_DROP),
status(Event_parse_data::ENABLED), expression(0), dropped(FALSE),
execution_count(0)
{
DBUG_ENTER("Event_queue_element::Event_queue_element");
starts= ends= execute_at= last_executed= 0;
starts_null= ends_null= execute_at_null= TRUE;
DBUG_VOID_RETURN;
}
/*
Destructor
SYNOPSIS
Event_queue_element::Event_queue_element()
*/
Event_queue_element::~Event_queue_element()
{
}
/*
Constructor
SYNOPSIS
Event_timed::Event_timed()
*/
Event_timed::Event_timed():
created(0), modified(0), sql_mode(0)
{
DBUG_ENTER("Event_timed::Event_timed");
init();
DBUG_VOID_RETURN;
}
/*
Destructor
SYNOPSIS
Event_timed::~Event_timed()
*/
Event_timed::~Event_timed()
{
}
/*
Constructor
SYNOPSIS
Event_job_data::Event_job_data()
*/
Event_job_data::Event_job_data()
:sql_mode(0)
{
}
/*
Init all member variables
SYNOPSIS
Event_timed::init()
*/
void
Event_timed::init()
{
DBUG_ENTER("Event_timed::init");
definer_user= NULL_CSTR;
definer_host= NULL_CSTR;
body= NULL_STR;
comment= NULL_STR;
sql_mode= 0;
DBUG_VOID_RETURN;
}
/**
Load an event's body from a row from mysql.event.
@details This method is silent on errors and should behave like that.
Callers should handle throwing of error messages. The reason is that the
class should not know about how to deal with communication.
@return Operation status
@retval FALSE OK
@retval TRUE Error
*/
bool
Event_job_data::load_from_row(THD *thd, TABLE *table)
{
char *ptr;
size_t len;
LEX_STRING tz_name;
DBUG_ENTER("Event_job_data::load_from_row");
if (!table)
DBUG_RETURN(TRUE);
if (table->s->fields < ET_FIELD_COUNT)
DBUG_RETURN(TRUE);
if (load_string_fields(table->field,
ET_FIELD_DB, &dbname,
ET_FIELD_NAME, &name,
ET_FIELD_BODY, &body,
ET_FIELD_DEFINER, &definer,
ET_FIELD_TIME_ZONE, &tz_name,
ET_FIELD_COUNT))
DBUG_RETURN(TRUE);
if (load_time_zone(thd, tz_name))
DBUG_RETURN(TRUE);
Event_creation_ctx::load_from_db(thd, &mem_root, dbname.str, name.str, table,
&creation_ctx);
ptr= strchr(definer.str, '@');
if (! ptr)
ptr= definer.str;
len= ptr - definer.str;
definer_user.str= strmake_root(&mem_root, definer.str, len);
definer_user.length= len;
len= definer.length - len - 1;
/* 1:because of @ */
definer_host.str= strmake_root(&mem_root, ptr + 1, len);
definer_host.length= len;
sql_mode= (sql_mode_t) table->field[ET_FIELD_SQL_MODE]->val_int();
DBUG_RETURN(FALSE);
}
/**
Load an event's body from a row from mysql.event.
@details This method is silent on errors and should behave like that.
Callers should handle throwing of error messages. The reason is that the
class should not know about how to deal with communication.
@return Operation status
@retval FALSE OK
@retval TRUE Error
*/
bool
Event_queue_element::load_from_row(THD *thd, TABLE *table)
{
char *ptr;
MYSQL_TIME time;
LEX_STRING tz_name;
DBUG_ENTER("Event_queue_element::load_from_row");
if (!table)
DBUG_RETURN(TRUE);
if (table->s->fields < ET_FIELD_COUNT)
DBUG_RETURN(TRUE);
if (load_string_fields(table->field,
ET_FIELD_DB, &dbname,
ET_FIELD_NAME, &name,
ET_FIELD_DEFINER, &definer,
ET_FIELD_TIME_ZONE, &tz_name,
ET_FIELD_COUNT))
DBUG_RETURN(TRUE);
if (load_time_zone(thd, tz_name))
DBUG_RETURN(TRUE);
starts_null= table->field[ET_FIELD_STARTS]->is_null();
my_bool not_used= FALSE;
if (!starts_null)
{
table->field[ET_FIELD_STARTS]->get_date(&time, TIME_NO_ZERO_DATE);
starts= my_tz_OFFSET0->TIME_to_gmt_sec(&time,¬_used);
}
ends_null= table->field[ET_FIELD_ENDS]->is_null();
if (!ends_null)
{
table->field[ET_FIELD_ENDS]->get_date(&time, TIME_NO_ZERO_DATE);
ends= my_tz_OFFSET0->TIME_to_gmt_sec(&time,¬_used);
}
if (!table->field[ET_FIELD_INTERVAL_EXPR]->is_null())
expression= table->field[ET_FIELD_INTERVAL_EXPR]->val_int();
else
expression= 0;
/*
If neigher STARTS and ENDS is set, then both fields are empty.
Hence, if ET_FIELD_EXECUTE_AT is empty there is an error.
*/
execute_at_null= table->field[ET_FIELD_EXECUTE_AT]->is_null();
DBUG_ASSERT(!(starts_null && ends_null && !expression && execute_at_null));
if (!expression && !execute_at_null)
{
if (table->field[ET_FIELD_EXECUTE_AT]->get_date(&time,
TIME_NO_ZERO_DATE))
DBUG_RETURN(TRUE);
execute_at= my_tz_OFFSET0->TIME_to_gmt_sec(&time,¬_used);
}
/*
We load the interval type from disk as string and then map it to
an integer. This decouples the values of enum interval_type
and values actually stored on disk. Therefore the type can be
reordered without risking incompatibilities of data between versions.
*/
if (!table->field[ET_FIELD_TRANSIENT_INTERVAL]->is_null())
{
int i;
char buff[MAX_FIELD_WIDTH];
String str(buff, sizeof(buff), &my_charset_bin);
LEX_STRING tmp;
table->field[ET_FIELD_TRANSIENT_INTERVAL]->val_str(&str);
if (!(tmp.length= str.length()))
DBUG_RETURN(TRUE);
tmp.str= str.c_ptr_safe();
i= find_string_in_array(interval_type_to_name, &tmp, system_charset_info);
if (i < 0)
DBUG_RETURN(TRUE);
interval= (interval_type) i;
}
if (!table->field[ET_FIELD_LAST_EXECUTED]->is_null())
{
table->field[ET_FIELD_LAST_EXECUTED]->get_date(&time,
TIME_NO_ZERO_DATE);
last_executed= my_tz_OFFSET0->TIME_to_gmt_sec(&time,¬_used);
}
if ((ptr= get_field(&mem_root, table->field[ET_FIELD_STATUS])) == NullS)
DBUG_RETURN(TRUE);
DBUG_PRINT("load_from_row", ("Event [%s] is [%s]", name.str, ptr));
/* Set event status (ENABLED | SLAVESIDE_DISABLED | DISABLED) */
switch (ptr[0])
{
case 'E' :
status = Event_parse_data::ENABLED;
break;
case 'S' :
status = Event_parse_data::SLAVESIDE_DISABLED;
break;
case 'D' :
default:
status = Event_parse_data::DISABLED;
break;
}
if ((ptr= get_field(&mem_root, table->field[ET_FIELD_ORIGINATOR])) == NullS)
DBUG_RETURN(TRUE);
originator = table->field[ET_FIELD_ORIGINATOR]->val_int();
/* ToDo : Andrey . Find a way not to allocate ptr on event_mem_root */
if ((ptr= get_field(&mem_root,
table->field[ET_FIELD_ON_COMPLETION])) == NullS)
DBUG_RETURN(TRUE);
on_completion= (ptr[0]=='D'? Event_parse_data::ON_COMPLETION_DROP:
Event_parse_data::ON_COMPLETION_PRESERVE);
DBUG_RETURN(FALSE);
}
/**
Load an event's body from a row from mysql.event.
@details This method is silent on errors and should behave like that.
Callers should handle throwing of error messages. The reason is that the
class should not know about how to deal with communication.
@return Operation status
@retval FALSE OK
@retval TRUE Error
*/
bool
Event_timed::load_from_row(THD *thd, TABLE *table)
{
char *ptr;
size_t len;
DBUG_ENTER("Event_timed::load_from_row");
if (Event_queue_element::load_from_row(thd, table))
DBUG_RETURN(TRUE);
if (load_string_fields(table->field,
ET_FIELD_BODY, &body,
ET_FIELD_BODY_UTF8, &body_utf8,
ET_FIELD_COUNT))
DBUG_RETURN(TRUE);
if (Event_creation_ctx::load_from_db(thd, &mem_root, dbname.str, name.str,
table, &creation_ctx))
{
push_warning_printf(thd,
Sql_condition::SL_WARNING,
ER_EVENT_INVALID_CREATION_CTX,
ER(ER_EVENT_INVALID_CREATION_CTX),
(const char *) dbname.str,
(const char *) name.str);
}
ptr= strchr(definer.str, '@');
if (! ptr)
ptr= definer.str;
len= ptr - definer.str;
definer_user.str= strmake_root(&mem_root, definer.str, len);
definer_user.length= len;
len= definer.length - len - 1;
/* 1:because of @ */
definer_host.str= strmake_root(&mem_root, ptr + 1, len);
definer_host.length= len;
created= table->field[ET_FIELD_CREATED]->val_int();
modified= table->field[ET_FIELD_MODIFIED]->val_int();
comment.str= get_field(&mem_root, table->field[ET_FIELD_COMMENT]);
if (comment.str != NullS)
comment.length= strlen(comment.str);
else
comment.length= 0;
sql_mode= (sql_mode_t) table->field[ET_FIELD_SQL_MODE]->val_int();
DBUG_RETURN(FALSE);
}
/*
add_interval() adds a specified interval to time 'ltime' in time
zone 'time_zone', and returns the result converted to the number of
seconds since epoch (aka Unix time; in UTC time zone). Zero result
means an error.
*/
static
my_time_t
add_interval(MYSQL_TIME *ltime, const Time_zone *time_zone,
interval_type scale, Interval interval)
{
if (date_add_interval(ltime, scale, interval))
return 0;
my_bool not_used;
return time_zone->TIME_to_gmt_sec(ltime, ¬_used);
}
/*
Computes the sum of a timestamp plus interval.
SYNOPSIS
get_next_time()
time_zone event time zone
next the sum
start add interval_value to this time
time_now current time
i_value quantity of time type interval to add
i_type type of interval to add (SECOND, MINUTE, HOUR, WEEK ...)
RETURN VALUE
0 OK
1 Error
NOTES
1) If the interval is conversible to SECOND, like MINUTE, HOUR, DAY, WEEK.
Then we use TIMEDIFF()'s implementation as underlying and number of
seconds as resolution for computation.
2) In all other cases - MONTH, QUARTER, YEAR we use MONTH as resolution
and PERIOD_DIFF()'s implementation
*/
static
bool get_next_time(const Time_zone *time_zone, my_time_t *next,
my_time_t start, my_time_t time_now,
int i_value, interval_type i_type)
{
DBUG_ENTER("get_next_time");
DBUG_PRINT("enter", ("start: %lu now: %lu", (long) start, (long) time_now));
DBUG_ASSERT(start <= time_now);
longlong months=0, seconds=0;
switch (i_type) {
case INTERVAL_YEAR:
months= i_value*12;
break;
case INTERVAL_QUARTER:
/* Has already been converted to months */
case INTERVAL_YEAR_MONTH:
case INTERVAL_MONTH:
months= i_value;
break;
case INTERVAL_WEEK:
/* WEEK has already been converted to days */
case INTERVAL_DAY:
seconds= i_value*24*3600;
break;
case INTERVAL_DAY_HOUR:
case INTERVAL_HOUR:
seconds= i_value*3600;
break;
case INTERVAL_DAY_MINUTE:
case INTERVAL_HOUR_MINUTE:
case INTERVAL_MINUTE:
seconds= i_value*60;
break;
case INTERVAL_DAY_SECOND:
case INTERVAL_HOUR_SECOND:
case INTERVAL_MINUTE_SECOND:
case INTERVAL_SECOND:
seconds= i_value;
break;
case INTERVAL_DAY_MICROSECOND:
case INTERVAL_HOUR_MICROSECOND:
case INTERVAL_MINUTE_MICROSECOND:
case INTERVAL_SECOND_MICROSECOND:
case INTERVAL_MICROSECOND:
/*
We should return an error here so SHOW EVENTS/ SELECT FROM I_S.EVENTS
would give an error then.
*/
DBUG_RETURN(1);
break;
case INTERVAL_LAST:
DBUG_ASSERT(0);
}
DBUG_PRINT("info", ("seconds: %ld months: %ld", (long) seconds, (long) months));
MYSQL_TIME local_start;
MYSQL_TIME local_now;
/* Convert times from UTC to local. */
{
time_zone->gmt_sec_to_TIME(&local_start, start);
time_zone->gmt_sec_to_TIME(&local_now, time_now);
}
Interval interval;
memset(&interval, 0, sizeof(interval));
my_time_t next_time= 0;
if (seconds)
{
longlong seconds_diff;
long microsec_diff;
bool negative= calc_time_diff(&local_now, &local_start, 1,
&seconds_diff, µsec_diff);
if (!negative)
{
/*
The formula below returns the interval that, when added to
local_start, will always give the time in the future.
*/
interval.second= seconds_diff - seconds_diff % seconds + seconds;
next_time= add_interval(&local_start, time_zone,
INTERVAL_SECOND, interval);
if (next_time == 0)
goto done;
}
if (next_time <= time_now)
{
/*
If 'negative' is true above, then 'next_time == 0', and
'next_time <= time_now' is also true. If negative is false,
then next_time was set, but perhaps to the value that is less
then time_now. See below for elaboration.
*/
DBUG_ASSERT(negative || next_time > 0);
/*
If local_now < local_start, i.e. STARTS time is in the future
according to the local time (it always in the past according
to UTC---this is a prerequisite of this function), then
STARTS is almost always in the past according to the local
time too. However, in the time zone that has backward
Daylight Saving Time shift, the following may happen: suppose
we have a backward DST shift at certain date after 2:59:59,
i.e. local time goes 1:59:59, 2:00:00, ... , 2:59:59, (shift
here) 2:00:00 (again), ... , 2:59:59 (again), 3:00:00, ... .
Now suppose the time has passed the first 2:59:59, has been
shifted backward, and now is (the second) 2:20:00. The user
does CREATE EVENT with STARTS 'current-date 2:40:00'. Local
time 2:40:00 from create statement is treated by time
functions as the first such time, so according to UTC it comes
before the second 2:20:00. But according to local time it is
obviously in the future, so we end up in this branch.
Since we are in the second pass through 2:00:00--2:59:59, and
any local time form this interval is treated by system
functions as the time from the first pass, we have to find the
time for the next execution that is past the DST-affected
interval (past the second 2:59:59 for our example,
i.e. starting from 3:00:00). We do this in the loop until the
local time is mapped onto future UTC time. 'start' time is in
the past, so we may use 'do { } while' here, and add the first
interval right away.
Alternatively, it could be that local_now >= local_start. Now
for the example above imagine we do CREATE EVENT with STARTS
'current-date 2:10:00'. Local start 2:10 is in the past (now
is local 2:20), so we add an interval, and get next execution
time, say, 2:40. It is in the future according to local time,
but, again, since we are in the second pass through
2:00:00--2:59:59, 2:40 will be converted into UTC time in the
past. So we will end up in this branch again, and may add
intervals in a 'do { } while' loop.
Note that for any given event we may end up here only if event
next execution time will map to the time interval that is
passed twice, and only if the server was started during the
second pass, or the event is being created during the second
pass. After that, we never will get here (unless we again
start the server during the second pass). In other words,
such a condition is extremely rare.
*/
interval.second= seconds;
do
{
next_time= add_interval(&local_start, time_zone,
INTERVAL_SECOND, interval);
if (next_time == 0)
goto done;
}
while (next_time <= time_now);
}
}
else
{
long diff_months= ((long) local_now.year - (long) local_start.year)*12 +
((long) local_now.month - (long) local_start.month);
/*
Unlike for seconds above, the formula below returns the interval
that, when added to the local_start, will give the time in the
past, or somewhere in the current month. We are interested in
the latter case, to see if this time has already passed, or is
yet to come this month.
Note that the time is guaranteed to be in the past unless
(diff_months % months == 0), but no good optimization is
possible here, because (diff_months % months == 0) is what will
happen most of the time, as get_next_time() will be called right
after the execution of the event. We could pass last_executed
time to this function, and see if the execution has already
happened this month, but for that we will have to convert
last_executed from seconds since epoch to local broken-down
time, and this will greatly reduce the effect of the
optimization. So instead we keep the code simple and clean.
*/
interval.month= (ulong) (diff_months - diff_months % months);
next_time= add_interval(&local_start, time_zone,
INTERVAL_MONTH, interval);
if (next_time == 0)
goto done;
if (next_time <= time_now)
{
interval.month= (ulong) months;
next_time= add_interval(&local_start, time_zone,
INTERVAL_MONTH, interval);
if (next_time == 0)
goto done;
}
}
DBUG_ASSERT(time_now < next_time);
*next= next_time;
done:
DBUG_PRINT("info", ("next_time: %ld", (long) next_time));
DBUG_RETURN(next_time == 0);
}
/*
Computes next execution time.
SYNOPSIS
Event_queue_element::compute_next_execution_time()
RETURN VALUE
FALSE OK
TRUE Error
NOTES
The time is set in execute_at, if no more executions the latter is
set to 0.
*/
bool
Event_queue_element::compute_next_execution_time()
{
my_time_t time_now;
DBUG_ENTER("Event_queue_element::compute_next_execution_time");
DBUG_PRINT("enter", ("starts: %lu ends: %lu last_executed: %lu this: 0x%lx",
(long) starts, (long) ends, (long) last_executed,
(long) this));
if (status != Event_parse_data::ENABLED)
{
DBUG_PRINT("compute_next_execution_time",
("Event %s is DISABLED", name.str));
goto ret;
}
/* If one-time, no need to do computation */
if (!expression)
{
/* Let's check whether it was executed */
if (last_executed)
{
DBUG_PRINT("info",("One-time event %s.%s of was already executed",
dbname.str, name.str));
dropped= (on_completion == Event_parse_data::ON_COMPLETION_DROP);
DBUG_PRINT("info",("One-time event will be dropped: %d.", dropped));
status= Event_parse_data::DISABLED;
}
goto ret;
}
time_now= (my_time_t) current_thd->query_start();
DBUG_PRINT("info",("NOW: [%lu]", (ulong) time_now));
/* if time_now is after ends don't execute anymore */
if (!ends_null && ends < time_now)
{
DBUG_PRINT("info", ("NOW after ENDS, don't execute anymore"));
/* time_now is after ends. don't execute anymore */
execute_at= 0;
execute_at_null= TRUE;
if (on_completion == Event_parse_data::ON_COMPLETION_DROP)
dropped= TRUE;
DBUG_PRINT("info", ("Dropped: %d", dropped));
status= Event_parse_data::DISABLED;
goto ret;
}
/*
Here time_now is before or equals ends if the latter is set.
Let's check whether time_now is before starts.
If so schedule for starts.
*/
if (!starts_null && time_now <= starts)
{
if (time_now == starts && starts == last_executed)
{
/*
do nothing or we will schedule for second time execution at starts.
*/
}
else
{
DBUG_PRINT("info", ("STARTS is future, NOW <= STARTS,sched for STARTS"));
/*
starts is in the future
time_now before starts. Scheduling for starts
*/