~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
 *  Copyright (C) 2010 PrimeBase Technologies GmbH, Germany
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Barry Leslie
 *
 * 2010-05-12
 */

/**
 * @details
 *
 * This plugin is an example events plugin that just prints some info for
 * the events that it is tracking. 
 *  
set global hello_events1_enable = ON;
set global hello_events1_watch_databases = "x";   
set global hello_events1_watch_tables = "x,y";

 */

#include <config.h>
#include <string>
#include <cstdio>
#include <boost/program_options.hpp>
#include <drizzled/item.h>
#include <drizzled/module/option_map.h>
#include <drizzled/session.h>
#include <drizzled/table/instance/base.h>
#include "hello_events.h"
#include <drizzled/plugin.h>

namespace po= boost::program_options;
using namespace drizzled;
using namespace plugin;
using namespace std;

#define PLUGIN_NAME "hello_events1"

static bool sysvar_hello_events_enabled;
static HelloEvents *hello_events= NULL;
static string sysvar_db_list;
static string sysvar_table_list;

/*
 * Event observer positions are used to set the order in which
 * event observers are called in the case that more than one
 * plugin is interested in the same event. You should only specify
 * the order if it really matters because if more than one plugin 
 * request the same calling position only the first one gets it and
 * the others will not be registered for the event. For this reason
 * your plugin should always provide a way to reposition the event
 * observer to resolve such conflicts.
 *
 * If position matters you will always initialy ask for the first position (1)
 * or the last position (-1) in the calling order, for example it makes no sence 
 * to initially ask to be called in position 13.
 */
typedef constrained_check<uint64_t, INT32_MAX-1, 1> position_constraint;
typedef constrained_check<int32_t, -1, INT32_MIN+1> post_drop_constraint;

static position_constraint sysvar_before_write_position;      // Call this event observer first.
static position_constraint sysvar_before_update_position;
static post_drop_constraint sysvar_post_drop_db_position;  // I want my event observer to be called last. No reason, I just do!


//==================================
// My table event observers: 
static bool observeBeforeInsertRecord(BeforeInsertRecordEventData &data)
{

  static int count= 0;
  count++;
  data.session.setVariable("BEFORE_INSERT_RECORD", boost::lexical_cast<std::string>(count));
  return false;
}

//---
static void observeAfterInsertRecord(AfterInsertRecordEventData &data)
{
  static int count= 0;
  count++;
  data.session.setVariable("AFTER_INSERT_RECORD", boost::lexical_cast<std::string>(count));
}

//---
static bool observeBeforeDeleteRecord(BeforeDeleteRecordEventData &data)
{
  static int count= 0;
  count++;
  data.session.setVariable("AFTER_DELETE_RECORD", boost::lexical_cast<std::string>(count));
  return false;
}

//---
static void observeAfterDeleteRecord(AfterDeleteRecordEventData &data)
{
  static int count= 0;
  count++;
  data.session.setVariable("AFTER_DELETE_RECORD", boost::lexical_cast<std::string>(count));
}

//---
static bool observeBeforeUpdateRecord(BeforeUpdateRecordEventData &data)
{
  static int count= 0;
  count++;
  data.session.setVariable("BEFORE_UPDATE_RECORD", boost::lexical_cast<std::string>(count));
  return false;
}

//---
static void observeAfterUpdateRecord(AfterUpdateRecordEventData &data)
{
  static int count= 0;
  count++;
  data.session.setVariable("AFTER_UPDATE_RECORD", boost::lexical_cast<std::string>(count));
}

//==================================
// My schema event observers: 
static void observeAfterDropTable(AfterDropTableEventData &data)
{
  static int count= 0;
  count++;
  data.session.setVariable("AFTER_DROP_TABLE", boost::lexical_cast<std::string>(count));
}

//---
static void observeAfterRenameTable(AfterRenameTableEventData &data)
{
  static int count= 0;
  count++;
  data.session.setVariable("AFTER_RENAME_TABLE", boost::lexical_cast<std::string>(count));
}

//---
static void observeAfterCreateDatabase(AfterCreateDatabaseEventData &data)
{
  static int count= 0;
  count++;
  data.session.setVariable("AFTER_CREATE_DATABASE", boost::lexical_cast<std::string>(count));
}

