~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/plugin/event_observer.h

  • Committer: Monty Taylor
  • Date: 2011-02-13 17:26:39 UTC
  • mfrom: (2157.2.2 give-in-to-pkg-config)
  • mto: This revision was merged to the branch mainline in revision 2166.
  • Revision ID: mordred@inaugust.com-20110213172639-nhy7i72sfhoq13ms
Merged in pkg-config fixes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
44
44
#define DRIZZLED_PLUGIN_EVENT_OBSERVER_H
45
45
 
46
46
#include "drizzled/plugin/plugin.h"
 
47
#include "drizzled/session.h"
47
48
 
48
49
#include <string>
49
50
 
 
51
#include "drizzled/visibility.h"
 
52
 
50
53
namespace drizzled
51
54
{
52
55
 
53
56
class Table;
54
57
class TableShare;
 
58
class Session;
55
59
 
56
60
namespace plugin
57
61
{
58
62
class EventObserverList;
59
63
class EventData;
60
 
 
61
 
 
62
 
class EventObserver : public Plugin
 
64
class EventObserver;
 
65
 
 
66
typedef std::vector<EventObserver *> EventObserverVector;
 
67
typedef EventObserver* EventObserverPtr;
 
68
 
 
69
class DRIZZLED_API EventObserver : public Plugin
 
70
{
 
71
  EventObserver();
 
72
  EventObserver(const EventObserver &);
 
73
  EventObserver& operator=(const EventObserver &);
 
74
public:
 
75
  explicit EventObserver(std::string name_arg)
 
76
    : Plugin(name_arg, "EventObserver")
 
77
  {}
 
78
  virtual ~EventObserver() {}
 
79
 
 
80
  enum EventType{
 
81
    /* Session events: */
 
82
    BEFORE_CREATE_DATABASE, AFTER_CREATE_DATABASE, 
 
83
    BEFORE_DROP_DATABASE,   AFTER_DROP_DATABASE,
 
84
    CONNECT_SESSION,
 
85
    DISCONNECT_SESSION,
 
86
    AFTER_STATEMENT,
 
87
    BEFORE_STATEMENT,
 
88
 
 
89
    /* Schema events: */
 
90
    BEFORE_DROP_TABLE,   AFTER_DROP_TABLE, 
 
91
    BEFORE_RENAME_TABLE, AFTER_RENAME_TABLE, 
 
92
 
 
93
    /* Table events: */
 
94
    BEFORE_INSERT_RECORD,   AFTER_INSERT_RECORD, 
 
95
    BEFORE_UPDATE_RECORD,   AFTER_UPDATE_RECORD, 
 
96
    BEFORE_DELETE_RECORD,   AFTER_DELETE_RECORD,
 
97
 
 
98
    /* The max event ID marker. */
 
99
    MAX_EVENT_COUNT
 
100
  };
 
101
 
 
102
  static const char *eventName(EventType event) 
63
103
  {
64
 
    EventObserver();
65
 
    EventObserver(const EventObserver &);
66
 
    EventObserver& operator=(const EventObserver &);
67
 
  public:
68
 
    explicit EventObserver(std::string name_arg)
69
 
      : Plugin(name_arg, "EventObserver")
70
 
    {}
71
 
    virtual ~EventObserver() {}
72
 
 
73
 
    enum EventType{
74
 
      /* Session events: */
75
 
      BEFORE_CREATE_DATABASE, AFTER_CREATE_DATABASE, 
76
 
      BEFORE_DROP_DATABASE,   AFTER_DROP_DATABASE,
77
 
 
78
 
      /* Schema events: */
79
 
      BEFORE_DROP_TABLE,   AFTER_DROP_TABLE, 
80
 
      BEFORE_RENAME_TABLE, AFTER_RENAME_TABLE, 
81
 
 
82
 
      /* Table events: */
83
 
      BEFORE_INSERT_RECORD,   AFTER_INSERT_RECORD, 
84
 
      BEFORE_UPDATE_RECORD,   AFTER_UPDATE_RECORD, 
85
 
      BEFORE_DELETE_RECORD,   AFTER_DELETE_RECORD,
86
 
 
87
 
      /* The max event ID marker. */
88
 
      MAX_EVENT_COUNT
89
 
    };
90
 
 
91
 
    static const char *eventName(EventType event) 
 
104
    switch(event) 
92
105
    {
93
 
      switch(event) 
94
 
      {
95
 
      case BEFORE_DROP_TABLE:
96
 
        return "BEFORE_DROP_TABLE";
97
 
 
98
 
      case AFTER_DROP_TABLE:
99
 
        return "AFTER_DROP_TABLE";
100
 
 
101
 
      case BEFORE_RENAME_TABLE:
102
 
        return "BEFORE_RENAME_TABLE";
103
 
 
104
 
      case AFTER_RENAME_TABLE:
105
 
        return "AFTER_RENAME_TABLE";
106
 
 
107
 
      case BEFORE_INSERT_RECORD:
108
 
        return "BEFORE_INSERT_RECORD";
109
 
 
110
 
      case AFTER_INSERT_RECORD:
111
 
        return "AFTER_INSERT_RECORD";
112
 
 
113
 
      case BEFORE_UPDATE_RECORD:
114
 
        return "BEFORE_UPDATE_RECORD";
115
 
 
116
 
      case AFTER_UPDATE_RECORD:
117
 
        return "AFTER_UPDATE_RECORD";
118
 
 
119
 
      case BEFORE_DELETE_RECORD:
120
 
        return "BEFORE_DELETE_RECORD";
121
 
 
122
 
      case AFTER_DELETE_RECORD:
123
 
        return "AFTER_DELETE_RECORD";
124
 
 
125
 
      case BEFORE_CREATE_DATABASE:
126
 
        return "BEFORE_CREATE_DATABASE";
127
 
 
128
 
      case AFTER_CREATE_DATABASE:
129
 
        return "AFTER_CREATE_DATABASE";
130
 
 
131
 
      case BEFORE_DROP_DATABASE:
132
 
        return "BEFORE_DROP_DATABASE";
133
 
 
134
 
      case AFTER_DROP_DATABASE:
135
 
        return "AFTER_DROP_DATABASE";
136
 
 
137
 
      case MAX_EVENT_COUNT:
138
 
        break;
139
 
      }
140
 
 
141
 
      return "Unknown";
 
106
    case BEFORE_DROP_TABLE:
 
107
      return "BEFORE_DROP_TABLE";
 
108
 
 
109
    case AFTER_DROP_TABLE:
 
110
      return "AFTER_DROP_TABLE";
 
111
 
 
112
    case BEFORE_RENAME_TABLE:
 
113
      return "BEFORE_RENAME_TABLE";
 
114
 
 
115
    case AFTER_RENAME_TABLE:
 
116
      return "AFTER_RENAME_TABLE";
 
117
 
 
118
    case BEFORE_INSERT_RECORD:
 
119
      return "BEFORE_INSERT_RECORD";
 
120
 
 
121
    case AFTER_INSERT_RECORD:
 
122
      return "AFTER_INSERT_RECORD";
 
123
 
 
124
    case BEFORE_UPDATE_RECORD:
 
125
      return "BEFORE_UPDATE_RECORD";
 
126
 
 
127
    case AFTER_UPDATE_RECORD:
 
128
      return "AFTER_UPDATE_RECORD";
 
129
 
 
130
    case BEFORE_DELETE_RECORD:
 
131
      return "BEFORE_DELETE_RECORD";
 
132
 
 
133
    case AFTER_DELETE_RECORD:
 
134
      return "AFTER_DELETE_RECORD";
 
135
 
 
136
    case BEFORE_CREATE_DATABASE:
 
137
      return "BEFORE_CREATE_DATABASE";
 
138
 
 
139
    case AFTER_CREATE_DATABASE:
 
140
      return "AFTER_CREATE_DATABASE";
 
141
 
 
142
    case BEFORE_DROP_DATABASE:
 
143
      return "BEFORE_DROP_DATABASE";
 
144
 
 
145
    case AFTER_DROP_DATABASE:
 
146
      return "AFTER_DROP_DATABASE";
 
147
 
 
148
    case CONNECT_SESSION:
 
149
      return "CONNECT_SESSION";
 
150
 
 
151
    case DISCONNECT_SESSION:
 
152
      return "DISCONNECT_SESSION";
 
153
 
 
154
    case AFTER_STATEMENT:
 
155
      return "AFTER_STATEMENT";
 
156
 
 
157
    case BEFORE_STATEMENT:
 
158
      return "BEFORE_STATEMENT";
 
159
 
 
160
    case MAX_EVENT_COUNT:
 
161
      break;
142
162
    }
143
163
 
144
 
    /*==========================================================*/
145
 
    /* registerEvents() must be implemented to allow the plugin to
146
 
     * register which events it is interested in.
147
 
   */
148
 
    virtual void registerTableEventsDo(TableShare &, EventObserverList &){}
149
 
    virtual void registerSchemaEventsDo(const std::string &/*db*/, EventObserverList &) {}
150
 
    virtual void registerSessionEventsDo(Session &, EventObserverList &) {}
151
 
 
152
 
    virtual bool observeEventDo(EventData &)= 0;
153
 
 
154
 
    /*==========================================================*/
155
 
    /* Static access methods called by drizzle: */
156
 
    static bool addPlugin(EventObserver *handler);
157
 
    static void removePlugin(EventObserver *handler);
158
 
 
159
 
    /*==========================================================*/
160
 
    /* Register an event of interest for this plugin. 
161
 
     * This is called from with in the plugin when registering itself.
162
 
     *
163
 
     * The position field is used to indicate the order the event observer is to be 
164
 
     * called. If the event observer must be called before any other observer then 
165
 
     * the position must be set to 1. If it must be called last then the position must be 
166
 
     * set to -1. A position of 0 indicated the position doesn't matter.
167
 
     *
168
 
     * If 2 plugins require the same position then which is called first in not guarenteed.
169
 
     * In this case a warrning will be logged but execution will continue.
170
 
     * 
171
 
     * It is good practice that if the event position matters not to hard code the position
172
 
     * but supply a systen variable so that it can be set at runtime so that the user can
173
 
     * decide which event should be called first.
174
 
   */
175
 
    void registerEvent(EventObserverList &observers, EventType event, int32_t position= 0); 
176
 
 
177
 
    /*==========================================================*/
178
 
    /* Called from drizzle to register all events for all event plugins 
179
 
     * interested in this table. 
180
 
   */
181
 
    static void registerTableEvents(TableShare &table_share); 
182
 
    static void deregisterTableEvents(TableShare &table_share); 
183
 
 
184
 
    /*==========================================================*/
185
 
    /* Called from drizzle to register all events for all event plugins 
186
 
     * interested in this database. 
187
 
   */
188
 
    static void registerSchemaEvents(Session &session, const std::string &db); 
189
 
    static void deregisterSchemaEvents(Session &session, const std::string &db); 
190
 
 
191
 
    /*==========================================================*/
192
 
    /* Called from drizzle to register all events for all event plugins 
193
 
     * interested in this session. 
194
 
   */
195
 
    static void registerSessionEvents(Session &session); 
196
 
    static void deregisterSessionEvents(Session &session); 
197
 
 
198
 
 
199
 
    /*==========================================================*/
200
 
    /* Static meathods called by drizzle to notify interested plugins 
201
 
     * of a schema an event,
202
 
   */
203
 
    static bool beforeDropTable(Session &session, const drizzled::TableIdentifier &table);
204
 
    static bool afterDropTable(Session &session, const drizzled::TableIdentifier &table, int err);
205
 
    static bool beforeRenameTable(Session &session, const drizzled::TableIdentifier &from, const drizzled::TableIdentifier &to);
206
 
    static bool afterRenameTable(Session &session, const drizzled::TableIdentifier &from, const drizzled::TableIdentifier &to, int err);
207
 
 
208
 
    /*==========================================================*/
209
 
    /* Static meathods called by drizzle to notify interested plugins 
210
 
     * of a table an event,
211
 
   */
212
 
    static bool beforeInsertRecord(Table &table, unsigned char *buf);
213
 
    static bool afterInsertRecord(Table &table, const unsigned char *buf, int err);
214
 
    static bool beforeDeleteRecord(Table &table, const unsigned char *buf);
215
 
    static bool afterDeleteRecord(Table &table, const unsigned char *buf, int err);
216
 
    static bool beforeUpdateRecord(Table &table, const unsigned char *old_data, unsigned char *new_data);
217
 
    static bool afterUpdateRecord(Table &table, const unsigned char *old_data, unsigned char *new_data, int err);
218
 
 
219
 
    /*==========================================================*/
220
 
    /* Static meathods called by drizzle to notify interested plugins 
221
 
     * of a table an event,
222
 
   */
223
 
    static bool beforeCreateDatabase(Session &session, const std::string &db);
224
 
    static bool afterCreateDatabase(Session &session, const std::string &db, int err);
225
 
    static bool beforeDropDatabase(Session &session, const std::string &db);
226
 
    static bool afterDropDatabase(Session &session, const std::string &db, int err);
227
 
 
228
 
  };
 
164
    return "Unknown";
 
165
  }
 
166
 
 
167
  /*==========================================================*/
 
168
  /* registerEvents() must be implemented to allow the plugin to
 
169
   * register which events it is interested in.
 
170
 */
 
171
  virtual void registerTableEventsDo(TableShare &, EventObserverList &){}
 
172
  virtual void registerSchemaEventsDo(const std::string &/*db*/, EventObserverList &) {}
 
173
  virtual void registerSessionEventsDo(Session &, EventObserverList &) {}
 
174
 
 
175
  virtual bool observeEventDo(EventData &)= 0;
 
176
 
 
177
  /*==========================================================*/
 
178
  /* Static access methods called by drizzle: */
 
179
  static bool addPlugin(EventObserver *handler);
 
180
  static void removePlugin(EventObserver *handler);
 
181
 
 
182
  /*==========================================================*/
 
183
  /* Register an event of interest for this plugin. 
 
184
   * This is called from with in the plugin when registering itself.
 
185
   *
 
186
   * The position field is used to indicate the order the event observer is to be 
 
187
   * called. If the event observer must be called before any other observer then 
 
188
   * the position must be set to 1. If it must be called last then the position must be 
 
189
   * set to -1. A position of 0 indicated the position doesn't matter.
 
190
   *
 
191
   * If 2 plugins require the same position then which is called first in not guarenteed.
 
192
   * In this case a warrning will be logged but execution will continue.
 
193
   * 
 
194
   * It is good practice that if the event position matters not to hard code the position
 
195
   * but supply a systen variable so that it can be set at runtime so that the user can
 
196
   * decide which event should be called first.
 
197
 */
 
198
  void registerEvent(EventObserverList &observers, EventType event, int32_t position= 0); 
 
199
 
 
200
  /*==========================================================*/
 
201
  /* Called from drizzle to register all events for all event plugins 
 
202
   * interested in this table. 
 
203
 */
 
204
  static void registerTableEvents(TableShare &table_share); 
 
205
  static void deregisterTableEvents(TableShare &table_share); 
 
206
 
 
207
  /*==========================================================*/
 
208
  /* Called from drizzle to register all events for all event plugins 
 
209
   * interested in this database. 
 
210
 */
 
211
  static void registerSchemaEvents(Session &session, const std::string &db); 
 
212
  static void deregisterSchemaEvents(Session &session, const std::string &db); 
 
213
 
 
214
  /*==========================================================*/
 
215
  /* Called from drizzle to register all events for all event plugins 
 
216
   * interested in this session. 
 
217
 */
 
218
  static void registerSessionEvents(Session &session); 
 
219
  static void deregisterSessionEvents(Session &session); 
 
220
 
 
221
 
 
222
  /*==========================================================*/
 
223
  /* Static meathods called by drizzle to notify interested plugins 
 
224
   * of a schema an event,
 
225
 */
 
226
  static bool beforeDropTable(Session &session, const drizzled::identifier::Table &table);
 
227
  static bool afterDropTable(Session &session, const drizzled::identifier::Table &table, int err);
 
228
  static bool beforeRenameTable(Session &session, const drizzled::identifier::Table &from, const drizzled::identifier::Table &to);
 
229
  static bool afterRenameTable(Session &session, const drizzled::identifier::Table &from, const drizzled::identifier::Table &to, int err);
 
230
  static bool connectSession(Session &session);
 
231
  static bool disconnectSession(Session &session);
 
232
  static bool beforeStatement(Session &session);
 
233
  static bool afterStatement(Session &session);
 
234
 
 
235
  /*==========================================================*/
 
236
  /* Static meathods called by drizzle to notify interested plugins 
 
237
   * of a table an event,
 
238
 */
 
239
  static bool beforeInsertRecord(Table &table, unsigned char *buf);
 
240
  static bool afterInsertRecord(Table &table, const unsigned char *buf, int err);
 
241
  static bool beforeDeleteRecord(Table &table, const unsigned char *buf);
 
242
  static bool afterDeleteRecord(Table &table, const unsigned char *buf, int err);
 
243
  static bool beforeUpdateRecord(Table &table, const unsigned char *old_data, unsigned char *new_data);
 
244
  static bool afterUpdateRecord(Table &table, const unsigned char *old_data, unsigned char *new_data, int err);
 
245
 
 
246
  /*==========================================================*/
 
247
  /* Static meathods called by drizzle to notify interested plugins 
 
248
   * of a table an event,
 
249
 */
 
250
  static bool beforeCreateDatabase(Session &session, const std::string &db);
 
251
  static bool afterCreateDatabase(Session &session, const std::string &db, int err);
 
252
  static bool beforeDropDatabase(Session &session, const std::string &db);
 
253
  static bool afterDropDatabase(Session &session, const std::string &db, int err);
 
254
 
 
255
  static const EventObserverVector &getEventObservers(void);
 
256
 
 
257
};
 
258
 
 
259
 
229
260
 
230
261
 
231
262
/* EventObserver data classes: */
264
295
 
265
296
  // Call all the event observers that are registered for this event.
266
297
  virtual bool callEventObservers();
 
298
  
 
299
  static bool hasEvents(Session &in_session) { return (in_session.getSessionObservers() != NULL);}
267
300
};
268
301
 
