MySQL 9.1.0
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, 2024, 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 designed to work 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 either included with
16 the program or referenced in the documentation.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License, version 2.0, for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
26
27/* This file defines structures needed by udf functions */
28
29#include <stddef.h>
30#include <sys/types.h>
31
32#include "lex_string.h"
33#include "my_inttypes.h"
34#include "my_table_map.h"
36
37class Item;
39class String;
40class THD;
41class my_decimal;
42struct CHARSET_INFO;
43
44struct udf_func {
48 char *dl;
49 void *dlhandle;
56};
57
58/*
59 A structure of extension attributes for a UDF argument.
60 The extension pointer of UDF_ARGS may point to the object of this
61 structure. There are udf_extension component services to set and get
62 the extension attributes of argument.
63*/
67};
68
69/*
70 A structure of extension attributes for return value of UDF.
71 The extension pointer of UDF_INIT may point to the object of this
72 structure. There are udf_extension component services to set and get
73 the extension attributes of return value.
74*/
81};
82
84 protected:
86 String *buffers{nullptr};
90 char *num_buffer{nullptr};
93 /// True when handler has been initialized and use count incremented
94 bool m_initialized{false};
95 /// True when init function has been called
96 bool m_init_func_called{false};
98 Udf_args_extension m_args_extension; /**< A struct that holds the extension
99 arguments for each UDF argument */
101 m_return_value_extension; /**< A struct that holds the extension arguments
102 for return value */
103 public:
105 bool m_original{true};
106
107 udf_handler(udf_func *udf_arg);
108 udf_handler(const udf_handler &) = default;
110 udf_handler &operator=(const udf_handler &) = default;
112 // Clean up string buffers
113 void clean_buffers();
114 void free_handler();
115
116 bool is_initialized() const { return m_initialized; }
117
118 const char *name() const { return u_d ? u_d->name.str : "?"; }
120 return (Item_result)(u_d ? (u_d->returns) : STRING_RESULT);
121 }
122 bool fix_fields(THD *thd, Item_result_field *item, uint arg_count,
123 Item **args);
124 void cleanup();
125 bool call_init_func();
126 double val_real(bool *null_value);
127 longlong val_int(bool *null_value);
128 my_decimal *val_decimal(bool *null_value, my_decimal *dec_buf);
129 String *val_str(String *str, String *save_str);
130 void clear();
131 void add(bool *null_value);
132
133 private:
134 bool get_arguments();
135 String *result_string(const char *res, size_t res_length, String *str,
136 String *save_str);
137 void get_string(uint index);
138 bool get_and_convert_string(uint index);
139};
140
141void udf_init_globals();
143void udf_unload_udfs();
144void udf_deinit_globals();
145udf_func *find_udf(const char *name, size_t len = 0, bool mark_used = false);
146void free_udf(udf_func *udf);
147bool mysql_create_function(THD *thd, udf_func *udf, bool if_not_exists);
148bool mysql_drop_function(THD *thd, const LEX_STRING *name);
149ulong udf_hash_size(void);
150void udf_hash_rlock(void);
151void udf_hash_unlock(void);
152typedef void udf_hash_for_each_func_t(udf_func *, void *);
153void udf_hash_for_each(udf_hash_for_each_func_t *func, void *arg);
154#endif /* SQL_UDF_INCLUDED */
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:251
Item with result field.
Definition: item.h:5829
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:930
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:167
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
my_decimal class limits 'decimal_t' type to what we need in MySQL.
Definition: my_decimal.h:96
Definition: sql_udf.h:83
bool m_init_func_called
True when init function has been called.
Definition: sql_udf.h:96
bool get_and_convert_string(uint index)
Get the details of the input String argument.
Definition: item_func.cc:5039
Item_result result_type() const
Definition: sql_udf.h:119
bool call_init_func()
Definition: item_func.cc:4777
const char * name() const
Definition: sql_udf.h:118
String * arg_buffers
Definition: sql_udf.h:87
table_map used_tables_cache
Definition: sql_udf.h:104
my_decimal * val_decimal(bool *null_value, my_decimal *dec_buf)
Definition: item_func.cc:4946
udf_handler(udf_handler &&)=default
UDF_INIT initid
Definition: sql_udf.h:89
longlong val_int(bool *null_value)
Definition: item_func.cc:4890
bool m_initialized
True when handler has been initialized and use count incremented.
Definition: sql_udf.h:94
uchar is_null
Definition: sql_udf.h:92
void get_string(uint index)
Get the details of the input String arguments.
Definition: item_func.cc:5019
udf_handler & operator=(const udf_handler &)=default
bool is_initialized() const
Definition: sql_udf.h:116
char * num_buffer
Definition: sql_udf.h:90
bool m_original
Definition: sql_udf.h:105
bool fix_fields(THD *thd, Item_result_field *item, uint arg_count, Item **args)
Definition: item_func.cc:4618
Item ** args
Definition: sql_udf.h:97
void cleanup()
Definition: item_func.cc:4570
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:4999
UDF_ARGS f_args
Definition: sql_udf.h:88
uchar error
Definition: sql_udf.h:91
void clear()
Definition: item_func.cc:4968
String * val_str(String *str, String *save_str)
Definition: item_func.cc:4912
double val_real(bool *null_value)
Definition: item_func.cc:4873
void free_handler()
Definition: item_func.cc:4599
String * buffers
Definition: sql_udf.h:86
udf_handler(udf_func *udf_arg)
Definition: item_func.cc:4565
void clean_buffers()
Definition: item_func.cc:4591
udf_handler(const udf_handler &)=default
bool get_arguments()
Definition: item_func.cc:4837
void add(bool *null_value)
Definition: item_func.cc:4975
udf_func * u_d
Definition: sql_udf.h:85
Udf_args_extension m_args_extension
A struct that holds the extension arguments for each UDF argument.
Definition: sql_udf.h:98
Udf_return_value_extension m_return_value_extension
A struct that holds the extension arguments for return value.
Definition: sql_udf.h:101
udf_handler & operator=(udf_handler &&)=default
Some integer typedefs for easier portability.
unsigned char uchar
Definition: my_inttypes.h:52
long long int longlong
Definition: my_inttypes.h:55
uint64_t table_map
Definition: my_table_map.h:30
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1105
void udf_init_globals()
Initialize the UDF global structures.
Definition: sql_udf.cc:200
void udf_hash_for_each_func_t(udf_func *, void *)
Definition: sql_udf.h:152
void udf_hash_rlock(void)
Definition: sql_udf.cc:961
void udf_hash_unlock(void)
Definition: sql_udf.cc:963
void free_udf(udf_func *udf)
Definition: sql_udf.cc:429
bool mysql_drop_function(THD *thd, const LEX_STRING *name)
Drop a user defined function.
Definition: sql_udf.cc:774
void udf_unload_udfs()
Deinitialize the UDF subsystem.
Definition: sql_udf.cc:352
void udf_read_functions_table()
Definition: sql_udf.cc:220
void udf_hash_for_each(udf_hash_for_each_func_t *func, void *arg)
Definition: sql_udf.cc:967
void udf_deinit_globals()
Deinitialize the UDF subsystem.
Definition: sql_udf.cc:379
bool mysql_create_function(THD *thd, udf_func *udf, bool if_not_exists)
Create a user defined function.
Definition: sql_udf.cc:612
ulong udf_hash_size(void)
Definition: sql_udf.cc:965
udf_func * find_udf(const char *name, size_t len=0, bool mark_used=false)
Definition: sql_udf.cc:451
case opt name
Definition: sslopt-case.h:29
Definition: m_ctype.h:421
Definition: mysql_lex_string.h:35
char * str
Definition: mysql_lex_string.h:36
Definition: udf_registration_types.h:48
Information about the result of a user defined function.
Definition: udf_registration_types.h:66
Definition: sql_udf.h:64
Udf_args_extension()
Definition: sql_udf.h:65
const CHARSET_INFO ** charset_info
Definition: sql_udf.h:66
Definition: sql_udf.h:75
const CHARSET_INFO * charset_info
Definition: sql_udf.h:79
Udf_return_value_extension(const CHARSET_INFO *charset_info=nullptr, Item_result result_type=INVALID_RESULT)
Definition: sql_udf.h:76
Item_result result_type
Definition: sql_udf.h:80
Definition: sql_udf.h:44
char * dl
Definition: sql_udf.h:48
ulong usage_count
Definition: sql_udf.h:55
Item_result returns
Definition: sql_udf.h:46
Udf_func_add func_add
Definition: sql_udf.h:54
void * dlhandle
Definition: sql_udf.h:49
Item_udftype type
Definition: sql_udf.h:47
LEX_STRING name
Definition: sql_udf.h:45
Udf_func_any func
Definition: sql_udf.h:50
Udf_func_clear func_clear
Definition: sql_udf.h:53
Udf_func_deinit func_deinit
Definition: sql_udf.h:52
Udf_func_init func_init
Definition: sql_udf.h:51
void(* Udf_func_deinit)(UDF_INIT *)
Definition: udf_registration_types.h:80
Item_result
Type of the user defined function return slot and arguments.
Definition: udf_registration_types.h:39
@ STRING_RESULT
not valid for UDFs
Definition: udf_registration_types.h:41
@ INVALID_RESULT
Definition: udf_registration_types.h:40
bool(* Udf_func_init)(UDF_INIT *, UDF_ARGS *, char *)
Definition: udf_registration_types.h:81
void(* Udf_func_clear)(UDF_INIT *, unsigned char *, unsigned char *)
Definition: udf_registration_types.h:77
void(* Udf_func_add)(UDF_INIT *, UDF_ARGS *, unsigned char *, unsigned char *)
Definition: udf_registration_types.h:78
void(* Udf_func_any)(void)
Definition: udf_registration_types.h:82
Item_udftype
Definition: udf_registration_types.h:75