~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/hello_events/hello_events.cc

  • Committer: Barry.Leslie at PrimeBase
  • Date: 2010-04-23 20:58:15 UTC
  • mto: This revision was merged to the branch mainline in revision 1544.
  • Revision ID: barry.leslie@primebase.com-20100423205815-xo2dm2x14wegz1gm
Added a demo plugin based on the the events plugin API.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2010, Joseph Daly <skinny.moey@gmail.com>
 
3
 * All rights reserved.
 
4
 *
 
5
 * Redistribution and use in source and binary forms, with or without
 
6
 * modification, are permitted provided that the following conditions are met:
 
7
 *
 
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.
 
16
 *
 
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.
 
28
 */
 
29
 
 
30
/**
 
31
 * @details
 
32
 *
 
33
 * This plugin is an example events plugin that just prints some info for
 
34
 * the events that it is tracking. 
 
35
 *  
 
36
set global hello_events_enable = ON;
 
37
set global hello_events_watch_databases = "x";   
 
38
set global hello_events_watch_tables = "x,y";
 
39
 
 
40
 */
 
41
 
 
42
#include <string>
 
43
 
 
44
#include "config.h"
 
45
#include "drizzled/session.h"
 
46
#include "hello_events.h"
 
47
 
 
48
using namespace drizzled;
 
49
using namespace plugin;
 
50
using namespace std;
 
51
 
 
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;
 
56
 
 
57
 
 
58
//==================================
 
59
// My table event observers: 
 
60
static bool preWriteRow(PreWriteRowEventData *data)
 
61
{
 
62
 
 
63
  fprintf(stderr, "EVENT preWriteRow(%s)\n", data->table->getTableName());
 
64
  return false;
 
65
}
 
66
 
 
67
//---
 
68
static void postWriteRow(PostWriteRowEventData *data)
 
69
{
 
70
  fprintf(stderr, "EVENT postWriteRow(%s) err = %d\n", data->table->getTableName(), data->err);
 
71
}
 
72
 
 
73
//---
 
74
static bool preDeleteRow(PreDeleteRowEventData *data)
 
75
{
 
76
  fprintf(stderr, "EVENT preDeleteRow(%s)\n", data->table->getTableName());
 
77
  return false;
 
78
}
 
79
 
 
80
//---
 
81
static void postDeleteRow(PostDeleteRowEventData *data)
 
82
{
 
83
  fprintf(stderr, "EVENT postDeleteRow(%s) err = %d\n", data->table->getTableName(), data->err);
 
84
}
 
85
 
 
86
//---
 
87
static bool preUpdateRow(PreUpdateRowEventData *data)
 
88
{
 
89
  fprintf(stderr, "EVENT preUpdateRow(%s)\n", data->table->getTableName());
 
90
  return false;
 
91
}
 
92
 
 
93
//---
 
94
static void postUpdateRow(PostUpdateRowEventData *data)
 
95
{
 
96
  fprintf(stderr, "EVENT postUpdateRow(%s) err = %d\n", data->table->getTableName(), data->err);
 
97
}
 
98
 
 
99
//==================================
 
100
// My schema event observers: 
 
101
static void postDropTable(PostDropTableEventData *data)
 
102
{
 
103
  fprintf(stderr, "EVENT postDropTable(%s) err = %d\n", data->table->getTableName().c_str(), data->err);
 
104
}
 
105
 
 
106
//---
 
107
static void postRenameTable(PostRenameTableEventData *data)
 
108
{
 
109
  fprintf(stderr, "EVENT postRenameTable(%s, %s) err = %d\n", data->from->getTableName().c_str(), data->to->getTableName().c_str(), data->err);
 
110
}
 
111
 
 
112
//---
 
113
static void postCreateDatabase(PostCreateDatabaseEventData *data)
 
114
{
 
115
  fprintf(stderr, "EVENT postCreateDatabase(%s) err = %d\n", data->db, data->err);
 
116
}
 
117
 
 
118
//---
 
119
static void postDropDatabase(PostDropDatabaseEventData *data)
 
120
{
 
121
  fprintf(stderr, "EVENT postDropDatabase(%s) err = %d\n", data->db, data->err);
 
122
}
 
123
 
 
124
//==================================
 
125
/* This is where I register which table events my pluggin is interested in.*/
 
126
void HelloEvents::registerTableEvents(TableShare *table_share, EventObservers *observers)
 
127
{
 
128
  if ((!is_enabled) || (table_share == NULL) 
 
129
    || !isTableInteresting(table_share->getTableName())
 
130
    || !isDatabaseInteresting(table_share->getSchemaName()))
 
131
    return;
 
132
    
 
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);
 
139
}
 
140
 
 
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)
 
144
{
 
145
  if ((!is_enabled) 
 
146
    || !isDatabaseInteresting(db->c_str()))
 
147
    return;
 
148
    
 
149
  registerEvent(observers, POST_DROP_TABLE);
 
150
  registerEvent(observers, POST_RENAME_TABLE);
 
151
}
 
152
 
 
153
//==================================
 
154
/* This is where I register which session events my pluggin is interested in.*/
 
155
void HelloEvents::registerSessionEvents(Session *session, EventObservers *observers)
 
156
{
 
157
  if ((!is_enabled) 
 
158
    || !isSessionInteresting(session))
 
159
    return;
 
160
    
 
161
  registerEvent(observers, POST_CREATE_DATABASE);
 
162
  registerEvent(observers, POST_DROP_DATABASE);
 
163
}
 
164
 
 
165
 
 
166
//==================================
 
167
/* The event observer.*/
 
168
bool HelloEvents::observeEvent(EventData *data)
 
