event.h 6.03 KB
Newer Older
1 2 3
#ifndef _EVENT_H_
#define _EVENT_H_
/* Copyright (C) 2004-2006 MySQL AB
unknown's avatar
unknown committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

   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; either version 2 of the License, or
   (at your option) any later version.

   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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

19

20 21 22 23 24 25 26 27 28 29

#define EVEX_OK                  0
#define EVEX_KEY_NOT_FOUND      -1
#define EVEX_OPEN_TABLE_FAILED  -2
#define EVEX_WRITE_ROW_FAILED   -3
#define EVEX_DELETE_ROW_FAILED  -4
#define EVEX_GET_FIELD_FAILED   -5
#define EVEX_PARSE_ERROR        -6
#define EVEX_INTERNAL_ERROR     -7
#define EVEX_NO_DB_ERROR        -8
30
#define EVEX_COMPILE_ERROR     -19
unknown's avatar
unknown committed
31
#define EVEX_GENERAL_ERROR     -20
32 33 34 35 36 37
#define EVEX_BAD_IDENTIFIER    -21 
#define EVEX_BODY_TOO_LONG     -22
#define EVEX_BAD_PARAMS        -23
#define EVEX_NOT_RUNNING       -24
#define EVEX_MICROSECOND_UNSUP -25
#define EVEX_CANT_KILL         -26
unknown's avatar
unknown committed
38 39 40

#define EVENT_EXEC_NO_MORE      (1L << 0)
#define EVENT_NOT_USED          (1L << 1)
41
#define EVENT_FREE_WHEN_FINISHED (1L << 2)
unknown's avatar
unknown committed
42

43
class Event_timed;
44

45
class Events
46
{
47 48 49
public:
  static ulong opt_event_scheduler;
  static TYPELIB opt_typelib;
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
  enum enum_table_field
  {
    FIELD_DB = 0,
    FIELD_NAME,
    FIELD_BODY,
    FIELD_DEFINER,
    FIELD_EXECUTE_AT,
    FIELD_INTERVAL_EXPR,
    FIELD_TRANSIENT_INTERVAL,
    FIELD_CREATED,
    FIELD_MODIFIED,
    FIELD_LAST_EXECUTED,
    FIELD_STARTS,
    FIELD_ENDS,
    FIELD_STATUS,
    FIELD_ON_COMPLETION,
    FIELD_SQL_MODE,
    FIELD_COMMENT,
    FIELD_COUNT /* a cool trick to count the number of fields :) */
  };

  static int
  create_event(THD *thd, Event_timed *et, uint create_options,
               uint *rows_affected);

  static int
  update_event(THD *thd, Event_timed *et, sp_name *new_name,
               uint *rows_affected);

  static int
  drop_event(THD *thd, Event_timed *et, bool drop_if_exists,
             uint *rows_affected);

  static int
  open_event_table(THD *thd, enum thr_lock_type lock_type, TABLE **table);

  static int
  show_create_event(THD *thd, sp_name *spn, LEX_STRING definer);

  static int
  reconstruct_interval_expression(String *buf, interval_type interval,
                                  longlong expression);

  static int
  drop_schema_events(THD *thd, char *db);
  
  static int
  dump_internal_status(THD *thd);
  
  static int
  init();
  
  static void
  shutdown();

  static void
  init_mutexes();
  
  static void
  destroy_mutexes();


private:
  /* Prevent use of these */
  Events(const Events &);
  void operator=(Events &);
117 118
};

119 120 121


class sp_head;
122

