1 /* Copyright (c) 2000, 2010, Oracle and/or its affiliates. 2 Copyright (c) 2008-2011 Monty Program Ab 3 4 This program is free software; you can redistribute it and/or modify 5 it under the terms of the GNU General Public License as published by 6 the Free Software Foundation; version 2 of the License. 7 8 This program is distributed in the hope that it will be useful, 9 but WITHOUT ANY WARRANTY; without even the implied warranty of 10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 GNU General Public License for more details. 12 13 You should have received a copy of the GNU General Public License 14 along with this program; if not, write to the Free Software 15 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */ 16 17 /* Functions to create an item. Used by sql/sql_yacc.yy */ 18 19 #ifndef ITEM_CREATE_H 20 #define ITEM_CREATE_H 21 22 #include "item_func.h" // Cast_target 23 24 typedef struct st_udf_func udf_func; 25 26 /** 27 Public function builder interface. 28 The parser (sql/sql_yacc.yy) uses a factory / builder pattern to 29 construct an <code>Item</code> object for each function call. 30 All the concrete function builders implements this interface, 31 either directly or indirectly with some adapter helpers. 32 Keeping the function creation separated from the bison grammar allows 33 to simplify the parser, and avoid the need to introduce a new token 34 for each function, which has undesirable side effects in the grammar. 35 */ 36 37 class Create_func 38 { 39 public: 40 /** 41 The builder create method. 42 Given the function name and list or arguments, this method creates 43 an <code>Item</code> that represents the function call. 44 In case or errors, a NULL item is returned, and an error is reported. 45 Note that the <code>thd</code> object may be modified by the builder. 46 In particular, the following members/methods can be set/called, 47 depending on the function called and the function possible side effects. 48 <ul> 49 <li><code>thd->lex->binlog_row_based_if_mixed</code></li> 50 <li><code>thd->lex->current_context()</code></li> 51 <li><code>thd->lex->safe_to_cache_query</code></li> 52 <li><code>thd->lex->uncacheable(UNCACHEABLE_SIDEEFFECT)</code></li> 53 <li><code>thd->lex->uncacheable(UNCACHEABLE_RAND)</code></li> 54 <li><code>thd->lex->add_time_zone_tables_to_query_tables(thd)</code></li> 55 </ul> 56 @param thd The current thread 57 @param name The function name 58 @param item_list The list of arguments to the function, can be NULL 59 @return An item representing the parsed function call, or NULL 60 */ 61 virtual Item *create_func(THD *thd, LEX_CSTRING *name, List<Item> *item_list) = 0; 62 63 protected: 64 /** Constructor */ Create_func()65 Create_func() {} 66 /** Destructor */ ~Create_func()67 virtual ~Create_func() {} 68 }; 69 70 71 /** 72 Adapter for functions that takes exactly zero arguments. 73 */ 74 75 class Create_func_arg0 : public Create_func 76 { 77 public: 78 virtual Item *create_func(THD *thd, LEX_CSTRING *name, 79 List<Item> *item_list); 80 81 /** 82 Builder method, with no arguments. 83 @param thd The current thread 84 @return An item representing the function call 85 */ 86 virtual Item *create_builder(THD *thd) = 0; 87 88 protected: 89 /** Constructor. */ Create_func_arg0()90 Create_func_arg0() {} 91 /** Destructor. */ ~Create_func_arg0()92 virtual ~Create_func_arg0() {} 93 }; 94 95 96 /** 97 Adapter for functions that takes exactly one argument. 98 */ 99 100 class Create_func_arg1 : public Create_func 101 { 102 public: 103 virtual Item *create_func(THD *thd, LEX_CSTRING *name, List<Item> *item_list); 104 105 /** 106 Builder method, with one argument. 107 @param thd The current thread 108 @param arg1 The first argument of the function 109 @return An item representing the function call 110 */ 111 virtual Item *create_1_arg(THD *thd, Item *arg1) = 0; 112 113 protected: 114 /** Constructor. */ Create_func_arg1()115 Create_func_arg1() {} 116 /** Destructor. */ ~Create_func_arg1()117 virtual ~Create_func_arg1() {} 118 }; 119 120 121 /** 122 Adapter for functions that takes exactly two arguments. 123 */ 124 125 class Create_func_arg2 : public Create_func 126 { 127 public: 128 virtual Item *create_func(THD *thd, LEX_CSTRING *name, List<Item> *item_list); 129 130 /** 131 Builder method, with two arguments. 132 @param thd The current thread 133 @param arg1 The first argument of the function 134 @param arg2 The second argument of the function 135 @return An item representing the function call 136 */ 137 virtual Item *create_2_arg(THD *thd, Item *arg1, Item *arg2) = 0; 138 139 protected: 140 /** Constructor. */ Create_func_arg2()141 Create_func_arg2() {} 142 /** Destructor. */ ~Create_func_arg2()143 virtual ~Create_func_arg2() {} 144 }; 145 146 147 /** 148 Adapter for functions that takes exactly three arguments. 149 */ 150 151 class Create_func_arg3 : public Create_func 152 { 153 public: 154 virtual Item *create_func(THD *thd, LEX_CSTRING *name, List<Item> *item_list); 155 156 /** 157 Builder method, with three arguments. 158 @param thd The current thread 159 @param arg1 The first argument of the function 160 @param arg2 The second argument of the function 161 @param arg3 The third argument of the function 162 @return An item representing the function call 163 */ 164 virtual Item *create_3_arg(THD *thd, Item *arg1, Item *arg2, Item *arg3) = 0; 165 166 protected: 167 /** Constructor. */ Create_func_arg3()168 Create_func_arg3() {} 169 /** Destructor. */ ~Create_func_arg3()170 virtual ~Create_func_arg3() {} 171 }; 172 173 174 175 176 /** 177 Adapter for native functions with a variable number of arguments. 178 The main use of this class is to discard the following calls: 179 <code>foo(expr1 AS name1, expr2 AS name2, ...)</code> 180 which are syntactically correct (the syntax can refer to a UDF), 181 but semantically invalid for native functions. 182 */ 183 184 class Create_native_func : public Create_func 185 { 186 public: 187 virtual Item *create_func(THD *thd, LEX_CSTRING *name, 188 List<Item> *item_list); 189 190 /** 191 Builder method, with no arguments. 192 @param thd The current thread 193 @param name The native function name 194 @param item_list The function parameters, none of which are named 195 @return An item representing the function call 196 */ 197 virtual Item *create_native(THD *thd, LEX_CSTRING *name, 198 List<Item> *item_list) = 0; 199 200 protected: 201 /** Constructor. */ Create_native_func()202 Create_native_func() {} 203 /** Destructor. */ ~Create_native_func()204 virtual ~Create_native_func() {} 205 }; 206 207 208 /** 209 Function builder for qualified functions. 210 This builder is used with functions call using a qualified function name 211 syntax, as in <code>db.func(expr, expr, ...)</code>. 212 */ 213 214 class Create_qfunc : public Create_func 215 { 216 public: 217 /** 218 The builder create method, for unqualified functions. 219 This builder will use the current database for the database name. 220 @param thd The current thread 221 @param name The function name 222 @param item_list The list of arguments to the function, can be NULL 223 @return An item representing the parsed function call 224 */ 225 virtual Item *create_func(THD *thd, LEX_CSTRING *name, 226 List<Item> *item_list); 227 228 /** 229 The builder create method, for qualified functions. 230 @param thd The current thread 231 @param db The database name 232 @param name The function name 233 @param use_explicit_name Should the function be represented as 'db.name'? 234 @param item_list The list of arguments to the function, can be NULL 235 @return An item representing the parsed function call 236 */ 237 virtual Item *create_with_db(THD *thd, LEX_CSTRING *db, LEX_CSTRING *name, 238 bool use_explicit_name, 239 List<Item> *item_list) = 0; 240 241 protected: 242 /** Constructor. */ Create_qfunc()243 Create_qfunc() {} 244 /** Destructor. */ ~Create_qfunc()245 virtual ~Create_qfunc() {} 246 }; 247 248 249 /** 250 Find the native function builder associated with a given function name. 251 @param thd The current thread 252 @param name The native function name 253 @return The native function builder associated with the name, or NULL 254 */ 255 extern Create_func *find_native_function_builder(THD *thd, 256 const LEX_CSTRING *name); 257 258 259 /** 260 Find the function builder for qualified functions. 261 @param thd The current thread 262 @return A function builder for qualified functions 263 */ 264 extern Create_qfunc * find_qualified_function_builder(THD *thd); 265 266 267 #ifdef HAVE_DLOPEN 268 /** 269 Function builder for User Defined Functions. 270 */ 271 272 class Create_udf_func : public Create_func 273 { 274 public: 275 virtual Item *create_func(THD *thd, LEX_CSTRING *name, 276 List<Item> *item_list); 277 278 /** 279 The builder create method, for User Defined Functions. 280 @param thd The current thread 281 @param fct The User Defined Function metadata 282 @param item_list The list of arguments to the function, can be NULL 283 @return An item representing the parsed function call 284 */ 285 Item *create(THD *thd, udf_func *fct, List<Item> *item_list); 286 287 /** Singleton. */ 288 static Create_udf_func s_singleton; 289 290 protected: 291 /** Constructor. */ Create_udf_func()292 Create_udf_func() {} 293 /** Destructor. */ ~Create_udf_func()294 virtual ~Create_udf_func() {} 295 }; 296 #endif 297 298 299 struct Native_func_registry 300 { 301 LEX_CSTRING name; 302 Create_func *builder; 303 }; 304 305 int item_create_init(); 306 int item_create_append(Native_func_registry array[]); 307 int item_create_remove(Native_func_registry array[]); 308 void item_create_cleanup(); 309 310 Item *create_func_dyncol_create(THD *thd, List<DYNCALL_CREATE_DEF> &list); 311 Item *create_func_dyncol_add(THD *thd, Item *str, 312 List<DYNCALL_CREATE_DEF> &list); 313 Item *create_func_dyncol_delete(THD *thd, Item *str, List<Item> &nums); 314 Item *create_func_dyncol_get(THD *thd, Item *num, Item *str, 315 const Type_handler *handler, 316 const char *c_len, const char *c_dec, 317 CHARSET_INFO *cs); 318 Item *create_func_dyncol_json(THD *thd, Item *str); 319 320 321 class Native_func_registry_array 322 { 323 const Native_func_registry *m_elements; 324 size_t m_count; 325 public: Native_func_registry_array()326 Native_func_registry_array() 327 :m_elements(NULL), 328 m_count(0) 329 { } Native_func_registry_array(const Native_func_registry * elements,size_t count)330 Native_func_registry_array(const Native_func_registry *elements, size_t count) 331 :m_elements(elements), 332 m_count(count) 333 { } element(size_t i)334 const Native_func_registry& element(size_t i) const 335 { 336 DBUG_ASSERT(i < m_count); 337 return m_elements[i]; 338 } count()339 size_t count() const { return m_count; } 340 bool append_to_hash(HASH *hash) const; 341 }; 342 343 344 #endif 345 346