MySQL 9.2.0
Source Code Documentation
query_result.h
Go to the documentation of this file.
1/* Copyright (c) 2015, 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 QUERY_RESULT_INCLUDED
25#define QUERY_RESULT_INCLUDED
26
27#include <assert.h>
28#include <sys/types.h>
29
30#include <cstddef>
31
32#include "mem_root_deque.h"
33#include "my_base.h"
34
35#include "my_inttypes.h"
36#include "my_io.h"
37#include "my_sys.h"
40#include "mysqld_error.h" // ER_*
41#include "sql/sql_exchange.h"
42#include "sql/sql_list.h"
43
44class Item;
45class Item_subselect;
46class PT_select_var;
50class THD;
51struct CHARSET_INFO;
52template <class Element_type>
53class mem_root_deque;
54
55/*
56 This is used to get result from a query
57*/
58
60 protected:
62
63 public:
64 /**
65 Number of records estimated in this result.
66 Valid only for materialized derived tables/views.
67 */
69 /**
70 Cost to execute the subquery which produces this result.
71 Valid only for materialized derived tables/views.
72 */
74
76 virtual ~Query_result() = default;
77
78 virtual bool needs_file_privilege() const { return false; }
79
80 /**
81 Returns true if the data has to be exported to object store.
82 */
83 virtual bool export_result_to_object_storage() const { return false; }
84
85 /**
86 Change wrapped Query_result.
87
88 Replace the wrapped query result object with new_result and call
89 prepare() on new_result.
90
91 This base class implementation doesn't wrap other Query_results.
92
93 @retval false Success
94 @retval true Error
95 */
96 virtual bool change_query_result(THD *, Query_result *) { return false; }
97 /// @return true if an interceptor object is needed for EXPLAIN
98 virtual bool need_explain_interceptor() const { return false; }
99
100 /**
101 Perform preparation specific to the query expression or DML statement.
102
103 @returns false if success, true if error
104 */
105 virtual bool prepare(THD *, const mem_root_deque<Item *> &,
106 Query_expression *u) {
107 unit = u;
108 return false;
109 }
110
111 /**
112 Prepare for execution of the query expression or DML statement.
113
114 Generally, this will have an implementation only for outer-most
115 Query_block objects, such as data change statements (for preparation
116 of the target table(s)) or dump statements (for preparation of target file).
117
118 @returns false if success, true if error
119 */
120 virtual bool start_execution(THD *) { return false; }
121
122 /// Create table, only needed to support CREATE TABLE ... SELECT
123 virtual bool create_table_for_query_block(THD *) { return false; }
124 /*
125 Because of peculiarities of prepared statements protocol
126 we need to know number of columns in the result set (if
127 there is a result set) apart from sending columns metadata.
128 */
129 virtual uint field_count(const mem_root_deque<Item *> &fields) const;
130 virtual bool send_result_set_metadata(THD *thd,
132 uint flags) = 0;
133 virtual bool send_data(THD *thd, const mem_root_deque<Item *> &items) = 0;
134 virtual bool send_eof(THD *thd) = 0;
135 /**
136 Check if this query result set supports cursors
137
138 @returns false if success, true if error
139 */
140 virtual bool check_supports_cursor() const {
141 my_error(ER_SP_BAD_CURSOR_QUERY, MYF(0));
142 return true;
143 }
144 virtual void abort_result_set(THD *) {}
145 /**
146 Cleanup after one execution of the unit, to be ready for a next execution
147 inside the same statement.
148 @returns true if error
149 */
150 virtual bool reset() {
151 assert(false);
152 return false;
153 }
154 /**
155 Cleanup after this execution. Completes the execution and resets object
156 before next execution of a prepared statement/stored procedure.
157 */
158 virtual void cleanup() { /* do nothing */
159 }
160 /**
161 @returns true if an alternative implementation may replace this with
162 a protocol adapter.
163 */
164 virtual bool use_protocol_adapter() const { return false; }
165 /**
166 @returns true if an alternative implementation may replace this with
167 a protocol wrapper.
168 */
169 virtual bool use_protocol_wrapper() const { return false; }
170
171 /// Only overridden (and non-empty) for Query_result_union, q.v.
172 virtual void set_limit(ha_rows) {}
173
174 /// @returns server side cursor, if associated with query result
175 virtual Server_side_cursor *cursor() const {
176 assert(false);
177 return nullptr;
178 }
179};
180
181/*
182 Base class for Query_result descendants which intercept and
183 transform result set rows. As the rows are not sent to the client,
184 sending of result set metadata should be suppressed as well.
185*/
186
188 public:
190 uint field_count(const mem_root_deque<Item *> &) const override { return 0; }
192 uint) override {
193 return false;
194 }
195};
196
198 /**
199 True if we have sent result set metadata to the client.
200 In this case the client always expects us to end the result
201 set with an eof or error packet
202 */
204
205 public:
208 uint flags) override;
209 bool send_data(THD *thd, const mem_root_deque<Item *> &items) override;
210 bool send_eof(THD *thd) override;
211 bool check_supports_cursor() const override { return false; }
212 void abort_result_set(THD *thd) override;
213 void cleanup() override { is_result_set_started = false; }
214 /**
215 An alternative implementation may provide an optimized protocol adapter
216 for this object.
217 */
218 bool use_protocol_adapter() const override { return true; }
219};
220
221class sql_exchange;
222
224 protected:
230
231 public:
234 path[0] = 0;
235 }
236 ~Query_result_to_file() override { assert(file < 0); }
237
238 bool needs_file_privilege() const override { return true; }
239 bool check_supports_cursor() const override;
240 bool send_eof(THD *thd) override;
241 void cleanup() override;
242};
243
245 protected:
247
248 public:
251
252 bool send_data(THD *, const mem_root_deque<Item *> &) override {
253 return false;
254 }
255 bool send_eof(THD *thd) override;
256 void cleanup() override;
257 bool export_result_to_object_storage() const override { return true; }
258 bool use_protocol_adapter() const override { return true; }
260};
261
262#define ESCAPE_CHARS "ntrb0ZN" // keep synchronous with READ_INFO::unescape
263
264/*
265 List of all possible characters of a numeric value text representation.
266*/
267#define NUMERIC_CHARS ".0123456789e+-"
268
272 int field_term_char; // first char of FIELDS TERMINATED BY or MAX_INT
273 /*
274 The is_ambiguous_field_sep field is true if a value of the field_sep_char
275 field is one of the 'n', 't', 'r' etc characters
276 (see the READ_INFO::unescape method and the ESCAPE_CHARS constant value).
277 */
279 /*
280 The is_ambiguous_field_term is true if field_sep_char contains the first
281 char of the FIELDS TERMINATED BY (ENCLOSED BY is empty), and items can
282 contain this character.
283 */
285 /*
286 The is_unsafe_field_sep field is true if a value of the field_sep_char
287 field is one of the '0'..'9', '+', '-', '.' and 'e' characters
288 (see the NUMERIC_CHARS constant value).
289 */
292 const CHARSET_INFO *write_cs; // output charset
293 public:
295 bool prepare(THD *thd, const mem_root_deque<Item *> &list,
296 Query_expression *u) override;
297 bool start_execution(THD *thd) override;
298 bool send_data(THD *thd, const mem_root_deque<Item *> &items) override;
299 void cleanup() override;
300};
301
303 public:
305 bool prepare(THD *thd, const mem_root_deque<Item *> &list,
306 Query_expression *u) override;
307 bool start_execution(THD *thd) override;
308 bool send_data(THD *thd, const mem_root_deque<Item *> &items) override;
309};
310
313
314 public:
317 var_list.clear();
318 }
319 bool prepare(THD *thd, const mem_root_deque<Item *> &list,
320 Query_expression *u) override;
321 bool send_data(THD *thd, const mem_root_deque<Item *> &items) override;
322 bool send_eof(THD *thd) override;
323 bool check_supports_cursor() const override;
324 void cleanup() override { row_count = 0; }
325 /**
326 An alternative implementation may provide an optimized protocol wrapper
327 for this object.
328 */
329 bool use_protocol_wrapper() const override { return true; }
330};
331
332/**
333 Base class for result from a subquery.
334*/
335
337 protected:
339
340 public:
342 : Query_result_interceptor(), item(item_arg) {}
343 bool send_data(THD *thd, const mem_root_deque<Item *> &items) override = 0;
344 bool send_eof(THD *) override { return false; }
345};
346
347#endif // QUERY_RESULT_INCLUDED
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:251
Base class that is common to all subqueries and subquery predicates.
Definition: item_subselect.h:80
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:930
Definition: sql_list.h:494
Definition: parse_tree_nodes.h:1351
Definition: query_result.h:311
List< PT_select_var > var_list
Definition: query_result.h:315
bool prepare(THD *thd, const mem_root_deque< Item * > &list, Query_expression *u) override
Perform preparation specific to the query expression or DML statement.
Definition: query_result.cc:999
void cleanup() override
Cleanup after this execution.
Definition: query_result.h:324
bool send_eof(THD *thd) override
Definition: query_result.cc:1048
ha_rows row_count
Definition: query_result.h:312
Query_dumpvar()
Definition: query_result.h:316
bool send_data(THD *thd, const mem_root_deque< Item * > &items) override
Definition: query_result.cc:1016
bool check_supports_cursor() const override
Check if this query result set supports cursors.
Definition: query_result.cc:1011
bool use_protocol_wrapper() const override
An alternative implementation may provide an optimized protocol wrapper for this object.
Definition: query_result.h:329
This class represents a query expression (one query block or several query blocks combined with UNION...
Definition: sql_lex.h:638
Definition: query_result.h:302
Query_result_dump(sql_exchange *ex)
Definition: query_result.h:304
bool send_data(THD *thd, const mem_root_deque< Item * > &items) override
Definition: query_result.cc:968
bool prepare(THD *thd, const mem_root_deque< Item * > &list, Query_expression *u) override
Perform preparation specific to the query expression or DML statement.
Definition: query_result.cc:957
bool start_execution(THD *thd) override
Prepare for execution of the query expression or DML statement.
Definition: query_result.cc:963
Definition: query_result.h:269
bool start_execution(THD *thd) override
Prepare for execution of the query expression or DML statement.
Definition: query_result.cc:643
const CHARSET_INFO * write_cs
Definition: query_result.h:292
bool is_unsafe_field_sep
Definition: query_result.h:290
bool prepare(THD *thd, const mem_root_deque< Item * > &list, Query_expression *u) override
Perform preparation specific to the query expression or DML statement.
Definition: query_result.cc:556
void cleanup() override
Cleanup after this execution.
Definition: query_result.cc:948
int field_sep_char
Definition: query_result.h:271
bool fixed_row_size
Definition: query_result.h:291
int escape_char
Definition: query_result.h:271
bool send_data(THD *thd, const mem_root_deque< Item * > &items) override
Definition: query_result.cc:654
size_t field_term_length
Definition: query_result.h:270
int line_sep_char
Definition: query_result.h:271
Query_result_export(sql_exchange *ex)
Definition: query_result.h:294
bool is_ambiguous_field_sep
Definition: query_result.h:278
bool is_ambiguous_field_term
Definition: query_result.h:284
int field_term_char
Definition: query_result.h:272
Definition: query_result.h:187
Query_result_interceptor()
Definition: query_result.h:189
bool send_result_set_metadata(THD *, const mem_root_deque< Item * > &, uint) override
Definition: query_result.h:191
uint field_count(const mem_root_deque< Item * > &) const override
Definition: query_result.h:190
Definition: query_result.h:197
void cleanup() override
Cleanup after this execution.
Definition: query_result.h:213
bool is_result_set_started
True if we have sent result set metadata to the client.
Definition: query_result.h:203
bool use_protocol_adapter() const override
An alternative implementation may provide an optimized protocol adapter for this object.
Definition: query_result.h:218
Query_result_send()
Definition: query_result.h:206
bool check_supports_cursor() const override
Check if this query result set supports cursors.
Definition: query_result.h:211
bool send_data(THD *thd, const mem_root_deque< Item * > &items) override
Definition: query_result.cc:99
void abort_result_set(THD *thd) override
Definition: query_result.cc:80
bool send_result_set_metadata(THD *thd, const mem_root_deque< Item * > &list, uint flags) override
Definition: query_result.cc:72
bool send_eof(THD *thd) override
Definition: query_result.cc:114
Base class for result from a subquery.
Definition: query_result.h:336
bool send_eof(THD *) override
Definition: query_result.h:344
Query_result_subquery(Item_subselect *item_arg)
Definition: query_result.h:341
bool send_data(THD *thd, const mem_root_deque< Item * > &items) override=0
Item_subselect * item
Definition: query_result.h:338
Definition: query_result.h:223
bool needs_file_privilege() const override
Definition: query_result.h:238
IO_CACHE cache
Definition: query_result.h:227
bool check_supports_cursor() const override
Check if this query result set supports cursors.
Definition: query_result.cc:435
sql_exchange * exchange
Definition: query_result.h:225
char path[FN_REFLEN]
Definition: query_result.h:229
Query_result_to_file(sql_exchange *ex)
Definition: query_result.h:232
File file
Definition: query_result.h:226
void cleanup() override
Cleanup after this execution.
Definition: query_result.cc:451
~Query_result_to_file() override
Definition: query_result.h:236
ha_rows row_count
Definition: query_result.h:228
bool send_eof(THD *thd) override
Definition: query_result.cc:440
Definition: query_result.h:244
Query_result_to_object_store(sql_exchange *ex)
Definition: query_result.h:249
sql_exchange * get_sql_exchange()
Definition: query_result.h:259
void cleanup() override
Reset the number of affected rows.
Definition: query_result.cc:478
bool export_result_to_object_storage() const override
Returns true if the data has to be exported to object store.
Definition: query_result.h:257
bool send_data(THD *, const mem_root_deque< Item * > &) override
Definition: query_result.h:252
sql_exchange * exchange
Definition: query_result.h:246
bool use_protocol_adapter() const override
Definition: query_result.h:258
bool send_eof(THD *thd) override
Returns true incase of an error.
Definition: query_result.cc:469
Definition: query_result.h:59
virtual Server_side_cursor * cursor() const
Definition: query_result.h:175
Query_result()
Definition: query_result.h:75
virtual bool send_eof(THD *thd)=0
virtual bool send_result_set_metadata(THD *thd, const mem_root_deque< Item * > &list, uint flags)=0
virtual bool send_data(THD *thd, const mem_root_deque< Item * > &items)=0
virtual bool change_query_result(THD *, Query_result *)
Change wrapped Query_result.
Definition: query_result.h:96
virtual bool need_explain_interceptor() const
Definition: query_result.h:98
virtual bool prepare(THD *, const mem_root_deque< Item * > &, Query_expression *u)
Perform preparation specific to the query expression or DML statement.
Definition: query_result.h:105
double estimated_cost
Cost to execute the subquery which produces this result.
Definition: query_result.h:73
ha_rows estimated_rowcount
Number of records estimated in this result.
Definition: query_result.h:68
virtual void cleanup()
Cleanup after this execution.
Definition: query_result.h:158
virtual bool use_protocol_wrapper() const
Definition: query_result.h:169
virtual bool reset()
Cleanup after one execution of the unit, to be ready for a next execution inside the same statement.
Definition: query_result.h:150
virtual bool export_result_to_object_storage() const
Returns true if the data has to be exported to object store.
Definition: query_result.h:83
virtual bool check_supports_cursor() const
Check if this query result set supports cursors.
Definition: query_result.h:140
virtual uint field_count(const mem_root_deque< Item * > &fields) const
Definition: query_result.cc:68
virtual void abort_result_set(THD *)
Definition: query_result.h:144
virtual bool use_protocol_adapter() const
Definition: query_result.h:164
virtual bool create_table_for_query_block(THD *)
Create table, only needed to support CREATE TABLE ... SELECT.
Definition: query_result.h:123
Query_expression * unit
Definition: query_result.h:61
virtual ~Query_result()=default
virtual bool needs_file_privilege() const
Definition: query_result.h:78
virtual void set_limit(ha_rows)
Only overridden (and non-empty) for Query_result_union, q.v.
Definition: query_result.h:172
virtual bool start_execution(THD *)
Prepare for execution of the query expression or DML statement.
Definition: query_result.h:120
Common base class for n-ary set operations, including unary.
Definition: query_term.h:555
Server_side_cursor – an interface for materialized implementation of cursors.
Definition: sql_cursor.h:51
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
A (partial) implementation of std::deque allocating its blocks on a MEM_ROOT.
Definition: mem_root_deque.h:111
Definition: sql_exchange.h:192
#define L
Definition: ctype-tis620.cc:75
void my_error(int nr, myf MyFlags,...)
Fill in and print a previously registered error message.
Definition: my_error.cc:216
static int flags[50]
Definition: hp_test1.cc:40
A better implementation of the UNIX ctype(3) library.
This file includes constants used by all storage engines.
my_off_t ha_rows
Definition: my_base.h:1141
Some integer typedefs for easier portability.
#define MYF(v)
Definition: my_inttypes.h:97
Common #defines and includes for file and socket I/O.
#define FN_REFLEN
Definition: my_io.h:87
Types to make file and socket I/O compatible.
int File
Definition: my_io_bits.h:51
Common header for many mysys elements.
Definition: os0file.h:89
std::list< T, ut::allocator< T > > list
Specialization of list which uses ut_allocator.
Definition: ut0new.h:2880
Definition: m_ctype.h:421
Definition: my_sys.h:336