MySQL  8.0.19
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, 2019, 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 struct CHARSET_INFO;
42 
43 struct udf_func {
47  char *dl;
48  void *dlhandle;
55 };
56 
57 /*
58  A structure of extension attributes for a UDF argument.
59  The extension pointer of UDF_ARGS may point to the object of this
60  structure. There are udf_extension component services to set and get
61  the extension attributes of argument.
62 */
66 };
67 
68 /*
69  A structure of extension attributes for return value of UDF.
70  The extension pointer of UDF_INIT may point to the object of this
71  structure. There are udf_extension component services to set and get
72  the extension attributes of return value.
73 */
80 };
81 
82 class udf_handler {
83  protected:
88  char *num_buffer;
92  Udf_args_extension m_args_extension; /**< A struct that holds the extension
93  arguments for each UDF argument */
95  m_return_value_extension; /**< A struct that holds the extension arguments
96  for return value */
97  public:
100 
101  udf_handler(udf_func *udf_arg);
102  ~udf_handler();
103  udf_handler(const udf_handler &) = default;
104  udf_handler(udf_handler &&) = default;
105  udf_handler &operator=(const udf_handler &) = default;
106  udf_handler &operator=(udf_handler &&) = default;
107 
108  const char *name() const { return u_d ? u_d->name.str : "?"; }
110  return (Item_result)(u_d ? (u_d->returns) : STRING_RESULT);
111  }
112  bool fix_fields(THD *thd, Item_result_field *item, uint arg_count,
113  Item **args);
114  void cleanup();
115  double val_real(bool *null_value);
116  longlong val_int(bool *null_value);
117  String *val_str(String *str, String *save_str);
118  my_decimal *val_decimal(bool *null_value, my_decimal *dec_buf);
119  void clear();
120  void add(bool *null_value);
121 
122  private:
123  bool get_arguments();
124  String *result_string(const char *res, size_t res_length, String *str,
125  String *save_str);
126  void get_string(uint index);
128 };
129 
130 void udf_init_globals();
132 void udf_unload_udfs();
133 void udf_deinit_globals();
134 udf_func *find_udf(const char *name, size_t len = 0, bool mark_used = false);
135 void free_udf(udf_func *udf);
136 bool mysql_create_function(THD *thd, udf_func *udf);
137 bool mysql_drop_function(THD *thd, const LEX_STRING *name);
138 ulong udf_hash_size(void);
139 void udf_hash_rlock(void);
140 void udf_hash_unlock(void);
141 typedef void udf_hash_for_each_func_t(udf_func *, void *);
142 void udf_hash_for_each(udf_hash_for_each_func_t *func, void *arg);
143 #endif /* SQL_UDF_INCLUDED */
udf_handler::get_arguments
bool get_arguments()
Definition: item_func.cc:4109
udf_read_functions_table
void udf_read_functions_table()
Definition: sql_udf.cc:197
udf_hash_for_each_func_t
void udf_hash_for_each_func_t(udf_func *, void *)
Definition: sql_udf.h:141
udf_func::dlhandle
void * dlhandle
Definition: sql_udf.h:48
Item
Definition: item.h:665
THD
Definition: sql_class.h:764
find_udf
udf_func * find_udf(const char *name, size_t len=0, bool mark_used=false)
Definition: sql_udf.cc:415
UDF_INIT
Information about the result of a user defined function.
Definition: udf_registration_types.h:65
Udf_func_deinit
void(* Udf_func_deinit)(UDF_INIT *)
Definition: udf_registration_types.h:79
udf_handler::used_tables_cache
table_map used_tables_cache
Definition: sql_udf.h:98
STRING_RESULT
@ STRING_RESULT
not valid for UDFs
Definition: udf_registration_types.h:40
Udf_return_value_extension::Udf_return_value_extension
Udf_return_value_extension(const CHARSET_INFO *charset_info=nullptr, Item_result result_type=INVALID_RESULT)
Definition: sql_udf.h:75
udf_handler::~udf_handler
~udf_handler()
Definition: item_func.cc:3894
udf_handler
Definition: sql_udf.h:82
udf_func::func
Udf_func_any func
Definition: sql_udf.h:49
CHARSET_INFO
Definition: m_ctype.h:354
udf_handler::add
void add(bool *null_value)
Definition: item_func.cc:3922
String
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:164
Udf_return_value_extension::result_type
Item_result result_type
Definition: sql_udf.h:79
longlong
long long int longlong
Definition: my_inttypes.h:54
udf_handler::result_string
String * result_string(const char *res, size_t res_length, String *str, String *save_str)
Process the result string returned by the udf() method.
Definition: item_func.cc:4247
udf_handler::udf_handler
udf_handler(udf_func *udf_arg)
Definition: item_func.cc:3884
udf_handler::f_args
UDF_ARGS f_args
Definition: sql_udf.h:86
Item_result_field
Item with result field.
Definition: item.h:4549
INVALID_RESULT
@ INVALID_RESULT
Definition: udf_registration_types.h:39
udf_func::name
LEX_STRING name
Definition: sql_udf.h:44
udf_handler::cleanup
void cleanup()
Definition: item_func.cc:3899
udf_func
Definition: sql_udf.h:43
udf_handler::clear
void clear()
Definition: item_func.cc:3916
udf_unload_udfs
void udf_unload_udfs()
Deintialize the UDF subsystem.
Definition: sql_udf.cc:313
udf_handler::initid
UDF_INIT initid
Definition: sql_udf.h:87
Item_result
Item_result
Type of the user defined function return slot and arguments.
Definition: udf_registration_types.h:38
udf_deinit_globals
void udf_deinit_globals()
Deintialize the UDF subsystem.
Definition: sql_udf.cc:340
udf_handler::error
uchar error
Definition: sql_udf.h:89
Udf_args_extension
Definition: sql_udf.h:63
udf_handler::is_null
uchar is_null
Definition: sql_udf.h:89
udf_handler::val_real
double val_real(bool *null_value)
Definition: item_func.cc:4145
udf_func::func_add
Udf_func_add func_add
Definition: sql_udf.h:53
udf_hash_rlock
void udf_hash_rlock(void)
Definition: sql_udf.cc:873
udf_handler::m_return_value_extension
Udf_return_value_extension m_return_value_extension
A struct that holds the extension arguments for return value.
Definition: sql_udf.h:95
Udf_func_clear
void(* Udf_func_clear)(UDF_INIT *, unsigned char *, unsigned char *)
Definition: udf_registration_types.h:76
Udf_args_extension::Udf_args_extension
Udf_args_extension()
Definition: sql_udf.h:64
my_inttypes.h
udf_hash_size
ulong udf_hash_size(void)
Definition: sql_udf.cc:877
udf_handler::val_str
String * val_str(String *str, String *save_str)
Definition: item_func.cc:4180
lex_string.h
udf_handler::m_args_extension
Udf_args_extension m_args_extension
A struct that holds the extension arguments for each UDF argument.
Definition: sql_udf.h:92
udf_handler::operator=
udf_handler & operator=(const udf_handler &)=default
udf_func::type
Item_udftype type
Definition: sql_udf.h:46
free_udf
void free_udf(udf_func *udf)
Definition: sql_udf.cc:390
udf_handler::val_decimal
my_decimal * val_decimal(bool *null_value, my_decimal *dec_buf)
Definition: item_func.cc:4213
udf_handler::buffers
String * buffers
Definition: sql_udf.h:85
Udf_func_any
void(* Udf_func_any)(void)
Definition: udf_registration_types.h:81
uint
unsigned int uint
Definition: uca-dump.cc:29
Udf_args_extension::charset_info
const CHARSET_INFO ** charset_info
Definition: sql_udf.h:65
udf_handler::initialized
bool initialized
Definition: sql_udf.h:90
udf_func::dl
char * dl
Definition: sql_udf.h:47
UDF_ARGS
Definition: udf_registration_types.h:47
udf_func::func_clear
Udf_func_clear func_clear
Definition: sql_udf.h:52
uchar
unsigned char uchar
Definition: my_inttypes.h:51
udf_func::func_init
Udf_func_init func_init
Definition: sql_udf.h:50
udf_handler::not_original
bool not_original
Definition: sql_udf.h:99
table_map
uint64_t table_map
Definition: my_table_map.h:30
udf_handler::args
Item ** args
Definition: sql_udf.h:91
my_decimal
my_decimal class limits 'decimal_t' type to what we need in MySQL.
Definition: my_decimal.h:91
my_table_map.h
mysql_create_function
bool mysql_create_function(THD *thd, udf_func *udf)
Create a user defined function.
Definition: sql_udf.cc:575
mysql_drop_function
bool mysql_drop_function(THD *thd, const LEX_STRING *name)
Drop a user defined function.
Definition: sql_udf.cc:706
udf_init_globals
void udf_init_globals()
Initialize the UDF global structures.
Definition: sql_udf.cc:177
name
const string name("\"Name\"")
MYSQL_LEX_STRING
Definition: mysql_lex_string.h:34
udf_handler::val_int
longlong val_int(bool *null_value)
Definition: item_func.cc:4160
udf_hash_for_each
void udf_hash_for_each(udf_hash_for_each_func_t *func, void *arg)
Definition: sql_udf.cc:879
Udf_func_init
bool(* Udf_func_init)(UDF_INIT *, UDF_ARGS *, char *)
Definition: udf_registration_types.h:80
udf_handler::u_d
udf_func * u_d
Definition: sql_udf.h:84
Udf_return_value_extension::charset_info
const CHARSET_INFO * charset_info
Definition: sql_udf.h:78
udf_func::func_deinit
Udf_func_deinit func_deinit
Definition: sql_udf.h:51
udf_handler::result_type
Item_result result_type() const
Definition: sql_udf.h:109
Udf_return_value_extension
Definition: sql_udf.h:74
udf_handler::fix_fields
bool fix_fields(THD *thd, Item_result_field *item, uint arg_count, Item **args)
Definition: item_func.cc:3932
udf_func::usage_count
ulong usage_count
Definition: sql_udf.h:54
ulong
unsigned long ulong
Definition: my_inttypes.h:48
Udf_func_add
void(* Udf_func_add)(UDF_INIT *, UDF_ARGS *, unsigned char *, unsigned char *)
Definition: udf_registration_types.h:77
MYSQL_LEX_STRING::str
char * str
Definition: mysql_lex_string.h:35
udf_handler::num_buffer
char * num_buffer
Definition: sql_udf.h:88
udf_registration_types.h
udf_hash_unlock
void udf_hash_unlock(void)
Definition: sql_udf.cc:875
udf_handler::get_string
void get_string(uint index)
Get the details of the input String arguments.
Definition: item_func.cc:4267
Item_udftype
Item_udftype
Definition: udf_registration_types.h:74
udf_handler::name
const char * name() const
Definition: sql_udf.h:108
udf_handler::get_and_convert_string
bool get_and_convert_string(uint index)
Get the details of the input String argument.
Definition: item_func.cc:4288
index
char * index(const char *, int c)
Definition: mysql.cc:2875
udf_func::returns
Item_result returns
Definition: sql_udf.h:45