~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_udf.cc

  • Committer: Brian Aker
  • Date: 2009-03-27 22:55:28 UTC
  • mto: This revision was merged to the branch mainline in revision 968.
  • Revision ID: brian@tangent.org-20090327225528-8y76cfx8a4oemqv9
Remove ref_count

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 
16
16
/* This implements 'user defined functions' */
17
17
#include <drizzled/server_includes.h>
18
 
#include <libdrizzle/gettext.h>
 
18
#include <drizzled/gettext.h>
 
19
#include <drizzled/sql_udf.h>
 
20
 
 
21
#include <map>
 
22
#include <string>
 
23
 
 
24
using namespace std;
19
25
 
20
26
static bool udf_startup= false; /* We do not lock because startup is single threaded */
21
 
static MEM_ROOT mem;
22
 
static HASH udf_hash;
23
 
 
24
 
extern "C" unsigned char* get_hash_key(const unsigned char *buff, size_t *length,
25
 
                               bool not_used __attribute__((unused)))
26
 
{
27
 
  udf_func *udf= (udf_func*) buff;
28
 
  *length= (uint) udf->name.length;
29
 
  return (unsigned char*) udf->name.str;
30
 
}
31
 
 
32
 
 
33
 
void udf_init()
34
 
{
35
 
  init_sql_alloc(&mem, UDF_ALLOC_BLOCK_SIZE, 0);
36
 
 
37
 
  if (hash_init(&udf_hash, system_charset_info, 32, 0, 0, get_hash_key, NULL, 0))
38
 
  {
39
 
    sql_print_error(_("Can't allocate memory for udf structures"));
40
 
    hash_free(&udf_hash);
41
 
    free_root(&mem, MYF(0));
42
 
    return;
43
 
  }
44
 
}
45
 
 
46
 
/* called by mysqld.cc clean_up() */
47
 
void udf_free()
48
 
{
49
 
  hash_free(&udf_hash);
50
 
  free_root(&mem, MYF(0));
51
 
}
 
27
static map<string, Function_builder *> udf_map;
 
28
 
52
29
 
53
30
/* This is only called if using_udf_functions != 0 */
54
 
udf_func *find_udf(const char *name, uint32_t length)
 
31
Function_builder *find_udf(const char *name, uint32_t length)
55
32
{
56
 
  udf_func *udf;
 
33
 
 
34
  /**
 
35
   * @todo: check without transform, then check with transform if needed
 
36
   */
 
37
  Function_builder *udf= NULL;
57
38
 
58
39
  if (udf_startup == false)
59
40
    return NULL;
60
41
 
61
 
  udf= (udf_func*) hash_search(&udf_hash,
62
 
                               (unsigned char*) name,
63
 
                               length ? length : (uint) strlen(name));
 
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;
64
50
 
65
51
  return (udf);
66
52
}
67
53
 
68
 
static bool add_udf(udf_func *udf)
 
54
static bool add_udf(Function_builder *udf)
69
55
{
70
 
  if (my_hash_insert(&udf_hash, (unsigned char*) udf))
71
 
    return false;
 
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;
72
64
 
73
65
  using_udf_functions= 1;
74
66
 
77
69
 
78
70
int initialize_udf(st_plugin_int *plugin)
79
71
{
80
 
  udf_func *f;
 
72
  Function_builder *f;
81
73
 
82
 
  if (udf_startup == false)
83
 
  {
84
 
    udf_init();
85
 
    udf_startup= true;
86
 
  }
 
74
  udf_startup= true;
87
75
 
88
76
  if (plugin->plugin->init)
89
77
  {
90
78
    int r;
91
79
    if ((r= plugin->plugin->init((void *)&f)))
92
80
    {
93
 
      sql_print_error("Plugin '%s' init function returned error %d.",
94
 
                      plugin->name.str, r);
 
81
      errmsg_printf(ERRMSG_LVL_ERROR,
 
82
                    _("Plugin '%s' init function returned error %d."),
 
83
                    plugin->name.str, r);
95
84
      return r;
96
85
    }
97
86
  }
98
87
  else
99
88
    return 1;
100
89
 
101
 
  if(!add_udf(f))
102
 
    return 2;
 
90
  if (!add_udf(f))
 
91
    return 1;
103
92
 
104
93
  return 0;
105
94
 
106
95
}
107
96
 
108
97
int finalize_udf(st_plugin_int *plugin)
109
 
110
 
  udf_func *udff = (udf_func *)plugin->data;
 
98
{
 
99
  Function_builder *udff = (Function_builder *)plugin->data;
111
100
 
112
101
  /* TODO: Issue warning on failure */
113
102
  if (udff && plugin->plugin->deinit)