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