~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_udf.cc

  • Committer: Andy Lester
  • Date: 2008-08-09 05:23:39 UTC
  • mto: (266.1.29 use-replace-funcs)
  • mto: This revision was merged to the branch mainline in revision 287.
  • Revision ID: andy@petdance.com-20080809052339-iafoesszmesweq6b
use NULL, not 0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 *
4
 
 *  Copyright (C) 2000 MySQL AB
5
 
 *  Copyright (C) 2008, 2009 Sun Microsystems, Inc.
6
 
 *
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.
10
 
 *
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.
15
 
 *
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
19
 
 */
 
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 */
20
15
 
21
16
/* This implements 'user defined functions' */
22
 
#include "config.h"
23
 
 
24
 
#include <boost/unordered_map.hpp>
25
 
 
26
 
#include <drizzled/gettext.h>
27
 
#include "drizzled/plugin/function.h"
28
 
#include <drizzled/function_container.h>
29
 
 
30
 
#include "drizzled/util/string.h"
31
 
 
32
 
namespace drizzled
33
 
{
34
 
 
35
 
static plugin::Function::UdfMap udf_registry;
36
 
 
37
 
const plugin::Function::UdfMap &plugin::Function::getMap()
38
 
{
39
 
  return udf_registry;
40
 
}
41
 
 
42
 
bool plugin::Function::addPlugin(const plugin::Function *udf)
43
 
{
44
 
  if (FunctionContainer::getMap().find(udf->getName()) != FunctionContainer::getMap().end())
45
 
  {
46
 
    errmsg_printf(error::ERROR,
47
 
                  _("A function named %s already exists!\n"),
48
 
                  udf->getName().c_str());
49
 
    return true;
50
 
  }
51
 
 
52
 
  if (udf_registry.find(udf->getName()) != udf_registry.end())
53
 
  {
54
 
    errmsg_printf(error::ERROR,
55
 
                  _("A function named %s already exists!\n"),
56
 
                  udf->getName().c_str());
57
 
    return true;
58
 
  }
59
 
 
60
 
  std::pair<UdfMap::iterator, bool> ret=
61
 
    udf_registry.insert(make_pair(udf->getName(), udf));
62
 
  if (ret.second == false)
63
 
  {
64
 
    errmsg_printf(error::ERROR,
65
 
                  _("Could not add Function!\n"));
66
 
    return true;
67
 
  }
68
 
 
69
 
  return false;
70
 
}
71
 
 
72
 
 
73
 
void plugin::Function::removePlugin(const plugin::Function *udf)
74
 
{
75
 
  udf_registry.erase(udf->getName());
76
 
}
77
 
 
78
 
const plugin::Function *plugin::Function::get(const char *name, size_t length)
79
 
{
80
 
  UdfMap::iterator iter= udf_registry.find(std::string(name, length));
81
 
  if (iter == udf_registry.end())
82
 
  {
 
17
#include <drizzled/server_includes.h>
 
18
 
 
19
static bool udf_startup= false; /* We do not lock because startup is single threaded */
 
20
static MEM_ROOT mem;
 
21
static HASH udf_hash;
 
22
 
 
23
extern "C" uchar* get_hash_key(const uchar *buff, size_t *length,
 
24
                               bool not_used __attribute__((unused)))
 
25
{
 
26
  udf_func *udf= (udf_func*) buff;
 
27
  *length= (uint) udf->name.length;
 
28
  return (uchar*) udf->name.str;
 
29
}
 
30
 
 
31
 
 
32
void udf_init()
 
33
{
 
34
  init_sql_alloc(&mem, UDF_ALLOC_BLOCK_SIZE, 0);
 
35
 
 
36
  if (hash_init(&udf_hash, system_charset_info, 32, 0, 0, get_hash_key, NULL, 0))
 
37
  {
 
38
    sql_print_error("Can't allocate memory for udf structures");
 
39
    hash_free(&udf_hash);
 
40
    free_root(&mem, MYF(0));
 
41
    return;
 
42
  }
 
43
}
 
44
 
 
45
/* called by mysqld.cc clean_up() */
 
46
void udf_free()
 
47
{
 
48
  hash_free(&udf_hash);
 
49
  free_root(&mem, MYF(0));
 
50
}
 
51
 
 
52
/* This is only called if using_udf_functions != 0 */
 
53
udf_func *find_udf(const char *name, uint length)
 
54
{
 
55
  udf_func *udf;
 
56
 
 
57
  if (udf_startup == false)
83
58
    return NULL;
84
 
  }
85
 
  return (*iter).second;
86
 
}
87
 
 
88
 
} /* namespace drizzled */
 
59
 
 
60
  udf= (udf_func*) hash_search(&udf_hash,
 
61
                               (uchar*) name,
 
62
                               length ? length : (uint) strlen(name));
 
63
 
 
64
  return (udf);
 
65
}
 
66
 
 
67
static bool add_udf(udf_func *udf)
 
68
{
 
69
  if (my_hash_insert(&udf_hash, (uchar*) udf))
 
70
    return false;
 
71
 
 
72
  using_udf_functions= 1;
 
73
 
 
74
  return true;
 
75
}
 
76
 
 
77
int initialize_udf(st_plugin_int *plugin)
 
78
{
 
79
  udf_func *udff;
 
80
 
 
81
  if (udf_startup == false)
 
82
  {
 
83
    udf_init();
 
84
    udf_startup= true;
 
85
  }
 
86
          
 
87
  /* allocate the udf_func structure */
 
88
  udff = (udf_func *)my_malloc(sizeof(udf_func), MYF(MY_WME | MY_ZEROFILL));
 
89
  if (udff == NULL) return 1;
 
90
 
 
91
  plugin->data= (void *)udff;
 
92
 
 
93
  if (plugin->plugin->init)
 
94
  {
 
95
    /* todo, if the plugin doesnt have an init, bail out */
 
96
 
 
97
    if (plugin->plugin->init((void *)udff))
 
98
    {
 
99
      sql_print_error("Plugin '%s' init function returned error.",
 
100
                      plugin->name.str);
 
101
      goto err;
 
102
    }
 
103
  }
 
104
  
 
105
  add_udf(udff);
 
106
 
 
107
  return 0;
 
108
err:
 
109
  my_free(udff, MYF(0));
 
110
  return 1;
 
111
}
 
112
 
 
113
int finalize_udf(st_plugin_int *plugin)
 
114
 
115
  udf_func *udff = (udf_func *)plugin->data;
 
116
 
 
117
  /* TODO: Issue warning on failure */
 
118
  if (udff && plugin->plugin->deinit)
 
119
    (void)plugin->plugin->deinit(udff);
 
120
 
 
121
  if (udff)
 
122
    my_free(udff, MYF(0));
 
123
 
 
124
  return 0;
 
125
}
 
126