269
302
//-----
283
316
 
284
317
  // Call all the event observers that are registered for this event.
285
318
  virtual bool callEventObservers();
 
319
  
286
320
};
287
321
 
288
322
//-----
302
336
 
303
337
  // Call all the event observers that are registered for this event.
304
338
  virtual bool callEventObservers();
 
339
  
 
340
  static bool hasEvents(Table &in_table) { return (in_table.getMutableShare()->getTableObservers() != NULL);}
305
341
};
306
342
 
307
343
//-----
357
393
};
358
394
 
359
395
//-----
 
396
class ConnectSessionEventData: public SessionEventData
 
397
{
 
398
public:
 
399
 
 
400
  ConnectSessionEventData(Session &session_arg):
 
401
    SessionEventData(EventObserver::CONNECT_SESSION, session_arg)
 
402
  {}  
 
403
};
 
404
 
 
405
//-----
 
406
class DisconnectSessionEventData: public SessionEventData
 
407
{
 
408
public:
 
409
 
 
410
  DisconnectSessionEventData(Session &session_arg):
 
411
    SessionEventData(EventObserver::DISCONNECT_SESSION, session_arg)
 
412
  {}  
 
413
};
 
414
 
 
415
//-----
 
416
class BeforeStatementEventData: public SessionEventData
 
