~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_udf.cc

  • Committer: Brian Aker
  • Date: 2009-03-20 18:52:05 UTC
  • mfrom: (950.1.1 mordred)
  • Revision ID: brian@tangent.org-20090320185205-g7o6kq17r25b6odf
Merge Monty

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
 
 
 
17
#include <drizzled/server_includes.h>
26
18
#include <drizzled/gettext.h>
27
 
#include "drizzled/plugin/function.h"
28
 
 
29
 
#include "drizzled/util/string.h"
30
 
 
31
 
namespace drizzled
32
 
{
33
 
 
34
 
static plugin::Function::UdfMap udf_registry;
35
 
 
36
 
const plugin::Function::UdfMap &plugin::Function::getMap()
37
 
{
38
 
  return udf_registry;
39
 
}
40
 
 
41
 
bool plugin::Function::addPlugin(const plugin::Function *udf)
42
 
{
43
 
  if (udf_registry.find(udf->getName()) != udf_registry.end())
44
 
  {
45
 
    errmsg_printf(ERRMSG_LVL_ERROR,
46
 
                  _("A function named %s already exists!\n"),
47
 
                  udf->getName().c_str());
48
 
    return true;
49
 
  }
50
 
  std::pair<UdfMap::iterator, bool> ret=
51
 
    udf_registry.insert(make_pair(udf->getName(), udf));
52
 
  if (ret.second == false)
53
 
  {
54
 
    errmsg_printf(ERRMSG_LVL_ERROR,
55
 
                  _("Could not add Function!\n"));
56
 
    return true;
57
 
  }
58
 
  return false;
59
 
}
60
 
 
61
 
 
62
 
void plugin::Function::removePlugin(const plugin::Function *udf)
63
 
{
64
 
  udf_registry.erase(udf->getName());
65
 
}
66
 
 
67
 
const plugin::Function *plugin::Function::get(const char *name, size_t length)
68
 
{
69
 
  UdfMap::iterator iter= udf_registry.find(std::string(name, length));
70
 
  if (iter == udf_registry.end())
71
 
  {
 
19
#include <drizzled/sql_udf.h>
 
20
 
 
21
#include <map>
 
22
#include <string>
 
23
 
 
24
using namespace std;
 
25
 
 
26
static bool udf_startup= false; /* We do not lock because startup is single threaded */
 
27
static map<string, Function_builder *> udf_map;
 
28
 
 
29
 
 
30
/* This is only called if using_udf_functions != 0 */
 
31
Function_builder *find_udf(const char *name, uint32_t length)
 
32
{
 
33
 
 
34
  /**
 
35
   * @todo: check without transform, then check with transform if needed
 
36
   */
 
37
  Function_builder *udf= NULL;
 
38
 
 
39
  if (udf_startup == false)
72
40
    return NULL;
 
41
 
 
42
  string find_str(name, length);
 
43
  transform(find_str.begin(), find_str.end(),
 
44
            find_str.begin(), ::tolower);
 
45
 
 
46
  map<string, Function_builder *>::iterator find_iter;
 
47
  find_iter=  udf_map.find(find_str);
 
48
  if (find_iter != udf_map.end())
 
49
    udf= (*find_iter).second;
 
50
 
 
51
  return (udf);
 
52
}
 
53
 
 
54
static bool add_udf(Function_builder *udf)
 
55
{
 
56
  /**
 
57
   * @todo: add all lower and all upper version
 
58
   */
 
59
  string add_str= udf->get_name();
 
60
  transform(add_str.begin(), add_str.end(),
 
61
            add_str.begin(), ::tolower);
 
62
 
 
63
  udf_map[add_str]= udf;
 
64
 
 
65
  using_udf_functions= 1;
 
66
 
 
67
  return true;
 
68
}
 
69
 
 
70
int initialize_udf(st_plugin_int *plugin)
 
71
{
 
72
  Function_builder *f;
 
73
 
 
74
  udf_startup= true;
 
75
 
 
76
  if (plugin->plugin->init)
 
77
  {
 
78
    int r;
 
79
    if ((r= plugin->plugin->init((void *)&f)))
 
80
    {
 
81
      errmsg_printf(ERRMSG_LVL_ERROR,
 
82
                    _("Plugin '%s' init function returned error %d."),
 
83
                    plugin->name.str, r);
 
84
      return r;
 
85
    }
73
86
  }
74
 
  return (*iter).second;
75
 
}
76
 
 
77
 
} /* namespace drizzled */
 
87
  else
 
88
    return 1;
 
89
 
 
90
  if (!add_udf(f))
 
91
    return 1;
 
92
 
 
93
  plugin->state= PLUGIN_IS_READY;
 
94
 
 
95
  return 0;
 
96
 
 
97
}
 
98
 
 
99
int finalize_udf(st_plugin_int *plugin)
 
100
{
 
101
  Function_builder *udff = (Function_builder *)plugin->data;
 
102
 
 
103
  /* TODO: Issue warning on failure */
 
104
  if (udff && plugin->plugin->deinit)
 
105
    (void)plugin->plugin->deinit(udff);
 
106
 
 
107
  if (udff)
 
108
    free(udff);
 
109
 
 
110
  return 0;
 
111
}
 
112