MySQL  8.0.18
Source Code Documentation
sql_udf.h
Go to the documentation of this file.
1 #ifndef SQL_UDF_INCLUDED
2 #define SQL_UDF_INCLUDED
3 
4 /* Copyright (c) 2000, 2018, 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, version 2.0,
8  as published by the Free Software Foundation.
9 
10  This program is also distributed with certain software (including
11  but not limited to OpenSSL) that is licensed under separate terms,
12  as designated in a particular file or component or in included license
13  documentation. The authors of MySQL hereby grant you an additional
14  permission to link the program and your derivative works with the
15  separately licensed software that they have included with MySQL.
16 
17  This program is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  GNU General Public License, version 2.0, for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with this program; if not, write to the Free Software
24  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
25 
26 /* This file defines structures needed by udf functions */
27 
28 #include <stddef.h>
29 #include <sys/types.h>
30 
31 #include "lex_string.h"
32 #include "my_inttypes.h"
33 #include "my_table_map.h"
35 
36 class Item;
37 class Item_result_field;
38 class String;
39 class THD;
40 class my_decimal;
41 
42 struct udf_func {
46  char *dl;
47  void *dlhandle;
54 };
55 
56 class udf_handler {
57  protected:
62  char *num_buffer;
66 
67  public:
71  : u_d(udf_arg),
72  buffers(0),
73  error(0),
74  is_null(0),
75  initialized(0),
76  not_original(0) {}
77  ~udf_handler();
78  udf_handler(const udf_handler &) = default;
79  udf_handler(udf_handler &&) = default;
80  udf_handler &operator=(const udf_handler &) = default;
81  udf_handler &operator=(udf_handler &&) = default;
82 
83  const char *name() const { return u_d ? u_d->name.str : "?"; }
85  return (Item_result)(u_d ? (u_d->returns) : STRING_RESULT);
86  }
87  bool get_arguments();
88  bool fix_fields(THD *thd, Item_result_field *item, uint arg_count,
89  Item **args);
90  void cleanup();
91  double val(bool *null_value) {
92  is_null = 0;
93  if (get_arguments()) {
94  *null_value = 1;
95  return 0.0;
96  }
98  double tmp = func(&initid, &f_args, &is_null, &error);
99  if (is_null || error) {
100  *null_value = 1;
101  return 0.0;
102  }
103  *null_value = 0;
104  return tmp;
105  }
106  longlong val_int(bool *null_value) {
107  is_null = 0;
108  if (get_arguments()) {
109  *null_value = 1;
110  return 0LL;
111  }
113  longlong tmp = func(&initid, &f_args, &is_null, &error);
114  if (is_null || error) {
115  *null_value = 1;
116  return 0LL;
117  }
118  *null_value = 0;
119  return tmp;
120  }
121  my_decimal *val_decimal(bool *null_value, my_decimal *dec_buf);
122  void clear() {
123  is_null = 0;
124  Udf_func_clear func = u_d->func_clear;
125  func(&initid, &is_null, &error);
126  }
127  void add(bool *null_value) {
128  if (get_arguments()) {
129  *null_value = 1;
130  return;
131  }
132  Udf_func_add func = u_d->func_add;
133  func(&initid, &f_args, &is_null, &error);
134  *null_value = (bool)(is_null || error);
135  }
136  String *val_str(String *str, String *save_str);
137 };
138 
139 void udf_init_globals();
141 void udf_unload_udfs();
142 void udf_deinit_globals();
143 udf_func *find_udf(const char *name, size_t len = 0, bool mark_used = 0);
144 void free_udf(udf_func *udf);
145 bool mysql_create_function(THD *thd, udf_func *udf);
146 bool mysql_drop_function(THD *thd, const LEX_STRING *name);
147 ulong udf_hash_size(void);
148 void udf_hash_rlock(void);
149 void udf_hash_unlock(void);
150 typedef void udf_hash_for_each_func_t(udf_func *, void *);
151 void udf_hash_for_each(udf_hash_for_each_func_t *func, void *arg);
152 #endif /* SQL_UDF_INCLUDED */
Definition: sql_udf.h:42
longlong val_int(bool *null_value)
Definition: sql_udf.h:106
void(* Udf_func_clear)(UDF_INIT *, unsigned char *, unsigned char *)
Definition: udf_registration_types.h:76
unsigned char uchar
Definition: my_inttypes.h:51
char * str
Definition: mysql_lex_string.h:35
void udf_unload_udfs()
Deintialize the UDF subsystem.
Definition: sql_udf.cc:312
udf_func * find_udf(const char *name, size_t len=0, bool mark_used=0)
Definition: sql_udf.cc:414
Item_result
Type of the user defined function return slot and arguments
Definition: udf_registration_types.h:38
not valid for UDFs
Definition: udf_registration_types.h:40
ulonglong table_map
Definition: my_table_map.h:32
Definition: mysql_lex_string.h:34
Item_udftype
Definition: udf_registration_types.h:74
char * num_buffer
Definition: sql_udf.h:62
void udf_hash_unlock(void)
Definition: sql_udf.cc:876
void(* Udf_func_add)(UDF_INIT *, UDF_ARGS *, unsigned char *, unsigned char *)
Definition: udf_registration_types.h:77
const string name("\ame\)
bool fix_fields(THD *thd, Item_result_field *item, uint arg_count, Item **args)
Definition: item_func.cc:3877
void udf_deinit_globals()
Deintialize the UDF subsystem.
Definition: sql_udf.cc:339
Some integer typedefs for easier portability.
bool mysql_create_function(THD *thd, udf_func *udf)
Create a user defined function.
Definition: sql_udf.cc:574
Item_result returns
Definition: sql_udf.h:44
#define bool
Definition: config_static.h:42
void udf_init_globals()
Initialize the UDF global structures.
Definition: sql_udf.cc:176
const char * name() const
Definition: sql_udf.h:83
void(* Udf_func_any)(void)
Definition: udf_registration_types.h:81
bool initialized
Definition: sql_udf.h:64
Definition: sql_udf.h:56
Using this class is fraught with peril, and you need to be very careful when doing so...
Definition: sql_string.h:161
Item_udftype type
Definition: sql_udf.h:45
my_decimal * val_decimal(bool *null_value, my_decimal *dec_buf)
Definition: item_func.cc:4119
LEX_STRING name
Definition: sql_udf.h:43
String * buffers
Definition: sql_udf.h:59
uchar is_null
Definition: sql_udf.h:63
void add(bool *null_value)
Definition: sql_udf.h:127
void cleanup()
Definition: item_func.cc:3860
UDF_INIT initid
Definition: sql_udf.h:61
~udf_handler()
Definition: item_func.cc:4253
my_decimal class limits &#39;decimal_t&#39; type to what we need in MySQL.
Definition: my_decimal.h:91
udf_handler(udf_func *udf_arg)
Definition: sql_udf.h:70
bool mysql_drop_function(THD *thd, const LEX_STRING *name)
Drop a user defined function.
Definition: sql_udf.cc:705
bool not_original
Definition: sql_udf.h:69
bool(* Udf_func_init)(UDF_INIT *, UDF_ARGS *, char *)
Definition: udf_registration_types.h:80
Definition: item.h:668
Udf_func_add func_add
Definition: sql_udf.h:52
unsigned int uint
Definition: uca-dump.cc:29
long long int longlong
Definition: my_inttypes.h:54
udf_handler & operator=(const udf_handler &)=default
udf_func * u_d
Definition: sql_udf.h:58
void clear()
Definition: sql_udf.h:122
Udf_func_deinit func_deinit
Definition: sql_udf.h:50
double val(bool *null_value)
Definition: sql_udf.h:91
void udf_hash_for_each(udf_hash_for_each_func_t *func, void *arg)
Definition: sql_udf.cc:880
double(* Udf_func_double)(UDF_INIT *, UDF_ARGS *, unsigned char *, unsigned char *)
Definition: udf_registration_types.h:82
char * dl
Definition: sql_udf.h:46
Udf_func_clear func_clear
Definition: sql_udf.h:51
Information about the result of a user defined function
Definition: udf_registration_types.h:65
void free_udf(udf_func *udf)
Definition: sql_udf.cc:389
Definition: udf_registration_types.h:47
void(* Udf_func_deinit)(UDF_INIT *)
Definition: udf_registration_types.h:79
ulong udf_hash_size(void)
Definition: sql_udf.cc:878
Udf_func_init func_init
Definition: sql_udf.h:49
String * val_str(String *str, String *save_str)
Definition: item_func.cc:4082
Item with result field.
Definition: item.h:4559
table_map used_tables_cache
Definition: sql_udf.h:68
bool get_arguments()
Definition: item_func.cc:4036
ulong usage_count
Definition: sql_udf.h:53
Item ** args
Definition: sql_udf.h:65
void udf_hash_rlock(void)
Definition: sql_udf.cc:874
void udf_hash_for_each_func_t(udf_func *, void *)
Definition: sql_udf.h:150
UDF_ARGS f_args
Definition: sql_udf.h:60
unsigned long ulong
Definition: my_inttypes.h:48
void * dlhandle
Definition: sql_udf.h:47
Item_result result_type() const
Definition: sql_udf.h:84
void udf_read_functions_table()
Definition: sql_udf.cc:196
Udf_func_any func
Definition: sql_udf.h:48
uchar error
Definition: sql_udf.h:63
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:778
long long(* Udf_func_longlong)(UDF_INIT *, UDF_ARGS *, unsigned char *, unsigned char *)
Definition: udf_registration_types.h:84