~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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* 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' */

/*
   Known bugs:
  
*/

#ifdef USE_PRAGMA_IMPLEMENTATION
#pragma implementation				// gcc: Class implementation
#endif

#include "mysql_priv.h"
#include <mysys/my_pthread.h>

extern "C"
{
#include <stdarg.h>
#include <mysys/hash.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" uchar* get_hash_key(const uchar *buff, size_t *length,
                               bool not_used __attribute__((unused)))
{
  udf_func *udf= (udf_func*) buff;
  *length= (uint) udf->name.length;
  return (uchar*) 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, uint length)
{
  udf_func *udf;

  if (udf_startup == false)
    return NULL;

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

  return (udf);
}

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

  using_udf_functions= 1;

  return true;
}

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

  if (udf_startup == false)
  {
    udf_init();
    udf_startup= true;
  }
	  
  /* allocate the udf_func structure */
  udff = (udf_func *)my_malloc(sizeof(udf_func), MYF(MY_WME | MY_ZEROFILL));
  if (udff == NULL) return 1;

  plugin->data= (void *)udff;

  if (plugin->plugin->init)
  {
    /* todo, if the plugin doesnt have an init, bail out */

    if (plugin->plugin->init((void *)udff))
    {
      sql_print_error("Plugin '%s' init function returned error.",
                      plugin->name.str);
      goto err;
    }
  }
  
  add_udf(udff);

  return 0;
err:
  my_free(udff, MYF(0));
  return 1;
}

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)
    my_free(udff, MYF(0));

  return 0;
}