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