~drizzle-trunk/drizzle/development

1502.5.3 by Barry.Leslie at PrimeBase
Added the source code for the events plugin.
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
1502.5.7 by Barry.Leslie at PrimeBase
Renamed the 'Event' plugin to 'EventObserver' plugin along with some internal class renames to make things clearer.
4
 *  Copyright (C) 2010 PrimeBase Technologies GmbH, Germany
1502.5.3 by Barry.Leslie at PrimeBase
Added the source code for the events plugin.
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; version 2 of the License.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License
16
 *  along with this program; if not, write to the Free Software
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1502.5.9 by Barry.Leslie at PrimeBase
- Cleaned up some style points.
18
 *
19
 * Barry Leslie
20
 *
21
 * 2010-05-12
1502.5.3 by Barry.Leslie at PrimeBase
Added the source code for the events plugin.
22
 */
23
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
24
#include <config.h>
1502.5.6 by Barry.Leslie at PrimeBase
Fixed a compiler error due to an uninitialized variavble and cleaned up some coding standard stuff.
25
1502.5.3 by Barry.Leslie at PrimeBase
Added the source code for the events plugin.
26
#include <string>
27
#include <vector>
28
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
29
#include <drizzled/session.h>
30
#include <drizzled/table_list.h>
31
#include <drizzled/table/instance.h>
32
#include <drizzled/module/registry.h>
33
#include <drizzled/plugin/event_observer.h>
1502.5.9 by Barry.Leslie at PrimeBase
- Cleaned up some style points.
34
#include <drizzled/util/functors.h>
35
#include <algorithm>
36
1502.5.3 by Barry.Leslie at PrimeBase
Added the source code for the events plugin.
37
38
39
namespace drizzled
40
{
41
42
namespace plugin
43
{
44
1502.5.8 by Barry.Leslie at PrimeBase
- Changed names to match the drizzle naming convention.
45
/*============================*/
1593 by Brian Aker
Merge in Barry.
46
  // Basic plugin registration stuff.
1836.4.2 by Brian Aker
More testing around events.
47
  EventObserverVector all_event_plugins;
48
49
  const EventObserverVector &EventObserver::getEventObservers(void)
50
  {
51
    return all_event_plugins;
52
  }
1593 by Brian Aker
Merge in Barry.
53
54
  //---------
55
  bool EventObserver::addPlugin(EventObserver *handler)
56
  {
57
    if (handler != NULL)
58
      all_event_plugins.push_back(handler);
59
    return false;
60
  }
61
62
  //---------
63
  void EventObserver::removePlugin(EventObserver *handler)
64
  {
65
    if (handler != NULL)
1966.2.13 by Brian Aker
Fix for solaris find for std::find.
66
      all_event_plugins.erase(std::find(all_event_plugins.begin(), all_event_plugins.end(), handler));
1593 by Brian Aker
Merge in Barry.
67
  }
68
69
70
  /* 
71
   * The Event Observer list class in which plugins register which events they
72
   * are interested in.
73
   *
74
   * Each table share for example, will have one of these hung on it to store
75
   * a list off all event observers interested in it and which events they are
76
   * interested in.
1502.5.3 by Barry.Leslie at PrimeBase
Added the source code for the events plugin.
77
 */
1593 by Brian Aker
Merge in Barry.
78
  class EventObserverList
1502.5.8 by Barry.Leslie at PrimeBase
- Changed names to match the drizzle naming convention.
79
  {
1593 by Brian Aker
Merge in Barry.
80
81
  public:
1966.2.9 by Brian Aker
Remove the use of "using std" from the plugin interface .cc files.
82
    typedef std::multimap<uint32_t, EventObserver *> ObserverMap;
1593 by Brian Aker
Merge in Barry.
83
84
  private:
85
    /* A list of lists indexed by event type. */
1966.2.9 by Brian Aker
Remove the use of "using std" from the plugin interface .cc files.
86
    std::vector<ObserverMap *> event_observer_lists;
1593 by Brian Aker
Merge in Barry.
87
88
  public:
89
90
    EventObserverList()
1502.5.8 by Barry.Leslie at PrimeBase
- Changed names to match the drizzle naming convention.
91
    {
1841.1.1 by Barry.Leslie at PrimeBase
Bug fix for memory leak in event_observer plugin.
92
			// Initialize the list with NULL pointers.
93
			event_observer_lists.assign(EventObserver::MAX_EVENT_COUNT, NULL);
1593 by Brian Aker
Merge in Barry.
94
    }
95
96
    ~EventObserverList()
97
    {
1966.2.14 by Brian Aker
Merge in some additional std namespace finds.
98
      std::for_each(event_observer_lists.begin(),
99
                    event_observer_lists.end(),
2207.6.1 by Olaf van der Spek
SafeDeletePtr isn't any safer
100
                    DeletePtr());
1966.2.14 by Brian Aker
Merge in some additional std namespace finds.
101
      event_observer_lists.clear();
1593 by Brian Aker
Merge in Barry.
102
    }
103
104
    /* Add the observer to the observer list for the even, positioning it if required.
105
     *
106
     * Note: Event observers are storted in a multimap object so that the order in which
107
     * they are called can be sorted based on the requested position. Lookups are never done
108
     * on the multimap, once filled it is used as a vector.
109
   */
110
    void addObserver(EventObserver *eventObserver, enum EventObserver::EventType event, int32_t position)
111
    {
112
      uint32_t event_pos;
113
      ObserverMap *observers;
114
115
      observers= event_observer_lists[event];
116
      if (observers == NULL) 
117
      {
118
        observers= new ObserverMap();
119
        event_observer_lists[event]= observers;
120
      }
121
122
      if (position == 0)
123
        event_pos= INT32_MAX; // Set the event position to be in the middle.
124
      else
125
        event_pos= (uint32_t) position;
126
127
      /* If positioned then check if the position is already taken. */
128
      if (position) 
129
      {
2192.5.2 by Olaf van der Spek
Use map::count
130
        if (observers->count(event_pos))
1593 by Brian Aker
Merge in Barry.
131
        {
2126.3.3 by Brian Aker
Merge in error message rework. Many error messages are fixed in this patch.
132
          errmsg_printf(error::WARN,
1593 by Brian Aker
Merge in Barry.
133
                        _("EventObserverList::addEventObserver() Duplicate event position %d for event '%s' from EventObserver plugin '%s'"),
134
                        position,
135
                        EventObserver::eventName(event), 
136
                        eventObserver->getName().c_str());
137
        }
138
      }
139
1966.2.9 by Brian Aker
Remove the use of "using std" from the plugin interface .cc files.
140
      observers->insert(std::pair<uint32_t, EventObserver *>(event_pos, eventObserver) );
1593 by Brian Aker
Merge in Barry.
141
    }
142
143
144
    /* Get the observer list for an event type. Will return NULL if no observer exists.*/
145
    ObserverMap *getObservers(enum EventObserver::EventType event)
146
    {
147
      return event_observer_lists[event];
148
    }
149
  };
150
151
152
  //---------
153
  /* registerEvent() is called from the event observer plugins to add themselves to
154
   * the event observer list to be notified when the specified event takes place.
155
   */ 
156
  void EventObserver::registerEvent(EventObserverList &observers, EventType event, int32_t position)
157
  {
158
    observers.addObserver(this, event, position);
159
  }
160
161
  /*========================================================*/
162
  /*              Table Event Observer handling:           */
163
  /*========================================================*/
164
165
  //----------
166
  /* For each EventObserver plugin call its registerTableEventsDo() meathod so that it can
167
   * register what events, if any, it is interested in on this table.
168
   */ 
1966.2.9 by Brian Aker
Remove the use of "using std" from the plugin interface .cc files.
169
  class RegisterTableEventsIterate : public std::unary_function<EventObserver *, void>
1593 by Brian Aker
Merge in Barry.
170
  {
171
    TableShare &table_share;
172
    EventObserverList &observers;
173
174
  public:
175
    RegisterTableEventsIterate(TableShare &table_share_arg, EventObserverList &observers_arg): 
176
      table_share(table_share_arg), observers(observers_arg) {}
177
    inline result_type operator() (argument_type eventObserver)
178
    {
179
      eventObserver->registerTableEventsDo(table_share, observers);
180
    }
181
  };
182
183
  //----------
184
  /* 
185
   * registerTableEvents() is called by drizzle to register all plugins that
186
   * may be interested in table events on the newly created TableShare object.
187
   */ 
188
  void EventObserver::registerTableEvents(TableShare &table_share)
189
  {
190
    if (all_event_plugins.empty())
191
      return;
192
193
    EventObserverList *observers;
194
195
    observers= table_share.getTableObservers();
196
1841.1.1 by Barry.Leslie at PrimeBase
Bug fix for memory leak in event_observer plugin.
197
    if (observers != NULL) 
198
		{
2126.3.3 by Brian Aker
Merge in error message rework. Many error messages are fixed in this patch.
199
			errmsg_printf(error::WARN,
1841.1.1 by Barry.Leslie at PrimeBase
Bug fix for memory leak in event_observer plugin.
200
									_("EventObserver::registerTableEvents(): Table already has events registered on it: probable programming error."));
201
			table_share.setTableObservers(NULL);
202
      delete observers;
1593 by Brian Aker
Merge in Barry.
203
    }
204
1841.1.1 by Barry.Leslie at PrimeBase
Bug fix for memory leak in event_observer plugin.
205
		observers= new EventObserverList();
206
		table_share.setTableObservers(observers);
207
 
1593 by Brian Aker
Merge in Barry.
208
1966.2.14 by Brian Aker
Merge in some additional std namespace finds.
209
    std::for_each(all_event_plugins.begin(), all_event_plugins.end(),
210
                  RegisterTableEventsIterate(table_share, *observers));
1593 by Brian Aker
Merge in Barry.
211
212
  }
213
214
  //----------
215
  /* Cleanup before freeing the TableShare object. */
216
  void EventObserver::deregisterTableEvents(TableShare &table_share)
217
  {
1841.1.1 by Barry.Leslie at PrimeBase
Bug fix for memory leak in event_observer plugin.
218
   if (all_event_plugins.empty())
1593 by Brian Aker
Merge in Barry.
219
      return;
220
221
    EventObserverList *observers;
222
223
    observers= table_share.getTableObservers();
224
225
    if (observers) 
226
    {
227
      table_share.setTableObservers(NULL);
228
      delete observers;
229
    }
230
  }
231
232
233
  /*========================================================*/
234
  /*              Schema Event Observer handling:           */
235
  /*========================================================*/
236
237
  //----------
238
  /* For each EventObserver plugin call its registerSchemaEventsDo() meathod so that it can
239
   * register what events, if any, it is interested in on the schema.
240
   */ 
1966.2.9 by Brian Aker
Remove the use of "using std" from the plugin interface .cc files.
241
  class RegisterSchemaEventsIterate : public std::unary_function<EventObserver *, void>
1593 by Brian Aker
Merge in Barry.
242
  {
243
    const std::string &db;
244
    EventObserverList &observers;
245
  public:
246
    RegisterSchemaEventsIterate(const std::string &db_arg, EventObserverList &observers_arg) :     
247
      db(db_arg),
248
      observers(observers_arg){}
249
250
    inline result_type operator() (argument_type eventObserver)
251
    {
252
      eventObserver->registerSchemaEventsDo(db, observers);
253
    }
254
  };
255
256
  //----------
257
  /* 
258
   * registerSchemaEvents() is called by drizzle to register all plugins that
259
   * may be interested in schema events on the database.
260
   */ 
261
  void EventObserver::registerSchemaEvents(Session &session, const std::string &db)
262
  {
263
    if (all_event_plugins.empty())
264
      return;
265
266
    EventObserverList *observers;
267
268
    observers= session.getSchemaObservers(db);
269
270
    if (observers == NULL) 
271
    {
272
      observers= new EventObserverList();
273
      session.setSchemaObservers(db, observers);
1841.1.1 by Barry.Leslie at PrimeBase
Bug fix for memory leak in event_observer plugin.
274
   }
1593 by Brian Aker
Merge in Barry.
275
1966.2.14 by Brian Aker
Merge in some additional std namespace finds.
276
    std::for_each(all_event_plugins.begin(), all_event_plugins.end(),
277
                  RegisterSchemaEventsIterate(db, *observers));
1593 by Brian Aker
Merge in Barry.
278
279
  }
280
281
  //----------
282
  /* Cleanup before freeing the Session object. */
283
  void EventObserver::deregisterSchemaEvents(Session &session, const std::string &db)
284
  {
285
    if (all_event_plugins.empty())
286
      return;
287
288
    EventObserverList *observers;
289
290
    observers= session.getSchemaObservers(db);
291
292
    if (observers) 
293
    {
294
      session.setSchemaObservers(db, NULL);
295
      delete observers;
296
    }
297
  }
298
299
  /*========================================================*/
300
  /*             Session Event Observer handling:           */
301
  /*========================================================*/
302
303
  //----------
304
  /* For each EventObserver plugin call its registerSessionEventsDo() meathod so that it can
305
   * register what events, if any, it is interested in on this session.
306
   */ 
1966.2.9 by Brian Aker
Remove the use of "using std" from the plugin interface .cc files.
307
  class RegisterSessionEventsIterate : public std::unary_function<EventObserver *, void>
1593 by Brian Aker
Merge in Barry.
308
  {
309
    Session &session;
310
    EventObserverList &observers;
311
  public:
312
    RegisterSessionEventsIterate(Session &session_arg, EventObserverList &observers_arg) : 
313
      session(session_arg), observers(observers_arg) {}
314
    inline result_type operator() (argument_type eventObserver)
315
    {
316
      eventObserver->registerSessionEventsDo(session, observers);
317
    }
318
  };
319
320
  //----------
321
  /* 
322
   * registerSessionEvents() is called by drizzle to register all plugins that
323
   * may be interested in session events on the newly created session.
324
   */ 
325
  void EventObserver::registerSessionEvents(Session &session)
326
  {
327
    if (all_event_plugins.empty())
328
      return;
329
330
    EventObserverList *observers;
331
332
    observers= session.getSessionObservers();
1841.1.1 by Barry.Leslie at PrimeBase
Bug fix for memory leak in event_observer plugin.
333
		if (observers) { // This should not happed
2126.3.3 by Brian Aker
Merge in error message rework. Many error messages are fixed in this patch.
334
			errmsg_printf(error::WARN,
1841.1.1 by Barry.Leslie at PrimeBase
Bug fix for memory leak in event_observer plugin.
335
									_("EventObserver::registerSessionEvents(): Session already has events registered on it: probable programming error."));
336
			session.setSessionObservers(NULL);
337
			delete observers;
338
		}
339
340
	observers= new EventObserverList();
341
	session.setSessionObservers(observers);
1593 by Brian Aker
Merge in Barry.
342
1966.2.14 by Brian Aker
Merge in some additional std namespace finds.
343
  std::for_each(all_event_plugins.begin(), all_event_plugins.end(),
344
                RegisterSessionEventsIterate(session, *observers));
1593 by Brian Aker
Merge in Barry.
345
346
  }
347
348
  //----------
349
  /* Cleanup before freeing the session object. */
350
  void EventObserver::deregisterSessionEvents(Session &session)
351
  {
352
    if (all_event_plugins.empty())
353
      return;
354
355
    EventObserverList *observers;
356
357
    observers= session.getSessionObservers();
358
359
    if (observers) 
360
    {
361
      session.setSessionObservers(NULL);
362
      delete observers;
363
    }
364
  }
365
366
367
  /* Event observer list iterator: */
368
  //----------
1966.2.9 by Brian Aker
Remove the use of "using std" from the plugin interface .cc files.
369
  class EventIterate : public std::unary_function<std::pair<uint32_t, EventObserver *>, bool>
1593 by Brian Aker
Merge in Barry.
370
  {
371
    EventData &data;
372
373
  public:
374
    EventIterate(EventData &data_arg) :
1966.2.9 by Brian Aker
Remove the use of "using std" from the plugin interface .cc files.
375
      std::unary_function<std::pair<uint32_t, EventObserver *>, bool>(),
1593 by Brian Aker
Merge in Barry.
376
      data(data_arg)
1502.5.3 by Barry.Leslie at PrimeBase
Added the source code for the events plugin.
377
    {}
378
1593 by Brian Aker
Merge in Barry.
379
    inline result_type operator()(argument_type handler)
1502.5.3 by Barry.Leslie at PrimeBase
Added the source code for the events plugin.
380
    {
1593 by Brian Aker
Merge in Barry.
381
      bool result= handler.second->observeEventDo(data);
382
      if (result)
383
      {
384
        /* TRANSLATORS: The leading word "EventObserver" is the name
385
          of the plugin api, and so should not be translated. */
2126.3.3 by Brian Aker
Merge in error message rework. Many error messages are fixed in this patch.
386
        errmsg_printf(error::ERROR,
1593 by Brian Aker
Merge in Barry.
387
                      _("EventIterate event handler '%s' failed for event '%s'"),
388
                      handler.second->getName().c_str(), handler.second->eventName(data.event));
389
390
      }
391
      return result;
1502.5.3 by Barry.Leslie at PrimeBase
Added the source code for the events plugin.
392
    }
1593 by Brian Aker
Merge in Barry.
393
  };
394
395
396
  /*==========================================================*/
397
  /* Generic meathods called by drizzle to notify all interested  
398
   * plugins of an event,
1502.5.8 by Barry.Leslie at PrimeBase
- Changed names to match the drizzle naming convention.
399
 */
1593 by Brian Aker
Merge in Barry.
400
401
  // Call all event observers interested in the event.
402
  bool EventData::callEventObservers()
403
  {
404
    EventObserverList::ObserverMap *eventObservers;
405
1841.1.2 by Barry.Leslie at PrimeBase
Bug fix for possible crash when a NULL event list is accessed.
406
    if (observerList == NULL)
407
      return false; // Nobody was interested in the event. :(
408
1593 by Brian Aker
Merge in Barry.
409
    eventObservers = observerList->getObservers(event);
410
411
    if (eventObservers == NULL)
412
      return false; // Nobody was interested in the event. :(
413
414
    /* Use find_if instead of foreach so that we can collect return codes */
415
    EventObserverList::ObserverMap::iterator iter=
1966.2.14 by Brian Aker
Merge in some additional std namespace finds.
416
      std::find_if(eventObservers->begin(), eventObservers->end(),
417
                   EventIterate(*this)); 
1593 by Brian Aker
Merge in Barry.
418
    /* If iter is == end() here, that means that all of the plugins returned
419
     * false, which in this case means they all succeeded. Since we want to 
420
     * return false on success, we return the value of the two being !=.
1502.5.3 by Barry.Leslie at PrimeBase
Added the source code for the events plugin.
421
   */
1593 by Brian Aker
Merge in Barry.
422
    return iter != eventObservers->end();
423
  }
424
425
  //--------
426
  bool SessionEventData::callEventObservers()
427
  {
428
    observerList= session.getSessionObservers();
429
430
    return EventData::callEventObservers();
431
  }
432
2154.2.17 by Brian Aker
Additional removal of session
433
  bool SessionEventData::hasEvents(Session &in_session)
434
  {
435
    return (in_session.getSessionObservers() != NULL);
436
  }
437
1593 by Brian Aker
Merge in Barry.
438
  //--------
439
  bool SchemaEventData::callEventObservers()
440
  {
1502.5.8 by Barry.Leslie at PrimeBase
- Changed names to match the drizzle naming convention.
441
    observerList= session.getSchemaObservers(db);
1593 by Brian Aker
Merge in Barry.
442
    if (!observerList) 
443
    {
444
      EventObserver::registerSchemaEvents(session, db);
445
      observerList= session.getSchemaObservers(db);
446
    }
447
448
    return EventData::callEventObservers();
449
  }
450
451
  //--------
452
  bool TableEventData::callEventObservers()
453
  {
1827.2.1 by Brian Aker
Encapsulate Table (remove its direct access of the share it contains)
454
    observerList= table.getMutableShare()->getTableObservers();
1593 by Brian Aker
Merge in Barry.
455
456
    return EventData::callEventObservers();
457
  }
458
2154.2.17 by Brian Aker
Additional removal of session
459
  bool TableEventData::hasEvents(Table &in_table)
460
  {
461
    return (in_table.getMutableShare()->getTableObservers() != NULL);
462
  }
463
1593 by Brian Aker
Merge in Barry.
464
  /*==========================================================*/
465
  /* Static meathods called by drizzle to notify interested plugins 
1841.1.2 by Barry.Leslie at PrimeBase
Bug fix for possible crash when a NULL event list is accessed.
466
   * of a schema event.
1593 by Brian Aker
Merge in Barry.
467
 */
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
468
  bool EventObserver::beforeDropTable(Session &session, const drizzled::identifier::Table &table)
1593 by Brian Aker
Merge in Barry.
469
  {
470
    if (all_event_plugins.empty())
471
      return false;
472
473
    BeforeDropTableEventData eventData(session, table);
474
    return eventData.callEventObservers();
475
  }
476
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
477
  bool EventObserver::afterDropTable(Session &session, const drizzled::identifier::Table &table, int err)
1593 by Brian Aker
Merge in Barry.
478
  {
479
    if (all_event_plugins.empty())
480
      return false;
481
482
    AfterDropTableEventData eventData(session, table, err);
483
    return eventData.callEventObservers();
484
  }
485
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
486
  bool EventObserver::beforeRenameTable(Session &session, const drizzled::identifier::Table &from, const drizzled::identifier::Table &to)
1593 by Brian Aker
Merge in Barry.
487
  {
488
    if (all_event_plugins.empty())
489
      return false;
490
491
    BeforeRenameTableEventData eventData(session, from, to);
492
    return eventData.callEventObservers();
493
  }
494
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
495
  bool EventObserver::afterRenameTable(Session &session, const drizzled::identifier::Table &from, const drizzled::identifier::Table &to, int err)
1593 by Brian Aker
Merge in Barry.
496
  {
497
    if (all_event_plugins.empty())
498
      return false;
499
500
    AfterRenameTableEventData eventData(session, from, to, err);
501
    return eventData.callEventObservers();
502
  }
503
504
  /*==========================================================*/
505
  /* Static meathods called by drizzle to notify interested plugins 
1841.1.2 by Barry.Leslie at PrimeBase
Bug fix for possible crash when a NULL event list is accessed.
506
   * of a table event.
507
   *
508
   * A quick test is done first to see if there are any interested observers.
1593 by Brian Aker
Merge in Barry.
509
 */
510
  bool EventObserver::beforeInsertRecord(Table &table, unsigned char *buf)
511
  {
1841.1.2 by Barry.Leslie at PrimeBase
Bug fix for possible crash when a NULL event list is accessed.
512
    if (all_event_plugins.empty() || !TableEventData::hasEvents(table))
1593 by Brian Aker
Merge in Barry.
513
      return false;
514
515
    BeforeInsertRecordEventData eventData(*(table.in_use), table, buf);
516
    return eventData.callEventObservers();
517
  }
518
519
  bool EventObserver::afterInsertRecord(Table &table, const unsigned char *buf, int err)
520
  {
1841.1.2 by Barry.Leslie at PrimeBase
Bug fix for possible crash when a NULL event list is accessed.
521
    if (all_event_plugins.empty() || !TableEventData::hasEvents(table))
1593 by Brian Aker
Merge in Barry.
522
      return false;
523
524
    AfterInsertRecordEventData eventData(*(table.in_use), table, buf, err);
525
    return eventData.callEventObservers();
526
  }
527
528
  bool EventObserver::beforeDeleteRecord(Table &table, const unsigned char *buf)
529
  {
1841.1.2 by Barry.Leslie at PrimeBase
Bug fix for possible crash when a NULL event list is accessed.
530
    if (all_event_plugins.empty() || !TableEventData::hasEvents(table))
1593 by Brian Aker
Merge in Barry.
531
      return false;
532
533
    BeforeDeleteRecordEventData eventData(*(table.in_use), table, buf);
534
    return eventData.callEventObservers();
535
  }
536
537
  bool EventObserver::afterDeleteRecord(Table &table, const unsigned char *buf, int err)
538
  {
1841.1.2 by Barry.Leslie at PrimeBase
Bug fix for possible crash when a NULL event list is accessed.
539
    if (all_event_plugins.empty() || !TableEventData::hasEvents(table))
1593 by Brian Aker
Merge in Barry.
540
      return false;
541
542
    AfterDeleteRecordEventData eventData(*(table.in_use), table, buf, err);
543
    return eventData.callEventObservers();
544
  }
545
546
  bool EventObserver::beforeUpdateRecord(Table &table, const unsigned char *old_data, unsigned char *new_data)
547
  {
1841.1.2 by Barry.Leslie at PrimeBase
Bug fix for possible crash when a NULL event list is accessed.
548
    if (all_event_plugins.empty() || !TableEventData::hasEvents(table))
1593 by Brian Aker
Merge in Barry.
549
      return false;
550
551
    BeforeUpdateRecordEventData eventData(*(table.in_use), table, old_data, new_data);
552
    return eventData.callEventObservers();
553
  }
554
555
  bool EventObserver::afterUpdateRecord(Table &table, const unsigned char *old_data, unsigned char *new_data, int err)
556
  {
1841.1.2 by Barry.Leslie at PrimeBase
Bug fix for possible crash when a NULL event list is accessed.
557
    if (all_event_plugins.empty() || !TableEventData::hasEvents(table))
1593 by Brian Aker
Merge in Barry.
558
      return false;
559
560
    AfterUpdateRecordEventData eventData(*(table.in_use), table, old_data, new_data, err);
561
    return eventData.callEventObservers();
562
  }
563
564
  /*==========================================================*/
565
  /* Static meathods called by drizzle to notify interested plugins 
1841.1.2 by Barry.Leslie at PrimeBase
Bug fix for possible crash when a NULL event list is accessed.
566
   * of a session event.
567
   *
568
   * A quick test is done first to see if there are any interested observers.
569
*/
1593 by Brian Aker
Merge in Barry.
570
  bool EventObserver::beforeCreateDatabase(Session &session, const std::string &db)
571
  {
1841.1.2 by Barry.Leslie at PrimeBase
Bug fix for possible crash when a NULL event list is accessed.
572
    if (all_event_plugins.empty() || !SessionEventData::hasEvents(session))
1593 by Brian Aker
Merge in Barry.
573
      return false;
574
575
    BeforeCreateDatabaseEventData eventData(session, db);
576
    return eventData.callEventObservers();
577
  }
578
579
  bool EventObserver::afterCreateDatabase(Session &session, const std::string &db, int err)
580
  {
1841.1.2 by Barry.Leslie at PrimeBase
Bug fix for possible crash when a NULL event list is accessed.
581
    if (all_event_plugins.empty() || !SessionEventData::hasEvents(session))
1593 by Brian Aker
Merge in Barry.
582
      return false;
583
584
    AfterCreateDatabaseEventData eventData(session, db, err);
585
    return eventData.callEventObservers();
586
  }
587
588
  bool EventObserver::beforeDropDatabase(Session &session, const std::string &db)
589
  {
1841.1.2 by Barry.Leslie at PrimeBase
Bug fix for possible crash when a NULL event list is accessed.
590
    if (all_event_plugins.empty() || !SessionEventData::hasEvents(session))
1593 by Brian Aker
Merge in Barry.
591
      return false;
592
593
    BeforeDropDatabaseEventData eventData(session, db);
594
    return eventData.callEventObservers();
595
  }
596
597
  bool EventObserver::afterDropDatabase(Session &session, const std::string &db, int err)
598
  {
1841.1.2 by Barry.Leslie at PrimeBase
Bug fix for possible crash when a NULL event list is accessed.
599
    if (all_event_plugins.empty() || !SessionEventData::hasEvents(session))
1593 by Brian Aker
Merge in Barry.
600
      return false;
601
602
    AfterDropDatabaseEventData eventData(session, db, err);
603
    return eventData.callEventObservers();
604
  }
1502.5.3 by Barry.Leslie at PrimeBase
Added the source code for the events plugin.
605
1836 by Brian Aker
Added support for pre/post triggers (this removes the need for the current
606
  bool EventObserver::connectSession(Session &session)
607
  {
1841.1.2 by Barry.Leslie at PrimeBase
Bug fix for possible crash when a NULL event list is accessed.
608
    if (all_event_plugins.empty() || !SessionEventData::hasEvents(session))
1836 by Brian Aker
Added support for pre/post triggers (this removes the need for the current
609
      return false;
610
611
    ConnectSessionEventData eventData(session);
612
    return eventData.callEventObservers();
613
  }
614
615
  bool EventObserver::disconnectSession(Session &session)
616
  {
1841.1.2 by Barry.Leslie at PrimeBase
Bug fix for possible crash when a NULL event list is accessed.
617
    if (all_event_plugins.empty() || !SessionEventData::hasEvents(session))
1836 by Brian Aker
Added support for pre/post triggers (this removes the need for the current
618
      return false;
619
620
    DisconnectSessionEventData eventData(session);
621
    return eventData.callEventObservers();
622
  }
623
624
  bool EventObserver::beforeStatement(Session &session)
625
  {
1841.1.2 by Barry.Leslie at PrimeBase
Bug fix for possible crash when a NULL event list is accessed.
626
    if (all_event_plugins.empty() || !SessionEventData::hasEvents(session))
1836 by Brian Aker
Added support for pre/post triggers (this removes the need for the current
627
      return false;
628
629
    BeforeStatementEventData eventData(session);
630
    return eventData.callEventObservers();
631
  }
632
633
  bool EventObserver::afterStatement(Session &session)
634
  {
1841.1.2 by Barry.Leslie at PrimeBase
Bug fix for possible crash when a NULL event list is accessed.
635
    if (all_event_plugins.empty() || !SessionEventData::hasEvents(session))
1836 by Brian Aker
Added support for pre/post triggers (this removes the need for the current
636
      return false;
637
638
    AfterStatementEventData eventData(session);
639
    return eventData.callEventObservers();
640
  }
641
1502.5.3 by Barry.Leslie at PrimeBase
Added the source code for the events plugin.
642
643
} /* namespace plugin */
644
} /* namespace drizzled */