~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/qcache.cc

  • Committer: Jay Pipes
  • Date: 2009-01-30 04:38:21 UTC
  • mto: This revision was merged to the branch mainline in revision 830.
  • Revision ID: jpipes@serialcoder-20090130043821-4d7jg2ftabefamxb
Fixes for the QUARTER() function to use new Temporal system and throw
errors on bad datetime values.

Added test case for QUARTER() function and modified func_time.test existing
test to correctly throw errors and report NULL, not 0 on NULL input.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems
5
 
 *  Copyright (C) 2010 Djellel Eddine Difallah
 
4
 *  Copyright (C) 2008 Mark Atwood, Toru Maesaka
6
5
 *
7
6
 *  This program is free software; you can redistribute it and/or modify
8
7
 *  it under the terms of the GNU General Public License as published by
18
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
18
 */
20
19
 
21
 
#include "config.h"
22
 
#include "drizzled/plugin/query_cache.h"
23
 
#include "drizzled/errmsg_print.h"
24
 
 
25
 
#include "drizzled/gettext.h"
26
 
 
27
 
#include <algorithm>
28
 
#include <vector>
29
 
 
30
 
class Session;
31
 
 
32
 
namespace drizzled
33
 
{
34
 
typedef std::vector<plugin::QueryCache *> QueryCaches;
35
 
QueryCaches all_query_cache;
36
 
 
37
 
/* Namespaces are here to prevent global symbol clashes with these classes */
38
 
 
39
 
class IsCachedIterate
40
 
 : public std::unary_function<plugin::QueryCache *, bool>
41
 
{
42
 
  Session *session;
43
 
public:
44
 
  IsCachedIterate(Session* session_arg) :
45
 
    std::unary_function<plugin::QueryCache *, bool>(),
46
 
    session(session_arg) { }
47
 
 
48
 
  inline result_type operator()(argument_type handler)
49
 
  {
50
 
    return handler->doIsCached(session);
51
 
  }
52
 
};
53
 
 
54
 
bool plugin::QueryCache::isCached(Session *session)
55
 
{
56
 
  /* Use find_if instead of foreach so that we can collect return codes */
57
 
  QueryCaches::iterator iter=
58
 
    std::find_if(all_query_cache.begin(), all_query_cache.end(),
59
 
            IsCachedIterate(session));
60
 
  /* If iter is == end() here, that means that all of the plugins returned
61
 
   * false, which in this case means they all succeeded. Since we want to 
62
 
   * return false on success, we return the value of the two being != 
63
 
   */
64
 
  return iter != all_query_cache.end();
65
 
}
66
 
 
67
 
 
68
 
class SendCachedResultsetIterate
69
 
 : public std::unary_function<plugin::QueryCache *, bool>
70
 
{
71
 
  Session *session;
72
 
public:
73
 
  SendCachedResultsetIterate(Session *session_arg) :
74
 
    std::unary_function<plugin::QueryCache *, bool>(),
75
 
    session(session_arg) { }
76
 
 
77
 
  inline result_type operator()(argument_type handler)
78
 
  {
79
 
    return handler->doSendCachedResultset(session);
80
 
  }
81
 
};
82
 
bool plugin::QueryCache::sendCachedResultset(Session *session)
83
 
{
84
 
  /* Use find_if instead of foreach so that we can collect return codes */
85
 
  QueryCaches::iterator iter=
86
 
    std::find_if(all_query_cache.begin(), all_query_cache.end(),
87
 
                 SendCachedResultsetIterate(session));
88
 
  /* If iter is == end() here, that means that all of the plugins returned
89
 
   * false, which in this case means they all succeeded. Since we want to 
90
 
   * return false on success, we return the value of the two being != 
91
 
   */
92
 
  return iter != all_query_cache.end();
93
 
}
94
 
 
95
 
class PrepareResultsetIterate : public std::unary_function<plugin::QueryCache *, bool>
96
 
{
97
 
  Session *session;
98
 
public:
99
 
  PrepareResultsetIterate(Session *session_arg) :
100
 
    std::unary_function<plugin::QueryCache *, bool>(),
101
 
    session(session_arg) { }
102
 
 
103
 
  inline result_type operator()(argument_type handler)
104
 
  {
105
 
    return handler->doPrepareResultset(session);
106
 
  }
107
 
};
108
 
bool plugin::QueryCache::prepareResultset(Session *session)
109
 
{
110
 
  /* Use find_if instead of foreach so that we can collect return codes */
111
 
  QueryCaches::iterator iter=
112
 
    std::find_if(all_query_cache.begin(), all_query_cache.end(),
113
 
                 PrepareResultsetIterate(session));
114
 
  /* If iter is == end() here, that means that all of the plugins returned
115
 
   * false, which in this case means they all succeeded. Since we want to 
116
 
   * return false on success, we return the value of the two being != 
117
 
   */
118
 
  return iter != all_query_cache.end();
119
 
}
120
 
 
121
 
class SetResultsetIterate : public std::unary_function<plugin::QueryCache *, bool>
122
 
{
123
 
  Session *session;
124
 
public:
125
 
  SetResultsetIterate(Session *session_arg) :
126
 
    std::unary_function<plugin::QueryCache *, bool>(),
127
 
    session(session_arg) { }
128
 
 
129
 
  inline result_type operator()(argument_type handler)
130
 
  {
131
 
    return handler->doSetResultset(session);
132
 
  }
133
 
};
134
 
 
135
 
bool plugin::QueryCache::setResultset(Session *session)
136
 
{
137
 
  /* Use find_if instead of foreach so that we can collect return codes */
138
 
  QueryCaches::iterator iter=
139
 
    std::find_if(all_query_cache.begin(), all_query_cache.end(),
140
 
                 SetResultsetIterate(session));
141
 
  /* If iter is == end() here, that means that all of the plugins returned
142
 
   * false, which in this case means they all succeeded. Since we want to 
143
 
   * return false on success, we return the value of the two being != 
144
 
   */
145
 
  return iter != all_query_cache.end();
146
 
}
147
 
 
148
 
class InsertRecordIterate
149
 
 : public std::unary_function<plugin::QueryCache *, bool>
150
 
{
151
 
  Session *session;
152
 
  List<Item> &item;
153
 
public:
154
 
  InsertRecordIterate(Session *session_arg, List<Item> &item_arg) :
155
 
    std::unary_function<plugin::QueryCache *, bool>(),
156
 
    session(session_arg), item(item_arg) { }
157
 
 
158
 
  inline result_type operator()(argument_type handler)
159
 
  {
160
 
    return handler->doInsertRecord(session, item);
161
 
  }
162
 
};
163
 
bool plugin::QueryCache::insertRecord(Session *session, List<Item> &items)
164
 
{
165
 
  /* Use find_if instead of foreach so that we can collect return codes */
166
 
  QueryCaches::iterator iter=
167
 
    std::find_if(all_query_cache.begin(), all_query_cache.end(),
168
 
                 InsertRecordIterate(session, items));
169
 
  /* If iter is == end() here, that means that all of the plugins returned
170
 
   * false, which in this case means they all succeeded. Since we want to 
171
 
   * return false on success, we return the value of the two being != 
172
 
   */
173
 
  return iter != all_query_cache.end();
174
 
}
175
 
 
176
 
 
177
 
 
178
 
bool plugin::QueryCache::addPlugin(plugin::QueryCache *handler)
179
 
{
180
 
  all_query_cache.push_back(handler);
181
 
  return false;
182
 
}
183
 
 
184
 
void plugin::QueryCache::removePlugin(plugin::QueryCache *handler)
185
 
{
186
 
  all_query_cache.erase(std::find(all_query_cache.begin(), all_query_cache.end(),
187
 
                                  handler));
188
 
}
189
 
 
190
 
} /* namespace drizzled */
 
