~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/plugin/event_observer.h

  • Committer: lbieber
  • Date: 2010-09-11 16:33:45 UTC
  • mfrom: (1757.1.2 build)
  • Revision ID: lbieber@orisndriz03-20100911163345-na1t8m18at9thsjl
Merge Vijay - added utf 8 tamil test case suite and test case for creating a database in tamil
Merge Brian - Small set of refactoring (includes one case of memset on a table object).

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
 
51
 
#include "drizzled/visibility.h"
52
 
 
53
50
namespace drizzled
54
51
{
55
52
 
56
53
class Table;
57
54
class TableShare;
58
 
class Session;
59
55
 
60
56
namespace plugin
61
57
{
62
58
class EventObserverList;
63
59
class EventData;
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) 
 
60
 
 
61
 
 
62
class EventObserver : public Plugin
103
63
  {
104
 
    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) 
105
92
    {
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;
 
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";
162
142
    }
163
143
 
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
 
 
 
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
  };
260
229
 
261
230
 
262
231
/* EventObserver data classes: */
295
264
 
296
265
  // Call all the event observers that are registered for this event.
297
266
  virtual bool callEventObservers();
298
 
  
299
 
  static bool hasEvents(Session &in_session) { return (in_session.getSessionObservers() != NULL);}
300
267
};
301
268
 
302
269
//-----
316
283
 
317
284
  // Call all the event observers that are registered for this event.
318
285
  virtual bool callEventObservers();
319
 
  
320
286
};
321
287
 
322
288
//-----
336
302
 
337
303
  // Call all the event observers that are registered for this event.
338
304
  virtual bool callEventObservers();
339
 
  
340
 
  static bool hasEvents(Table &in_table) { return (in_table.getMutableShare()->getTableObservers() != NULL);}
341
305
};
342
306
 
343
307
//-----
393
357
};
394
358
 
395
359
//-----
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
 
//-----
436
360
class BeforeDropTableEventData: public SchemaEventData
437
361
{
438
362
public:
439
 
  const drizzled::identifier::Table &table;
 
363
  const drizzled::TableIdentifier &table;
440
364
 
441
 
  BeforeDropTableEventData(Session &session_arg, const drizzled::identifier::Table &table_arg): 
 
365
  BeforeDropTableEventData(Session &session_arg, const drizzled::TableIdentifier &table_arg): 
442
366
    SchemaEventData(EventObserver::BEFORE_DROP_TABLE, session_arg, table_arg.getSchemaName()), 
443
367
    table(table_arg)
444
368
  {}  
448
372
class AfterDropTableEventData: public SchemaEventData
449
373
{
450
374
public:
451
 
  const drizzled::identifier::Table &table;
 
375
  const drizzled::TableIdentifier &table;
452
376
  int err;
453
377
 
454
 
  AfterDropTableEventData(Session &session_arg, const drizzled::identifier::Table &table_arg, int err_arg): 
 
378
  AfterDropTableEventData(Session &session_arg, const drizzled::TableIdentifier &table_arg, int err_arg): 
455
379
    SchemaEventData(EventObserver::AFTER_DROP_TABLE, session_arg, table_arg.getSchemaName()), 
456
380
    table(table_arg), 
457
381
    err(err_arg)
462
386
class BeforeRenameTableEventData: public SchemaEventData
463
387
{
464
388
public:
465
 
  const drizzled::identifier::Table &from;
466
 
  const drizzled::identifier::Table &to;
 
389
  const drizzled::TableIdentifier &from;
 
390
  const drizzled::TableIdentifier &to;
467
391
 
468
 
  BeforeRenameTableEventData(Session &session_arg, const drizzled::identifier::Table &from_arg, const drizzled::identifier::Table &to_arg): 
 
392
  BeforeRenameTableEventData(Session &session_arg, const drizzled::TableIdentifier &from_arg, const drizzled::TableIdentifier &to_arg): 
469
393
    SchemaEventData(EventObserver::BEFORE_RENAME_TABLE, session_arg, from_arg.getSchemaName()), 
470
394
    from(from_arg), 
471
395
    to(to_arg)
476
400
class AfterRenameTableEventData: public SchemaEventData
477
401
{
478
402
public:
479
 
  const drizzled::identifier::Table &from;
480
 
  const drizzled::identifier::Table &to;
 
403
  const drizzled::TableIdentifier &from;
 
404
  const drizzled::TableIdentifier &to;
481
405
  int err;
482
406
 
483
 
  AfterRenameTableEventData(Session &session_arg, const drizzled::identifier::Table &from_arg, const drizzled::identifier::Table &to_arg, int err_arg): 
 
407
  AfterRenameTableEventData(Session &session_arg, const drizzled::TableIdentifier &from_arg, const drizzled::TableIdentifier &to_arg, int err_arg): 
484
408
    SchemaEventData(EventObserver::AFTER_RENAME_TABLE, session_arg, from_arg.getSchemaName()), 
485
409
    from(from_arg), 
486
410
    to(to_arg),