unknown's avatar
unknown committed
123
class Event_timed
124
{
unknown's avatar
unknown committed
125 126
  Event_timed(const Event_timed &);	/* Prevent use of these */
  void operator=(Event_timed &);
127 128
  my_bool in_spawned_thread;
  ulong locked_by_thread_id;
unknown's avatar
unknown committed
129
  my_bool running;
130
  ulong thread_id;
unknown's avatar
unknown committed
131
  pthread_mutex_t LOCK_running;
132
  pthread_cond_t COND_finished;
133

unknown's avatar
unknown committed
134 135 136
  bool status_changed;
  bool last_executed_changed;

137
public:
138 139 140 141 142 143 144 145 146 147 148 149
  enum enum_status
  {
    ENABLED = 1,
    DISABLED
  };

  enum enum_on_completion
  {
    ON_COMPLETION_DROP = 1,
    ON_COMPLETION_PRESERVE
  };

150 151
  TIME last_executed;

unknown's avatar
unknown committed
152 153 154 155 156 157 158 159 160 161 162 163
  LEX_STRING dbname;
  LEX_STRING name;
  LEX_STRING body;

  LEX_STRING definer_user;
  LEX_STRING definer_host;
  LEX_STRING definer;// combination of user and host

  LEX_STRING comment;
  TIME starts;
  TIME ends;
  TIME execute_at;
164 165 166
  my_bool starts_null;
  my_bool ends_null;
  my_bool execute_at_null;
unknown's avatar
unknown committed
167 168 169 170

  longlong expression;
  interval_type interval;

171 172
  ulonglong created;
  ulonglong modified;
173 174
  enum enum_on_completion on_completion;
  enum enum_status status;
unknown's avatar
unknown committed
175
  sp_head *sphead;
176
  ulong sql_mode;
unknown's avatar
unknown committed
177
  const uchar *body_begin;
178

unknown's avatar
unknown committed
179 180 181 182
  bool dropped;
  bool free_sphead_on_delete;
  uint flags;//all kind of purposes

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
  static void *operator new(size_t size)
  {
    void *p;
    DBUG_ENTER("Event_timed::new(size)");
    p= my_malloc(size, MYF(0));
    DBUG_PRINT("info", ("alloc_ptr=0x%lx", p));
    DBUG_RETURN(p);
  }

  static void *operator new(size_t size, MEM_ROOT *mem_root)
  { return (void*) alloc_root(mem_root, (uint) size); }

  static void operator delete(void *ptr, size_t size)
  {
    DBUG_ENTER("Event_timed::delete(ptr,size)");
    DBUG_PRINT("enter", ("free_ptr=0x%lx", ptr));
    TRASH(ptr, size);
    my_free((gptr) ptr, MYF(0));
    DBUG_VOID_RETURN;
  }

  static void operator delete(void *ptr, MEM_ROOT *mem_root)
  {
    /*
      Don't free the memory it will be done by the mem_root but
      we need to call the destructor because we free other resources
      which are not allocated on the root but on the heap, or we
      deinit mutexes.
    */
    DBUG_ASSERT(0);
  }

215
  Event_timed();
216

217
  ~Event_timed();
218

219 220
  void
  init();
221

222
  void
223
  deinit_mutexes();
224

225
  int
226
  init_definer(THD *thd);
227

228 229 230 231
  int
  init_execute_at(THD *thd, Item *expr);

  int
unknown's avatar
unknown committed
232
  init_interval(THD *thd, Item *expr, interval_type new_interval);
233 234

  void
unknown's avatar
unknown committed
235
  init_name(THD *thd, sp_name *spn);
236 237 238 239 240 241

  int
  init_starts(THD *thd, Item *starts);

  int
  init_ends(THD *thd, Item *ends);
242

243
  void
244
  init_body(THD *thd);
245 246

  void
unknown's avatar
unknown committed
247
  init_comment(THD *thd, LEX_STRING *set_comment);
248 249 250

  int
  load_from_row(MEM_ROOT *mem_root, TABLE *table);
251

252
  bool
253
  compute_next_execution_time();
254

unknown's avatar
unknown committed
255
  int
256
  drop(THD *thd);
257

258 259 260
  void
  mark_last_executed(THD *thd);

261 262 263
  bool
  update_fields(THD *thd);

264 265
  int
  get_create_event(THD *thd, String *buf);
266

267
  int
268
  execute(THD *thd, MEM_ROOT *mem_root);
269 270

  int
271
  compile(THD *thd, MEM_ROOT *mem_root);
272

273 274
  bool
  is_running();
275 276

  int
277
  spawn_now(void * (*thread_func)(void*), void *arg);
unknown's avatar
unknown committed
278
  
279
  bool
280 281 282
  spawn_thread_finish(THD *thd);
  
  void
283 284
  free_sp();

285
  bool
286 287 288 289
  has_equal_db(Event_timed *etn);

  int
  kill_thread(THD *thd);
290 291

  void
292
  set_thread_id(ulong tid) { thread_id= tid; }
293 294 295 296
};


#endif /* _EVENT_H_ */