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