//---
static void observeAfterDropDatabase(AfterDropDatabaseEventData &data)
{
  static int count= 0;
  count++;
  data.session.setVariable("AFTER_DROP_DATABASE", boost::lexical_cast<std::string>(count));
}

//---
static void observeConnectSession(ConnectSessionEventData &data)
{
  static int count= 0;
  count++;
  data.session.setVariable("CONNECT_SESSION", boost::lexical_cast<std::string>(count));
}

//---
static void observeDisconnectSession(DisconnectSessionEventData &data)
{
  static int count= 0;
  count++;
  data.session.setVariable("DISCONNECT_SESSION", boost::lexical_cast<std::string>(count));
}

//---
static void observeBeforeStatement(BeforeStatementEventData &data)
{
  static int count= 0;
  count++;
  data.session.setVariable("BEFORE_STATEMENT", boost::lexical_cast<std::string>(count));
}

//---
static void observeAfterStatement(AfterStatementEventData &data)
{
  static int count= 0;
  count++;
  data.session.setVariable("AFTER_STATEMENT", boost::lexical_cast<std::string>(count));
}

HelloEvents::~HelloEvents()
{ }

//==================================
/* This is where I register which table events my pluggin is interested in.*/
void HelloEvents::registerTableEventsDo(TableShare &table_share, EventObserverList &observers)
{
  if ((is_enabled == false) 
    || !isTableInteresting(table_share.getTableName())
    || !isDatabaseInteresting(table_share.getSchemaName()))
    return;
    
  registerEvent(observers, BEFORE_INSERT_RECORD, sysvar_before_write_position.get());
  // I want to be called first if passible
  registerEvent(observers, AFTER_INSERT_RECORD);
  registerEvent(observers, BEFORE_UPDATE_RECORD, sysvar_before_update_position.get());
  registerEvent(observers, AFTER_UPDATE_RECORD);
  registerEvent(observers, BEFORE_DELETE_RECORD);
  registerEvent(observers, AFTER_DELETE_RECORD);
}

//==================================
/* This is where I register which schema events my pluggin is interested in.*/
void HelloEvents::registerSchemaEventsDo(const std::string &db, EventObserverList &observers)
{
  if ((is_enabled == false) 
    || !isDatabaseInteresting(db))
    return;
    
  registerEvent(observers, AFTER_DROP_TABLE);
  registerEvent(observers, AFTER_RENAME_TABLE);
}

//==================================
/* This is where I register which session events my pluggin is interested in.*/
void HelloEvents::registerSessionEventsDo(Session &session, EventObserverList &observers)
{
  if ((is_enabled == false) 
    || !isSessionInteresting(session))
    return;
    
  registerEvent(observers, AFTER_CREATE_DATABASE);
  registerEvent(observers, AFTER_DROP_DATABASE, sysvar_post_drop_db_position.get());
  registerEvent(observers, DISCONNECT_SESSION);
  registerEvent(observers, CONNECT_SESSION);
  registerEvent(observers, BEFORE_STATEMENT);
  registerEvent(observers, AFTER_STATEMENT);
}


//==================================
/* The event observer.*/
bool HelloEvents::observeEventDo(EventData &data)
{
  switch (data.event) {
  case AFTER_DROP_TABLE:
    observeAfterDropTable((AfterDropTableEventData &)data);
    break;
    
  case AFTER_RENAME_TABLE:
    observeAfterRenameTable((AfterRenameTableEventData &)data);
    break;
    
  case BEFORE_INSERT_RECORD:
    observeBeforeInsertRecord((BeforeInsertRecordEventData &)data);
    break;
    
  case AFTER_INSERT_RECORD:
    observeAfterInsertRecord((AfterInsertRecordEventData &)data);
    break;     
       
  case BEFORE_UPDATE_RECORD:
    observeBeforeUpdateRecord((BeforeUpdateRecordEventData &)data);
    break;
             
  case AFTER_UPDATE_RECORD:
     observeAfterUpdateRecord((AfterUpdateRecordEventData &)data);
    break;     
    
  case BEFORE_DELETE_RECORD:
    observeBeforeDeleteRecord((BeforeDeleteRecordEventData &)data);
    break;

  case AFTER_DELETE_RECORD:
    observeAfterDeleteRecord((AfterDeleteRecordEventData &)data);
    break;

  case AFTER_CREATE_DATABASE:
    observeAfterCreateDatabase((AfterCreateDatabaseEventData &)data);
    break;

  case AFTER_DROP_DATABASE:
    observeAfterDropDatabase((AfterDropDatabaseEventData &)data);
    break;

  case CONNECT_SESSION:
    observeConnectSession((ConnectSessionEventData &)data);
    break;

  case DISCONNECT_SESSION:
    observeDisconnectSession((DisconnectSessionEventData &)data);
    break;

  case BEFORE_STATEMENT:
    observeBeforeStatement((BeforeStatementEventData &)data);
    break;

  case AFTER_STATEMENT:
    observeAfterStatement((AfterStatementEventData &)data);
    break;

  default:
    fprintf(stderr, "HelloEvents: Unexpected event '%s'\n", EventObserver::eventName(data.event));
 
  }
  
  return false;
}

