| 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-1301 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 */ |
| 65 | Create_func() {} |
| 66 | /** Destructor */ |
| 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. */ |
| 97 | Create_native_func() {} |
| 98 | /** Destructor. */ |
| 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. */ |
| 138 | Create_qfunc() {} |
| 139 | /** Destructor. */ |
| 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. */ |
| 187 | Create_udf_func() {} |
| 188 | /** Destructor. */ |
| 189 | virtual ~Create_udf_func() {} |
| 190 | }; |
| 191 | #endif |
| 192 | |
| 193 | |
| 194 | Item *create_temporal_literal(THD *thd, |
| 195 | const char *str, size_t length, |
| 196 | CHARSET_INFO *cs, |
| 197 | enum_field_types type, |
| 198 | bool send_error); |
| 199 | inline |
| 200 | Item *create_temporal_literal(THD *thd, const String *str, |
| 201 | enum_field_types type, |
| 202 | bool send_error) |
| 203 | { |
| 204 | return create_temporal_literal(thd, |
| 205 | str->ptr(), str->length(), str->charset(), |
| 206 | type, send_error); |
| 207 | } |
| 208 | |
| 209 | struct Native_func_registry |
| 210 | { |
| 211 | LEX_CSTRING name; |
| 212 | Create_func *builder; |
| 213 | }; |
| 214 | |
| 215 | int item_create_init(); |
| 216 | int item_create_append(Native_func_registry array[]); |
| 217 | void item_create_cleanup(); |
| 218 | |
| 219 | Item *create_func_dyncol_create(THD *thd, List<DYNCALL_CREATE_DEF> &list); |
| 220 | Item *create_func_dyncol_add(THD *thd, Item *str, |
| 221 | List<DYNCALL_CREATE_DEF> &list); |
| 222 | Item *create_func_dyncol_delete(THD *thd, Item *str, List<Item> &nums); |
| 223 | Item *create_func_dyncol_get(THD *thd, Item *num, Item *str, |
| 224 | const Type_handler *handler, |
| 225 | const char *c_len, const char *c_dec, |
| 226 | CHARSET_INFO *cs); |
| 227 | Item *create_func_dyncol_json(THD *thd, Item *str); |
| 228 | #endif |
| 229 | |
| 230 | |