1 /* Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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 #include "my_global.h"
29 #include "mysql/mysql_lex_string.h"     // LEX_STRING
30 #include "item_func.h"                  // Cast_target
31 
32 class Item;
33 class PT_item_list;
34 class THD;
35 
36 typedef struct charset_info_st CHARSET_INFO;
37 typedef struct st_udf_func udf_func;
38 struct Cast_type;
39 
40 /**
41   Public function builder interface.
42   The parser (sql/sql_yacc.yy) uses a factory / builder pattern to
43   construct an <code>Item</code> object for each function call.
44   All the concrete function builders implements this interface,
45   either directly or indirectly with some adapter helpers.
46   Keeping the function creation separated from the bison grammar allows
47   to simplify the parser, and avoid the need to introduce a new token
48   for each function, which has undesirable side effects in the grammar.
49 */
50 
51 class Create_func
52 {
53 public:
54   /**
55     The builder create method.
56     Given the function name and list or arguments, this method creates
57     an <code>Item</code> that represents the function call.
58     In case or errors, a NULL item is returned, and an error is reported.
59     Note that the <code>thd</code> object may be modified by the builder.
60     In particular, the following members/methods can be set/called,
61     depending on the function called and the function possible side effects.
62     <ul>
63       <li><code>thd->lex->binlog_row_based_if_mixed</code></li>
64       <li><code>thd->lex->current_context()</code></li>
65       <li><code>thd->lex->safe_to_cache_query</code></li>
66       <li><code>thd->lex->uncacheable(UNCACHEABLE_SIDEEFFECT)</code></li>
67       <li><code>thd->lex->uncacheable(UNCACHEABLE_RAND)</code></li>
68       <li><code>thd->lex->add_time_zone_tables_to_query_tables(thd)</code></li>
69     </ul>
70     @param thd The current thread
71     @param name The function name
72     @param item_list The list of arguments to the function, can be NULL
73     @return An item representing the parsed function call, or NULL
74   */
75   virtual Item *create_func(THD *thd, LEX_STRING name, PT_item_list *item_list)
76     = 0;
77 
78 protected:
79   /** Constructor */
Create_func()80   Create_func() {}
81   /** Destructor */
~Create_func()82   virtual ~Create_func() {}
83 };
84 
85 
86 /**
87   Function builder for qualified functions.
88   This builder is used with functions call using a qualified function name
89   syntax, as in <code>db.func(expr, expr, ...)</code>.
90 */
91 
92 class Create_qfunc : public Create_func
93 {
94 public:
95   /**
96     The builder create method, for unqualified functions.
97     This builder will use the current database for the database name.
98     @param thd The current thread
99     @param name The function name
100     @param item_list The list of arguments to the function, can be NULL
101     @return An item representing the parsed function call
102   */
103   virtual Item *create_func(THD *thd, LEX_STRING name, PT_item_list *item_list);
104 
105   /**
106     The builder create method, for qualified functions.
107     @param thd The current thread
108     @param db The database name or NULL_STR to use the default db name
109     @param name The function name
110     @param use_explicit_name Should the function be represented as 'db.name'?
111     @param item_list The list of arguments to the function, can be NULL
112     @return An item representing the parsed function call
113   */
114   virtual Item* create(THD *thd, LEX_STRING db, LEX_STRING name,
115                        bool use_explicit_name, PT_item_list *item_list) = 0;
116 
117 protected:
118   /** Constructor. */
Create_qfunc()119   Create_qfunc() {}
120   /** Destructor. */
~Create_qfunc()121   virtual ~Create_qfunc() {}
122 };
123 
124 
125 /**
126   Find the native function builder associated with a given function name.
127   @param thd The current thread
128   @param name The native function name
129   @return The native function builder associated with the name, or NULL
130 */
131 extern Create_func * find_native_function_builder(THD *thd, LEX_STRING name);
132 
133 
134 /**
135   Find the function builder for qualified functions.
136   @param thd The current thread
137   @return A function builder for qualified functions
138 */
139 extern Create_qfunc * find_qualified_function_builder(THD *thd);
140 
141 
142 #ifdef HAVE_DLOPEN
143 /**
144   Function builder for User Defined Functions.
145 */
146 
147 class Create_udf_func : public Create_func
148 {
149 public:
150   virtual Item *create_func(THD *thd, LEX_STRING name, PT_item_list *item_list);
151 
152   /**
153     The builder create method, for User Defined Functions.
154     @param thd The current thread
155     @param fct The User Defined Function metadata
156     @param item_list The list of arguments to the function, can be NULL
157     @return An item representing the parsed function call
158   */
159   Item *create(THD *thd, udf_func *fct, PT_item_list *item_list);
160 
161   /** Singleton. */
162   static Create_udf_func s_singleton;
163 
164 protected:
165   /** Constructor. */
Create_udf_func()166   Create_udf_func() {}
167   /** Destructor. */
~Create_udf_func()168   virtual ~Create_udf_func() {}
169 };
170 #endif
171 
172 
173 /**
174   Builder for cast expressions.
175   @param thd The current thread
176   @param pos Location of casting expression
177   @param a The item to cast
178   @param type the type casted into
179 */
180 Item *
181 create_func_cast(THD *thd, const POS &pos, Item *a, const Cast_type *type);
182 Item *
183 create_func_cast(THD *thd, const POS &pos, Item *a, Cast_target cast_target,
184                  const CHARSET_INFO *cs_arg);
185 
186 Item *create_temporal_literal(THD *thd,
187                               const char *str, size_t length,
188                               const CHARSET_INFO *cs,
189                               enum_field_types type, bool send_error);
190 
191 int item_create_init();
192 void item_create_cleanup();
193 
194 #endif
195 
196