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' */
|
|
243.1.17
by Jay Pipes
FINAL PHASE removal of mysql_priv.h (Bye, bye my friend.) |
17 |
#include <drizzled/server_includes.h> |
538
by Monty Taylor
Moved gettext.h into drizzled in anticipation of the new client lib. |
18 |
#include <drizzled/gettext.h> |
584.1.15
by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes. |
19 |
#include <drizzled/sql_udf.h> |
1
by brian
clean slate |
20 |
|
873.2.22
by Monty Taylor
Got rid of my_hash_sort_utf8mb4 for udfs. |
21 |
#include <map> |
22 |
#include <string> |
|
23 |
||
24 |
using namespace std; |
|
25 |
||
134.1.1
by Mark Atwood
more hackery to get plugin UDFs working |
26 |
static bool udf_startup= false; /* We do not lock because startup is single threaded */ |
942.1.12
by Monty Taylor
Converted udf_func into a factory. |
27 |
static map<string, Function_builder *> udf_map; |
1
by brian
clean slate |
28 |
|
29 |
||
30 |
/* This is only called if using_udf_functions != 0 */
|
|
942.1.12
by Monty Taylor
Converted udf_func into a factory. |
31 |
Function_builder *find_udf(const char *name, uint32_t length) |
134.1.1
by Mark Atwood
more hackery to get plugin UDFs working |
32 |
{
|
942.1.12
by Monty Taylor
Converted udf_func into a factory. |
33 |
|
34 |
/**
|
|
35 |
* @todo: check without transform, then check with transform if needed
|
|
36 |
*/
|
|
37 |
Function_builder *udf= NULL; |
|
134.1.1
by Mark Atwood
more hackery to get plugin UDFs working |
38 |
|
39 |
if (udf_startup == false) |
|
40 |
return NULL; |
|
41 |
||
873.2.22
by Monty Taylor
Got rid of my_hash_sort_utf8mb4 for udfs. |
42 |
string find_str(name, length); |
43 |
transform(find_str.begin(), find_str.end(), |
|
44 |
find_str.begin(), ::tolower); |
|
45 |
||
942.1.12
by Monty Taylor
Converted udf_func into a factory. |
46 |
map<string, Function_builder *>::iterator find_iter; |
873.2.22
by Monty Taylor
Got rid of my_hash_sort_utf8mb4 for udfs. |
47 |
find_iter= udf_map.find(find_str); |
48 |
if (find_iter != udf_map.end()) |
|
49 |
udf= (*find_iter).second; |
|
134.1.1
by Mark Atwood
more hackery to get plugin UDFs working |
50 |
|
51 |
return (udf); |
|
52 |
}
|
|
53 |
||
942.1.12
by Monty Taylor
Converted udf_func into a factory. |
54 |
static bool add_udf(Function_builder *udf) |
134.1.1
by Mark Atwood
more hackery to get plugin UDFs working |
55 |
{
|
942.1.12
by Monty Taylor
Converted udf_func into a factory. |
56 |
/**
|
57 |
* @todo: add all lower and all upper version
|
|
58 |
*/
|
|
59 |
string add_str= udf->get_name(); |
|
873.2.22
by Monty Taylor
Got rid of my_hash_sort_utf8mb4 for udfs. |
60 |
transform(add_str.begin(), add_str.end(), |
61 |
add_str.begin(), ::tolower); |
|
62 |
||
63 |
udf_map[add_str]= udf; |
|
134.1.1
by Mark Atwood
more hackery to get plugin UDFs working |
64 |
|
65 |
using_udf_functions= 1; |
|
66 |
||
67 |
return true; |
|
68 |
}
|
|
69 |
||
968.2.31
by Monty Taylor
Fixed UDF de-initialization. |
70 |
static bool remove_udf(Function_builder *udf) |
71 |
{
|
|
72 |
return udf_map.erase(udf->get_name()); |
|
73 |
}
|
|
74 |
||
134.1.1
by Mark Atwood
more hackery to get plugin UDFs working |
75 |
int initialize_udf(st_plugin_int *plugin) |
76 |
{
|
|
942.1.12
by Monty Taylor
Converted udf_func into a factory. |
77 |
Function_builder *f; |
134.1.1
by Mark Atwood
more hackery to get plugin UDFs working |
78 |
|
942.1.13
by Monty Taylor
Removed unused code. |
79 |
udf_startup= true; |
134.1.1
by Mark Atwood
more hackery to get plugin UDFs working |
80 |
|
81 |
if (plugin->plugin->init) |
|
82 |
{
|
|
139.1.8
by Stewart Smith
UDFs are now normal Item_func objects. Simplifies handling them a lot. |
83 |
int r; |
84 |
if ((r= plugin->plugin->init((void *)&f))) |
|
134.1.1
by Mark Atwood
more hackery to get plugin UDFs working |
85 |
{
|
942.1.13
by Monty Taylor
Removed unused code. |
86 |
errmsg_printf(ERRMSG_LVL_ERROR, |
87 |
_("Plugin '%s' init function returned error %d."), |
|
88 |
plugin->name.str, r); |
|
139.1.8
by Stewart Smith
UDFs are now normal Item_func objects. Simplifies handling them a lot. |
89 |
return r; |
134.1.1
by Mark Atwood
more hackery to get plugin UDFs working |
90 |
}
|
91 |
}
|
|
139.1.8
by Stewart Smith
UDFs are now normal Item_func objects. Simplifies handling them a lot. |
92 |
else
|
93 |
return 1; |
|
94 |
||
810
by Brian Aker
Fix for making sure I_S has good information about which plugins are |
95 |
if (!add_udf(f)) |
96 |
return 1; |
|
97 |
||
968.2.31
by Monty Taylor
Fixed UDF de-initialization. |
98 |
plugin->data= f; |
134.1.1
by Mark Atwood
more hackery to get plugin UDFs working |
99 |
return 0; |
139.1.8
by Stewart Smith
UDFs are now normal Item_func objects. Simplifies handling them a lot. |
100 |
|
134.1.1
by Mark Atwood
more hackery to get plugin UDFs working |
101 |
}
|
102 |
||
103 |
int finalize_udf(st_plugin_int *plugin) |
|
660.1.3
by Eric Herman
removed trailing whitespace with simple script: |
104 |
{
|
968.2.31
by Monty Taylor
Fixed UDF de-initialization. |
105 |
Function_builder *udf = static_cast<Function_builder *>(plugin->data); |
106 |
||
107 |
if (udf != NULL) |
|
108 |
{
|
|
109 |
remove_udf(udf); |
|
110 |
||
111 |
if (plugin->plugin->deinit) |
|
112 |
{
|
|
113 |
if (plugin->plugin->deinit((void *)udf)) |
|
114 |
{
|
|
115 |
/* TRANSLATORS: The leading word "udf" is the name
|
|
116 |
of the plugin api, and so should not be translated. */
|
|
117 |
errmsg_printf(ERRMSG_LVL_ERROR, _("udf plugin '%s' deinit() failed"), |
|
118 |
plugin->name.str); |
|
119 |
}
|
|
120 |
}
|
|
121 |
||
122 |
}
|
|
134.1.1
by Mark Atwood
more hackery to get plugin UDFs working |
123 |
|
124 |
return 0; |
|
125 |
}
|
|
126 |