25
25
* This plugin is an example events plugin that just prints some info for
26
26
* the events that it is tracking.
28
set global hello_events1_enable = ON;
29
set global hello_events1_watch_databases = "x";
30
set global hello_events1_watch_tables = "x,y";
28
set global hello_events_enable = ON;
29
set global hello_events_watch_databases = "x";
30
set global hello_events_watch_tables = "x,y";
34
34
#include "config.h"
37
#include <boost/program_options.hpp>
38
#include <drizzled/module/option_map.h>
39
38
#include "drizzled/session.h"
40
39
#include "hello_events.h"
42
namespace po= boost::program_options;
43
41
using namespace drizzled;
44
42
using namespace plugin;
45
43
using namespace std;
47
45
#define PLUGIN_NAME "hello_events1"
49
static bool sysvar_hello_events_enabled;
47
static bool sysvar_hello_events_enabled= true;
50
48
static HelloEvents *hello_events= NULL;
51
static string sysvar_db_list;
52
static string sysvar_table_list;
49
static char *sysvar_table_list= NULL;
50
static char *sysvar_db_list= NULL;
55
53
* Event observer positions are used to set the order in which
65
63
* or the last position (-1) in the calling order, for example it makes no sence
66
64
* to initially ask to be called in position 13.
68
typedef constrained_check<uint64_t, INT32_MAX-1, 1> position_constraint;
69
typedef constrained_check<int32_t, -1, INT32_MIN+1> post_drop_constraint;
71
static position_constraint sysvar_before_write_position; // Call this event observer first.
72
static position_constraint sysvar_before_update_position;
73
static post_drop_constraint sysvar_post_drop_db_position; // I want my event observer to be called last. No reason, I just do!
66
static int32_t sysvar_before_write_position= 1; // Call this event observer first.
67
static int32_t sysvar_before_update_position= 1;
68
static int32_t sysvar_post_drop_db_position= -1; // I want my event observer to be called last. No reason, I just do!
76
71
//==================================
78
73
static bool observeBeforeInsertRecord(BeforeInsertRecordEventData &data)
83
data.session.setVariable("BEFORE_INSERT_RECORD", boost::lexical_cast<std::string>(count));
76
fprintf(stderr, PLUGIN_NAME" EVENT observeBeforeInsertRecord(%s)\n", data.table.getTableName());
88
81
static void observeAfterInsertRecord(AfterInsertRecordEventData &data)
92
data.session.setVariable("AFTER_INSERT_RECORD", boost::lexical_cast<std::string>(count));
83
fprintf(stderr, PLUGIN_NAME" EVENT observeAfterInsertRecord(%s) err = %d\n", data.table.getTableName(), data.err);
96
87
static bool observeBeforeDeleteRecord(BeforeDeleteRecordEventData &data)
100
data.session.setVariable("AFTER_DELETE_RECORD", boost::lexical_cast<std::string>(count));
89
fprintf(stderr, PLUGIN_NAME" EVENT observeBeforeDeleteRecord(%s)\n", data.table.getTableName());
105
94
static void observeAfterDeleteRecord(AfterDeleteRecordEventData &data)
109
data.session.setVariable("AFTER_DELETE_RECORD", boost::lexical_cast<std::string>(count));
96
fprintf(stderr, PLUGIN_NAME" EVENT observeAfterDeleteRecord(%s) err = %d\n", data.table.getTableName(), data.err);
113
100
static bool observeBeforeUpdateRecord(BeforeUpdateRecordEventData &data)
117
data.session.setVariable("BEFORE_UPDATE_RECORD", boost::lexical_cast<std::string>(count));
102
fprintf(stderr, PLUGIN_NAME" EVENT observeBeforeUpdateRecord(%s)\n", data.table.getTableName());
122
107
static void observeAfterUpdateRecord(AfterUpdateRecordEventData &data)
126
data.session.setVariable("AFTER_UPDATE_RECORD", boost::lexical_cast<std::string>(count));
109
fprintf(stderr, PLUGIN_NAME" EVENT observeAfterUpdateRecord(%s) err = %d\n", data.table.getTableName(), data.err);
129
112
//==================================
130
113
// My schema event observers:
131
114
static void observeAfterDropTable(AfterDropTableEventData &data)
135
data.session.setVariable("AFTER_DROP_TABLE", boost::lexical_cast<std::string>(count));
116
fprintf(stderr, PLUGIN_NAME" EVENT observeAfterDropTable(%s) err = %d\n", data.table.getTableName().c_str(), data.err);
139
120
static void observeAfterRenameTable(AfterRenameTableEventData &data)
143
data.session.setVariable("AFTER_RENAME_TABLE", boost::lexical_cast<std::string>(count));
122
fprintf(stderr, PLUGIN_NAME" EVENT observeAfterRenameTable(%s, %s) err = %d\n", data.from.getTableName().c_str(), data.to.getTableName().c_str(), data.err);
147
126
static void observeAfterCreateDatabase(AfterCreateDatabaseEventData &data)
151
data.session.setVariable("AFTER_CREATE_DATABASE", boost::lexical_cast<std::string>(count));
128
fprintf(stderr, PLUGIN_NAME" EVENT observeAfterCreateDatabase(%s) err = %d\n", data.db.c_str(), data.err);
155
132
static void observeAfterDropDatabase(AfterDropDatabaseEventData &data)
159
data.session.setVariable("AFTER_DROP_DATABASE", boost::lexical_cast<std::string>(count));
163
static void observeConnectSession(ConnectSessionEventData &data)
167
data.session.setVariable("CONNECT_SESSION", boost::lexical_cast<std::string>(count));
171
static void observeDisconnectSession(DisconnectSessionEventData &data)
175
data.session.setVariable("DISCONNECT_SESSION", boost::lexical_cast<std::string>(count));
179
static void observeBeforeStatement(BeforeStatementEventData &data)
183
data.session.setVariable("BEFORE_STATEMENT", boost::lexical_cast<std::string>(count));
187
static void observeAfterStatement(AfterStatementEventData &data)
191
data.session.setVariable("AFTER_STATEMENT", boost::lexical_cast<std::string>(count));
194
HelloEvents::~HelloEvents()
134
fprintf(stderr, PLUGIN_NAME" EVENT observeAfterDropDatabase(%s) err = %d\n", data.db.c_str(), data.err);
197
137
//==================================
198
138
/* This is where I register which table events my pluggin is interested in.*/
203
143
|| !isDatabaseInteresting(table_share.getSchemaName()))
206
registerEvent(observers, BEFORE_INSERT_RECORD, sysvar_before_write_position.get());
207
// I want to be called first if passible
146
registerEvent(observers, BEFORE_INSERT_RECORD, sysvar_before_write_position); // I want to be called first if passible
208
147
registerEvent(observers, AFTER_INSERT_RECORD);
209
registerEvent(observers, BEFORE_UPDATE_RECORD, sysvar_before_update_position.get());
148
registerEvent(observers, BEFORE_UPDATE_RECORD, sysvar_before_update_position);
210
149
registerEvent(observers, AFTER_UPDATE_RECORD);
211
150
registerEvent(observers, BEFORE_DELETE_RECORD);
212
151
registerEvent(observers, AFTER_DELETE_RECORD);
235
174
registerEvent(observers, AFTER_CREATE_DATABASE);
236
registerEvent(observers, AFTER_DROP_DATABASE, sysvar_post_drop_db_position.get());
237
registerEvent(observers, DISCONNECT_SESSION);
238
registerEvent(observers, CONNECT_SESSION);
239
registerEvent(observers, BEFORE_STATEMENT);
240
registerEvent(observers, AFTER_STATEMENT);
175
registerEvent(observers, AFTER_DROP_DATABASE, sysvar_post_drop_db_position);
319
238
/* Plugin initialization and system variables */
321
static void enable(Session*, sql_var_t)
240
static void enable(Session *,
323
245
if (hello_events)
325
if (sysvar_hello_events_enabled)
247
if (*(bool *)save != false)
327
249
hello_events->enable();
250
*(bool *) var_ptr= (bool) true;
331
254
hello_events->disable();
255
*(bool *) var_ptr= (bool) false;
337
static int set_db_list(Session *, set_var *var)
261
static void set_db_list(Session *,
339
const char *db_list= var->value->str_value.ptr();
343
266
if (hello_events)
345
hello_events->setDatabasesOfInterest(db_list);
346
sysvar_db_list.assign(db_list);
268
hello_events->setDatabasesOfInterest(*(const char **) save);
269
*(const char **) var_ptr= hello_events->getDatabasesOfInterest();
351
static int set_table_list(Session *, set_var *var)
273
static void set_table_list(Session *,
353
const char *table_list= var->value->str_value.ptr();
354
if (table_list == NULL)
357
278
if (hello_events)
359
hello_events->setTablesOfInterest(table_list);
360
sysvar_table_list.assign(table_list);
280
hello_events->setTablesOfInterest(*(const char **) save);
281
*(const char **) var_ptr= hello_events->getTablesOfInterest();
374
294
hello_events->enable();
377
context.registerVariable(new sys_var_bool_ptr("enable",
378
&sysvar_hello_events_enabled,
380
context.registerVariable(new sys_var_std_string("watch_databases",
383
context.registerVariable(new sys_var_std_string("watch_tables",
386
context.registerVariable(new sys_var_constrained_value<uint64_t>("before_write_position",
387
sysvar_before_write_position));
388
context.registerVariable(new sys_var_constrained_value<uint64_t>("before_update_position",
389
sysvar_before_update_position));
390
context.registerVariable(new sys_var_constrained_value<int32_t>("post_drop_position",
391
sysvar_post_drop_db_position));
397
static void init_options(drizzled::module::option_context &context)
400
po::value<bool>(&sysvar_hello_events_enabled)->default_value(false)->zero_tokens(),
401
N_("Enable Example Events Plugin"));
402
context("watch-databases",
403
po::value<string>(&sysvar_db_list)->default_value(""),
404
N_("A comma delimited list of databases to watch"));
405
context("watch-tables",
406
po::value<string>(&sysvar_table_list)->default_value(""),
407
N_("A comma delimited list of databases to watch"));
408
context("before-write-position",
409
po::value<position_constraint>(&sysvar_before_write_position)->default_value(1),
410
N_("Before write row event observer call position"));
411
context("before-update-position",
412
po::value<position_constraint>(&sysvar_before_update_position)->default_value(1),
413
N_("Before update row event observer call position"));
414
context("post-drop-db-position",
415
po::value<post_drop_constraint>(&sysvar_post_drop_db_position)->default_value(-1),
416
N_("After drop database event observer call position"));
300
static DRIZZLE_SYSVAR_STR(watch_databases,
303
N_("A comma delimited list of databases to watch"),
304
NULL, /* check func */
305
set_db_list, /* update func */
308
static DRIZZLE_SYSVAR_STR(watch_tables,
311
N_("A comma delimited list of tables to watch"),
312
NULL, /* check func */
313
set_table_list, /* update func */
316
static DRIZZLE_SYSVAR_BOOL(enable,
317
sysvar_hello_events_enabled,
319
N_("Enable Example Events Plugin"),
320
NULL, /* check func */
321
enable, /* update func */
322
false /* default */);
324
static DRIZZLE_SYSVAR_INT(before_write_position,
325
sysvar_before_write_position,
327
N_("Before write row event observer call position"),
328
NULL, /* check func */
329
NULL, /* update func */
332
INT32_MAX -1, /* max */
335
static DRIZZLE_SYSVAR_INT(before_update_position,
336
sysvar_before_update_position,
338
N_("Before update row event observer call position"),
339
NULL, /* check func */
340
NULL, /* update func */
343
INT32_MAX -1, /* max */
346
static DRIZZLE_SYSVAR_INT(post_drop_db_position,
347
sysvar_post_drop_db_position,
349
N_("After drop database event observer call position"),
350
NULL, /* check func */
351
NULL, /* update func */
353
INT32_MIN +1, /* min */
357
static drizzle_sys_var* system_var[]= {
358
DRIZZLE_SYSVAR(watch_databases),
359
DRIZZLE_SYSVAR(watch_tables),
360
DRIZZLE_SYSVAR(enable),
361
DRIZZLE_SYSVAR(before_write_position),
362
DRIZZLE_SYSVAR(before_update_position),
363
DRIZZLE_SYSVAR(post_drop_db_position),
421
367
DRIZZLE_DECLARE_PLUGIN