~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to server/sql_udf.cc

Merged.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
16
/* This implements 'user defined functions' */
17
 
#include <drizzled/server_includes.h>
18
 
#include <libdrizzle/gettext.h>
 
17
 
 
18
/*
 
19
   Known bugs:
 
20
  
 
21
*/
 
22
 
 
23
#ifdef USE_PRAGMA_IMPLEMENTATION
 
24
#pragma implementation                          // gcc: Class implementation
 
25
#endif
 
26
 
 
27
#include "mysql_priv.h"
 
28
#include <my_pthread.h>
 
29
 
 
30
extern "C"
 
31
{
 
32
#include <stdarg.h>
 
33
#include <hash.h>
 
34
}
19
35
 
20
36
static bool udf_startup= false; /* We do not lock because startup is single threaded */
21
37
static MEM_ROOT mem;
22
38
static HASH udf_hash;
23
39
 
24
 
extern "C" unsigned char* get_hash_key(const unsigned char *buff, size_t *length,
 
40
extern "C" uchar* get_hash_key(const uchar *buff, size_t *length,
25
41
                               bool not_used __attribute__((unused)))
26
42
{
27
43
  udf_func *udf= (udf_func*) buff;
28
44
  *length= (uint) udf->name.length;
29
 
  return (unsigned char*) udf->name.str;
 
45
  return (uchar*) udf->name.str;
30
46
}
31
47
 
32
48
 
36
52
 
37
53
  if (hash_init(&udf_hash, system_charset_info, 32, 0, 0, get_hash_key, NULL, 0))
38
54
  {
39
 
    sql_print_error(_("Can't allocate memory for udf structures"));
 
55
    sql_print_error("Can't allocate memory for udf structures");
40
56
    hash_free(&udf_hash);
41
57
    free_root(&mem, MYF(0));
42
58
    return;
51
67
}
52
68
 
53
69
/* This is only called if using_udf_functions != 0 */
54
 
udf_func *find_udf(const char *name, uint32_t length)
 
70
udf_func *find_udf(const char *name, uint length)
55
71
{
56
72
  udf_func *udf;
57
73
 
59
75
    return NULL;
60
76
 
61
77
  udf= (udf_func*) hash_search(&udf_hash,
62
 
                               (unsigned char*) name,
 
78
                               (uchar*) name,
63
79
                               length ? length : (uint) strlen(name));
64
80
 
65
81
  return (udf);
67
83
 
68
84
static bool add_udf(udf_func *udf)
69
85
{
70
 
  if (my_hash_insert(&udf_hash, (unsigned char*) udf))
 
86
  if (my_hash_insert(&udf_hash, (uchar*) udf))
71
87
    return false;
72
88
 
73
89
  using_udf_functions= 1;
77
93
 
78
94
int initialize_udf(st_plugin_int *plugin)
79
95
{
80
 
  udf_func *f;
 
96
  udf_func *udff;
81
97
 
82
98
  if (udf_startup == false)
83
99
  {
84
100
    udf_init();
85
101
    udf_startup= true;
86
102
  }
 
103
          
 
104
  /* allocate the udf_func structure */
 
105
  udff = (udf_func *)my_malloc(sizeof(udf_func), MYF(MY_WME | MY_ZEROFILL));
 
106
  if (udff == NULL) return 1;
 
107
 
 
108
  plugin->data= (void *)udff;
87
109
 
88
110
  if (plugin->plugin->init)
89
111
  {
90
 
    int r;
91
 
    if ((r= plugin->plugin->init((void *)&f)))
 
112
    /* todo, if the plugin doesnt have an init, bail out */
 
113
 
 
114
    if (plugin->plugin->init((void *)udff))
92
115
    {
93
 
      sql_print_error("Plugin '%s' init function returned error %d.",
94
 
                      plugin->name.str, r);
95
 
      return r;
 
116
      sql_print_error("Plugin '%s' init function returned error.",
 
117
                      plugin->name.str);
 
118
      goto err;
96
119
    }
97
120
  }
98
 
  else
99
 
    return 1;
100
 
 
101
 
  if(!add_udf(f))
102
 
    return 2;
 
121
  
 
122
  add_udf(udff);
103
123
 
104
124
  return 0;
105
 
 
 
125
err:
 
126
  my_free(udff, MYF(0));
 
127
  return 1;
106
128
}
107
129
 
108
130
int finalize_udf(st_plugin_int *plugin)
114
136
    (void)plugin->plugin->deinit(udff);
115
137
 
116
138
  if (udff)
117
 
    free(udff);
 
139
    my_free(udff, MYF(0));
118
140
 
119
141
  return 0;
120
142
}