417
{
 
418
public:
 
419
 
 
420
  BeforeStatementEventData(Session &session_arg):
 
421
    SessionEventData(EventObserver::BEFORE_STATEMENT, session_arg)
 
422
  {}  
 
423
};
 
424
 
 
425
//-----
 
426
class AfterStatementEventData: public SessionEventData
 
427
{
 
428
public:
 
429
 
 
430
  AfterStatementEventData(Session &session_arg):
 
431
    SessionEventData(EventObserver::AFTER_STATEMENT, session_arg)
 
432
  {}  
 
433
};
 
434
 
 
435
//-----
360
436
class BeforeDropTableEventData: public SchemaEventData
361
437
{
362
438
public:
363
 
  const drizzled::TableIdentifier &table;
 
439
  const drizzled::identifier::Table &table;
364
440
 
365
 
  BeforeDropTableEventData(Session &session_arg, const drizzled::TableIdentifier &table_arg): 
 
441
  BeforeDropTableEventData(Session &session_arg, const drizzled::identifier::Table &table_arg): 
366
442
    SchemaEventData(EventObserver::BEFORE_DROP_TABLE, session_arg, table_arg.getSchemaName()), 
367
443
    table(table_arg)
368
444
  {}  
372
448
class AfterDropTableEventData: public SchemaEventData
373
449
{
374
450
public:
375
 
  const drizzled::TableIdentifier &table;
 
451
  const drizzled::identifier::Table &table;
376
452
  int err;
377
453
 
378
 
  AfterDropTableEventData(Session &session_arg, const drizzled::TableIdentifier &table_arg, int err_arg): 
 
454
  AfterDropTableEventData(Session &session_arg, const drizzled::identifier::Table &table_arg, int err_arg): 
379
455
    SchemaEventData(EventObserver::AFTER_DROP_TABLE, session_arg, table_arg.getSchemaName()), 
380
456
    table(table_arg), 
381
457
    err(err_arg)
386
462
class BeforeRenameTableEventData: public SchemaEventData
387
463
{
388
464
public:
389
 
  const drizzled::TableIdentifier &from;
390
 
  const drizzled::TableIdentifier &to;
 
465
  const drizzled::identifier::Table &from;
 
466
  const drizzled::identifier::Table &to;
391
467
 
392
 
  BeforeRenameTableEventData(Session &session_arg, const drizzled::TableIdentifier &from_arg, const drizzled::TableIdentifier &to_arg): 
 
468
  BeforeRenameTableEventData(Session &session_arg, const drizzled::identifier::Table &from_arg, const drizzled::identifier::Table &to_arg): 
393
469
    SchemaEventData(EventObserver::BEFORE_RENAME_TABLE, session_arg, from_arg.getSchemaName()), 
394
470
    from(from_arg), 
395
471
    to(to_arg)
400
476
class AfterRenameTableEventData: public SchemaEventData
401
477
{
402
478
public:
403
 
  const drizzled::TableIdentifier &from;
404
 
  const drizzled::TableIdentifier &to;
 
479
  const drizzled::identifier::Table &from;
 
480
  const drizzled::identifier::Table &to;
405
481
  int err;
406
482
 
407
 
  AfterRenameTableEventData(Session &session_arg, const drizzled::TableIdentifier &from_arg, const drizzled::TableIdentifier &to_arg, int err_arg): 
 
483
  AfterRenameTableEventData(Session &session_arg, const drizzled::identifier::Table &from_arg, const drizzled::identifier::Table &to_arg, int err_arg): 
408
484
    SchemaEventData(EventObserver::AFTER_RENAME_TABLE, session_arg, from_arg.getSchemaName()), 
409
485
    from(from_arg), 
410
486
    to(to_arg),