2
* Copyright (c) 2010, Joseph Daly <skinny.moey@gmail.com>
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
8
* * Redistributions of source code must retain the above copyright notice,
9
* this list of conditions and the following disclaimer.
10
* * Redistributions in binary form must reproduce the above copyright notice,
11
* this list of conditions and the following disclaimer in the documentation
12
* and/or other materials provided with the distribution.
13
* * Neither the name of Joseph Daly nor the names of its contributors
14
* may be used to endorse or promote products derived from this software
15
* without specific prior written permission.
17
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27
* THE POSSIBILITY OF SUCH DAMAGE.
33
* This plugin is an example events plugin that just prints some info for
34
* the events that it is tracking.
36
set global hello_events_enable = ON;
37
set global hello_events_watch_databases = "x";
38
set global hello_events_watch_tables = "x,y";
45
#include "drizzled/session.h"
46
#include "hello_events.h"
48
using namespace drizzled;
49
using namespace plugin;
52
static bool sysvar_hello_events_enabled= true;
53
static HelloEvents *hello_events= NULL;
54
static char *sysvar_table_list= NULL;
55
static char *sysvar_db_list= NULL;
58
//==================================
59
// My table event observers:
60
static bool preWriteRow(PreWriteRowEventData *data)
63
fprintf(stderr, "EVENT preWriteRow(%s)\n", data->table->getTableName());
68
static void postWriteRow(PostWriteRowEventData *data)
70
fprintf(stderr, "EVENT postWriteRow(%s) err = %d\n", data->table->getTableName(), data->err);
74
static bool preDeleteRow(PreDeleteRowEventData *data)
76
fprintf(stderr, "EVENT preDeleteRow(%s)\n", data->table->getTableName());
81
static void postDeleteRow(PostDeleteRowEventData *data)
83
fprintf(stderr, "EVENT postDeleteRow(%s) err = %d\n", data->table->getTableName(), data->err);
87
static bool preUpdateRow(PreUpdateRowEventData *data)
89
fprintf(stderr, "EVENT preUpdateRow(%s)\n", data->table->getTableName());
94
static void postUpdateRow(PostUpdateRowEventData *data)
96
fprintf(stderr, "EVENT postUpdateRow(%s) err = %d\n", data->table->getTableName(), data->err);
99
//==================================
100
// My schema event observers:
101
static void postDropTable(PostDropTableEventData *data)
103
fprintf(stderr, "EVENT postDropTable(%s) err = %d\n", data->table->getTableName().c_str(), data->err);
107
static void postRenameTable(PostRenameTableEventData *data)
109
fprintf(stderr, "EVENT postRenameTable(%s, %s) err = %d\n", data->from->getTableName().c_str(), data->to->getTableName().c_str(), data->err);
113
static void postCreateDatabase(PostCreateDatabaseEventData *data)
115
fprintf(stderr, "EVENT postCreateDatabase(%s) err = %d\n", data->db, data->err);
119
static void postDropDatabase(PostDropDatabaseEventData *data)
121
fprintf(stderr, "EVENT postDropDatabase(%s) err = %d\n", data->db, data->err);
124
//==================================
125
/* This is where I register which table events my pluggin is interested in.*/
126
void HelloEvents::registerTableEvents(TableShare *table_share, EventObservers *observers)
128
if ((!is_enabled) || (table_share == NULL)
129
|| !isTableInteresting(table_share->getTableName())
130
|| !isDatabaseInteresting(table_share->getSchemaName()))
133
registerEvent(observers, PRE_WRITE_ROW);
134
registerEvent(observers, POST_WRITE_ROW);
135
registerEvent(observers, PRE_UPDATE_ROW);
136
registerEvent(observers, POST_UPDATE_ROW);
137
registerEvent(observers, PRE_DELETE_ROW);
138
registerEvent(observers, POST_DELETE_ROW);
141
//==================================
142
/* This is where I register which schema events my pluggin is interested in.*/
143
void HelloEvents::registerSchemaEvents(const std::string *db, EventObservers *observers)
146
|| !isDatabaseInteresting(db->c_str()))
149
registerEvent(observers, POST_DROP_TABLE);
150
registerEvent(observers, POST_RENAME_TABLE);
153
//==================================
154
/* This is where I register which session events my pluggin is interested in.*/
155
void HelloEvents::registerSessionEvents(Session *session, EventObservers *observers)
158
|| !isSessionInteresting(session))
161
registerEvent(observers, POST_CREATE_DATABASE);
162
registerEvent(observers, POST_DROP_DATABASE);
166
//==================================
167
/* The event observer.*/
168
bool HelloEvents::observeEvent(EventData *data)
174
case POST_DROP_TABLE:
175
postDropTable((PostDropTableEventData *)data);
178
case POST_RENAME_TABLE:
179
postRenameTable((PostRenameTableEventData *)data);
183
result = preWriteRow((PreWriteRowEventData *)data);
187
postWriteRow((PostWriteRowEventData *)data);
191
result = preUpdateRow((PreUpdateRowEventData *)data);
194
case POST_UPDATE_ROW:
195
postUpdateRow((PostUpdateRowEventData *)data);
199
result = preDeleteRow((PreDeleteRowEventData *)data);
202
case POST_DELETE_ROW:
203
postDeleteRow((PostDeleteRowEventData *)data);
206
case POST_CREATE_DATABASE:
207
postCreateDatabase((PostCreateDatabaseEventData *)data);
210
case POST_DROP_DATABASE:
211
postDropDatabase((PostDropDatabaseEventData *)data);
215
fprintf(stderr, "HelloEvents: Unexpected event '%s'\n", Event::eventName(data->event));
222
//==================================
223
// Some custom things for my plugin:
226
/* Plugin initialization and system variables */
227
static void enable(Session *,
234
if (*(bool *)save != false)
236
hello_events->enable();
237
*(bool *) var_ptr= (bool) true;
241
hello_events->disable();
242
*(bool *) var_ptr= (bool) false;
248
static void set_db_list(Session *,
255
hello_events->setDatabasesOfInterest(*(const char **) save);
256
*(const char **) var_ptr= hello_events->getDatabasesOfInterest();
260
static void set_table_list(Session *,
267
hello_events->setTablesOfInterest(*(const char **) save);
268
*(const char **) var_ptr= hello_events->getTablesOfInterest();
273
static int init(Context &context)
275
hello_events= new HelloEvents("hello_events");
277
context.add(hello_events);
279
if (sysvar_hello_events_enabled)
281
hello_events->enable();
287
static DRIZZLE_SYSVAR_STR(watch_databases,
290
N_("A comma delimited list of databases to watch"),
291
NULL, /* check func */
292
set_db_list, /* update func */
295
static DRIZZLE_SYSVAR_STR(watch_tables,
298
N_("A comma delimited list of tables to watch"),
299
NULL, /* check func */
300
set_table_list, /* update func */
303
static DRIZZLE_SYSVAR_BOOL(enable,
304
sysvar_hello_events_enabled,
306
N_("Enable Example Events Plugin"),
307
NULL, /* check func */
308
enable, /* update func */
309
false /* default */);
311
static drizzle_sys_var* system_var[]= {
312
DRIZZLE_SYSVAR(watch_databases),
313
DRIZZLE_SYSVAR(watch_tables),
314
DRIZZLE_SYSVAR(enable),
318
DRIZZLE_DECLARE_PLUGIN
324
N_("An example events Plugin"),
326
init, /* Plugin Init */
327
system_var, /* system variables */
328
NULL /* config options */
330
DRIZZLE_DECLARE_PLUGIN_END;