Drizzled Public API Documentation

event_observer.h
1 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3  *
4  * Definitions required for EventObserver plugin
5  *
6  * Copyright (C) 2010 PrimeBase Technologies GmbH, Germany
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  * Barry Leslie
22  *
23  * 2010-05-12
24  */
25 
26 /*
27  * How to add a new event:
28  *
29  * In event_observer.h
30  * 1: Add your event to EventType.
31  * 2: Add it to EventObserver::eventName().
32  * 3: Cerate a EventData class for it based on SessionEventData, SchemaEventData,
33  * or TableEventData depending on the event type class.
34  * 4: Add a static method to the EventObserver class, similar to beforeInsertRecord() for example,
35  * that will be called by drizzle.
36  *
37  * In event_observer.cc
38  * 5: Impliment your static event method, similar to beforeInsertRecord() for example.
39  * 6: Depending on the event type class add an event vector for it to the SessionEventObservers,
40  * SchemaEventObservers, or TableEventObservers class.
41  *
42  */
43 #pragma once
44 
45 #include <drizzled/plugin/plugin.h>
46 
47 #include <string>
48 
49 #include <drizzled/visibility.h>
50 
51 namespace drizzled {
52 namespace plugin {
53 
54 typedef std::vector<EventObserver *> EventObserverVector;
55 typedef EventObserver* EventObserverPtr;
56 
58 {
59 public:
60  explicit EventObserver(std::string name_arg)
61  : Plugin(name_arg, "EventObserver")
62  {}
63 
64  enum EventType{
65  /* Session events: */
66  BEFORE_CREATE_DATABASE, AFTER_CREATE_DATABASE,
67  BEFORE_DROP_DATABASE, AFTER_DROP_DATABASE,
68  CONNECT_SESSION,
69  DISCONNECT_SESSION,
70  AFTER_STATEMENT,
71  BEFORE_STATEMENT,
72 
73  /* Schema events: */
74  BEFORE_DROP_TABLE, AFTER_DROP_TABLE,
75  BEFORE_RENAME_TABLE, AFTER_RENAME_TABLE,
76 
77  /* Table events: */
78  BEFORE_INSERT_RECORD, AFTER_INSERT_RECORD,
79  BEFORE_UPDATE_RECORD, AFTER_UPDATE_RECORD,
80  BEFORE_DELETE_RECORD, AFTER_DELETE_RECORD,
81 
82  /* The max event ID marker. */
83  MAX_EVENT_COUNT
84  };
85 
86  static const char *eventName(EventType event)
87  {
88  switch(event)
89  {
90  case BEFORE_DROP_TABLE:
91  return "BEFORE_DROP_TABLE";
92 
93  case AFTER_DROP_TABLE:
94  return "AFTER_DROP_TABLE";
95 
96  case BEFORE_RENAME_TABLE:
97  return "BEFORE_RENAME_TABLE";
98 
99  case AFTER_RENAME_TABLE:
100  return "AFTER_RENAME_TABLE";
101 
102  case BEFORE_INSERT_RECORD:
103  return "BEFORE_INSERT_RECORD";
104 
105  case AFTER_INSERT_RECORD:
106  return "AFTER_INSERT_RECORD";
107 
108  case BEFORE_UPDATE_RECORD:
109  return "BEFORE_UPDATE_RECORD";
110 
111  case AFTER_UPDATE_RECORD:
112  return "AFTER_UPDATE_RECORD";
113 
114  case BEFORE_DELETE_RECORD:
115  return "BEFORE_DELETE_RECORD";
116 
117  case AFTER_DELETE_RECORD:
118  return "AFTER_DELETE_RECORD";
119 
120  case BEFORE_CREATE_DATABASE:
121  return "BEFORE_CREATE_DATABASE";
122 
123  case AFTER_CREATE_DATABASE:
124  return "AFTER_CREATE_DATABASE";
125 
126  case BEFORE_DROP_DATABASE:
127  return "BEFORE_DROP_DATABASE";
128 
129  case AFTER_DROP_DATABASE:
130  return "AFTER_DROP_DATABASE";
131 
132  case CONNECT_SESSION:
133  return "CONNECT_SESSION";
134 
135  case DISCONNECT_SESSION:
136  return "DISCONNECT_SESSION";
137 
138  case AFTER_STATEMENT:
139  return "AFTER_STATEMENT";
140 
141  case BEFORE_STATEMENT:
142  return "BEFORE_STATEMENT";
143 
144  case MAX_EVENT_COUNT:
145  break;
146  }
147 
148  return "Unknown";
149  }
150 
151  /*==========================================================*/
152  /* registerEvents() must be implemented to allow the plugin to
153  * register which events it is interested in.
154  */
155  virtual void registerTableEventsDo(TableShare &, EventObserverList &){}
156  virtual void registerSchemaEventsDo(const std::string &/*db*/, EventObserverList &) {}
157  virtual void registerSessionEventsDo(Session &, EventObserverList &) {}
158 
159  virtual bool observeEventDo(EventData &)= 0;
160 
161  /*==========================================================*/
162  /* Static access methods called by drizzle: */
163  static bool addPlugin(EventObserver *handler);
164  static void removePlugin(EventObserver *handler);
165 
166  /*==========================================================*/
167  /* Register an event of interest for this plugin.
168  * This is called from with in the plugin when registering itself.
169  *
170  * The position field is used to indicate the order the event observer is to be
171  * called. If the event observer must be called before any other observer then
172  * the position must be set to 1. If it must be called last then the position must be
173  * set to -1. A position of 0 indicated the position doesn't matter.
174  *
175  * If 2 plugins require the same position then which is called first in not guarenteed.
176  * In this case a warrning will be logged but execution will continue.
177  *
178  * It is good practice that if the event position matters not to hard code the position
179  * but supply a systen variable so that it can be set at runtime so that the user can
180  * decide which event should be called first.
181  */
182  void registerEvent(EventObserverList &observers, EventType event, int32_t position= 0);
183 
184  /*==========================================================*/
185  /* Called from drizzle to register all events for all event plugins
186  * interested in this table.
187  */
188  static void registerTableEvents(TableShare &table_share);
189  static void deregisterTableEvents(TableShare &table_share);
190 
191  /*==========================================================*/
192  /* Called from drizzle to register all events for all event plugins
193  * interested in this database.
194  */
195  static void registerSchemaEvents(Session &session, const std::string &db);
196  static void deregisterSchemaEvents(EventObserverList *observers);
197 
198  /*==========================================================*/
199  /* Called from drizzle to register all events for all event plugins
200  * interested in this session.
201  */
202  static void registerSessionEvents(Session &session);
203  static void deregisterSessionEvents(EventObserverList *observers);
204 
205 
206  /*==========================================================*/
207  /* Static meathods called by drizzle to notify interested plugins
208  * of a schema an event,
209  */
210  static bool beforeDropTable(Session &session, const drizzled::identifier::Table &table);
211  static bool afterDropTable(Session &session, const drizzled::identifier::Table &table, int err);
212  static bool beforeRenameTable(Session &session, const drizzled::identifier::Table &from, const drizzled::identifier::Table &to);
213  static bool afterRenameTable(Session &session, const drizzled::identifier::Table &from, const drizzled::identifier::Table &to, int err);
214  static bool connectSession(Session &session);
215  static bool disconnectSession(Session &session);
216  static bool beforeStatement(Session &session);
217  static bool afterStatement(Session &session);
218 
219  /*==========================================================*/
220  /* Static meathods called by drizzle to notify interested plugins
221  * of a table an event,
222  */
223  static bool beforeInsertRecord(Table &table, unsigned char *buf);
224  static bool afterInsertRecord(Table &table, const unsigned char *buf, int err);
225  static bool beforeDeleteRecord(Table &table, const unsigned char *buf);
226  static bool afterDeleteRecord(Table &table, const unsigned char *buf, int err);
227  static bool beforeUpdateRecord(Table &table, const unsigned char *old_data, unsigned char *new_data);
228  static bool afterUpdateRecord(Table &table, const unsigned char *old_data, unsigned char *new_data, int err);
229 
230  /*==========================================================*/
231  /* Static meathods called by drizzle to notify interested plugins
232  * of a table an event,
233  */
234  static bool beforeCreateDatabase(Session &session, const std::string &db);
235  static bool afterCreateDatabase(Session &session, const std::string &db, int err);
236  static bool beforeDropDatabase(Session &session, const std::string &db);
237  static bool afterDropDatabase(Session &session, const std::string &db, int err);
238 
239  static const EventObserverVector &getEventObservers(void);
240 
241 };
242 
243 
244 
245 
246 /* EventObserver data classes: */
247 //======================================
249 {
250 public:
251  EventObserver::EventType event;
252 
253  EventData(EventObserver::EventType event_arg):
254  event(event_arg),
255  observerList(NULL)
256  {}
257  virtual ~EventData(){}
258 
259  // Call all the event observers that are registered for this event.
260  virtual bool callEventObservers();
261 
262 protected:
263  EventObserverList *observerList;
264 
265 };
266 
267 //-----
269 {
270 public:
271  Session &session;
272 
273  SessionEventData(EventObserver::EventType event_arg, Session &session_arg):
274  EventData(event_arg),
275  session(session_arg)
276  {}
277 
278  // Call all the event observers that are registered for this event.
279  virtual bool callEventObservers();
280 
281  static bool hasEvents(Session &in_session);
282 };
283 
284 //-----
286 {
287 public:
288  Session &session;
289  const std::string &db;
290 
291  SchemaEventData(EventObserver::EventType event_arg, Session &session_arg, const std::string &db_arg):
292  EventData(event_arg),
293  session(session_arg),
294  db(db_arg)
295  {}
296 
297  // Call all the event observers that are registered for this event.
298  virtual bool callEventObservers();
299 
300 };
301 
302 //-----
304 {
305 public:
306  Session &session;
307  Table &table;
308 
309  TableEventData(EventObserver::EventType event_arg, Session &session_arg, Table &table_arg):
310  EventData(event_arg),
311  session(session_arg),
312  table(table_arg)
313  {}
314 
315  // Call all the event observers that are registered for this event.
316  virtual bool callEventObservers();
317 
318  static bool hasEvents(Table &in_table);
319 };
320 
321 //-----
323 {
324 public:
325  const std::string &db;
326 
327  BeforeCreateDatabaseEventData(Session &session_arg, const std::string &db_arg):
328  SessionEventData(EventObserver::BEFORE_CREATE_DATABASE, session_arg),
329  db(db_arg)
330  {}
331 };
332 
333 //-----
335 {
336 public:
337  const std::string &db;
338  int err;
339 
340  AfterCreateDatabaseEventData(Session &session_arg, const std::string &db_arg, int err_arg):
341  SessionEventData(EventObserver::AFTER_CREATE_DATABASE, session_arg),
342  db(db_arg),
343  err(err_arg)
344  {}
345 };
346 
347 //-----
349 {
350 public:
351  const std::string &db;
352 
353  BeforeDropDatabaseEventData(Session &session_arg, const std::string &db_arg):
354  SessionEventData(EventObserver::BEFORE_DROP_DATABASE, session_arg),
355  db(db_arg)
356  {}
357 };
358 
359 //-----
361 {
362 public:
363  const std::string &db;
364  int err;
365 
366  AfterDropDatabaseEventData(Session &session_arg, const std::string &db_arg, int err_arg):
367  SessionEventData(EventObserver::AFTER_DROP_DATABASE, session_arg),
368  db(db_arg),
369  err(err_arg)
370  {}
371 };
372 
373 //-----
375 {
376 public:
377 
378  ConnectSessionEventData(Session &session_arg):
379  SessionEventData(EventObserver::CONNECT_SESSION, session_arg)
380  {}
381 };
382 
383 //-----
385 {
386 public:
387 
388  DisconnectSessionEventData(Session &session_arg):
389  SessionEventData(EventObserver::DISCONNECT_SESSION, session_arg)
390  {}
391 };
392 
393 //-----
395 {
396 public:
397 
398  BeforeStatementEventData(Session &session_arg):
399  SessionEventData(EventObserver::BEFORE_STATEMENT, session_arg)
400  {}
401 };
402 
403 //-----
405 {
406 public:
407 
408  AfterStatementEventData(Session &session_arg):
409  SessionEventData(EventObserver::AFTER_STATEMENT, session_arg)
410  {}
411 };
412 
413 //-----
415 {
416 public:
417  const drizzled::identifier::Table &table;
418 
419  BeforeDropTableEventData(Session &session_arg, const drizzled::identifier::Table &table_arg):
420  SchemaEventData(EventObserver::BEFORE_DROP_TABLE, session_arg, table_arg.getSchemaName()),
421  table(table_arg)
422  {}
423 };
424 
425 //-----
427 {
428 public:
429  const drizzled::identifier::Table &table;
430  int err;
431 
432  AfterDropTableEventData(Session &session_arg, const drizzled::identifier::Table &table_arg, int err_arg):
433  SchemaEventData(EventObserver::AFTER_DROP_TABLE, session_arg, table_arg.getSchemaName()),
434  table(table_arg),
435  err(err_arg)
436  {}
437 };
438 
439 //-----
441 {
442 public:
443  const drizzled::identifier::Table &from;
444  const drizzled::identifier::Table &to;
445 
446  BeforeRenameTableEventData(Session &session_arg, const drizzled::identifier::Table &from_arg, const drizzled::identifier::Table &to_arg):
447  SchemaEventData(EventObserver::BEFORE_RENAME_TABLE, session_arg, from_arg.getSchemaName()),
448  from(from_arg),
449  to(to_arg)
450  {}
451 };
452 
453 //-----
455 {
456 public:
457  const drizzled::identifier::Table &from;
458  const drizzled::identifier::Table &to;
459  int err;
460 
461  AfterRenameTableEventData(Session &session_arg, const drizzled::identifier::Table &from_arg, const drizzled::identifier::Table &to_arg, int err_arg):
462  SchemaEventData(EventObserver::AFTER_RENAME_TABLE, session_arg, from_arg.getSchemaName()),
463  from(from_arg),
464  to(to_arg),
465  err(err_arg)
466  {}
467 };
468 
469 //-----
471 {
472 public:
473  unsigned char *row;
474 
475  BeforeInsertRecordEventData(Session &session_arg, Table &table_arg, unsigned char *row_arg):
476  TableEventData(EventObserver::BEFORE_INSERT_RECORD, session_arg, table_arg),
477  row(row_arg)
478  {}
479 };
480 
481 //-----
483 {
484 public:
485  const unsigned char *row;
486  int err;
487 
488  AfterInsertRecordEventData(Session &session_arg, Table &table_arg, const unsigned char *row_arg, int err_arg):
489  TableEventData(EventObserver::AFTER_INSERT_RECORD, session_arg, table_arg),
490  row(row_arg),
491  err(err_arg)
492  {}
493 };
494 
495 //-----
497 {
498 public:
499  const unsigned char *row;
500 
501  BeforeDeleteRecordEventData(Session &session_arg, Table &table_arg, const unsigned char *row_arg):
502  TableEventData(EventObserver::BEFORE_DELETE_RECORD, session_arg, table_arg),
503  row(row_arg)
504  {}
505 };
506 
507 //-----
509 {
510 public:
511  const unsigned char *row;
512  int err;
513 
514  AfterDeleteRecordEventData(Session &session_arg, Table &table_arg, const unsigned char *row_arg, int err_arg):
515  TableEventData(EventObserver::AFTER_DELETE_RECORD, session_arg, table_arg),
516  row(row_arg),
517  err(err_arg)
518  {}
519 };
520 
521 //-----
523 {
524 public:
525  const unsigned char *old_row;
526  unsigned char *new_row;
527 
528  BeforeUpdateRecordEventData(Session &session_arg, Table &table_arg,
529  const unsigned char *old_row_arg,
530  unsigned char *new_row_arg):
531  TableEventData(EventObserver::BEFORE_UPDATE_RECORD, session_arg, table_arg),
532  old_row(old_row_arg),
533  new_row(new_row_arg)
534  {}
535 };
536 
537 //-----
539 {
540 public:
541  const unsigned char *old_row;
542  const unsigned char *new_row;
543  int err;
544 
545  AfterUpdateRecordEventData(Session &session_arg, Table &table_arg,
546  const unsigned char *old_row_arg,
547  const unsigned char *new_row_arg,
548  int err_arg):
549  TableEventData(EventObserver::AFTER_UPDATE_RECORD, session_arg, table_arg),
550  old_row(old_row_arg),
551  new_row(new_row_arg),
552  err(err_arg)
553  {}
554 };
555 
556 //=======================================================
557 
558 } /* namespace plugin */
559 } /* namespace drizzled */
560