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