20
#include <drizzled/server_includes.h>
 
21
#include <drizzled/qcache.h>
 
22
#include <drizzled/gettext.h>
 
23
 
 
24
int qcache_initializer(st_plugin_int *plugin)
 
25
{
 
26
  qcache_t *p;
 
27
 
 
28
  p= new qcache_t;
 
29
  if (p == NULL) return 1;
 
30
  memset(p, 0, sizeof(qcache_t));
 
31
 
 
32
  plugin->data= (void *)p;
 
33
 
 
34
  if (plugin->plugin->init)
 
35
  {
 
36
    if (plugin->plugin->init((void *)p))
 
37
    {
 
38
      errmsg_printf(ERRMSG_LVL_ERROR, _("qcache plugin '%s' init() failed"),
 
39
                    plugin->name.str);
 
40
      goto err;
 
41
    }
 
42
  }
 
43
  plugin->state= PLUGIN_IS_READY;
 
44
 
 
45
  return 0;
 
46
 
 
47
err:
 
48
  delete p;
 
49
  return 1;
 
50
}
 
51
 
 
52
int qcache_finalizer(st_plugin_int *plugin)
 
53
{
 
54
  qcache_t *p= (qcache_t *) plugin->data;
 
55
 
 
56
  if (plugin->plugin->deinit)
 
57
  {
 
58
    if (plugin->plugin->deinit((void *)p))
 
59
    {
 
60
      errmsg_printf(ERRMSG_LVL_ERROR, _("qcache plugin '%s' deinit() failed"),
 
61
                    plugin->name.str);
 
62
    }
 
63
  }
 
64
 
 
65
  if (p) delete p;
 
66
  return 0;
 
67
}
 
