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