~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* Copyright (C) 2000 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

/* This implements 'user defined functions' */
#include <drizzled/server_includes.h>
#include <drizzled/gettext.h>

static bool udf_startup= false; /* We do not lock because startup is single threaded */
static MEM_ROOT mem;
static HASH udf_hash;

extern "C" unsigned char* get_hash_key(const unsigned char *buff, size_t *length,
                               bool not_used __attribute__((unused)))
{
  udf_func *udf= (udf_func*) buff;
  *length= (uint) udf->name.length;
  return (unsigned char*) udf->name.str;
}


void udf_init()
{
  init_sql_alloc(&mem, UDF_ALLOC_BLOCK_SIZE, 0);

  if (hash_init(&udf_hash, system_charset_info, 32, 0, 0, get_hash_key, NULL, 0))
  {
    sql_print_error(_("Can't allocate memory for udf structures"));
    hash_free(&udf_hash);
    free_root(&mem, MYF(0));
    return;
  }
}

/* called by mysqld.cc clean_up() */
void udf_free()
{
  hash_free(&udf_hash);
  free_root(&mem, MYF(0));
}

/* This is only called if using_udf_functions != 0 */
udf_func *find_udf(const char *name, uint32_t length)
{
  udf_func *udf;

  if (udf_startup == false)
    return NULL;

  udf= (udf_func*) hash_search(&udf_hash,
                               (unsigned char*) name,
                               length ? length : (uint) strlen(name));

  return (udf);
}

static bool add_udf(udf_func *udf)
{
  if (my_hash_insert(&udf_hash, (unsigned char*) udf))
    return false;

  using_udf_functions= 1;

  return true;
}

int initialize_udf(st_plugin_int *plugin)
{
  udf_func *f;

  if (udf_startup == false)
  {
    udf_init();
    udf_startup= true;
  }

  if (plugin->plugin->init)
  {
    int r;
    if ((r= plugin->plugin->init((void *)&f)))
    {
      sql_print_error("Plugin '%s' init function returned error %d.",
                      plugin->name.str, r);
      return r;
    }
  }
  else
    return 1;

  if(!add_udf(f))
    return 2;

  return 0;

}

int finalize_udf(st_plugin_int *plugin)
{ 
  udf_func *udff = (udf_func *)plugin->data;

  /* TODO: Issue warning on failure */
  if (udff && plugin->plugin->deinit)
    (void)plugin->plugin->deinit(udff);

  if (udff)
    free(udff);

  return 0;
}