~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_udf.cc

  • Committer: Brian Aker
  • Date: 2009-05-05 00:40:45 UTC
  • mfrom: (1003.2.7 mordred)
  • Revision ID: brian@gaz-20090505004045-yns4biv63thufj1b
Tags: 2009.05.1005
Merge Monty

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
/* This implements 'user defined functions' */
17
17
#include <drizzled/server_includes.h>
18
18
#include <drizzled/gettext.h>
19
 
#include <mysys/hash.h>
20
19
#include <drizzled/sql_udf.h>
 
20
#include <drizzled/registry.h>
 
21
#include "drizzled/plugin_registry.h"
21
22
 
22
 
#include <map>
23
23
#include <string>
24
24
 
25
25
using namespace std;
26
26
 
27
 
static bool udf_startup= false; /* We do not lock because startup is single threaded */
28
 
static MEM_ROOT mem;
29
 
static map<string, udf_func *> udf_map;
30
 
 
31
 
extern "C" unsigned char* get_hash_key(const unsigned char *buff, size_t *length,
32
 
                               bool )
33
 
{
34
 
  udf_func *udf= (udf_func*) buff;
35
 
  *length= (uint32_t) udf->name.length;
36
 
  return (unsigned char*) udf->name.str;
37
 
}
38
 
 
39
 
 
40
 
void udf_init()
41
 
{
42
 
  init_sql_alloc(&mem, UDF_ALLOC_BLOCK_SIZE, 0);
43
 
}
44
 
 
45
 
/* called by mysqld.cc clean_up() */
46
 
void udf_free()
47
 
{
48
 
  free_root(&mem, MYF(0));
49
 
}
 
27
static drizzled::Registry<Function_builder *> udf_registry;
50
28
 
51
29
/* This is only called if using_udf_functions != 0 */
52
 
udf_func *find_udf(const char *name, uint32_t length)
53
 
{
54
 
  udf_func *udf= NULL;
55
 
 
56
 
  if (udf_startup == false)
57
 
    return NULL;
58
 
 
59
 
  string find_str(name, length);
60
 
  transform(find_str.begin(), find_str.end(),
61
 
            find_str.begin(), ::tolower);
62
 
 
63
 
  map<string, udf_func *>::iterator find_iter;
64
 
  find_iter=  udf_map.find(find_str);
65
 
  if (find_iter != udf_map.end())
66
 
    udf= (*find_iter).second;
67
 
 
68
 
  return (udf);
69
 
}
70
 
 
71
 
static bool add_udf(udf_func *udf)
72
 
{
73
 
  string add_str(udf->name.str, udf->name.length);
74
 
  transform(add_str.begin(), add_str.end(),
75
 
            add_str.begin(), ::tolower);
76
 
 
77
 
  udf_map[add_str]= udf;
78
 
 
79
 
  using_udf_functions= 1;
80
 
 
81
 
  return true;
82
 
}
83
 
 
84
 
int initialize_udf(st_plugin_int *plugin)
85
 
{
86
 
  udf_func *f;
87
 
 
88
 
  if (udf_startup == false)
89
 
  {
90
 
    udf_init();
91
 
    udf_startup= true;
92
 
  }
93
 
 
94
 
  if (plugin->plugin->init)
95
 
  {
96
 
    int r;
97
 
    if ((r= plugin->plugin->init((void *)&f)))
98
 
    {
99
 
      errmsg_printf(ERRMSG_LVL_ERROR, "Plugin '%s' init function returned error %d.",
100
 
                    plugin->name.str, r);
101
 
      return r;
102
 
    }
103
 
  }
104
 
  else
105
 
    return 1;
106
 
 
107
 
  if (!add_udf(f))
108
 
    return 1;
109
 
 
110
 
  plugin->state= PLUGIN_IS_READY;
111
 
 
112
 
  return 0;
113
 
 
114
 
}
115
 
 
116
 
int finalize_udf(st_plugin_int *plugin)
117
 
{
118
 
  udf_func *udff = (udf_func *)plugin->data;
119
 
 
120
 
  /* TODO: Issue warning on failure */
121
 
  if (udff && plugin->plugin->deinit)
122
 
    (void)plugin->plugin->deinit(udff);
123
 
 
124
 
  if (udff)
125
 
    free(udff);
126
 
 
127
 
  return 0;
128
 
}
 
30
Function_builder *find_udf(const char *name, uint32_t length)
 
31
{
 
32
  return udf_registry.find(name, length);
 
33
}
 
34
 
 
35
void add_udf(Function_builder *udf)
 
36
{
 
37
  udf_registry.add(udf);
 
38
}
 
39
 
 
40
void remove_udf(Function_builder *udf)
 
41
{
 
42
  udf_registry.remove(udf);
 
43
}
 
44
 
129
45