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