~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000-2001, 2003-2006 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 */
15
16
17
/* This file defines structures needed by udf functions */
18
19
#ifdef USE_PRAGMA_INTERFACE
20
#pragma interface
21
#endif
22
23
enum Item_udftype {UDFTYPE_FUNCTION=1,UDFTYPE_AGGREGATE};
24
25
typedef void (*Udf_func_clear)(UDF_INIT *, uchar *, uchar *);
26
typedef void (*Udf_func_add)(UDF_INIT *, UDF_ARGS *, uchar *, uchar *);
27
typedef void (*Udf_func_deinit)(UDF_INIT*);
28
typedef my_bool (*Udf_func_init)(UDF_INIT *, UDF_ARGS *,  char *);
29
typedef void (*Udf_func_any)();
30
typedef double (*Udf_func_double)(UDF_INIT *, UDF_ARGS *, uchar *, uchar *);
31
typedef longlong (*Udf_func_longlong)(UDF_INIT *, UDF_ARGS *, uchar *,
32
                                      uchar *);
33
34
typedef struct st_udf_func
35
{
36
  LEX_STRING name;
37
  Item_result returns;
38
  Item_udftype type;
39
  char *dl;
40
  void *dlhandle;
41
  Udf_func_any func;
42
  Udf_func_init func_init;
43
  Udf_func_deinit func_deinit;
44
  Udf_func_clear func_clear;
45
  Udf_func_add func_add;
46
  ulong usage_count;
47
} udf_func;
48
49
class Item_result_field;
50
51
class udf_handler :public Sql_alloc
52
{
53
 protected:
54
  udf_func *u_d;
55
  String *buffers;
56
  UDF_ARGS f_args;
57
  UDF_INIT initid;
58
  char *num_buffer;
59
  uchar error, is_null;
60
  bool initialized;
61
  Item **args;
62
63
 public:
64
  table_map used_tables_cache;
65
  bool const_item_cache;
66
  bool not_original;
67
  udf_handler(udf_func *udf_arg) :u_d(udf_arg), buffers(0), error(0),
68
    is_null(0), initialized(0), not_original(0)
69
  {}
70
  ~udf_handler();
71
  const char *name() const { return u_d ? u_d->name.str : "?"; }
72
  Item_result result_type () const
73
  { return u_d	? u_d->returns : STRING_RESULT;}
74
  bool get_arguments();
75
  bool fix_fields(THD *thd, Item_result_field *item,
76
		  uint arg_count, Item **args);
77
  void cleanup();
78
  double val(my_bool *null_value)
79
  {
80
    is_null= 0;
81
    if (get_arguments())
82
    {
83
      *null_value=1;
84
      return 0.0;
85
    }
86
    Udf_func_double func= (Udf_func_double) u_d->func;
87
    double tmp=func(&initid, &f_args, &is_null, &error);
88
    if (is_null || error)
89
    {
90
      *null_value=1;
91
      return 0.0;
92
    }
93
    *null_value=0;
94
    return tmp;
95
  }
96
  longlong val_int(my_bool *null_value)
97
  {
98
    is_null= 0;
99
    if (get_arguments())
100
    {
101
      *null_value=1;
102
      return LL(0);
103
    }
104
    Udf_func_longlong func= (Udf_func_longlong) u_d->func;
105
    longlong tmp=func(&initid, &f_args, &is_null, &error);
106
    if (is_null || error)
107
    {
108
      *null_value=1;
109
      return LL(0);
110
    }
111
    *null_value=0;
112
    return tmp;
113
  }
114
  my_decimal *val_decimal(my_bool *null_value, my_decimal *dec_buf);
115
  void clear()
116
  {
117
    is_null= 0;
118
    Udf_func_clear func= u_d->func_clear;
119
    func(&initid, &is_null, &error);
120
  }
121
  void add(my_bool *null_value)
122
  {
123
    if (get_arguments())
124
    {
125
      *null_value=1;
126
      return;
127
    }
128
    Udf_func_add func= u_d->func_add;
129
    func(&initid, &f_args, &is_null, &error);
130
    *null_value= (my_bool) (is_null || error);
131
  }
132
  String *val_str(String *str,String *save_str);
133
};
134
135
136
#ifdef HAVE_DLOPEN
137
void udf_init(void),udf_free(void);
138
udf_func *find_udf(const char *name, uint len=0,bool mark_used=0);
139
void free_udf(udf_func *udf);
140
int mysql_create_function(THD *thd,udf_func *udf);
141
int mysql_drop_function(THD *thd,const LEX_STRING *name);
142
#endif