~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_udf.cc

  • Committer: Brian Aker
  • Date: 2009-07-11 19:23:04 UTC
  • mfrom: (1089.1.14 merge)
  • Revision ID: brian@gaz-20090711192304-ootijyl5yf9jq9kd
Merge Brian

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 
16
16
/* This implements 'user defined functions' */
17
17
#include <drizzled/server_includes.h>
18
 
 
19
 
static bool udf_startup= false; /* We do not lock because startup is single threaded */
20
 
static MEM_ROOT mem;
21
 
static HASH udf_hash;
22
 
 
23
 
extern "C" uchar* get_hash_key(const uchar *buff, size_t *length,
24
 
                               bool not_used __attribute__((unused)))
25
 
{
26
 
  udf_func *udf= (udf_func*) buff;
27
 
  *length= (uint) udf->name.length;
28
 
  return (uchar*) udf->name.str;
29
 
}
30
 
 
31
 
 
32
 
void udf_init()
33
 
{
34
 
  init_sql_alloc(&mem, UDF_ALLOC_BLOCK_SIZE, 0);
35
 
 
36
 
  if (hash_init(&udf_hash, system_charset_info, 32, 0, 0, get_hash_key, NULL, 0))
37
 
  {
38
 
    sql_print_error("Can't allocate memory for udf structures");
39
 
    hash_free(&udf_hash);
40
 
    free_root(&mem, MYF(0));
41
 
    return;
42
 
  }
43
 
}
44
 
 
45
 
/* called by mysqld.cc clean_up() */
46
 
void udf_free()
47
 
{
48
 
  hash_free(&udf_hash);
49
 
  free_root(&mem, MYF(0));
50
 
}
 
18
#include <drizzled/gettext.h>
 
19
#include <drizzled/sql_udf.h>
 
20
#include <drizzled/registry.h>
 
21
#include "drizzled/plugin_registry.h"
 
22
 
 
23
#include <string>
 
24
 
 
25
using namespace std;
 
26
 
 
27
static drizzled::Registry<Function_builder *> udf_registry;
51
28
 
52
29
/* This is only called if using_udf_functions != 0 */
53
 
udf_func *find_udf(const char *name, uint length)
54
 
{
55
 
  udf_func *udf;
56
 
 
57
 
  if (udf_startup == false)
58
 
    return NULL;
59
 
 
60
 
  udf= (udf_func*) hash_search(&udf_hash,
61
 
                               (uchar*) name,
62
 
                               length ? length : (uint) strlen(name));
63
 
 
64
 
  return (udf);
65
 
}
66
 
 
67
 
static bool add_udf(udf_func *udf)
68
 
{
69
 
  if (my_hash_insert(&udf_hash, (uchar*) udf))
70
 
    return false;
71
 
 
72
 
  using_udf_functions= 1;
73
 
 
74
 
  return true;
75
 
}
76
 
 
77
 
int initialize_udf(st_plugin_int *plugin)
78
 
{
79
 
  udf_func *udff;
80
 
 
81
 
  if (udf_startup == false)
82
 
  {
83
 
    udf_init();
84
 
    udf_startup= true;
85
 
  }
86
 
          
87
 
  /* allocate the udf_func structure */
88
 
  udff = (udf_func *)my_malloc(sizeof(udf_func), MYF(MY_WME | MY_ZEROFILL));
89
 
  if (udff == NULL) return 1;
90
 
 
91
 
  plugin->data= (void *)udff;
92
 
 
93
 
  if (plugin->plugin->init)
94
 
  {
95
 
    /* todo, if the plugin doesnt have an init, bail out */
96
 
 
97
 
    if (plugin->plugin->init((void *)udff))
98
 
    {
99
 
      sql_print_error("Plugin '%s' init function returned error.",
100
 
                      plugin->name.str);
101
 
      goto err;
102
 
    }
103
 
  }
104
 
  
105
 
  add_udf(udff);
106
 
 
107
 
  return 0;
108
 
err:
109
 
  my_free(udff, MYF(0));
110
 
  return 1;
111
 
}
112
 
 
113
 
int finalize_udf(st_plugin_int *plugin)
114
 
115
 
  udf_func *udff = (udf_func *)plugin->data;
116
 
 
117
 
  /* TODO: Issue warning on failure */
118
 
  if (udff && plugin->plugin->deinit)
119
 
    (void)plugin->plugin->deinit(udff);
120
 
 
121
 
  if (udff)
122
 
    my_free(udff, MYF(0));
123
 
 
124
 
  return 0;
125
 
}
 
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
 
126
45