MySQL 8.0.30
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;
42class THD;
43struct CHARSET_INFO;
44template <class Element_type>
45class mem_root_deque;
46
47/*
48 This is used to get result from a query
49*/
50
52 protected:
54
55 public:
56 /**
57 Number of records estimated in this result.
58 Valid only for materialized derived tables/views.
59 */
61 /**
62 Cost to execute the subquery which produces this result.
63 Valid only for materialized derived tables/views.
64 */
66
68 virtual ~Query_result() = default;
69
70 virtual bool needs_file_privilege() const { return false; }
71
72 /**
73 Change wrapped Query_result.
74
75 Replace the wrapped query result object with new_result and call
76 prepare() on new_result.
77
78 This base class implementation doesn't wrap other Query_results.
79
80 @retval false Success
81 @retval true Error
82 */
83 virtual bool change_query_result(THD *, Query_result *) { return false; }
84 /// @return true if an interceptor object is needed for EXPLAIN
85 virtual bool need_explain_interceptor() const { return false; }
86
87 /**
88 Perform preparation specific to the query expression or DML statement.
89
90 @returns false if success, true if error
91 */
92 virtual bool prepare(THD *, const mem_root_deque<Item *> &,
94 unit = u;
95 return false;
96 }
97
98 /**
99 Prepare for execution of the query expression or DML statement.
100
101 Generally, this will have an implementation only for outer-most
102 Query_block objects, such as data change statements (for preparation
103 of the target table(s)) or dump statements (for preparation of target file).
104
105 @returns false if success, true if error
106 */
107 virtual bool start_execution(THD *) { return false; }
108
109 /// Create table, only needed to support CREATE TABLE ... SELECT
110 virtual bool create_table_for_query_block(THD *) { return false; }
111 /*
112 Because of peculiarities of prepared statements protocol
113 we need to know number of columns in the result set (if
114 there is a result set) apart from sending columns metadata.
115 */
116 virtual uint field_count(const mem_root_deque<Item *> &fields) const;
117 virtual bool send_result_set_metadata(THD *thd,
119 uint flags) = 0;
120 virtual bool send_data(THD *thd, const mem_root_deque<Item *> &items) = 0;
121 virtual bool send_eof(THD *thd) = 0;
122 /**
123 Check if this query returns a result set and therefore is allowed in
124 cursors and set an error message if it is not the case.
125
126 @retval false success
127 @retval true error, an error message is set
128 */
129 virtual bool check_simple_query_block() 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(0);
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(THD *) { /* 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
158/*
159 Base class for Query_result descendants which intercept and
160 transform result set rows. As the rows are not sent to the client,
161 sending of result set metadata should be suppressed as well.
162*/
163
165 public:
167 uint field_count(const mem_root_deque<Item *> &) const override { return 0; }
169 uint) override {
170 return false;
171 }
172 bool is_interceptor() const final { return true; }
173};
174
176 /**
177 True if we have sent result set metadata to the client.
178 In this case the client always expects us to end the result
179 set with an eof or error packet
180 */
182
183 public:
186 uint flags) override;
187 bool send_data(THD *thd, const mem_root_deque<Item *> &items) override;
188 bool send_eof(THD *thd) override;
189 bool check_simple_query_block() const override { return false; }
190 void abort_result_set(THD *thd) override;
191 void cleanup(THD *) override { is_result_set_started = false; }
192};
193
194class sql_exchange;
195
197 protected:
203
204 public:
207 path[0] = 0;
208 }
209 ~Query_result_to_file() override { assert(file < 0); }
210
211 bool needs_file_privilege() const override { return true; }
212
213 bool send_eof(THD *thd) override;
214 void cleanup(THD *thd) override;
215};
216
217#define ESCAPE_CHARS "ntrb0ZN" // keep synchronous with READ_INFO::unescape
218
219/*
220 List of all possible characters of a numeric value text representation.
221*/
222#define NUMERIC_CHARS ".0123456789e+-"
223
227 int field_term_char; // first char of FIELDS TERMINATED BY or MAX_INT
228 /*
229 The is_ambiguous_field_sep field is true if a value of the field_sep_char
230 field is one of the 'n', 't', 'r' etc characters
231 (see the READ_INFO::unescape method and the ESCAPE_CHARS constant value).
232 */
234 /*
235 The is_ambiguous_field_term is true if field_sep_char contains the first
236 char of the FIELDS TERMINATED BY (ENCLOSED BY is empty), and items can
237 contain this character.
238 */
240 /*
241 The is_unsafe_field_sep field is true if a value of the field_sep_char
242 field is one of the '0'..'9', '+', '-', '.' and 'e' characters
243 (see the NUMERIC_CHARS constant value).
244 */
247 const CHARSET_INFO *write_cs; // output charset
248 public:
250 bool prepare(THD *thd, const mem_root_deque<Item *> &list,
251 Query_expression *u) override;
252 bool start_execution(THD *thd) override;
253 bool send_data(THD *thd, const mem_root_deque<Item *> &items) override;
254 void cleanup(THD *thd) override;
255};
256
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};
265
268
269 public:
272 var_list.clear();
273 }
274 bool prepare(THD *thd, const mem_root_deque<Item *> &list,
275 Query_expression *u) override;
276 bool send_data(THD *thd, const mem_root_deque<Item *> &items) override;
277 bool send_eof(THD *thd) override;
278 bool check_simple_query_block() const override;
279 void cleanup(THD *) override { row_count = 0; }
280};
281
282/**
283 Base class for result from a subquery.
284*/
285
287 protected:
289
290 public:
292 : Query_result_interceptor(), item(item_arg) {}
293 bool send_data(THD *thd, const mem_root_deque<Item *> &items) override = 0;
294 bool send_eof(THD *) override { return false; }
295};
296
297#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:802
Definition: sql_list.h:433
Definition: parse_tree_nodes.h:1194
Definition: query_result.h:266
List< PT_select_var > var_list
Definition: query_result.h:270
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:685
bool send_eof(THD *thd) override
Definition: query_result.cc:733
ha_rows row_count
Definition: query_result.h:267
void cleanup(THD *) override
Cleanup after this execution.
Definition: query_result.h:279
Query_dumpvar()
Definition: query_result.h:271
bool send_data(THD *thd, const mem_root_deque< Item * > &items) override
Definition: query_result.cc:702
bool check_simple_query_block() const override
Check if this query returns a result set and therefore is allowed in cursors and set an error message...
Definition: query_result.cc:697
This class represents a query expression (one query block or several query blocks combined with UNION...
Definition: sql_lex.h:629
Definition: query_result.h:257
Query_result_dump(sql_exchange *ex)
Definition: query_result.h:259
bool send_data(THD *thd, const mem_root_deque< Item * > &items) override
Definition: query_result.cc:654
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:643
bool start_execution(THD *thd) override
Prepare for execution of the query expression or DML statement.
Definition: query_result.cc:649
Definition: query_result.h:224
void cleanup(THD *thd) override
Cleanup after this execution.
Definition: query_result.cc:634
bool start_execution(THD *thd) override
Prepare for execution of the query expression or DML statement.
Definition: query_result.cc:333
const CHARSET_INFO * write_cs
Definition: query_result.h:247
bool is_unsafe_field_sep
Definition: query_result.h:245
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:247
int field_sep_char
Definition: query_result.h:226
bool fixed_row_size
Definition: query_result.h:246
int escape_char
Definition: query_result.h:226
bool send_data(THD *thd, const mem_root_deque< Item * > &items) override
Definition: query_result.cc:344
size_t field_term_length
Definition: query_result.h:225
int line_sep_char
Definition: query_result.h:226
Query_result_export(sql_exchange *ex)
Definition: query_result.h:249
bool is_ambiguous_field_sep
Definition: query_result.h:233
bool is_ambiguous_field_term
Definition: query_result.h:239
int field_term_char
Definition: query_result.h:227
Definition: query_result.h:164
Query_result_interceptor()
Definition: query_result.h:166
bool is_interceptor() const final
Checks if this Query_result intercepts and transforms the result set.
Definition: query_result.h:172
bool send_result_set_metadata(THD *, const mem_root_deque< Item * > &, uint) override
Definition: query_result.h:168
uint field_count(const mem_root_deque< Item * > &) const override
Definition: query_result.h:167
Definition: query_result.h:175
bool is_result_set_started
True if we have sent result set metadata to the client.
Definition: query_result.h:181
Query_result_send()
Definition: query_result.h:184
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
void cleanup(THD *) override
Cleanup after this execution.
Definition: query_result.h:191
bool check_simple_query_block() const override
Check if this query returns a result set and therefore is allowed in cursors and set an error message...
Definition: query_result.h:189
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:286
bool send_eof(THD *) override
Definition: query_result.h:294
Query_result_subquery(Item_subselect *item_arg)
Definition: query_result.h:291
bool send_data(THD *thd, const mem_root_deque< Item * > &items) override=0
Item_subselect * item
Definition: query_result.h:288
Definition: query_result.h:196
bool needs_file_privilege() const override
Definition: query_result.h:211
IO_CACHE cache
Definition: query_result.h:200
sql_exchange * exchange
Definition: query_result.h:198
char path[FN_REFLEN]
Definition: query_result.h:202
Query_result_to_file(sql_exchange *ex)
Definition: query_result.h:205
File file
Definition: query_result.h:199
~Query_result_to_file() override
Definition: query_result.h:209
ha_rows row_count
Definition: query_result.h:201
void cleanup(THD *thd) override
Cleanup after this execution.
Definition: query_result.cc:158
bool send_eof(THD *thd) override
Definition: query_result.cc:147
Definition: query_result.h:51
Query_result()
Definition: query_result.h:67
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:83
virtual bool need_explain_interceptor() const
Definition: query_result.h:85
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:92
double estimated_cost
Cost to execute the subquery which produces this result.
Definition: query_result.h:65
ha_rows estimated_rowcount
Number of records estimated in this result.
Definition: query_result.h:60
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_simple_query_block() const
Check if this query returns a result set and therefore is allowed in cursors and set an error message...
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 void cleanup(THD *)
Cleanup after this execution.
Definition: query_result.h:147
virtual bool create_table_for_query_block(THD *)
Create table, only needed to support CREATE TABLE ... SELECT.
Definition: query_result.h:110
Query_expression * unit
Definition: query_result.h:53
virtual ~Query_result()=default
virtual bool needs_file_privilege() const
Definition: query_result.h:70
virtual bool start_execution(THD *)
Prepare for execution of the query expression or DML statement.
Definition: query_result.h:107
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:922
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:76
#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:2859
Definition: m_ctype.h:354
Definition: my_sys.h:340
unsigned int uint
Definition: uca-dump.cc:29