44
44
#define DRIZZLED_PLUGIN_EVENT_OBSERVER_H
46
46
#include "drizzled/plugin/plugin.h"
47
#include "drizzled/session.h"
51
#include "drizzled/visibility.h"
62
58
class EventObserverList;
66
typedef std::vector<EventObserver *> EventObserverVector;
67
typedef EventObserver* EventObserverPtr;
69
class DRIZZLED_API EventObserver : public Plugin
72
EventObserver(const EventObserver &);
73
EventObserver& operator=(const EventObserver &);
75
explicit EventObserver(std::string name_arg)
76
: Plugin(name_arg, "EventObserver")
78
virtual ~EventObserver() {}
82
BEFORE_CREATE_DATABASE, AFTER_CREATE_DATABASE,
83
BEFORE_DROP_DATABASE, AFTER_DROP_DATABASE,
90
BEFORE_DROP_TABLE, AFTER_DROP_TABLE,
91
BEFORE_RENAME_TABLE, AFTER_RENAME_TABLE,
94
BEFORE_INSERT_RECORD, AFTER_INSERT_RECORD,
95
BEFORE_UPDATE_RECORD, AFTER_UPDATE_RECORD,
96
BEFORE_DELETE_RECORD, AFTER_DELETE_RECORD,
98
/* The max event ID marker. */
102
static const char *eventName(EventType event)
62
class EventObserver : public Plugin
65
EventObserver(const EventObserver &);
66
EventObserver& operator=(const EventObserver &);
68
explicit EventObserver(std::string name_arg)
69
: Plugin(name_arg, "EventObserver")
71
virtual ~EventObserver() {}
75
BEFORE_CREATE_DATABASE, AFTER_CREATE_DATABASE,
76
BEFORE_DROP_DATABASE, AFTER_DROP_DATABASE,
79
BEFORE_DROP_TABLE, AFTER_DROP_TABLE,
80
BEFORE_RENAME_TABLE, AFTER_RENAME_TABLE,
83
BEFORE_INSERT_RECORD, AFTER_INSERT_RECORD,
84
BEFORE_UPDATE_RECORD, AFTER_UPDATE_RECORD,
85
BEFORE_DELETE_RECORD, AFTER_DELETE_RECORD,
87
/* The max event ID marker. */
91
static const char *eventName(EventType event)
106
case BEFORE_DROP_TABLE:
107
return "BEFORE_DROP_TABLE";
109
case AFTER_DROP_TABLE:
110
return "AFTER_DROP_TABLE";
112
case BEFORE_RENAME_TABLE:
113
return "BEFORE_RENAME_TABLE";
115
case AFTER_RENAME_TABLE:
116
return "AFTER_RENAME_TABLE";
118
case BEFORE_INSERT_RECORD:
119
return "BEFORE_INSERT_RECORD";
121
case AFTER_INSERT_RECORD:
122
return "AFTER_INSERT_RECORD";
124
case BEFORE_UPDATE_RECORD:
125
return "BEFORE_UPDATE_RECORD";
127
case AFTER_UPDATE_RECORD:
128
return "AFTER_UPDATE_RECORD";
130
case BEFORE_DELETE_RECORD:
131
return "BEFORE_DELETE_RECORD";
133
case AFTER_DELETE_RECORD:
134
return "AFTER_DELETE_RECORD";
136
case BEFORE_CREATE_DATABASE:
137
return "BEFORE_CREATE_DATABASE";
139
case AFTER_CREATE_DATABASE:
140
return "AFTER_CREATE_DATABASE";
142
case BEFORE_DROP_DATABASE:
143
return "BEFORE_DROP_DATABASE";
145
case AFTER_DROP_DATABASE:
146
return "AFTER_DROP_DATABASE";
148
case CONNECT_SESSION:
149
return "CONNECT_SESSION";
151
case DISCONNECT_SESSION:
152
return "DISCONNECT_SESSION";
154
case AFTER_STATEMENT:
155
return "AFTER_STATEMENT";
157
case BEFORE_STATEMENT:
158
return "BEFORE_STATEMENT";
160
case MAX_EVENT_COUNT:
95
case BEFORE_DROP_TABLE:
96
return "BEFORE_DROP_TABLE";
98
case AFTER_DROP_TABLE:
99
return "AFTER_DROP_TABLE";
101
case BEFORE_RENAME_TABLE:
102
return "BEFORE_RENAME_TABLE";
104
case AFTER_RENAME_TABLE:
105
return "AFTER_RENAME_TABLE";
107
case BEFORE_INSERT_RECORD:
108
return "BEFORE_INSERT_RECORD";
110
case AFTER_INSERT_RECORD:
111
return "AFTER_INSERT_RECORD";
113
case BEFORE_UPDATE_RECORD:
114
return "BEFORE_UPDATE_RECORD";
116
case AFTER_UPDATE_RECORD:
117
return "AFTER_UPDATE_RECORD";
119
case BEFORE_DELETE_RECORD:
120
return "BEFORE_DELETE_RECORD";
122
case AFTER_DELETE_RECORD:
123
return "AFTER_DELETE_RECORD";
125
case BEFORE_CREATE_DATABASE:
126
return "BEFORE_CREATE_DATABASE";
128
case AFTER_CREATE_DATABASE:
129
return "AFTER_CREATE_DATABASE";
131
case BEFORE_DROP_DATABASE:
132
return "BEFORE_DROP_DATABASE";
134
case AFTER_DROP_DATABASE:
135
return "AFTER_DROP_DATABASE";
137
case MAX_EVENT_COUNT:
167
/*==========================================================*/
168
/* registerEvents() must be implemented to allow the plugin to
169
* register which events it is interested in.
171
virtual void registerTableEventsDo(TableShare &, EventObserverList &){}
172
virtual void registerSchemaEventsDo(const std::string &/*db*/, EventObserverList &) {}
173
virtual void registerSessionEventsDo(Session &, EventObserverList &) {}
175
virtual bool observeEventDo(EventData &)= 0;
177
/*==========================================================*/
178
/* Static access methods called by drizzle: */
179
static bool addPlugin(EventObserver *handler);
180
static void removePlugin(EventObserver *handler);
182
/*==========================================================*/
183
/* Register an event of interest for this plugin.
184
* This is called from with in the plugin when registering itself.
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.
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.
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.
198
void registerEvent(EventObserverList &observers, EventType event, int32_t position= 0);
200
/*==========================================================*/
201
/* Called from drizzle to register all events for all event plugins
202
* interested in this table.
204
static void registerTableEvents(TableShare &table_share);
205
static void deregisterTableEvents(TableShare &table_share);
207
/*==========================================================*/
208
/* Called from drizzle to register all events for all event plugins
209
* interested in this database.
211
static void registerSchemaEvents(Session &session, const std::string &db);
212
static void deregisterSchemaEvents(Session &session, const std::string &db);
214
/*==========================================================*/
215
/* Called from drizzle to register all events for all event plugins
216
* interested in this session.
218
static void registerSessionEvents(Session &session);
219
static void deregisterSessionEvents(Session &session);
222
/*==========================================================*/
223
/* Static meathods called by drizzle to notify interested plugins
224
* of a schema an event,
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);
235
/*==========================================================*/
236
/* Static meathods called by drizzle to notify interested plugins
237
* of a table an event,
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);
246
/*==========================================================*/
247
/* Static meathods called by drizzle to notify interested plugins
248
* of a table an event,
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);
255
static const EventObserverVector &getEventObservers(void);
144
/*==========================================================*/
145
/* registerEvents() must be implemented to allow the plugin to
146
* register which events it is interested in.
148
virtual void registerTableEventsDo(TableShare &, EventObserverList &){}
149
virtual void registerSchemaEventsDo(const std::string &/*db*/, EventObserverList &) {}
150
virtual void registerSessionEventsDo(Session &, EventObserverList &) {}
152
virtual bool observeEventDo(EventData &)= 0;
154
/*==========================================================*/
155
/* Static access methods called by drizzle: */
156
static bool addPlugin(EventObserver *handler);
157
static void removePlugin(EventObserver *handler);
159
/*==========================================================*/
160
/* Register an event of interest for this plugin.
161
* This is called from with in the plugin when registering itself.
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.
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.
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.
175
void registerEvent(EventObserverList &observers, EventType event, int32_t position= 0);
177
/*==========================================================*/
178
/* Called from drizzle to register all events for all event plugins
179
* interested in this table.
181
static void registerTableEvents(TableShare &table_share);
182
static void deregisterTableEvents(TableShare &table_share);
184
/*==========================================================*/
185
/* Called from drizzle to register all events for all event plugins
186
* interested in this database.
188
static void registerSchemaEvents(Session &session, const std::string &db);
189
static void deregisterSchemaEvents(Session &session, const std::string &db);
191
/*==========================================================*/
192
/* Called from drizzle to register all events for all event plugins
193
* interested in this session.
195
static void registerSessionEvents(Session &session);
196
static void deregisterSessionEvents(Session &session);
199
/*==========================================================*/
200
/* Static meathods called by drizzle to notify interested plugins
201
* of a schema an event,
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);
208
/*==========================================================*/
209
/* Static meathods called by drizzle to notify interested plugins
210
* of a table an event,
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);
219
/*==========================================================*/
220
/* Static meathods called by drizzle to notify interested plugins
221
* of a table an event,
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);
262
231
/* EventObserver data classes: */