68
 
 
69
/*
 
70
  The plugin_foreach() iterator requires that we convert all the parameters
 
71
  of a plugin API entry point into a single void pointer, plus the session.
 
72
  So we need the following structure for qcache_invalidate_db() which requires
 
73
  multiple arguments.
 
74
*/
 
75
typedef struct db_invalidation_parms
 
76
{
 
77
  const char *dbname;
 
78
  bool transactional;
 
79
} db_invalidation_parms_t;
 
80
 
 
81
 
 
82
/*
 
83
  Following functions:
 
84
 
 
85
    qcache_try_fetch_and_send_iterate();
 
86
    qcache_set_iterate();
 
87
    qcache_invalidate_table_iterate();
 
88
    qcache_invalidate_db_iterate();
 
89
    qcache_flush_iterate();
 
90
 
 
91
  are called by plugin_foreach() _once_ for each Query Cache plugin.
 
92
*/
 
93
 
 
94
static bool qcache_try_fetch_and_send_iterate(Session *session, 
 
95
                                              plugin_ref plugin, void *p)
 
96
{
 
97
  qcache_t *l= plugin_data(plugin, qcache_t *);
 
98
  bool is_transactional = (bool *)p;
 
99
 
 
100
  if (l && l->qcache_try_fetch_and_send)
 
101
  {
 
102
    if (l->qcache_try_fetch_and_send(session, is_transactional))
 
103
    {
 
104
      errmsg_printf(ERRMSG_LVL_ERROR,
 
105
                    _("qcache plugin '%s' try_fetch_and_send() failed"),
 
106
                    (char *)plugin_name(plugin));
 
107
      return true;
 
108
    }
 
109
  }
 
110
  return false;
 
111
}
 
112
 
 
113
static bool qcache_set_iterate(Session *session, plugin_ref plugin, void *p)
 
114
{
 
115
  qcache_t *l = plugin_data(plugin, qcache_t *);
 
116
  bool transactional = (bool *)p;
 
117
 
 
118
  if (l && l->qcache_set)
 
119
  {
 
120
    if (l->qcache_set(session, transactional))
 
121
    {
 
122
      errmsg_printf(ERRMSG_LVL_ERROR, _("qcache plugin '%s' set() failed"),
 
123
                    (char *)plugin_name(plugin));
 
124
      return true;
 
125
    }
 
126
  }
 
127
  return false;
 
128
}
 
129
 
 
130
static bool qcache_invalidate_table_iterate(Session *session,
 
131
                                            plugin_ref plugin, void *p)
 
132
{
 
133
  qcache_t *l = plugin_data(plugin, qcache_t *);
 
134
  bool transactional = (bool *)p;
 
135
 
 
136
  if (l && l->qcache_invalidate_table)
 
137
  {
 
138
    if (l->qcache_invalidate_table(session, transactional))
 
139
    {
 
140
      errmsg_printf(ERRMSG_LVL_ERROR,
 
141
                    _("qcache plugin '%s' invalidate_table() failed"),
 
142
                    (char *)plugin_name(plugin));
 
143
      return true;
 
144
    }
 
145
  }
 
146
  return false;
 
147
}
 
148
 
 
149
static bool qcache_invalidate_db_iterate(Session *session,
 
150
                                         plugin_ref plugin, void *p)
 
