MySQL 8.4.2
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_list.h"
42
43class Item;
44class Item_subselect;
45class PT_select_var;
49class THD;
50struct CHARSET_INFO;
51template <class Element_type>
52class mem_root_deque;
53
54/*
55 This is used to get result from a query
56*/
57
59 protected:
61
62 public:
63 /**
64 Number of records estimated in this result.
65 Valid only for materialized derived tables/views.
66 */
68 /**
69 Cost to execute the subquery which produces this result.
70 Valid only for materialized derived tables/views.
71 */
73
75 virtual ~Query_result() = default;
76
77 virtual bool needs_file_privilege() const { return false; }
78
79 /**
80 Change wrapped Query_result.
81
82 Replace the wrapped query result object with new_result and call
83 prepare() on new_result.
84
85 This base class implementation doesn't wrap other Query_results.
86
87 @retval false Success
88 @retval true Error
89 */
90 virtual bool change_query_result(THD *, Query_result *) { return false; }
91 /// @return true if an interceptor object is needed for EXPLAIN
92 virtual bool need_explain_interceptor() const { return false; }
93
94 /**
95 Perform preparation specific to the query expression or DML statement.
96
97 @returns false if success, true if error
98 */
99 virtual bool prepare(THD *, const mem_root_deque<Item *> &,
100 Query_expression *u) {
101 unit = u;
102 return false;
103 }
104
105 /**
106 Prepare for execution of the query expression or DML statement.
107
108 Generally, this will have an implementation only for outer-most
109 Query_block objects, such as data change statements (for preparation
110 of the target table(s)) or dump statements (for preparation of target file).
111
112 @returns false if success, true if error
113 */
114 virtual bool start_execution(THD *) { return false; }
115
116 /// Create table, only needed to support CREATE TABLE ... SELECT
117 virtual bool create_table_for_query_block(THD *) { return false; }
118 /*
119 Because of peculiarities of prepared statements protocol
120 we need to know number of columns in the result set (if
121 there is a result set) apart from sending columns metadata.
122 */
123 virtual uint field_count(const mem_root_deque<Item *> &fields) const;
124 virtual bool send_result_set_metadata(THD *thd,
126 uint flags) = 0;
127 virtual bool send_data(THD *thd, const mem_root_deque<Item *> &items) = 0;
128 virtual bool send_eof(THD *thd) = 0;
129 /**
130 Check if this query result set supports cursors
131
132 @returns false if success, true if error
133 */
134 virtual bool check_supports_cursor() const {
135 my_error(ER_SP_BAD_CURSOR_QUERY, MYF(0));
136 return true;
137 }
138 virtual void abort_result_set(THD *) {}
139 /**
140 Cleanup after one execution of the unit, to be ready for a next execution
141 inside the same statement.
142 @returns true if error
143 */
144 virtual bool reset() {
145 assert(false);
146 return false;
147 }
148 /**
149 Cleanup after this execution. Completes the execution and resets object
150 before next execution of a prepared statement/stored procedure.
151 */
152 virtual void cleanup() { /* do nothing */
153 }
154 /**
155 @returns true if an alternative implementation may replace this with
156 a protocol adapter.
157 */
158 virtual bool use_protocol_adapter() const { return false; }
159 /**
160 @returns true if an alternative implementation may replace this with
161 a protocol wrapper.
162 */
163 virtual bool use_protocol_wrapper() const { return false; }
164
165 /// Only overridden (and non-empty) for Query_result_union, q.v.
166 virtual void set_limit(ha_rows) {}
167
168 /// @returns server side cursor, if associated with query result
169 virtual Server_side_cursor *cursor() const {
170 assert(false);
171 return nullptr;
172 }
173};
174
175/*
176 Base class for Query_result descendants which intercept and
177 transform result set rows. As the rows are not sent to the client,
178 sending of result set metadata should be suppressed as well.
179*/
180
182 public:
184 uint field_count(const mem_root_deque<Item *> &) const override { return 0; }
186 uint) override {
187 return false;
188 }
189};
190
192 /**
193 True if we have sent result set metadata to the client.
194 In this case the client always expects us to end the result
195 set with an eof or error packet
196 */
198
199 public:
202 uint flags) override;
203 bool send_data(THD *thd, const mem_root_deque<Item *> &items) override;
204 bool send_eof(THD *thd) override;
205 bool check_supports_cursor() const override { return false; }
206 void abort_result_set(THD *thd) override;
207 void cleanup() override { is_result_set_started = false; }
208 /**
209 An alternative implementation may provide an optimized protocol adapter
210 for this object.
211 */
212 bool use_protocol_adapter() const override { return true; }
213};
214
215class sql_exchange;
216
218 protected:
224
225 public:
228 path[0] = 0;
229 }
230 ~Query_result_to_file() override { assert(file < 0); }
231
232 bool needs_file_privilege() const override { return true; }
233 bool check_supports_cursor() const override;
234 bool send_eof(THD *thd) override;
235 void cleanup() override;
236};
237
238#define ESCAPE_CHARS "ntrb0ZN" // keep synchronous with READ_INFO::unescape
239
240/*
241 List of all possible characters of a numeric value text representation.
242*/
243#define NUMERIC_CHARS ".0123456789e+-"
244
248 int field_term_char; // first char of FIELDS TERMINATED BY or MAX_INT
249 /*
250 The is_ambiguous_field_sep field is true if a value of the field_sep_char
251 field is one of the 'n', 't', 'r' etc characters
252 (see the READ_INFO::unescape method and the ESCAPE_CHARS constant value).
253 */
255 /*
256 The is_ambiguous_field_term is true if field_sep_char contains the first
257 char of the FIELDS TERMINATED BY (ENCLOSED BY is empty), and items can
258 contain this character.
259 */
261 /*
262 The is_unsafe_field_sep field is true if a value of the field_sep_char
263 field is one of the '0'..'9', '+', '-', '.' and 'e' characters
264 (see the NUMERIC_CHARS constant value).
265 */
268 const CHARSET_INFO *write_cs; // output charset
269 public:
271 bool prepare(THD *thd, const mem_root_deque<Item *> &list,
272 Query_expression *u) override;
273 bool start_execution(THD *thd) override;
274 bool send_data(THD *thd, const mem_root_deque<Item *> &items) override;
275 void cleanup() override;
276};
277
279 public:
281 bool prepare(THD *thd, const mem_root_deque<Item *> &list,
282 Query_expression *u) override;
283 bool start_execution(THD *thd) override;
284 bool send_data(THD *thd, const mem_root_deque<Item *> &items) override;
285};
286
289
290 public:
293 var_list.clear();
294 }
295 bool prepare(THD *thd, const mem_root_deque<Item *> &list,
296 Query_expression *u) override;
297 bool send_data(THD *thd, const mem_root_deque<Item *> &items) override;
298 bool send_eof(THD *thd) override;
299 bool check_supports_cursor() const override;
300 void cleanup() override { row_count = 0; }
301 /**
302 An alternative implementation may provide an optimized protocol wrapper
303 for this object.
304 */
305 bool use_protocol_wrapper() const override { return true; }
306};
307
308/**
309 Base class for result from a subquery.
310*/
311
313 protected:
315
316 public:
318 : Query_result_interceptor(), item(item_arg) {}
319 bool send_data(THD *thd, const mem_root_deque<Item *> &items) override = 0;
320 bool send_eof(THD *) override { return false; }
321};
322
323#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:936
Definition: sql_list.h:467
Definition: parse_tree_nodes.h:1322
Definition: query_result.h:287
List< PT_select_var > var_list
Definition: query_result.h:291
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:693
void cleanup() override
Cleanup after this execution.
Definition: query_result.h:300
bool send_eof(THD *thd) override
Definition: query_result.cc:741
ha_rows row_count
Definition: query_result.h:288
Query_dumpvar()
Definition: query_result.h:292
bool send_data(THD *thd, const mem_root_deque< Item * > &items) override
Definition: query_result.cc:710
bool check_supports_cursor() const override
Check if this query result set supports cursors.
Definition: query_result.cc:705
bool use_protocol_wrapper() const override
An alternative implementation may provide an optimized protocol wrapper for this object.
Definition: query_result.h:305
This class represents a query expression (one query block or several query blocks combined with UNION...
Definition: sql_lex.h:626
Definition: query_result.h:278
Query_result_dump(sql_exchange *ex)
Definition: query_result.h:280
bool send_data(THD *thd, const mem_root_deque< Item * > &items) override
Definition: query_result.cc:662
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:651
bool start_execution(THD *thd) override
Prepare for execution of the query expression or DML statement.
Definition: query_result.cc:657
Definition: query_result.h:245
bool start_execution(THD *thd) override
Prepare for execution of the query expression or DML statement.
Definition: query_result.cc:341
const CHARSET_INFO * write_cs
Definition: query_result.h:268
bool is_unsafe_field_sep
Definition: query_result.h:266
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:255
void cleanup() override
Cleanup after this execution.
Definition: query_result.cc:642
int field_sep_char
Definition: query_result.h:247
bool fixed_row_size
Definition: query_result.h:267
int escape_char
Definition: query_result.h:247
bool send_data(THD *thd, const mem_root_deque< Item * > &items) override
Definition: query_result.cc:352
size_t field_term_length
Definition: query_result.h:246
int line_sep_char
Definition: query_result.h:247
Query_result_export(sql_exchange *ex)
Definition: query_result.h:270
bool is_ambiguous_field_sep
Definition: query_result.h:254
bool is_ambiguous_field_term
Definition: query_result.h:260
int field_term_char
Definition: query_result.h:248
Definition: query_result.h:181
Query_result_interceptor()
Definition: query_result.h:183
bool send_result_set_metadata(THD *, const mem_root_deque< Item * > &, uint) override
Definition: query_result.h:185
uint field_count(const mem_root_deque< Item * > &) const override
Definition: query_result.h:184
Definition: query_result.h:191
void cleanup() override
Cleanup after this execution.
Definition: query_result.h:207
bool is_result_set_started
True if we have sent result set metadata to the client.
Definition: query_result.h:197
bool use_protocol_adapter() const override
An alternative implementation may provide an optimized protocol adapter for this object.
Definition: query_result.h:212
Query_result_send()
Definition: query_result.h:200
bool check_supports_cursor() const override
Check if this query result set supports cursors.
Definition: query_result.h:205
bool send_data(THD *thd, const mem_root_deque< Item * > &items) override
Definition: query_result.cc:97
void abort_result_set(THD *thd) override
Definition: query_result.cc:78
bool send_result_set_metadata(THD *thd, const mem_root_deque< Item * > &list, uint flags) override
Definition: query_result.cc:70
bool send_eof(THD *thd) override
Definition: query_result.cc:112
Base class for result from a subquery.
Definition: query_result.h:312
bool send_eof(THD *) override
Definition: query_result.h:320
Query_result_subquery(Item_subselect *item_arg)
Definition: query_result.h:317
bool send_data(THD *thd, const mem_root_deque< Item * > &items) override=0
Item_subselect * item
Definition: query_result.h:314
Definition: query_result.h:217
bool needs_file_privilege() const override
Definition: query_result.h:232
IO_CACHE cache
Definition: query_result.h:221
bool check_supports_cursor() const override
Check if this query result set supports cursors.
Definition: query_result.cc:150
sql_exchange * exchange
Definition: query_result.h:219
char path[FN_REFLEN]
Definition: query_result.h:223
Query_result_to_file(sql_exchange *ex)
Definition: query_result.h:226
File file
Definition: query_result.h:220
void cleanup() override
Cleanup after this execution.
Definition: query_result.cc:166
~Query_result_to_file() override
Definition: query_result.h:230
ha_rows row_count
Definition: query_result.h:222
bool send_eof(THD *thd) override
Definition: query_result.cc:155
Definition: query_result.h:58
virtual Server_side_cursor * cursor() const
Definition: query_result.h:169
Query_result()
Definition: query_result.h:74
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:90
virtual bool need_explain_interceptor() const
Definition: query_result.h:92
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:99
double estimated_cost
Cost to execute the subquery which produces this result.
Definition: query_result.h:72
ha_rows estimated_rowcount
Number of records estimated in this result.
Definition: query_result.h:67
virtual void cleanup()
Cleanup after this execution.
Definition: query_result.h:152
virtual bool use_protocol_wrapper() const
Definition: query_result.h:163
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:144
virtual bool check_supports_cursor() const
Check if this query result set supports cursors.
Definition: query_result.h:134
virtual uint field_count(const mem_root_deque< Item * > &fields) const
Definition: query_result.cc:66
virtual void abort_result_set(THD *)
Definition: query_result.h:138
virtual bool use_protocol_adapter() const
Definition: query_result.h:158
virtual bool create_table_for_query_block(THD *)
Create table, only needed to support CREATE TABLE ... SELECT.
Definition: query_result.h:117
Query_expression * unit
Definition: query_result.h:60
virtual ~Query_result()=default
virtual bool needs_file_privilege() const
Definition: query_result.h:77
virtual void set_limit(ha_rows)
Only overridden (and non-empty) for Query_result_union, q.v.
Definition: query_result.h:166
virtual bool start_execution(THD *)
Prepare for execution of the query expression or DML statement.
Definition: query_result.h:114
Common base class for n-ary set operations, including unary.
Definition: query_term.h:402
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
Used to hold information about file and file structure in exchange via non-DB file (....
Definition: sql_exchange.h:79
#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:83
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:2878
Definition: m_ctype.h:423
Definition: my_sys.h:346