~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*);
197 by Brian Aker
More my_bool cleanup.
28
typedef bool (*Udf_func_init)(UDF_INIT *, UDF_ARGS *,  char *);
1 by brian
clean slate
29
typedef void (*Udf_func_any)();
30
typedef double (*Udf_func_double)(UDF_INIT *, UDF_ARGS *, uchar *, uchar *);
152 by Brian Aker
longlong replacement
31
typedef int64_t (*Udf_func_int64_t)(UDF_INIT *, UDF_ARGS *, uchar *,
1 by brian
clean slate
32
                                      uchar *);
33
34
typedef struct st_udf_func
35
{
36
  LEX_STRING name;
37
  Item_result returns;
38
  Item_udftype type;
39
  Udf_func_any func;
40
  Udf_func_init func_init;
41
  Udf_func_deinit func_deinit;
42
  Udf_func_clear func_clear;
43
  Udf_func_add func_add;
134.1.1 by Mark Atwood
more hackery to get plugin UDFs working
44
  void *data;
1 by brian
clean slate
45
} udf_func;
46
47
class Item_result_field;
48
49
class udf_handler :public Sql_alloc
50
{
51
 protected:
52
  udf_func *u_d;
53
  String *buffers;
54
  UDF_ARGS f_args;
55
  UDF_INIT initid;
56
  char *num_buffer;
57
  uchar error, is_null;
58
  bool initialized;
59
  Item **args;
60
61
 public:
62
  table_map used_tables_cache;
63
  bool const_item_cache;
64
  bool not_original;
65
  udf_handler(udf_func *udf_arg) :u_d(udf_arg), buffers(0), error(0),
66
    is_null(0), initialized(0), not_original(0)
67
  {}
68
  ~udf_handler();
69
  const char *name() const { return u_d ? u_d->name.str : "?"; }
70
  Item_result result_type () const
71
  { return u_d	? u_d->returns : STRING_RESULT;}
72
  bool get_arguments();
73
  bool fix_fields(THD *thd, Item_result_field *item,
74
		  uint arg_count, Item **args);
75
  void cleanup();
275 by Brian Aker
Full removal of my_bool from central server.
76
  double val(bool *null_value)
1 by brian
clean slate
77
  {
78
    is_null= 0;
79
    if (get_arguments())
80
    {
81
      *null_value=1;
82
      return 0.0;
83
    }
84
    Udf_func_double func= (Udf_func_double) u_d->func;
85
    double tmp=func(&initid, &f_args, &is_null, &error);
86
    if (is_null || error)
87
    {
88
      *null_value=1;
89
      return 0.0;
90
    }
91
    *null_value=0;
92
    return tmp;
93
  }
275 by Brian Aker
Full removal of my_bool from central server.
94
  int64_t val_int(bool *null_value)
1 by brian
clean slate
95
  {
96
    is_null= 0;
97
    if (get_arguments())
98
    {
99
      *null_value=1;
80.1.1 by Brian Aker
LL() cleanup
100
      return 0LL;
1 by brian
clean slate
101
    }
152 by Brian Aker
longlong replacement
102
    Udf_func_int64_t func= (Udf_func_int64_t) u_d->func;
103
    int64_t tmp=func(&initid, &f_args, &is_null, &error);
1 by brian
clean slate
104
    if (is_null || error)
105
    {
106
      *null_value=1;
80.1.1 by Brian Aker
LL() cleanup
107
      return 0LL;
1 by brian
clean slate
108
    }
109
    *null_value=0;
110
    return tmp;
111
  }
275 by Brian Aker
Full removal of my_bool from central server.
112
  my_decimal *val_decimal(bool *null_value, my_decimal *dec_buf);
1 by brian
clean slate
113
  void clear()
114
  {
115
    is_null= 0;
116
    Udf_func_clear func= u_d->func_clear;
117
    func(&initid, &is_null, &error);
118
  }
275 by Brian Aker
Full removal of my_bool from central server.
119
  void add(bool *null_value)
1 by brian
clean slate
120
  {
121
    if (get_arguments())
122
    {
123
      *null_value=1;
124
      return;
125
    }
126
    Udf_func_add func= u_d->func_add;
127
    func(&initid, &f_args, &is_null, &error);
197 by Brian Aker
More my_bool cleanup.
128
    *null_value= (bool) (is_null || error);
1 by brian
clean slate
129
  }
130
  String *val_str(String *str,String *save_str);
131
};
132
133
134
void udf_init(void),udf_free(void);
141 by Brian Aker
Code cleanup. Mainly dead stuff :)
135
udf_func *find_udf(const char *name, uint len=0);
1 by brian
clean slate
136
void free_udf(udf_func *udf);
137
int mysql_create_function(THD *thd,udf_func *udf);