~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/plugin/event_observer.h

  • Committer: David Shrewsbury
  • Date: 2010-09-09 21:56:04 UTC
  • mto: (1754.1.1 build)
  • mto: This revision was merged to the branch mainline in revision 1755.
  • Revision ID: shrewsbury.dave@gmail.com-20100909215604-hi1jaml3z3z9ozb7
Use of String::c_ptr() in temporal functions can corrupt PBXT row cache, so make sure temporaries are used.

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