MySQL  8.0.18
Source Code Documentation
query_result.h
Go to the documentation of this file.
1 /* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
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 <stddef.h>
27 #include <sys/types.h>
28 
29 #include "m_ctype.h"
30 #include "my_base.h"
31 #include "my_dbug.h"
32 #include "my_inttypes.h"
33 #include "my_io.h"
34 #include "my_sys.h"
35 #include "mysqld_error.h" // ER_*
36 #include "sql/sql_list.h"
37 
38 class Item;
39 class Item_subselect;
40 class PT_select_var;
41 class SELECT_LEX_UNIT;
42 class THD;
43 
44 /*
45  This is used to get result from a query
46 */
47 
48 class Query_result {
49  protected:
51 
52  public:
53  /**
54  Number of records estimated in this result.
55  Valid only for materialized derived tables/views.
56  */
58  /**
59  Cost to execute the subquery which produces this result.
60  Valid only for materialized derived tables/views.
61  */
63 
65  virtual ~Query_result() {}
66 
67  virtual bool needs_file_privilege() const { return false; }
68 
69  /**
70  Change wrapped Query_result.
71 
72  Replace the wrapped query result object with new_result and call
73  prepare() on new_result.
74 
75  This base class implementation doesn't wrap other Query_results.
76 
77  @retval false Success
78  @retval true Error
79  */
80  virtual bool change_query_result(THD *, Query_result *) { return false; }
81  /// @return true if an interceptor object is needed for EXPLAIN
82  virtual bool need_explain_interceptor() const { return false; }
83 
84  /**
85  Perform preparation specific to the query expression or DML statement.
86 
87  @returns false if success, true if error
88  */
89  virtual bool prepare(THD *, List<Item> &, SELECT_LEX_UNIT *u) {
90  unit = u;
91  return false;
92  }
93 
94  /**
95  Optimize the result processing of a query expression, applicable to
96  data change operation (not simple select queries).
97 
98  @returns false if success, true if error
99  */
100  virtual bool optimize() { return false; }
101 
102  /**
103  Prepare for execution of the query expression or DML statement.
104 
105  Generally, this will have an implementation only for outer-most
106  SELECT_LEX objects, such as data change statements (for preparation
107  of the target table(s)) or dump statements (for preparation of target file).
108 
109  @returns false if success, true if error
110  */
111  virtual bool start_execution(THD *) { return false; }
112 
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(List<Item> &fields) const { return fields.elements; }
119  virtual bool send_result_set_metadata(THD *thd, List<Item> &list,
120  uint flags) = 0;
121  virtual bool send_data(THD *thd, List<Item> &items) = 0;
122  virtual void send_error(THD *, uint errcode, const char *err) {
123  my_message(errcode, err, MYF(0));
124  }
125  virtual bool send_eof(THD *thd) = 0;
126  /**
127  Check if this query returns a result set and therefore is allowed in
128  cursors and set an error message if it is not the case.
129 
130  @retval false success
131  @retval true error, an error message is set
132  */
133  virtual bool check_simple_select() 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  DBUG_ASSERT(0);
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(THD *) { /* do nothing */
152  }
153 
154  void begin_dataset() {}
155 
156  /// @returns Pointer to count of rows retained by this result.
157  virtual const ha_rows *row_count() const /* purecov: inspected */
158  {
159  DBUG_ASSERT(false);
160  return nullptr;
161  } /* purecov: inspected */
162 
163  /**
164  Checks if this Query_result intercepts and transforms the result set.
165 
166  @return true if it is an interceptor, false otherwise
167  */
168  virtual bool is_interceptor() const { return false; }
169 };
170 
171 /*
172  Base class for Query_result descendands 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(List<Item> &) const override { return 0; }
182  return false;
183  }
184  bool is_interceptor() const override final { return true; }
185 };
186 
188  /**
189  True if we have sent result set metadata to the client.
190  In this case the client always expects us to end the result
191  set with an eof or error packet
192  */
194 
195  public:
198  uint flags) override;
199  bool send_data(THD *thd, List<Item> &items) override;
200  bool send_eof(THD *thd) override;
201  bool check_simple_select() const override { return false; }
202  void abort_result_set(THD *thd) override;
203  void cleanup(THD *) override { is_result_set_started = false; }
204 };
205 
206 class sql_exchange;
207 
209  protected:
215 
216  public:
219  path[0] = 0;
220  }
222 
223  bool needs_file_privilege() const override { return true; }
224 
225  void send_error(THD *thd, uint errcode, const char *err) override;
226  bool send_eof(THD *thd) override;
227  void cleanup(THD *thd) 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, List<Item> &list, SELECT_LEX_UNIT *u) override;
264  bool start_execution(THD *thd) override;
265  bool send_data(THD *thd, List<Item> &items) override;
266  void cleanup(THD *thd) override;
267 };
268 
270  public:
272  bool prepare(THD *thd, List<Item> &list, SELECT_LEX_UNIT *u) override;
273  bool start_execution(THD *thd) override;
274  bool send_data(THD *thd, List<Item> &items) override;
275 };
276 
279 
280  public:
283  var_list.empty();
284  }
285  bool prepare(THD *thd, List<Item> &list, SELECT_LEX_UNIT *u) override;
286  bool send_data(THD *thd, List<Item> &items) override;
287  bool send_eof(THD *thd) override;
288  bool check_simple_select() const override;
289  void cleanup(THD *) override { row_count = 0; }
290 };
291 
292 /**
293  Base class for result from a subquery.
294 */
295 
297  protected:
299 
300  public:
302  : Query_result_interceptor(), item(item_arg) {}
303  bool send_data(THD *thd, List<Item> &items) override = 0;
304  bool send_eof(THD *) override { return false; }
305 };
306 
307 #endif // QUERY_RESULT_INCLUDED
Query_result_send()
Definition: query_result.h:196
virtual const ha_rows * row_count() const
Definition: query_result.h:157
int field_term_char
Definition: query_result.h:240
IO_CACHE cache
Definition: query_result.h:212
List< PT_select_var > var_list
Definition: query_result.h:281
Query_result_interceptor()
Definition: query_result.h:179
int escape_char
Definition: query_result.h:239
virtual void send_error(THD *, uint errcode, const char *err)
Definition: query_result.h:122
virtual bool prepare(THD *, List< Item > &, SELECT_LEX_UNIT *u)
Perform preparation specific to the query expression or DML statement.
Definition: query_result.h:89
bool send_eof(THD *thd) override
Definition: query_result.cc:151
SELECT_LEX_UNIT * unit
Definition: query_result.h:50
Some integer typedefs for easier portability.
void my_error(int nr, myf MyFlags,...)
Fill in and print a previously registered error message.
Definition: my_error.cc:215
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:696
Query_result_dump(sql_exchange *ex)
Definition: query_result.h:271
bool send_data(THD *thd, List< Item > &items) override
Definition: query_result.cc:88
bool fixed_row_size
Definition: query_result.h:259
Query_dumpvar()
Definition: query_result.h:282
double estimated_cost
Cost to execute the subquery which produces this result.
Definition: query_result.h:62
void cleanup(THD *) override
Cleanup after this execution.
Definition: query_result.h:203
bool is_unsafe_field_sep
Definition: query_result.h:258
virtual bool is_interceptor() const
Checks if this Query_result intercepts and transforms the result set.
Definition: query_result.h:168
bool is_ambiguous_field_sep
Definition: query_result.h:246
ha_rows row_count
Definition: query_result.h:213
Definition: query_result.h:48
int field_sep_char
Definition: query_result.h:239
Query_result_subquery(Item_subselect *item_arg)
Definition: query_result.h:301
bool send_eof(THD *) override
Definition: query_result.h:304
~Query_result_to_file() override
Definition: query_result.h:221
class udf_list * list
bool start_execution(THD *thd) override
Prepare for execution of the query expression or DML statement.
Definition: query_result.cc:331
bool prepare(THD *thd, List< Item > &list, SELECT_LEX_UNIT *u) override
Perform preparation specific to the query expression or DML statement.
Definition: query_result.cc:241
int line_sep_char
Definition: query_result.h:239
bool needs_file_privilege() const override
Definition: query_result.h:223
Used to hold information about file and file structure in exchange via non-DB file (...
Definition: sql_exchange.h:76
Definition: item_subselect.h:73
bool send_eof(THD *thd) override
Definition: query_result.cc:734
virtual ~Query_result()
Definition: query_result.h:65
Definition: query_result.h:269
This file includes constants used by all storage engines.
virtual bool needs_file_privilege() const
Definition: query_result.h:67
bool send_data(THD *thd, List< Item > &items) override
Definition: query_result.cc:652
void empty()
Definition: sql_list.h:141
#define DBUG_ASSERT(A)
Definition: my_dbug.h:197
void cleanup(THD *) override
Cleanup after this execution.
Definition: query_result.h:289
char path[FN_REFLEN]
Definition: query_result.h:214
sql_exchange * exchange
Definition: query_result.h:210
size_t field_term_length
Definition: query_result.h:238
bool send_result_set_metadata(THD *, List< Item > &, uint) override
Definition: query_result.h:181
virtual bool send_result_set_metadata(THD *thd, List< Item > &list, uint flags)=0
const CHARSET_INFO * write_cs
Definition: query_result.h:260
bool start_execution(THD *thd) override
Prepare for execution of the query expression or DML statement.
Definition: query_result.cc:647
Item_subselect * item
Definition: query_result.h:298
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
Definition: my_sys.h:360
bool is_ambiguous_field_term
Definition: query_result.h:252
void send_error(THD *thd, uint errcode, const char *err) override
Definition: query_result.cc:140
ha_rows row_count
Definition: query_result.h:278
bool send_data(THD *thd, List< Item > &items) override
Definition: query_result.cc:701
Definition: item.h:668
unsigned int uint
Definition: uca-dump.cc:29
#define final(a, b, c)
Definition: hash.c:109
#define FN_REFLEN
Definition: my_io.h:82
Query_result_to_file(sql_exchange *ex)
Definition: query_result.h:217
void cleanup(THD *thd) override
Cleanup after this execution.
Definition: query_result.cc:633
#define MYF(v)
Definition: my_inttypes.h:114
void my_message(uint my_err, const char *str, myf MyFlags)
Print an error message.
Definition: my_error.cc:310
void cleanup(THD *thd) override
Cleanup after this execution.
Definition: query_result.cc:162
static Value err()
Create a Value object that represents an error condition.
Definition: json_binary.cc:908
Base class for result from a subquery.
Definition: query_result.h:296
This class represents a query expression (one query block or several query blocks combined with UNION...
Definition: sql_lex.h:554
void begin_dataset()
Definition: query_result.h:154
Definition: query_result.h:277
Definition: m_ctype.h:359
bool prepare(THD *thd, List< Item > &list, SELECT_LEX_UNIT *u) override
Perform preparation specific to the query expression or DML statement.
Definition: query_result.cc:685
virtual uint field_count(List< Item > &fields) const
Definition: query_result.h:118
Common header for many mysys elements.
Query_result_export(sql_exchange *ex)
Definition: query_result.h:262
bool send_data(THD *thd, List< Item > &items) override
Definition: query_result.cc:342
int File
Definition: my_io_bits.h:50
Definition: query_result.h:177
Definition: query_result.h:187
virtual bool optimize()
Optimize the result processing of a query expression, applicable to data change operation (not simple...
Definition: query_result.h:100
bool is_interceptor() const override final
Checks if this Query_result intercepts and transforms the result set.
Definition: query_result.h:184
ha_rows estimated_rowcount
Number of records estimated in this result.
Definition: query_result.h:57
Definition: query_result.h:208
Definition: parse_tree_nodes.h:1600
uint elements
Definition: sql_list.h:135
bool send_data(THD *thd, List< Item > &items) override=0
virtual bool need_explain_interceptor() const
Definition: query_result.h:82
Common #defines and includes for file and socket I/O.
#define NULL
Definition: types.h:55
#define L
Definition: ctype-tis620.cc:73
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:201
A better implementation of the UNIX ctype(3) library.
virtual bool start_execution(THD *)
Prepare for execution of the query expression or DML statement.
Definition: query_result.h:111
Query_result()
Definition: query_result.h:64
virtual bool send_eof(THD *thd)=0
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:133
static int flags[50]
Definition: hp_test1.cc:39
virtual void abort_result_set(THD *)
Definition: query_result.h:137
void abort_result_set(THD *thd) override
Definition: query_result.cc:69
virtual void cleanup(THD *)
Cleanup after this execution.
Definition: query_result.h:151
File file
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:193
virtual bool change_query_result(THD *, Query_result *)
Change wrapped Query_result.
Definition: query_result.h:80
uint field_count(List< Item > &) const override
Definition: query_result.h:180
#define false
Definition: config_static.h:43
Definition: query_result.h:237
virtual bool send_data(THD *thd, List< Item > &items)=0
my_off_t ha_rows
Definition: my_base.h:1130
bool send_eof(THD *thd) override
Definition: query_result.cc:102
Definition: items.h:34
bool send_result_set_metadata(THD *thd, List< Item > &list, uint flags) override
Definition: query_result.cc:61
bool prepare(THD *thd, List< Item > &list, SELECT_LEX_UNIT *u) override
Perform preparation specific to the query expression or DML statement.
Definition: query_result.cc:642
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:778