169
{
 
170
  bool result = false;
 
171
  
 
172
  switch (data->event)
 
173
  {
 
174
      case POST_DROP_TABLE:
 
175
        postDropTable((PostDropTableEventData *)data);
 
176
        break;
 
177
        
 
178
      case POST_RENAME_TABLE:
 
179
        postRenameTable((PostRenameTableEventData *)data);
 
180
        break;
 
181
        
 
182
      case PRE_WRITE_ROW:
 
183
         result = preWriteRow((PreWriteRowEventData *)data);
 
184
        break;
 
185
        
 
186
      case POST_WRITE_ROW:
 
187
        postWriteRow((PostWriteRowEventData *)data);
 
188
        break;     
 
189
           
 
190
      case PRE_UPDATE_ROW:
 
191
        result = preUpdateRow((PreUpdateRowEventData *)data);
 
192
        break;
 
193
                 
 
194
      case POST_UPDATE_ROW:
 
195
         postUpdateRow((PostUpdateRowEventData *)data);
 
196
        break;     
 
197
        
 
198
      case PRE_DELETE_ROW:
 
199
        result = preDeleteRow((PreDeleteRowEventData *)data);
 
200
        break;
 
201
 
 
202
      case POST_DELETE_ROW:
 
203
        postDeleteRow((PostDeleteRowEventData *)data);
 
204
        break;
 
205
 
 
206
      case POST_CREATE_DATABASE:
 
207
        postCreateDatabase((PostCreateDatabaseEventData *)data);
 
208
        break;
 
209
 
 
210
      case POST_DROP_DATABASE:
 
211
        postDropDatabase((PostDropDatabaseEventData *)data);
 
212
        break;
 
213
 
 
214
      default:
 
215
        fprintf(stderr, "HelloEvents: Unexpected event '%s'\n", Event::eventName(data->event));
 
216
 
 
217
  }
 
218
  
 
219
  return false;
 
220
}
 
221
 
 
222
//==================================
 
223
// Some custom things for my plugin:
 
224
 
 
225
 
 
226
/* Plugin initialization and system variables */
 
227
static void enable(Session *,
 
228
                   drizzle_sys_var *,
 
229
                   void *var_ptr,
 
230
                   const void *save)
 
231
{
 
232
  if (hello_events)
 
233
  {
 
234
    if (*(bool *)save != false)
 
235
    {
 
236
      hello_events->enable();
 
237
      *(bool *) var_ptr= (bool) true;
 
238
    }
 
239
    else
 
240
    {
 
241
      hello_events->disable();
 
242
      *(bool *) var_ptr= (bool) false;
 
243
    }
 
244
  }
 
245
}
 
246
 
 
247
 
 
248
static void set_db_list(Session *,
 
249
                   drizzle_sys_var *, 
 
250
                   void *var_ptr,     
 
251
                   const void *save)
 
252
{
 
253
  if (hello_events)
 
254
  {
 
255
    hello_events->setDatabasesOfInterest(*(const char **) save);
 
256
    *(const char **) var_ptr= hello_events->getDatabasesOfInterest();
 
257
  }
 
258
}
 
259
 
 
260
static void set_table_list(Session *,
 
261
                   drizzle_sys_var *, 
 
262
                   void *var_ptr,     
 
263
                   const void *save)
 
264
{
 
265
  if (hello_events)
 
266
  {
 
267
    hello_events->setTablesOfInterest(*(const char **) save);
 
268
    *(const char **) var_ptr= hello_events->getTablesOfInterest();
 
269
  }
 
270
}
 
271
 
 
272
 
 
273
static int init(Context &context)
 
274
{
 
275
  hello_events= new HelloEvents("hello_events");
 
276
 
 
277
  context.add(hello_events);
 
278
 
 
279
  if (sysvar_hello_events_enabled)
 
280
  {
 
281
    hello_events->enable();
 
282
  }
 
283
 
 
284
  return 0;
 
285
}
 
286
 
 
287
static DRIZZLE_SYSVAR_STR(watch_databases,
 
288
                           sysvar_db_list,
 
289
                           PLUGIN_VAR_OPCMDARG,
 
290
                           N_("A comma delimited list of databases to watch"),
 
291
                           NULL, /* check func */
 
292
                           set_db_list, /* update func */
 
293
                           "" /* default */);
 
294
 
 
295
static DRIZZLE_SYSVAR_STR(watch_tables,
 
296
                           sysvar_table_list,
 
297
                           PLUGIN_VAR_OPCMDARG,
 
298
                           N_("A comma delimited list of tables to watch"),
 
299
                           NULL, /* check func */
 
300
                           set_table_list, /* update func */
 
301
                           "" /* default */);
 
302
 
 
303
static DRIZZLE_SYSVAR_BOOL(enable,
 
304
                           sysvar_hello_events_enabled,
 
305
                           PLUGIN_VAR_NOCMDARG,
 
306
                           N_("Enable Example Events Plugin"),
 
307
                           NULL, /* check func */
 
308
                           enable, /* update func */
 
309
                           false /* default */);
 
310
 
 
311
static drizzle_sys_var* system_var[]= {
 
312
  DRIZZLE_SYSVAR(watch_databases),
 
313
  DRIZZLE_SYSVAR(watch_tables),
 
314
  DRIZZLE_SYSVAR(enable),
 
315
  NULL
 
316
};
 
317
 
 
318
DRIZZLE_DECLARE_PLUGIN
 
319
{
 
320
  DRIZZLE_VERSION_ID,
 
321
  "hello_events",
 
322
  "0.1",
 
323
  "Barry Leslie",
 
324
  N_("An example events Plugin"),
 
325
  PLUGIN_LICENSE_BSD,
 
326
  init,   /* Plugin Init      */
 
327
  system_var, /* system variables */
 
328
  NULL    /* config options   */
 
329
}
 
330
DRIZZLE_DECLARE_PLUGIN_END;