1 #ifndef _EVENT_H_
2 #define _EVENT_H_
3 /* Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved.
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; version 2 of the License.
8 
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335  USA */
17 
18 /**
19   @defgroup Event_Scheduler Event Scheduler
20   @ingroup Runtime_Environment
21   @{
22 
23   @file events.h
24 
25   A public interface of Events_Scheduler module.
26 */
27 
28 #ifdef HAVE_PSI_INTERFACE
29 extern PSI_mutex_key key_event_scheduler_LOCK_scheduler_state;
30 extern PSI_cond_key key_event_scheduler_COND_state;
31 extern PSI_thread_key key_thread_event_scheduler, key_thread_event_worker;
32 #endif /* HAVE_PSI_INTERFACE */
33 
34 /* Always defined, for SHOW PROCESSLIST. */
35 extern PSI_stage_info stage_waiting_on_empty_queue;
36 extern PSI_stage_info stage_waiting_for_next_activation;
37 extern PSI_stage_info stage_waiting_for_scheduler_to_stop;
38 
39 #include "sql_string.h"                         /* LEX_CSTRING */
40 #include "my_time.h"                            /* interval_type */
41 
42 class Event_db_repository;
43 class Event_parse_data;
44 class Event_queue;
45 class Event_scheduler;
46 struct TABLE_LIST;
47 class THD;
48 typedef class Item COND;
49 
50 int
51 sortcmp_lex_string(const LEX_CSTRING *s, const LEX_CSTRING *t,
52                    const CHARSET_INFO *cs);
53 
54 /**
55   @brief A facade to the functionality of the Event Scheduler.
56 
57   Every public operation against the scheduler has to be executed via the
58   interface provided by a static method of this class. No instance of this
59   class is ever created and it has no non-static data members.
60 
61   The life cycle of the Events module is the following:
62 
63   At server start up:
64      init_mutexes() -> init()
65   When the server is running:
66      create_event(), drop_event(), start_or_stop_event_scheduler(), etc
67   At shutdown:
68      deinit(), destroy_mutexes().
69 
70   The peculiar initialization and shutdown cycle is an adaptation to the
71   outside server startup/shutdown framework and mimics the rest of MySQL
72   subsystems (ACL, time zone tables, etc).
73 */
74 
75 class Events
76 {
77 public:
78   /*
79     the following block is to support --event-scheduler command line option
80     and the @@global.event_scheduler SQL variable.
81     See sys_var.cc
82   */
83   enum enum_opt_event_scheduler { EVENTS_OFF, EVENTS_ON, EVENTS_DISABLED,
84                                   EVENTS_ORIGINAL };
85   /* Protected using LOCK_global_system_variables only. */
86   static ulong opt_event_scheduler, startup_state;
87   static ulong inited;
88   static bool check_if_system_tables_error();
89   static bool start(int *err_no);
90   static bool stop();
91 
92 public:
93   /* A hack needed for Event_queue_element */
94   static Event_db_repository *
get_db_repository()95   get_db_repository() { return db_repository; }
96 
97   static bool init(THD *thd, bool opt_noacl);
98 
99   static void
100   deinit();
101 
102   static void
103   init_mutexes();
104 
105   static void
106   destroy_mutexes();
107 
108   static bool
109   create_event(THD *thd, Event_parse_data *parse_data);
110 
111   static bool
112   update_event(THD *thd, Event_parse_data *parse_data,
113                LEX_CSTRING *new_dbname, LEX_CSTRING *new_name);
114 
115   static bool
116   drop_event(THD *thd, const LEX_CSTRING *dbname, const LEX_CSTRING *name,
117              bool if_exists);
118 
119   static void
120   drop_schema_events(THD *thd, const char *db);
121 
122   static bool
123   show_create_event(THD *thd, const LEX_CSTRING *dbname,
124                     const LEX_CSTRING *name);
125 
126   /* Needed for both SHOW CREATE EVENT and INFORMATION_SCHEMA */
127   static int
128   reconstruct_interval_expression(String *buf, interval_type interval,
129                                   longlong expression);
130 
131   static int
132   fill_schema_events(THD *thd, TABLE_LIST *tables, COND * /* cond */);
133 
134   static void
135   dump_internal_status();
136 
set_original_state(ulong startup_state_org)137   static void set_original_state(ulong startup_state_org)
138   {
139     startup_state= startup_state_org;
140   }
141 
142 private:
143 
144   static bool
145   load_events_from_db(THD *thd);
146 
147 private:
148   static Event_queue         *event_queue;
149   static Event_scheduler     *scheduler;
150   static Event_db_repository *db_repository;
151 
152 private:
153   /* Prevent use of these */
154   Events(const Events &);
155   void operator=(Events &);
156 };
157 
158 /**
159   @} (end of group Event Scheduler)
160 */
161 
162 #endif /* _EVENT_H_ */
163