1
by brian
clean slate |
1 |
/* Copyright (C) 2000 MySQL AB
|
2 |
||
3 |
This program is free software; you can redistribute it and/or modify
|
|
4 |
it under the terms of the GNU General Public License as published by
|
|
5 |
the Free Software Foundation; version 2 of the License.
|
|
6 |
||
7 |
This program is distributed in the hope that it will be useful,
|
|
8 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
9 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
10 |
GNU General Public License for more details.
|
|
11 |
||
12 |
You should have received a copy of the GNU General Public License
|
|
13 |
along with this program; if not, write to the Free Software
|
|
14 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
15 |
||
16 |
/* This implements 'user defined functions' */
|
|
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" |
|
212.5.13
by Monty Taylor
Moved my_sys/my_pthread/my_nosys and mysys_err to mysys. |
28 |
#include <mysys/my_pthread.h> |
1
by brian
clean slate |
29 |
|
30 |
extern "C" |
|
31 |
{
|
|
32 |
#include <stdarg.h> |
|
212.5.17
by Monty Taylor
Moved queues and hash. |
33 |
#include <mysys/hash.h> |
1
by brian
clean slate |
34 |
}
|
35 |
||
134.1.1
by Mark Atwood
more hackery to get plugin UDFs working |
36 |
static bool udf_startup= false; /* We do not lock because startup is single threaded */ |
1
by brian
clean slate |
37 |
static MEM_ROOT mem; |
38 |
static HASH udf_hash; |
|
39 |
||
40 |
extern "C" uchar* get_hash_key(const uchar *buff, size_t *length, |
|
146
by Brian Aker
my_bool cleanup. |
41 |
bool not_used __attribute__((unused))) |
1
by brian
clean slate |
42 |
{
|
134.1.1
by Mark Atwood
more hackery to get plugin UDFs working |
43 |
udf_func *udf= (udf_func*) buff; |
44 |
*length= (uint) udf->name.length; |
|
1
by brian
clean slate |
45 |
return (uchar*) udf->name.str; |
46 |
}
|
|
47 |
||
48 |
||
49 |
void udf_init() |
|
50 |
{
|
|
51 |
init_sql_alloc(&mem, UDF_ALLOC_BLOCK_SIZE, 0); |
|
134.1.1
by Mark Atwood
more hackery to get plugin UDFs working |
52 |
|
53 |
if (hash_init(&udf_hash, system_charset_info, 32, 0, 0, get_hash_key, NULL, 0)) |
|
1
by brian
clean slate |
54 |
{
|
55 |
sql_print_error("Can't allocate memory for udf structures"); |
|
56 |
hash_free(&udf_hash); |
|
134.1.1
by Mark Atwood
more hackery to get plugin UDFs working |
57 |
free_root(&mem, MYF(0)); |
51.2.2
by Patrick Galbraith
Removed DBUGs from |
58 |
return; |
1
by brian
clean slate |
59 |
}
|
60 |
}
|
|
61 |
||
134.1.1
by Mark Atwood
more hackery to get plugin UDFs working |
62 |
/* called by mysqld.cc clean_up() */
|
1
by brian
clean slate |
63 |
void udf_free() |
64 |
{
|
|
65 |
hash_free(&udf_hash); |
|
134.1.1
by Mark Atwood
more hackery to get plugin UDFs working |
66 |
free_root(&mem, MYF(0)); |
67 |
}
|
|
1
by brian
clean slate |
68 |
|
69 |
/* This is only called if using_udf_functions != 0 */
|
|
141
by Brian Aker
Code cleanup. Mainly dead stuff :) |
70 |
udf_func *find_udf(const char *name, uint length) |
134.1.1
by Mark Atwood
more hackery to get plugin UDFs working |
71 |
{
|
1
by brian
clean slate |
72 |
udf_func *udf; |
134.1.1
by Mark Atwood
more hackery to get plugin UDFs working |
73 |
|
74 |
if (udf_startup == false) |
|
75 |
return NULL; |
|
76 |
||
77 |
udf= (udf_func*) hash_search(&udf_hash, |
|
78 |
(uchar*) name, |
|
79 |
length ? length : (uint) strlen(name)); |
|
80 |
||
81 |
return (udf); |
|
82 |
}
|
|
83 |
||
84 |
static bool add_udf(udf_func *udf) |
|
85 |
{
|
|
86 |
if (my_hash_insert(&udf_hash, (uchar*) udf)) |
|
87 |
return false; |
|
88 |
||
89 |
using_udf_functions= 1; |
|
90 |
||
91 |
return true; |
|
92 |
}
|
|
93 |
||
94 |
int initialize_udf(st_plugin_int *plugin) |
|
95 |
{
|
|
96 |
udf_func *udff; |
|
97 |
||
98 |
if (udf_startup == false) |
|
99 |
{
|
|
100 |
udf_init(); |
|
101 |
udf_startup= true; |
|
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; |
|
109 |
||
110 |
if (plugin->plugin->init) |
|
111 |
{
|
|
112 |
/* todo, if the plugin doesnt have an init, bail out */
|
|
113 |
||
114 |
if (plugin->plugin->init((void *)udff)) |
|
115 |
{
|
|
116 |
sql_print_error("Plugin '%s' init function returned error.", |
|
117 |
plugin->name.str); |
|
118 |
goto err; |
|
119 |
}
|
|
120 |
}
|
|
51.2.2
by Patrick Galbraith
Removed DBUGs from |
121 |
|
134.1.1
by Mark Atwood
more hackery to get plugin UDFs working |
122 |
add_udf(udff); |
123 |
||
124 |
return 0; |
|
125 |
err: |
|
126 |
my_free(udff, MYF(0)); |
|
127 |
return 1; |
|
128 |
}
|
|
129 |
||
130 |
int finalize_udf(st_plugin_int *plugin) |
|
131 |
{
|
|
132 |
udf_func *udff = (udf_func *)plugin->data; |
|
133 |
||
134 |
/* TODO: Issue warning on failure */
|
|
135 |
if (udff && plugin->plugin->deinit) |
|
136 |
(void)plugin->plugin->deinit(udff); |
|
137 |
||
138 |
if (udff) |
|
139 |
my_free(udff, MYF(0)); |
|
140 |
||
141 |
return 0; |
|
142 |
}
|
|
143 |