MySQL 8.0.32
Source Code Documentation
show_query_builder.h
Go to the documentation of this file.
1/* Copyright (c) 2016, 2022, 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#ifndef SQL_DD_SHOW_QUERY_BUILDER_H
24#define SQL_DD_SHOW_QUERY_BUILDER_H
25
26#include "lex_string.h"
27#include "sql/mem_root_array.h"
28
29class Item;
31class PT_order_list;
34class Query_block;
35class String;
36class THD;
37struct YYLTYPE;
38
39typedef YYLTYPE POS;
40
41namespace dd {
42namespace info_schema {
43
44/**
45 This class provide framework to build a Query_block using ParseTree
46 nodes.
47
48 Note that this class is designed to help build queries that are
49 required to implement SHOW commands over data dictionary tables. It
50 does not provide complete framework, e.g., you cannot add a GROUP BY
51 node for now, mainly because that is not needed to implement SHOW
52 command.
53
54 This class is used by implementation of SHOW command in
55 sql/dd/show.cc. The class enables code re-usability.
56
57 One can build Query_block that represents following,
58
59 ...
60 SELECT star_select_item, select_item1, select_item2, ...
61 FROM from_item OR FROM PT_derived_table
62 WHERE condition AND condition AND ...
63 ORDER BY order_by_field1, order_by_field2 , ...
64 ...
65
66 Where as, a 'condition' can be one of,
67 field_name = "value"
68 field_name LIKE "value%"
69
70 One can think of enhancing this framework on need basis.
71
72 Note to server general team: This framework can be used by
73 sql/sql_show_status.* implementation. For now, this file is kept
74 inside sql/dd, but we can think of moving it out to sql/.
75
76 The memory used while building the this Parse Tree is thd->mem_root.
77*/
78
80 public:
81 Select_lex_builder(const POS *pc, THD *thd);
82
83 /**
84 Add item representing star in "SELECT '*' ...".
85
86 @return false on success.
87 true on failure.
88 */
89
91
92 /**
93 Add item representing a column as,
94 @code
95 SELECT <field_name> AS <alias>, ...
96 @endcode
97
98 The item will be appended to existing list of select items
99 for this query.
100
101 @return false on success.
102 true on failure.
103 */
104
105 bool add_select_item(const LEX_CSTRING &field_name, const LEX_CSTRING &alias);
106
107 /**
108 Add expression as an item tree, with an alias to name the resulting column.
109
110 The item will be appended to existing list of select items
111 for this query block.
112
113 @return false on success.
114 true on failure.
115 */
116
117 bool add_select_expr(Item *select_list_item, const LEX_CSTRING &alias);
118
119 /**
120 Add item representing a FROM clause table as,
121 @code
122 SELECT ... FROM <schema_name>.<table_name> ...
123 @endcode
124
125 Only single table can be added. We cannot build a query with
126 JOIN clause for now.
127
128 @return false on success.
129 true on failure.
130 */
131
132 bool add_from_item(const LEX_CSTRING &schema_name,
133 const LEX_CSTRING &table_name);
134
135 /**
136 Add item representing a FROM clause table as,
137 @code
138 SELECT ... FROM <sub query or derived table> ...
139 @endcode
140
141 Only single table can be added. We cannot build a query with
142 JOIN clause for now.
143
144 @return false on success.
145 true on failure.
146 */
147
149
150 /**
151 Prepare item representing a LIKE condition,
152 @code
153 SELECT ... WHERE <field_name> LIKE <value%> ...
154 @endcode
155
156 This item should be intern added to Select_lex_builder using
157 add_condition() method.
158
159 @return pointer to Item* on success.
160 nullptr on failure.
161 */
162
163 Item *prepare_like_item(const LEX_CSTRING &field_name, const String *wild);
164
165 /**
166 Prepare item representing a equal to comparison condition,
167 @code
168 SELECT ... WHERE <field_name> = <value> ...
169 @endcode
170
171 This item should be intern added to Select_lex_builder using
172 add_condition() method.
173
174 @return pointer to Item* on success.
175 nullptr on failure.
176 */
177
178 Item *prepare_equal_item(const LEX_CSTRING &field_name,
179 const LEX_CSTRING &value);
180
181 /**
182 Add a WHERE clause condition to Select_lex_builder.
183 @code
184 SELECT ... WHERE ... AND <condition> ...
185 @endcode
186
187 If there are existing conditions, then the new condition is
188 append to the WHERE clause conditions with a 'AND' condition.
189
190 @return false on success.
191 true on failure.
192 */
193
194 bool add_condition(Item *a);
195
196 /**
197 Add a ORDER BY clause field to Select_lex_builder.
198 @code
199 SELECT ... ORDER BY <field_name>, ...
200 @endcode
201
202 If there are existing ORDER BY field, then we append a new
203 field to the ORDER BY clause. All the fields are added to be
204 order in ascending order.
205
206 @return false on success.
207 true on failure.
208 */
209
210 bool add_order_by(const LEX_CSTRING &field_name);
211
212 /**
213 This function build ParseTree node that represents this
214 Select_lex_builder as sub-query. This enables us to build a
215 Query_block containing a sub-query in its FROM clause. This
216 sub-query is represented by ParseTree node PT_derived_table.
217 @code
218 SELECT ... FROM <PT_dervied_table>, ...
219 @endcode
220
221 @return pointer to PT_derived_table on success.
222 nullptr on failure.
223 */
224
226
227 /**
228 Prepare a Query_block using all the information information
229 added to this Select_lex_builder.
230
231 @return pointer to Query_block* on success.
232 nullptr on failure.
233 */
234
236
237 private:
238 /**
239 Prepare a list of expression used to build select items for
240 the query being built.
241
242 @return false on success.
243 true on failure.
244 */
245
246 bool add_to_select_item_list(Item *expr);
247
248 private:
249 // Parser current position represented by YYLTYPE
250 const POS *m_pos;
251
252 // Current thread
254
255 // List of select_items for the query.
257
258 // Table reference in FROM clause for the query.
260
261 // Expression representing a WHERE clause.
263
264 // List of order by clause elements.
266};
267
268} // namespace info_schema
269} // namespace dd
270
271#endif /* SQL_DD_SHOW_QUERY_BUILDER_H */
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:850
A typesafe replacement for DYNAMIC_ARRAY.
Definition: mem_root_array.h:60
Definition: parse_tree_nodes.h:483
Definition: parse_tree_nodes.h:213
Definition: parse_tree_nodes.h:381
Definition: parse_tree_nodes.h:404
This class represents a query block, aka a query specification, which is a query consisting of a SELE...
Definition: sql_lex.h:1153
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:166
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:33
This class provide framework to build a Query_block using ParseTree nodes.
Definition: show_query_builder.h:79
Query_block * prepare_query_block()
Prepare a Query_block using all the information information added to this Select_lex_builder.
Definition: show_query_builder.cc:300
PT_select_item_list * m_select_item_list
Definition: show_query_builder.h:256
bool add_condition(Item *a)
Add a WHERE clause condition to Select_lex_builder.
Definition: show_query_builder.cc:229
Item * m_where_clause
Definition: show_query_builder.h:262
bool add_star_select_item()
Add item representing star in "SELECT '*' ...".
Definition: show_query_builder.cc:76
bool add_select_item(const LEX_CSTRING &field_name, const LEX_CSTRING &alias)
Add item representing a column as,.
Definition: show_query_builder.cc:88
const POS * m_pos
Definition: show_query_builder.h:250
PT_order_list * m_order_by_list
Definition: show_query_builder.h:265
Item * prepare_equal_item(const LEX_CSTRING &field_name, const LEX_CSTRING &value)
Prepare item representing a equal to comparison condition,.
Definition: show_query_builder.cc:200
PT_derived_table * prepare_derived_table(const LEX_CSTRING &table_alias)
This function build ParseTree node that represents this Select_lex_builder as sub-query.
Definition: show_query_builder.cc:272
Mem_root_array_YY< PT_table_reference * > m_table_reference_list
Definition: show_query_builder.h:259
bool add_select_expr(Item *select_list_item, const LEX_CSTRING &alias)
Add expression as an item tree, with an alias to name the resulting column.
Definition: show_query_builder.cc:108
bool add_to_select_item_list(Item *expr)
Prepare a list of expression used to build select items for the query being built.
Definition: show_query_builder.cc:62
bool add_from_item(const LEX_CSTRING &schema_name, const LEX_CSTRING &table_name)
Add item representing a FROM clause table as,.
Definition: show_query_builder.cc:124
Select_lex_builder(const POS *pc, THD *thd)
Definition: show_query_builder.cc:53
Item * prepare_like_item(const LEX_CSTRING &field_name, const String *wild)
Prepare item representing a LIKE condition,.
Definition: show_query_builder.cc:172
THD * m_thd
Definition: show_query_builder.h:253
bool add_order_by(const LEX_CSTRING &field_name)
Add a ORDER BY clause field to Select_lex_builder.
Definition: show_query_builder.cc:249
The version of the current data dictionary table definitions.
Definition: dictionary_client.h:42
const char * table_name
Definition: rules_table_service.cc:55
YYLTYPE POS
Definition: show_query_builder.h:37
Definition: mysql_lex_string.h:39
Bison "location" class.
Definition: parse_location.h:42