~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_udf.cc

  • Committer: Brian Aker
  • Date: 2009-01-24 09:43:35 UTC
  • Revision ID: brian@gir-3.local-20090124094335-6qdtvc35gl5fvivz
Adding in an example singe thread scheduler

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"
 
17
#include <drizzled/server_includes.h>
23
18
#include <drizzled/gettext.h>
24
 
#include "drizzled/hash.h"
25
 
#include "drizzled/plugin/function.h"
26
 
 
27
 
using namespace std;
28
 
 
29
 
namespace drizzled
30
 
{
31
 
 
32
 
typedef hash_map<string, const plugin::Function *> UdfMap;
33
 
static UdfMap udf_registry;
34
 
 
35
 
bool plugin::Function::addPlugin(const plugin::Function *udf)
36
 
{
37
 
  string lower_name(udf->getName());
38
 
  transform(lower_name.begin(), lower_name.end(),
39
 
            lower_name.begin(), ::tolower);
40
 
  if (udf_registry.find(lower_name) != udf_registry.end())
41
 
  {
42
 
    errmsg_printf(ERRMSG_LVL_ERROR,
43
 
                  _("A function named %s already exists!\n"),
44
 
                  udf->getName().c_str());
45
 
    return true;
46
 
  }
47
 
  pair<UdfMap::iterator, bool> ret=
48
 
    udf_registry.insert(make_pair(lower_name, udf));
49
 
  if (ret.second == false)
50
 
  {
51
 
    errmsg_printf(ERRMSG_LVL_ERROR,
52
 
                  _("Could not add Function!\n"));
53
 
    return true;
54
 
  }
55
 
  return false;
56
 
}
57
 
 
58
 
 
59
 
void plugin::Function::removePlugin(const plugin::Function *udf)
60
 
{
61
 
  string lower_name(udf->getName());
62
 
  transform(lower_name.begin(), lower_name.end(),
63
 
            lower_name.begin(), ::tolower);
64
 
  udf_registry.erase(lower_name);
65
 
}
66
 
 
67
 
const plugin::Function *plugin::Function::get(const char *name, size_t length)
68
 
{
69
 
  string lower_name(name, length);
70
 
  transform(lower_name.begin(), lower_name.end(),
71
 
            lower_name.begin(), ::tolower);
72
 
  UdfMap::iterator iter= udf_registry.find(lower_name);
73
 
  if (iter == udf_registry.end())
74
 
  {
 
19
#include <mysys/hash.h>
 
20
#include <drizzled/sql_udf.h>
 
21
 
 
22
static bool udf_startup= false; /* We do not lock because startup is single threaded */
 
23
static MEM_ROOT mem;
 
24
static HASH udf_hash;
 
25
 
 
26
extern "C" unsigned char* get_hash_key(const unsigned char *buff, size_t *length,
 
27
                               bool not_used __attribute__((unused)))
 
28
{
 
29
  udf_func *udf= (udf_func*) buff;
 
30
  *length= (uint) udf->name.length;
 
31
  return (unsigned char*) udf->name.str;
 
32
}
 
33
 
 
34
 
 
35
void udf_init()
 
36
{
 
37
  init_sql_alloc(&mem, UDF_ALLOC_BLOCK_SIZE, 0);
 
38
 
 
39
  if (hash_init(&udf_hash, system_charset_info, 32, 0, 0, get_hash_key, NULL, 0))
 
40
  {
 
41
    errmsg_printf(ERRMSG_LVL_ERROR, _("Can't allocate memory for udf structures"));
 
42
    hash_free(&udf_hash);
 
43
    free_root(&mem, MYF(0));
 
44
    return;
 
45
  }
 
46
}
 
47
 
 
48
/* called by mysqld.cc clean_up() */
 
49
void udf_free()
 
50
{
 
51
  hash_free(&udf_hash);
 
52
  free_root(&mem, MYF(0));
 
53
}
 
54
 
 
55
/* This is only called if using_udf_functions != 0 */
 
56
udf_func *find_udf(const char *name, uint32_t length)
 
57
{
 
58
  udf_func *udf;
 
59
 
 
60
  if (udf_startup == false)
75
61
    return NULL;
76
 
  }
77
 
  return (*iter).second;
78
 
}
79
 
 
80
 
} /* namespace drizzled */
 
62
 
 
63
  udf= (udf_func*) hash_search(&udf_hash,
 
64
                               (unsigned char*) name,
 
65
                               length ? length : (uint) strlen(name));
 
66
 
 
67
  return (udf);
 
68
}
 
69
 
 
70
static bool add_udf(udf_func *udf)
 
71
{
 
72
  if (my_hash_insert(&udf_hash, (unsigned char*) udf))
 
73
    return false;
 
74
 
 
75
  using_udf_functions= 1;
 
76
 
 
77
  return true;
 
78
}
 
79
 
 
80
int initialize_udf(st_plugin_int *plugin)
 
81
{
 
82
  udf_func *f;
 
83
 
 
84
  if (udf_startup == false)
 
85
  {
 
86
    udf_init();
 
87
    udf_startup= true;
 
88
  }
 
89
 
 
90
  if (plugin->plugin->init)
 
91
  {
 
92
    int r;
 
93
    if ((r= plugin->plugin->init((void *)&f)))
 
94
    {
 
95
      errmsg_printf(ERRMSG_LVL_ERROR, "Plugin '%s' init function returned error %d.",
 
96
                    plugin->name.str, r);
 
97
      return r;
 
98
    }
 
99
  }
 
100
  else
 
101
    return 1;
 
102
 
 
103
  if(!add_udf(f))
 
104
    return 2;
 
105
 
 
106
  return 0;
 
107
 
 
108
}
 
109
 
 
110
int finalize_udf(st_plugin_int *plugin)
 
111
{
 
112
  udf_func *udff = (udf_func *)plugin->data;
 
113
 
 
114
  /* TODO: Issue warning on failure */
 
115
  if (udff && plugin->plugin->deinit)
 
116
    (void)plugin->plugin->deinit(udff);
 
117
 
 
118
  if (udff)
 
119
    free(udff);
 
120
 
 
121
  return 0;
 
122
}
 
123