//==================================
// Some custom things for my plugin:


/* Plugin initialization and system variables */

static void enable(Session*, sql_var_t)
{
  if (hello_events)
  {
    if (sysvar_hello_events_enabled)
    {
      hello_events->enable();
    }
    else
    {
      hello_events->disable();
    }
  }
}


static int set_db_list(Session *, set_var *var)
{
  const char *db_list= var->value->str_value.ptr();
  if (db_list == NULL)
    return 1;

  if (hello_events)
  {
    hello_events->setDatabasesOfInterest(db_list);
    sysvar_db_list= db_list;
  }
  return 0;
}

static int set_table_list(Session *, set_var *var)
{
  const char *table_list= var->value->str_value.ptr();
  if (table_list == NULL)
    return 1;

  if (hello_events)
  {
    hello_events->setTablesOfInterest(table_list);
    sysvar_table_list= table_list;
  }
  return 0;
}


static int init(module::Context &context)
{
  hello_events= new HelloEvents(PLUGIN_NAME);

  context.add(hello_events);

  if (sysvar_hello_events_enabled)
  {
    hello_events->enable();
  }

  context.registerVariable(new sys_var_bool_ptr("enable",
                                                &sysvar_hello_events_enabled,
                                                enable));
  context.registerVariable(new sys_var_std_string("watch_databases",
                                                  sysvar_db_list,
                                                  set_db_list));
  context.registerVariable(new sys_var_std_string("watch_tables",
                                                  sysvar_table_list,
                                                  set_table_list));
  context.registerVariable(new sys_var_constrained_value<uint64_t>("before_write_position",
                                                         sysvar_before_write_position));
  context.registerVariable(new sys_var_constrained_value<uint64_t>("before_update_position",
                                                         sysvar_before_update_position));
  context.registerVariable(new sys_var_constrained_value<int32_t>("post_drop_position",
                                                         sysvar_post_drop_db_position));


  return 0;
}

static void init_options(drizzled::module::option_context &context)
{
  context("enable",
          po::value<bool>(&sysvar_hello_events_enabled)->default_value(false)->zero_tokens(),
          N_("Enable Example Events Plugin"));
  context("watch-databases",
          po::value<string>(&sysvar_db_list)->default_value(""),
          N_("A comma delimited list of databases to watch"));
  context("watch-tables",
          po::value<string>(&sysvar_table_list)->default_value(""),
          N_("A comma delimited list of databases to watch"));
  context("before-write-position",
          po::value<position_constraint>(&sysvar_before_write_position)->default_value(1),
          N_("Before write row event observer call position"));
  context("before-update-position",
          po::value<position_constraint>(&sysvar_before_update_position)->default_value(1),
          N_("Before update row event observer call position"));
  context("post-drop-db-position",
          po::value<post_drop_constraint>(&sysvar_post_drop_db_position)->default_value(-1),
          N_("After drop database event observer call position"));
}



DRIZZLE_DECLARE_PLUGIN
{
  DRIZZLE_VERSION_ID,
  PLUGIN_NAME,
  "0.1",
  "Barry Leslie",
  N_("An example events Plugin"),
  PLUGIN_LICENSE_BSD,
  init,   /* Plugin Init      */
  NULL, /* depends */
  init_options    /* config options   */
}
DRIZZLE_DECLARE_PLUGIN_END;