1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2000 MySQL AB
5
* Copyright (C) 2008, 2009 Sun Microsystems, Inc.
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; version 2 of the License.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1
/* Copyright (C) 2000 MySQL AB
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.
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.
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 */
21
16
/* This implements 'user defined functions' */
24
#include <boost/unordered_map.hpp>
17
#include <drizzled/server_includes.h>
26
18
#include <drizzled/gettext.h>
27
#include "drizzled/plugin/function.h"
28
#include <drizzled/function_container.h>
30
#include "drizzled/util/string.h"
35
static plugin::Function::UdfMap udf_registry;
37
const plugin::Function::UdfMap &plugin::Function::getMap()
42
bool plugin::Function::addPlugin(const plugin::Function *udf)
44
if (FunctionContainer::getMap().find(udf->getName()) != FunctionContainer::getMap().end())
46
errmsg_printf(error::ERROR,
47
_("A function named %s already exists!\n"),
48
udf->getName().c_str());
52
if (udf_registry.find(udf->getName()) != udf_registry.end())
54
errmsg_printf(error::ERROR,
55
_("A function named %s already exists!\n"),
56
udf->getName().c_str());
60
std::pair<UdfMap::iterator, bool> ret=
61
udf_registry.insert(make_pair(udf->getName(), udf));
62
if (ret.second == false)
64
errmsg_printf(error::ERROR,
65
_("Could not add Function!\n"));
73
void plugin::Function::removePlugin(const plugin::Function *udf)
75
udf_registry.erase(udf->getName());
78
const plugin::Function *plugin::Function::get(const char *name, size_t length)
80
UdfMap::iterator iter= udf_registry.find(std::string(name, length));
81
if (iter == udf_registry.end())
19
#include <mysys/hash.h>
20
#include <drizzled/sql_udf.h>
22
static bool udf_startup= false; /* We do not lock because startup is single threaded */
26
extern "C" unsigned char* get_hash_key(const unsigned char *buff, size_t *length,
27
bool not_used __attribute__((unused)))
29
udf_func *udf= (udf_func*) buff;
30
*length= (uint) udf->name.length;
31
return (unsigned char*) udf->name.str;
37
init_sql_alloc(&mem, UDF_ALLOC_BLOCK_SIZE, 0);
39
if (hash_init(&udf_hash, system_charset_info, 32, 0, 0, get_hash_key, NULL, 0))
41
sql_print_error(_("Can't allocate memory for udf structures"));
43
free_root(&mem, MYF(0));
48
/* called by mysqld.cc clean_up() */
52
free_root(&mem, MYF(0));
55
/* This is only called if using_udf_functions != 0 */
56
udf_func *find_udf(const char *name, uint32_t length)
60
if (udf_startup == false)
85
return (*iter).second;
88
} /* namespace drizzled */
63
udf= (udf_func*) hash_search(&udf_hash,
64
(unsigned char*) name,
65
length ? length : (uint) strlen(name));
70
static bool add_udf(udf_func *udf)
72
if (my_hash_insert(&udf_hash, (unsigned char*) udf))
75
using_udf_functions= 1;
80
int initialize_udf(st_plugin_int *plugin)
84
if (udf_startup == false)
90
if (plugin->plugin->init)
93
if ((r= plugin->plugin->init((void *)&f)))
95
sql_print_error("Plugin '%s' init function returned error %d.",
110
int finalize_udf(st_plugin_int *plugin)
112
udf_func *udff = (udf_func *)plugin->data;
114
/* TODO: Issue warning on failure */
115
if (udff && plugin->plugin->deinit)
116
(void)plugin->plugin->deinit(udff);