MySQL  8.0.27
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, 2021, Oracle and/or its affiliates.
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;
54  ulong usage_count;
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:
85  String *buffers{nullptr};
88  char *num_buffer{nullptr};
91  /// True when handler has been initialized and use count incremented
92  bool m_initialized{false};
93  /// True when init function has been called
94  bool m_init_func_called{false};
96  Udf_args_extension m_args_extension; /**< A struct that holds the extension
97  arguments for each UDF argument */
99  m_return_value_extension; /**< A struct that holds the extension arguments
100  for return value */
101  public:
103  bool m_original{true};
104 
105  udf_handler(udf_func *udf_arg);
106  udf_handler(const udf_handler &) = default;
107  udf_handler(udf_handler &&) = default;
108  udf_handler &operator=(const udf_handler &) = default;
110  // Clean up string buffers
111  void clean_buffers();
112  void free_handler();
113 
114  bool is_initialized() const { return m_initialized; }
115 
116  const char *name() const { return u_d ? u_d->name.str : "?"; }
118  return (Item_result)(u_d ? (u_d->returns) : STRING_RESULT);
119  }
120  bool fix_fields(THD *thd, Item_result_field *item, uint arg_count,
121  Item **args);
122  void cleanup();
123  bool call_init_func();
124  double val_real(bool *null_value);
125  longlong val_int(bool *null_value);
126  my_decimal *val_decimal(bool *null_value, my_decimal *dec_buf);
127  String *val_str(String *str, String *save_str);
128  void clear();
129  void add(bool *null_value);
130 
131  private:
132  bool get_arguments();
133  String *result_string(const char *res, size_t res_length, String *str,
134  String *save_str);
135  void get_string(uint index);
136  bool get_and_convert_string(uint index);
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 = false);
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 */
Item with result field.
Definition: item.h:5445
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:802
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:165
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:821
my_decimal class limits 'decimal_t' type to what we need in MySQL.
Definition: my_decimal.h:92
Definition: sql_udf.h:82
const char * name() const
Definition: sql_udf.h:116
bool m_init_func_called
True when init function has been called.
Definition: sql_udf.h:94
bool get_and_convert_string(uint index)
Get the details of the input String argument.
Definition: item_func.cc:4767
Item_result result_type() const
Definition: sql_udf.h:117
bool call_init_func()
Definition: item_func.cc:4510
table_map used_tables_cache
Definition: sql_udf.h:102
my_decimal * val_decimal(bool *null_value, my_decimal *dec_buf)
Definition: item_func.cc:4674
udf_handler(udf_handler &&)=default
UDF_INIT initid
Definition: sql_udf.h:87
longlong val_int(bool *null_value)
Definition: item_func.cc:4618
bool m_initialized
True when handler has been initialized and use count incremented.
Definition: sql_udf.h:92
uchar is_null
Definition: sql_udf.h:90
void get_string(uint index)
Get the details of the input String arguments.
Definition: item_func.cc:4747
bool is_initialized() const
Definition: sql_udf.h:114
char * num_buffer
Definition: sql_udf.h:88
bool m_original
Definition: sql_udf.h:103
bool fix_fields(THD *thd, Item_result_field *item, uint arg_count, Item **args)
Definition: item_func.cc:4368
Item ** args
Definition: sql_udf.h:95
void cleanup()
Definition: item_func.cc:4322
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:4727
UDF_ARGS f_args
Definition: sql_udf.h:86
udf_handler & operator=(udf_handler &&)=default
uchar error
Definition: sql_udf.h:89
void clear()
Definition: item_func.cc:4696
String * val_str(String *str, String *save_str)
Definition: item_func.cc:4640
double val_real(bool *null_value)
Definition: item_func.cc:4601
void free_handler()
Definition: item_func.cc:4350
String * buffers
Definition: sql_udf.h:85
udf_handler(udf_func *udf_arg)
Definition: item_func.cc:4317
void clean_buffers()
Definition: item_func.cc:4343
udf_handler(const udf_handler &)=default
bool get_arguments()
Definition: item_func.cc:4565
void add(bool *null_value)
Definition: item_func.cc:4703
udf_func * u_d
Definition: sql_udf.h:84
Udf_args_extension m_args_extension
A struct that holds the extension arguments for each UDF argument.
Definition: sql_udf.h:96
Udf_return_value_extension m_return_value_extension
A struct that holds the extension arguments for return value.
Definition: sql_udf.h:99
udf_handler & operator=(const udf_handler &)=default
Dialog Client Authentication nullptr
Definition: dialog.cc:352
Some integer typedefs for easier portability.
unsigned char uchar
Definition: my_inttypes.h:51
long long int longlong
Definition: my_inttypes.h:54
uint64_t table_map
Definition: my_table_map.h:29
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1056
void udf_init_globals()
Initialize the UDF global structures.
Definition: sql_udf.cc:193
bool mysql_create_function(THD *thd, udf_func *udf)
Create a user defined function.
Definition: sql_udf.cc:604
void udf_hash_for_each_func_t(udf_func *, void *)
Definition: sql_udf.h:150
void udf_hash_rlock(void)
Definition: sql_udf.cc:942
void udf_hash_unlock(void)
Definition: sql_udf.cc:944
void free_udf(udf_func *udf)
Definition: sql_udf.cc:422
bool mysql_drop_function(THD *thd, const LEX_STRING *name)
Drop a user defined function.
Definition: sql_udf.cc:755
void udf_unload_udfs()
Deintialize the UDF subsystem.
Definition: sql_udf.cc:345
void udf_read_functions_table()
Definition: sql_udf.cc:213
void udf_hash_for_each(udf_hash_for_each_func_t *func, void *arg)
Definition: sql_udf.cc:948
void udf_deinit_globals()
Deintialize the UDF subsystem.
Definition: sql_udf.cc:372
udf_func * find_udf(const char *name, size_t len=0, bool mark_used=false)
Definition: sql_udf.cc:444
ulong udf_hash_size(void)
Definition: sql_udf.cc:946
case opt name
Definition: sslopt-case.h:32
Definition: m_ctype.h:354
Definition: mysql_lex_string.h:34
char * str
Definition: mysql_lex_string.h:35
Definition: udf_registration_types.h:47
Information about the result of a user defined function.
Definition: udf_registration_types.h:65
Definition: sql_udf.h:63
Udf_args_extension()
Definition: sql_udf.h:64
const CHARSET_INFO ** charset_info
Definition: sql_udf.h:65
Definition: sql_udf.h:74
const CHARSET_INFO * charset_info
Definition: sql_udf.h:78
Udf_return_value_extension(const CHARSET_INFO *charset_info=nullptr, Item_result result_type=INVALID_RESULT)
Definition: sql_udf.h:75
Item_result result_type
Definition: sql_udf.h:79
Definition: sql_udf.h:43
char * dl
Definition: sql_udf.h:47
ulong usage_count
Definition: sql_udf.h:54
Item_result returns
Definition: sql_udf.h:45
Udf_func_add func_add
Definition: sql_udf.h:53
void * dlhandle
Definition: sql_udf.h:48
Item_udftype type
Definition: sql_udf.h:46
LEX_STRING name
Definition: sql_udf.h:44
Udf_func_any func
Definition: sql_udf.h:49
Udf_func_clear func_clear
Definition: sql_udf.h:52
Udf_func_deinit func_deinit
Definition: sql_udf.h:51
Udf_func_init func_init
Definition: sql_udf.h:50
unsigned int uint
Definition: uca-dump.cc:29
void(* Udf_func_deinit)(UDF_INIT *)
Definition: udf_registration_types.h:79
Item_result
Type of the user defined function return slot and arguments.
Definition: udf_registration_types.h:38
@ STRING_RESULT
not valid for UDFs
Definition: udf_registration_types.h:40
@ INVALID_RESULT
Definition: udf_registration_types.h:39
bool(* Udf_func_init)(UDF_INIT *, UDF_ARGS *, char *)
Definition: udf_registration_types.h:80
void(* Udf_func_clear)(UDF_INIT *, unsigned char *, unsigned char *)
Definition: udf_registration_types.h:76
void(* Udf_func_add)(UDF_INIT *, UDF_ARGS *, unsigned char *, unsigned char *)
Definition: udf_registration_types.h:77
void(* Udf_func_any)(void)
Definition: udf_registration_types.h:81
Item_udftype
Definition: udf_registration_types.h:74