MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sql_udf.h
1 #ifndef SQL_UDF_INCLUDED
2 #define SQL_UDF_INCLUDED
3 
4 /* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; version 2 of the License.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software Foundation,
17  51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
18 
19 
20 /* This file defines structures needed by udf functions */
21 
22 enum Item_udftype {UDFTYPE_FUNCTION=1,UDFTYPE_AGGREGATE};
23 
24 typedef void (*Udf_func_clear)(UDF_INIT *, uchar *, uchar *);
25 typedef void (*Udf_func_add)(UDF_INIT *, UDF_ARGS *, uchar *, uchar *);
26 typedef void (*Udf_func_deinit)(UDF_INIT*);
27 typedef my_bool (*Udf_func_init)(UDF_INIT *, UDF_ARGS *, char *);
28 typedef void (*Udf_func_any)();
29 typedef double (*Udf_func_double)(UDF_INIT *, UDF_ARGS *, uchar *, uchar *);
30 typedef longlong (*Udf_func_longlong)(UDF_INIT *, UDF_ARGS *, uchar *,
31  uchar *);
32 
33 typedef struct st_udf_func
34 {
35  LEX_STRING name;
36  Item_result returns;
37  Item_udftype type;
38  char *dl;
39  void *dlhandle;
40  Udf_func_any func;
41  Udf_func_init func_init;
42  Udf_func_deinit func_deinit;
43  Udf_func_clear func_clear;
44  Udf_func_add func_add;
45  ulong usage_count;
46 } udf_func;
47 
48 class Item_result_field;
49 
50 class udf_handler :public Sql_alloc
51 {
52  protected:
53  udf_func *u_d;
54  String *buffers;
55  UDF_ARGS f_args;
56  UDF_INIT initid;
57  char *num_buffer;
58  uchar error, is_null;
59  bool initialized;
60  Item **args;
61 
62  public:
63  table_map used_tables_cache;
64  bool const_item_cache;
65  bool not_original;
66  udf_handler(udf_func *udf_arg) :u_d(udf_arg), buffers(0), error(0),
67  is_null(0), initialized(0), not_original(0)
68  {}
69  ~udf_handler();
70  const char *name() const { return u_d ? u_d->name.str : "?"; }
71  Item_result result_type () const
72  { return u_d ? u_d->returns : STRING_RESULT;}
73  bool get_arguments();
74  bool fix_fields(THD *thd, Item_result_field *item,
75  uint arg_count, Item **args);
76  void cleanup();
77  double val(my_bool *null_value)
78  {
79  is_null= 0;
80  if (get_arguments())
81  {
82  *null_value=1;
83  return 0.0;
84  }
85  Udf_func_double func= (Udf_func_double) u_d->func;
86  double tmp=func(&initid, &f_args, &is_null, &error);
87  if (is_null || error)
88  {
89  *null_value=1;
90  return 0.0;
91  }
92  *null_value=0;
93  return tmp;
94  }
95  longlong val_int(my_bool *null_value)
96  {
97  is_null= 0;
98  if (get_arguments())
99  {
100  *null_value=1;
101  return LL(0);
102  }
103  Udf_func_longlong func= (Udf_func_longlong) u_d->func;
104  longlong tmp=func(&initid, &f_args, &is_null, &error);
105  if (is_null || error)
106  {
107  *null_value=1;
108  return LL(0);
109  }
110  *null_value=0;
111  return tmp;
112  }
113  my_decimal *val_decimal(my_bool *null_value, my_decimal *dec_buf);
114  void clear()
115  {
116  is_null= 0;
117  Udf_func_clear func= u_d->func_clear;
118  func(&initid, &is_null, &error);
119  }
120  void add(my_bool *null_value)
121  {
122  if (get_arguments())
123  {
124  *null_value=1;
125  return;
126  }
127  Udf_func_add func= u_d->func_add;
128  func(&initid, &f_args, &is_null, &error);
129  *null_value= (my_bool) (is_null || error);
130  }
131  String *val_str(String *str,String *save_str);
132 };
133 
134 
135 #ifdef HAVE_DLOPEN
136 void udf_init(void),udf_free(void);
137 udf_func *find_udf(const char *name, uint len=0,bool mark_used=0);
138 void free_udf(udf_func *udf);
139 int mysql_create_function(THD *thd,udf_func *udf);
140 int mysql_drop_function(THD *thd,const LEX_STRING *name);
141 #endif
142 #endif /* SQL_UDF_INCLUDED */