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 native functions with a variable number of arguments. 73 The main use of this class is to discard the following calls: 74 <code>foo(expr1 AS name1, expr2 AS name2, ...)</code> 75 which are syntactically correct (the syntax can refer to a UDF), 76 but semantically invalid for native functions. 77 */ 78 79 class Create_native_func : public Create_func 80 { 81 public: 82 virtual Item *create_func(THD *thd, LEX_CSTRING *name, 83 List<Item> *item_list); 84 85 /** 86 Builder method, with no arguments. 87 @param thd The current thread 88 @param name The native function name 89 @param item_list The function parameters, none of which are named 90 @return An item representing the function call 91 */ 92 virtual Item *create_native(THD *thd, LEX_CSTRING *name, 93 List<Item> *item_list) = 0; 94 95 protected: 96 /** Constructor. */ Create_native_func()97 Create_native_func() {} 98 /** Destructor. */ ~Create_native_func()99 virtual ~Create_native_func() {} 100 }; 101 102 103 /** 104 Function builder for qualified functions. 105 This builder is used with functions call using a qualified function name 106 syntax, as in <code>db.func(expr, expr, ...)</code>. 107 */ 108 109 class Create_qfunc : public Create_func 110 { 111 public: 112 /** 113 The builder create method, for unqualified functions. 114 This builder will use the current database for the database name. 115 @param thd The current thread 116 @param name The function name 117 @param item_list The list of arguments to the function, can be NULL 118 @return An item representing the parsed function call 119 */ 120 virtual Item *create_func(THD *thd, LEX_CSTRING *name, 121 List<Item> *item_list); 122 123 /** 124 The builder create method, for qualified functions. 125 @param thd The current thread 126 @param db The database name 127 @param name The function name 128 @param use_explicit_name Should the function be represented as 'db.name'? 129 @param item_list The list of arguments to the function, can be NULL 130 @return An item representing the parsed function call 131 */ 132 virtual Item *create_with_db(THD *thd, LEX_CSTRING *db, LEX_CSTRING *name, 133 bool use_explicit_name, 134 List<Item> *item_list) = 0; 135 136 protected: 137 /** Constructor. */ Create_qfunc()138 Create_qfunc() {} 139 /** Destructor. */ ~Create_qfunc()140 virtual ~Create_qfunc() {} 141 }; 142 143 144 /** 145 Find the native function builder associated with a given function name. 146 @param thd The current thread 147 @param name The native function name 148 @return The native function builder associated with the name, or NULL 149 */ 150 extern Create_func *find_native_function_builder(THD *thd, 151 const LEX_CSTRING *name); 152 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 #ifdef HAVE_DLOPEN 163 /** 164 Function builder for User Defined Functions. 165 */ 166 167 class Create_udf_func : public Create_func 168 { 169 public: 170 virtual Item *create_func(THD *thd, LEX_CSTRING *name, 171 List<Item> *item_list); 172 173 /** 174 The builder create method, for User Defined Functions. 175 @param thd The current thread 176 @param fct The User Defined Function metadata 177 @param item_list The list of arguments to the function, can be NULL 178 @return An item representing the parsed function call 179 */ 180 Item *create(THD *thd, udf_func *fct, List<Item> *item_list); 181 182 /** Singleton. */ 183 static Create_udf_func s_singleton; 184 185 protected: 186 /** Constructor. */ Create_udf_func()187 Create_udf_func() {} 188 /** Destructor. */ ~Create_udf_func()189 virtual ~Create_udf_func() {} 190 }; 191 #endif 192 193 194 struct Native_func_registry 195 { 196 LEX_CSTRING name; 197 Create_func *builder; 198 }; 199 200 int item_create_init(); 201 int item_create_append(Native_func_registry array[]); 202 int item_create_remove(Native_func_registry array[]); 203 void item_create_cleanup(); 204 205 Item *create_func_dyncol_create(THD *thd, List<DYNCALL_CREATE_DEF> &list); 206 Item *create_func_dyncol_add(THD *thd, Item *str, 207 List<DYNCALL_CREATE_DEF> &list); 208 Item *create_func_dyncol_delete(THD *thd, Item *str, List<Item> &nums); 209 Item *create_func_dyncol_get(THD *thd, Item *num, Item *str, 210 const Type_handler *handler, 211 const char *c_len, const char *c_dec, 212 CHARSET_INFO *cs); 213 Item *create_func_dyncol_json(THD *thd, Item *str); 214 #endif 215 216