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