1 /* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. 2 3 This program is free software; you can redistribute it and/or modify 4 it under the terms of the GNU General Public License, version 2.0, 5 as published by the Free Software Foundation. 6 7 This program is also distributed with certain software (including 8 but not limited to OpenSSL) that is licensed under separate terms, 9 as designated in a particular file or component or in included license 10 documentation. The authors of MySQL hereby grant you an additional 11 permission to link the program and your derivative works with the 12 separately licensed software that they have included with MySQL. 13 14 This program is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 GNU General Public License, version 2.0, for more details. 18 19 You should have received a copy of the GNU General Public License 20 along with this program; if not, write to the Free Software 21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ 22 23 /* Functions to create an item. Used by sql/sql_yacc.yy */ 24 25 #ifndef ITEM_CREATE_H 26 #define ITEM_CREATE_H 27 28 /** 29 @file sql/item_create.h 30 Builder for SQL functions. 31 */ 32 33 #include <stddef.h> 34 35 #include "lex_string.h" 36 #include "sql/parse_tree_node_base.h" // POS 37 38 /** 39 @addtogroup GROUP_PARSER 40 @{ 41 */ 42 43 class Item; 44 class PT_item_list; 45 class THD; 46 struct Cast_type; 47 struct CHARSET_INFO; 48 struct udf_func; 49 enum enum_field_types : int; 50 enum class Json_on_response_type : uint16; 51 52 /* For type casts */ 53 54 enum Cast_target : unsigned char { 55 ITEM_CAST_SIGNED_INT, 56 ITEM_CAST_UNSIGNED_INT, 57 ITEM_CAST_DATE, 58 ITEM_CAST_TIME, 59 ITEM_CAST_DATETIME, 60 ITEM_CAST_CHAR, 61 ITEM_CAST_DECIMAL, 62 ITEM_CAST_JSON, 63 ITEM_CAST_FLOAT, 64 ITEM_CAST_DOUBLE, 65 }; 66 67 /** 68 Public function builder interface. 69 The parser (sql/sql_yacc.yy) uses a factory / builder pattern to 70 construct an <code>Item</code> object for each function call. 71 All the concrete function builders implements this interface, 72 either directly or indirectly with some adapter helpers. 73 Keeping the function creation separated from the bison grammar allows 74 to simplify the parser, and avoid the need to introduce a new token 75 for each function, which has undesirable side effects in the grammar. 76 */ 77 78 class Create_func { 79 public: 80 /** 81 The builder create method. 82 Given the function name and list or arguments, this method creates 83 an <code>Item</code> that represents the function call. 84 In case or errors, a NULL item is returned, and an error is reported. 85 Note that the <code>thd</code> object may be modified by the builder. 86 In particular, the following members/methods can be set/called, 87 depending on the function called and the function possible side effects. 88 <ul> 89 <li><code>thd->lex->binlog_row_based_if_mixed</code></li> 90 <li><code>thd->lex->current_context()</code></li> 91 <li><code>thd->lex->safe_to_cache_query</code></li> 92 <li><code>thd->lex->uncacheable(UNCACHEABLE_SIDEEFFECT)</code></li> 93 <li><code>thd->lex->uncacheable(UNCACHEABLE_RAND)</code></li> 94 <li><code>thd->lex->add_time_zone_tables_to_query_tables(thd)</code></li> 95 </ul> 96 @param thd The current thread 97 @param name The function name 98 @param item_list The list of arguments to the function, can be NULL 99 @return An item representing the parsed function call, or NULL 100 */ 101 virtual Item *create_func(THD *thd, LEX_STRING name, 102 PT_item_list *item_list) = 0; 103 104 protected: 105 Create_func() = default; ~Create_func()106 virtual ~Create_func() {} 107 }; 108 109 /** 110 Function builder for qualified functions. 111 This builder is used with functions call using a qualified function name 112 syntax, as in <code>db.func(expr, expr, ...)</code>. 113 */ 114 115 class Create_qfunc : public Create_func { 116 public: 117 /** 118 The builder create method, for unqualified functions. 119 This builder will use the current database for the database name. 120 @param thd The current thread 121 @param name The function name 122 @param item_list The list of arguments to the function, can be NULL 123 @return An item representing the parsed function call 124 */ 125 virtual Item *create_func(THD *thd, LEX_STRING name, PT_item_list *item_list); 126 127 /** 128 The builder create method, for qualified functions. 129 @param thd The current thread 130 @param db The database name or NULL_STR to use the default db name 131 @param name The function name 132 @param use_explicit_name Should the function be represented as 'db.name'? 133 @param item_list The list of arguments to the function, can be NULL 134 @return An item representing the parsed function call 135 */ 136 virtual Item *create(THD *thd, LEX_STRING db, LEX_STRING name, 137 bool use_explicit_name, PT_item_list *item_list) = 0; 138 139 protected: 140 /** Constructor. */ Create_qfunc()141 Create_qfunc() {} 142 /** Destructor. */ ~Create_qfunc()143 virtual ~Create_qfunc() {} 144 }; 145 146 /** 147 Find the native function builder associated with a given function name. 148 149 @param name The native function name 150 @return The native function builder associated with the name, or NULL 151 */ 152 extern Create_func *find_native_function_builder(const LEX_STRING &name); 153 154 /** 155 Find the function builder for qualified functions. 156 @param thd The current thread 157 @return A function builder for qualified functions 158 */ 159 extern Create_qfunc *find_qualified_function_builder(THD *thd); 160 161 /** 162 Function builder for User Defined Functions. 163 */ 164 165 class Create_udf_func : public Create_func { 166 public: 167 virtual Item *create_func(THD *thd, LEX_STRING name, PT_item_list *item_list); 168 169 /** 170 The builder create method, for User Defined Functions. 171 @param thd The current thread 172 @param fct The User Defined Function metadata 173 @param item_list The list of arguments to the function, can be NULL 174 @return An item representing the parsed function call 175 */ 176 Item *create(THD *thd, udf_func *fct, PT_item_list *item_list); 177 178 /** Singleton. */ 179 static Create_udf_func s_singleton; 180 181 protected: 182 /** Constructor. */ Create_udf_func()183 Create_udf_func() {} 184 /** Destructor. */ ~Create_udf_func()185 virtual ~Create_udf_func() {} 186 }; 187 188 /** 189 Builder for cast expressions. 190 @param thd The current thread 191 @param pos Location of casting expression 192 @param a The item to cast 193 @param type the type casted into 194 @param as_array Cast to array 195 */ 196 Item *create_func_cast(THD *thd, const POS &pos, Item *a, const Cast_type &type, 197 bool as_array); 198 199 Item *create_func_cast(THD *thd, const POS &pos, Item *a, 200 Cast_target cast_target, const CHARSET_INFO *cs_arg); 201 202 /** 203 Creates an Item that represents a JSON_VALUE expression. 204 205 @param thd thread handler 206 @param pos the location of the expression 207 @param arg the JSON input argument to the JSON_VALUE expression 208 @param path the path to extract from the JSON document 209 @param type the target type of the JSON_VALUE expression 210 @param on_empty_type the type of the ON EMPTY clause 211 @param on_empty_default the default value specified in ON EMPTY, if any 212 @param on_error_type the type of the ON ERROR clause 213 @param on_error_default the default value specified in ON ERROR, if any 214 @return an Item on success, or nullptr on error 215 */ 216 Item *create_func_json_value(THD *thd, const POS &pos, Item *arg, Item *path, 217 const Cast_type &type, 218 Json_on_response_type on_empty_type, 219 Item *on_empty_default, 220 Json_on_response_type on_error_type, 221 Item *on_error_default); 222 223 Item *create_temporal_literal(THD *thd, const char *str, size_t length, 224 const CHARSET_INFO *cs, enum_field_types type, 225 bool send_error); 226 227 /** 228 Load the hash table for native functions. 229 Note: this code is not thread safe, and is intended to be used at server 230 startup only (before going multi-threaded) 231 232 @retval false OK. 233 @retval true An exception was caught. 234 */ 235 bool item_create_init(); 236 237 /** 238 Empty the hash table for native functions. 239 Note: this code is not thread safe, and is intended to be used at server 240 shutdown only (after thread requests have been executed). 241 */ 242 void item_create_cleanup(); 243 244 /** 245 @} (end of group GROUP_PARSER) 246 */ 247 248 #endif 249