151
{
 
152
  qcache_t *l = plugin_data(plugin, qcache_t *);
 
153
  db_invalidation_parms_t *parms = (db_invalidation_parms_t *)p;
 
154
 
 
155
  if (l && l->qcache_invalidate_db)
 
156
  {
 
157
    if (l->qcache_invalidate_db(session, parms->dbname, parms->transactional))
 
158
    {
 
159
      errmsg_printf(ERRMSG_LVL_ERROR,
 
160
                    _("qcache plugin '%s' invalidate_db() failed"),
 
161
                    (char *)plugin_name(plugin));
 
162
      return true;
 
163
    }
 
164
  }
 
165
  return false;
 
166
}
 
167
 
 
168
static bool qcache_flush_iterate(Session *session, plugin_ref plugin, void *p)
 
169
{
 
170
  qcache_t *l = plugin_data(plugin, qcache_t *);
 
171
  
 
172
  if (p) return true; /* flush has no parameters, return failure */
 
173
 
 
174
  if (l && l->qcache_flush)
 
175
  {
 
176
    if (l->qcache_flush(session))
 
177
    {
 
178
      errmsg_printf(ERRMSG_LVL_ERROR, _("qcache plugin '%s' flush() failed"),
 
179
                    (char *)plugin_name(plugin));
 
180
      return true;
 
181
    }
 
182
  }
 
183
  return false;
 
184
}
 
185
 
 
186
 
 
187
/*
 
188
  Following functions:
 
189
 
 
190
    drizzle_qcache_try_fetch_and_send();
 
191
    drizzle_qcache_set();
 
192
    drizzle_qcache_invalidate_table();
 
193
    drizzle_qcache_invalidate_db();
 
194
    drizzle_qcache_flush();
 
195
 
 
196
  are the entry points to the query cache plugin that is called by the
 
197
  rest of the Drizzle server code.
 
198
*/
 
199
 
 
200
bool drizzle_qcache_try_fetch_and_send(Session *session, bool transactional)
 
201
{
 
202
  bool foreach_rv;
 
203
  foreach_rv= plugin_foreach(session,
 
204
                             qcache_try_fetch_and_send_iterate,
 
205
                             DRIZZLE_QCACHE_PLUGIN,
 
206
                             (void *) &transactional);
 
207
  return foreach_rv;
 
208
}
 
209
 
 
210
bool drizzle_qcache_set(Session *session, bool transactional)
 
211
{
 
212
  bool foreach_rv;
 
213
  foreach_rv= plugin_foreach(session,
 
214
                             qcache_set_iterate,
 
215
                             DRIZZLE_QCACHE_PLUGIN,
 
216
                             (void *) &transactional);
 
217
  return foreach_rv;
 
218
}
 
219
 
 
220
bool drizzle_qcache_invalidate_table(Session *session, bool transactional)
 
221
{
 
222
  bool foreach_rv;
 
223
  foreach_rv= plugin_foreach(session,
 
224
                             qcache_invalidate_table_iterate,
 
225
                             DRIZZLE_QCACHE_PLUGIN,
 
226
                             (void *) &transactional);
 
227
  return foreach_rv;
 
228
}
 
229
 
 
230
bool drizzle_qcache_invalidate_db(Session *session, const char *dbname,
 
231
                                  bool transactional)
 
232
{
 
233
  bool foreach_rv;
 
234
  db_invalidation_parms_t parms;
 
235
 
 
236
  /* marshall the parameters */
 
237
  parms.dbname = dbname;
 
238
  parms.transactional = transactional;
 
239
 
 
240
  foreach_rv= plugin_foreach(session,
 
241
                             qcache_invalidate_db_iterate,
 
242
                             DRIZZLE_QCACHE_PLUGIN,
 
243
                             (void *) &parms);
 
244
  return foreach_rv;
 
245
}
 
246
 
 
247
bool drizzle_qcache_flush(Session *session)
 
248
{
 
249
  bool foreach_rv;
 
250
  foreach_rv= plugin_foreach(session,
 
251
                             qcache_flush_iterate,
 
252
                             DRIZZLE_QCACHE_PLUGIN,
 
253
                             NULL);
 
254
  return foreach_rv;
 
255
}