MySQL  8.0.27
Source Code Documentation
query_result.h
Go to the documentation of this file.
1 /* Copyright (c) 2015, 2021, 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 "m_ctype.h"
32 #include "mem_root_deque.h"
33 #include "my_base.h"
34 #include "my_compiler.h" // MY_ATTRIBUTE
35 
36 #include "my_inttypes.h"
37 #include "my_io.h"
38 #include "my_sys.h"
40 #include "mysqld_error.h" // ER_*
41 #include "sql/sql_list.h"
42 
43 class Item;
44 class Item_subselect;
45 class PT_select_var;
46 class Query_expression;
47 class THD;
48 struct CHARSET_INFO;
49 struct TABLE_LIST;
50 
51 /*
52  This is used to get result from a query
53 */
54 
55 class Query_result {
56  protected:
58 
59  public:
60  /**
61  Number of records estimated in this result.
62  Valid only for materialized derived tables/views.
63  */
65  /**
66  Cost to execute the subquery which produces this result.
67  Valid only for materialized derived tables/views.
68  */
70 
72  virtual ~Query_result() = default;
73 
74  virtual bool needs_file_privilege() const { return false; }
75 
76  /**
77  Change wrapped Query_result.
78 
79  Replace the wrapped query result object with new_result and call
80  prepare() on new_result.
81 
82  This base class implementation doesn't wrap other Query_results.
83 
84  @retval false Success
85  @retval true Error
86  */
87  virtual bool change_query_result(THD *, Query_result *) { return false; }
88  /// @return true if an interceptor object is needed for EXPLAIN
89  virtual bool need_explain_interceptor() const { return false; }
90 
91  /**
92  Perform preparation specific to the query expression or DML statement.
93 
94  @returns false if success, true if error
95  */
96  virtual bool prepare(THD *, const mem_root_deque<Item *> &,
97  Query_expression *u) {
98  unit = u;
99  return false;
100  }
101 
102  /**
103  Optimize the result processing of a query expression, applicable to
104  data change operation (not simple select queries).
105 
106  @returns false if success, true if error
107  */
108  virtual bool optimize() { return false; }
109 
110  /**
111  Prepare for execution of the query expression or DML statement.
112 
113  Generally, this will have an implementation only for outer-most
114  Query_block objects, such as data change statements (for preparation
115  of the target table(s)) or dump statements (for preparation of target file).
116 
117  @returns false if success, true if error
118  */
119  virtual bool start_execution(THD *) { return false; }
120 
121  /// Create table, only needed to support CREATE TABLE ... SELECT
122  virtual bool create_table_for_query_block(THD *) { return false; }
123  /*
124  Because of peculiarities of prepared statements protocol
125  we need to know number of columns in the result set (if
126  there is a result set) apart from sending columns metadata.
127  */
128  virtual uint field_count(const mem_root_deque<Item *> &fields) const;
129  virtual bool send_result_set_metadata(THD *thd,
130  const mem_root_deque<Item *> &list,
131  uint flags) = 0;
132  virtual bool send_data(THD *thd, const mem_root_deque<Item *> &items) = 0;
133  virtual void send_error(THD *, uint errcode, const char *err) {
134  my_message(errcode, err, MYF(0));
135  }
136  virtual bool send_eof(THD *thd) = 0;
137  /**
138  Check if this query returns a result set and therefore is allowed in
139  cursors and set an error message if it is not the case.
140 
141  @retval false success
142  @retval true error, an error message is set
143  */
144  virtual bool check_simple_query_block() const {
145  my_error(ER_SP_BAD_CURSOR_QUERY, MYF(0));
146  return true;
147  }
148  virtual void abort_result_set(THD *) {}
149  /**
150  Cleanup after one execution of the unit, to be ready for a next execution
151  inside the same statement.
152  @returns true if error
153  */
154  virtual bool reset() {
155  assert(0);
156  return false;
157  }
158  /**
159  Cleanup after this execution. Completes the execution and resets object
160  before next execution of a prepared statement/stored procedure.
161  */
162  virtual void cleanup(THD *) { /* do nothing */
163  }
164 
165  void begin_dataset() {}
166 
167  /// @returns Pointer to count of rows retained by this result.
168  virtual const ha_rows *row_count() const /* purecov: inspected */
169  {
170  assert(false);
171  return nullptr;
172  } /* purecov: inspected */
173 
174  /**
175  Checks if this Query_result intercepts and transforms the result set.
176 
177  @return true if it is an interceptor, false otherwise
178  */
179  virtual bool is_interceptor() const { return false; }
180 
181  /**
182  If this Query_result performs modifications to tables: tells if it modifies
183  the given table's row as it's read (a.k.a. "on the fly"), or rather buffers
184  it to a temporary structure and modifies it in a post-all-reads phase.
185  @param t TABLE to answer for
186  @return true if "on the fly"
187  */
188  virtual bool immediate_update(TABLE_LIST *t [[maybe_unused]]) const {
189  assert(false);
190  return false;
191  }
192 };
193 
194 /*
195  Base class for Query_result descendands which intercept and
196  transform result set rows. As the rows are not sent to the client,
197  sending of result set metadata should be suppressed as well.
198 */
199 
201  public:
203  uint field_count(const mem_root_deque<Item *> &) const override { return 0; }
205  uint) override {
206  return false;
207  }
208  bool is_interceptor() const final { return true; }
209 };
210 
212  /**
213  True if we have sent result set metadata to the client.
214  In this case the client always expects us to end the result
215  set with an eof or error packet
216  */
218 
219  public:
221  bool send_result_set_metadata(THD *thd, const mem_root_deque<Item *> &list,
222  uint flags) override;
223  bool send_data(THD *thd, const mem_root_deque<Item *> &items) override;
224  bool send_eof(THD *thd) override;
225  bool check_simple_query_block() const override { return false; }
226  void abort_result_set(THD *thd) override;
227  void cleanup(THD *) override { is_result_set_started = false; }
228 };
229 
230 class sql_exchange;
231 
233  protected:
239 
240  public:
243  path[0] = 0;
244  }
245  ~Query_result_to_file() override { assert(file < 0); }
246 
247  bool needs_file_privilege() const override { return true; }
248 
249  void send_error(THD *thd, uint errcode, const char *err) override;
250  bool send_eof(THD *thd) override;
251  void cleanup(THD *thd) override;
252 };
253 
254 #define ESCAPE_CHARS "ntrb0ZN" // keep synchronous with READ_INFO::unescape
255 
256 /*
257  List of all possible characters of a numeric value text representation.
258 */
259 #define NUMERIC_CHARS ".0123456789e+-"
260 
264  int field_term_char; // first char of FIELDS TERMINATED BY or MAX_INT
265  /*
266  The is_ambiguous_field_sep field is true if a value of the field_sep_char
267  field is one of the 'n', 't', 'r' etc characters
268  (see the READ_INFO::unescape method and the ESCAPE_CHARS constant value).
269  */
271  /*
272  The is_ambiguous_field_term is true if field_sep_char contains the first
273  char of the FIELDS TERMINATED BY (ENCLOSED BY is empty), and items can
274  contain this character.
275  */
277  /*
278  The is_unsafe_field_sep field is true if a value of the field_sep_char
279  field is one of the '0'..'9', '+', '-', '.' and 'e' characters
280  (see the NUMERIC_CHARS constant value).
281  */
284  const CHARSET_INFO *write_cs; // output charset
285  public:
287  bool prepare(THD *thd, const mem_root_deque<Item *> &list,
288  Query_expression *u) override;
289  bool start_execution(THD *thd) override;
290  bool send_data(THD *thd, const mem_root_deque<Item *> &items) override;
291  void cleanup(THD *thd) override;
292 };
293 
295  public:
297  bool prepare(THD *thd, const mem_root_deque<Item *> &list,
298  Query_expression *u) override;
299  bool start_execution(THD *thd) override;
300  bool send_data(THD *thd, const mem_root_deque<Item *> &items) override;
301 };
302 
305 
306  public:
309  var_list.clear();
310  }
311  bool prepare(THD *thd, const mem_root_deque<Item *> &list,
312  Query_expression *u) override;
313  bool send_data(THD *thd, const mem_root_deque<Item *> &items) override;
314  bool send_eof(THD *thd) override;
315  bool check_simple_query_block() const override;
316  void cleanup(THD *) override { row_count = 0; }
317 };
318 
319 /**
320  Base class for result from a subquery.
321 */
322 
324  protected:
326 
327  public:
329  : Query_result_interceptor(), item(item_arg) {}
330  bool send_data(THD *thd, const mem_root_deque<Item *> &items) override = 0;
331  bool send_eof(THD *) override { return false; }
332 };
333 
334 #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: parse_tree_nodes.h:1225
Definition: query_result.h:303
List< PT_select_var > var_list
Definition: query_result.h:307
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:695
bool send_eof(THD *thd) override
Definition: query_result.cc:743
ha_rows row_count
Definition: query_result.h:304
void cleanup(THD *) override
Cleanup after this execution.
Definition: query_result.h:316
Query_dumpvar()
Definition: query_result.h:308
bool send_data(THD *thd, const mem_root_deque< Item * > &items) override
Definition: query_result.cc:712
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:707
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:294
Query_result_dump(sql_exchange *ex)
Definition: query_result.h:296
bool send_data(THD *thd, const mem_root_deque< Item * > &items) override
Definition: query_result.cc:664
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:653
bool start_execution(THD *thd) override
Prepare for execution of the query expression or DML statement.
Definition: query_result.cc:659
Definition: query_result.h:261
void cleanup(THD *thd) override
Cleanup after this execution.
Definition: query_result.cc:644
bool start_execution(THD *thd) override
Prepare for execution of the query expression or DML statement.
Definition: query_result.cc:343
const CHARSET_INFO * write_cs
Definition: query_result.h:284
bool is_unsafe_field_sep
Definition: query_result.h:282
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:257
int field_sep_char
Definition: query_result.h:263
bool fixed_row_size
Definition: query_result.h:283
int escape_char
Definition: query_result.h:263
bool send_data(THD *thd, const mem_root_deque< Item * > &items) override
Definition: query_result.cc:354
size_t field_term_length
Definition: query_result.h:262
int line_sep_char
Definition: query_result.h:263
Query_result_export(sql_exchange *ex)
Definition: query_result.h:286
bool is_ambiguous_field_sep
Definition: query_result.h:270
bool is_ambiguous_field_term
Definition: query_result.h:276
int field_term_char
Definition: query_result.h:264
Definition: query_result.h:200
Query_result_interceptor()
Definition: query_result.h:202
bool is_interceptor() const final
Checks if this Query_result intercepts and transforms the result set.
Definition: query_result.h:208
bool send_result_set_metadata(THD *, const mem_root_deque< Item * > &, uint) override
Definition: query_result.h:204
uint field_count(const mem_root_deque< Item * > &) const override
Definition: query_result.h:203
Definition: query_result.h:211
bool is_result_set_started
True if we have sent result set metadata to the client.
Definition: query_result.h:217
Query_result_send()
Definition: query_result.h:220
bool send_data(THD *thd, const mem_root_deque< Item * > &items) override
Definition: query_result.cc:93
void abort_result_set(THD *thd) override
Definition: query_result.cc:74
void cleanup(THD *) override
Cleanup after this execution.
Definition: query_result.h:227
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:225
bool send_result_set_metadata(THD *thd, const mem_root_deque< Item * > &list, uint flags) override
Definition: query_result.cc:66
bool send_eof(THD *thd) override
Definition: query_result.cc:108
Base class for result from a subquery.
Definition: query_result.h:323
bool send_eof(THD *) override
Definition: query_result.h:331
Query_result_subquery(Item_subselect *item_arg)
Definition: query_result.h:328
bool send_data(THD *thd, const mem_root_deque< Item * > &items) override=0
Item_subselect * item
Definition: query_result.h:325
Definition: query_result.h:232
bool needs_file_privilege() const override
Definition: query_result.h:247
IO_CACHE cache
Definition: query_result.h:236
sql_exchange * exchange
Definition: query_result.h:234
char path[FN_REFLEN]
Definition: query_result.h:238
void send_error(THD *thd, uint errcode, const char *err) override
Definition: query_result.cc:146
Query_result_to_file(sql_exchange *ex)
Definition: query_result.h:241
File file
Definition: query_result.h:235
~Query_result_to_file() override
Definition: query_result.h:245
ha_rows row_count
Definition: query_result.h:237
void cleanup(THD *thd) override
Cleanup after this execution.
Definition: query_result.cc:168
bool send_eof(THD *thd) override
Definition: query_result.cc:157
Definition: query_result.h:55
Query_result()
Definition: query_result.h:71
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:179
virtual bool change_query_result(THD *, Query_result *)
Change wrapped Query_result.
Definition: query_result.h:87
virtual bool need_explain_interceptor() const
Definition: query_result.h:89
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:96
double estimated_cost
Cost to execute the subquery which produces this result.
Definition: query_result.h:69
ha_rows estimated_rowcount
Number of records estimated in this result.
Definition: query_result.h:64
virtual bool optimize()
Optimize the result processing of a query expression, applicable to data change operation (not simple...
Definition: query_result.h:108
virtual void send_error(THD *, uint errcode, const char *err)
Definition: query_result.h:133
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:154
virtual const ha_rows * row_count() const
Definition: query_result.h:168
virtual bool immediate_update(TABLE_LIST *t[[maybe_unused]]) const
If this Query_result performs modifications to tables: tells if it modifies the given table's row as ...
Definition: query_result.h:188
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:144
virtual uint field_count(const mem_root_deque< Item * > &fields) const
Definition: query_result.cc:62
void begin_dataset()
Definition: query_result.h:165
virtual void abort_result_set(THD *)
Definition: query_result.h:148
virtual void cleanup(THD *)
Cleanup after this execution.
Definition: query_result.h:162
virtual bool create_table_for_query_block(THD *)
Create table, only needed to support CREATE TABLE ... SELECT.
Definition: query_result.h:122
Query_expression * unit
Definition: query_result.h:57
virtual ~Query_result()=default
virtual bool needs_file_privilege() const
Definition: query_result.h:74
virtual bool start_execution(THD *)
Prepare for execution of the query expression or DML statement.
Definition: query_result.h:119
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:821
void clear()
Definition: sql_list.h:141
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
Dialog Client Authentication nullptr
Definition: dialog.cc:352
void my_message(uint my_err, const char *str, myf MyFlags)
Print an error message.
Definition: my_error.cc:310
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:1138
Header for compiler-dependent features.
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
static Value err()
Create a Value object that represents an error condition.
Definition: json_binary.cc:908
Definition: m_ctype.h:354
Definition: my_sys.h:340
Definition: table.h:2694
unsigned int uint
Definition: uca-dump.cc:29