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