MySQL  8.0.18
Source Code Documentation
sql_prepare.h
Go to the documentation of this file.
1 #ifndef SQL_PREPARE_H
2 #define SQL_PREPARE_H
3 /* Copyright (c) 2009, 2019, Oracle and/or its affiliates. All rights reserved.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License, version 2.0,
7  as published by the Free Software Foundation.
8 
9  This program is also distributed with certain software (including
10  but not limited to OpenSSL) that is licensed under separate terms,
11  as designated in a particular file or component or in included license
12  documentation. The authors of MySQL hereby grant you an additional
13  permission to link the program and your derivative works with the
14  separately licensed software that they have included with MySQL.
15 
16  This program is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  GNU General Public License, version 2.0, for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with this program; if not, write to the Free Software
23  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
24 
25 #include <stddef.h>
26 #include <sys/types.h>
27 #include <new>
28 
29 #include "lex_string.h"
30 #include "my_alloc.h"
31 #include "my_command.h"
32 #include "my_dbug.h"
33 #include "my_inttypes.h"
34 #include "my_psi_config.h"
36 #include "mysql_com.h"
37 #include "sql/sql_class.h" // Query_arena
38 #include "sql/sql_error.h"
39 #include "sql/sql_list.h"
40 
41 class Item;
42 class Item_param;
43 class Prepared_statement;
44 class Query_result_send;
45 class String;
46 struct LEX;
47 struct PS_PARAM;
48 struct TABLE_LIST;
49 union COM_DATA;
50 
51 /**
52  An interface that is used to take an action when
53  the locking module notices that a table version has changed
54  since the last execution. "Table" here may refer to any kind of
55  table -- a base table, a temporary table, a view or an
56  information schema table.
57 
58  When we open and lock tables for execution of a prepared
59  statement, we must verify that they did not change
60  since statement prepare. If some table did change, the statement
61  parse tree *may* be no longer valid, e.g. in case it contains
62  optimizations that depend on table metadata.
63 
64  This class provides an interface (a method) that is
65  invoked when such a situation takes place.
66  The implementation of the method simply reports an error, but
67  the exact details depend on the nature of the SQL statement.
68 
69  At most 1 instance of this class is active at a time, in which
70  case THD::m_reprepare_observer is not NULL.
71 
72  @sa check_and_update_table_version() for details of the
73  version tracking algorithm
74 
75  @sa Open_tables_state::m_reprepare_observer for the life cycle
76  of metadata observers.
77 */
78 
80  public:
81  /**
82  Check if a change of metadata is OK. In future
83  the signature of this method may be extended to accept the old
84  and the new versions, but since currently the check is very
85  simple, we only need the THD to report an error.
86  */
87  bool report_error(THD *thd);
88  bool is_invalidated() const { return m_invalidated; }
90 
91  private:
93 };
94 
95 bool mysql_stmt_precheck(THD *thd, const COM_DATA *com_data,
97  Prepared_statement **stmt);
98 void mysqld_stmt_prepare(THD *thd, const char *query, uint length,
99  Prepared_statement *stmt);
100 void mysqld_stmt_execute(THD *thd, Prepared_statement *stmt, bool has_new_types,
101  ulong execute_flags, PS_PARAM *parameters);
102 void mysqld_stmt_close(THD *thd, Prepared_statement *stmt);
103 void mysql_sql_stmt_prepare(THD *thd);
104 void mysql_sql_stmt_execute(THD *thd);
105 void mysql_sql_stmt_close(THD *thd);
106 void mysqld_stmt_fetch(THD *thd, Prepared_statement *stmt, ulong num_rows);
107 void mysqld_stmt_reset(THD *thd, Prepared_statement *stmt);
109  uint param_number, uchar *longdata, ulong length);
110 bool reinit_stmt_before_use(THD *thd, LEX *lex);
111 bool select_like_stmt_cmd_test(THD *thd, class Sql_cmd_dml *cmd,
112  ulong setup_tables_done_option);
113 bool mysql_test_show(Prepared_statement *stmt, TABLE_LIST *tables);
114 
115 /**
116  Execute a fragment of server code in an isolated context, so that
117  it doesn't leave any effect on THD. THD must have no open tables.
118  The code must not leave any open tables around.
119  The result of execution (if any) is stored in Ed_result.
120 */
121 
123  public:
124  virtual bool execute_server_code(THD *thd) = 0;
125  virtual ~Server_runnable();
126 };
127 
128 /**
129  Execute direct interface.
130 
131  @todo Implement support for prelocked mode.
132 */
133 
134 class Ed_row;
135 
136 /**
137  Ed_result_set -- a container with result set rows.
138  @todo Implement support for result set metadata and
139  automatic type conversion.
140 */
141 
143  public:
144  operator List<Ed_row> &() { return *m_rows; }
145  unsigned int size() const { return m_rows->elements; }
146  Ed_row *get_fields() { return m_fields; }
147 
148  Ed_result_set(List<Ed_row> *rows_arg, Ed_row *fields, size_t column_count,
149  MEM_ROOT *mem_root_arg);
150 
151  /** We don't call member destructors, they all are POD types. */
153 
154  size_t get_field_count() const { return m_column_count; }
155 
156  static void operator delete(void *, size_t) noexcept {
157  // Does nothing because m_mem_root is deallocated in the destructor
158  }
159 
160  static void operator delete(
161  void *, MEM_ROOT *, const std::nothrow_t &)noexcept { /* never called */
162  }
163 
164  private:
165  Ed_result_set(const Ed_result_set &); /* not implemented */
166  Ed_result_set &operator=(Ed_result_set &); /* not implemented */
167  private:
173  friend class Ed_connection;
174 };
175 
177  public:
178  /**
179  Construct a new "execute direct" connection.
180 
181  The connection can be used to execute SQL statements.
182  If the connection failed to initialize, the error
183  will be returned on the attempt to execute a statement.
184 
185  @pre thd must have no open tables
186  while the connection is used. However,
187  Ed_connection works okay in LOCK TABLES mode.
188  Other properties of THD, such as the current warning
189  information, errors, etc. do not matter and are
190  preserved by Ed_connection. One thread may have many
191  Ed_connections created for it.
192  */
193  Ed_connection(THD *thd);
194 
195  /**
196  Execute one SQL statement.
197 
198  Until this method is executed, no other methods of
199  Ed_connection can be used. Life cycle of Ed_connection is:
200 
201  Initialized -> a statement has been executed ->
202  look at result, move to next result ->
203  look at result, move to next result ->
204  ...
205  moved beyond the last result == Initialized.
206 
207  This method can be called repeatedly. Once it's invoked,
208  results of the previous execution are lost.
209 
210  A result of execute_direct() can be either:
211 
212  - success, no result set rows. In this case get_field_count()
213  returns 0. This happens after execution of INSERT, UPDATE,
214  DELETE, DROP and similar statements. Some other methods, such
215  as get_affected_rows() can be used to retrieve additional
216  result information.
217 
218  - success, there are some result set rows (maybe 0). E.g.
219  happens after SELECT. In this case get_field_count() returns
220  the number of columns in a result set and store_result()
221  can be used to retrieve a result set..
222 
223  - an error, methods to retrieve error information can
224  be used.
225 
226  @return execution status
227  @retval false success, use get_field_count()
228  to determine what to do next.
229  @retval true error, use get_last_error()
230  to see the error number.
231  */
232  bool execute_direct(LEX_STRING sql_text);
233 
234  /**
235  Same as the previous, but takes an instance of Server_runnable
236  instead of SQL statement text.
237 
238  @return execution status
239 
240  @retval false success, use get_field_count()
241  if your code fragment is supposed to
242  return a result set
243  @retval true failure
244  */
245  bool execute_direct(Server_runnable *server_runnable);
246 
247  /**
248  The following three members are only valid if execute_direct()
249  or move_to_next_result() returned an error.
250  They never fail, but if they are called when there is no
251  result, or no error, the result is not defined.
252  */
253  const char *get_last_error() const {
255  }
256 
257  unsigned int get_last_errno() const {
259  }
260 
262 
264 
265  private:
267  /**
268  Execute direct interface does not support multi-statements, only
269  multi-results. So we never have a situation when we have
270  a mix of result sets and OK or error packets. We either
271  have a single result set, a single error, or a single OK,
272  or we have a series of result sets, followed by an OK or error.
273  */
277  friend class Protocol_local;
278 
279  private:
280  void free_old_result();
281  void add_result_set(Ed_result_set *ed_result_set);
282 
283  private:
284  Ed_connection(const Ed_connection &); /* not implemented */
285  Ed_connection &operator=(Ed_connection &); /* not implemented */
286 };
287 
288 /** One result set column. */
289 
290 struct Ed_column final : public LEX_STRING {
291  /** Implementation note: destructor for this class is never called. */
292 };
293 
294 /** One result set record. */
295 
296 class Ed_row final {
297  public:
298  const Ed_column &operator[](const unsigned int column_index) const {
299  return *get_column(column_index);
300  }
301  const Ed_column *get_column(const unsigned int column_index) const {
302  DBUG_ASSERT(column_index < size());
303  return m_column_array + column_index;
304  }
305  size_t size() const { return m_column_count; }
306 
307  Ed_row(Ed_column *column_array_arg, size_t column_count_arg)
308  : m_column_array(column_array_arg), m_column_count(column_count_arg) {}
309 
310  private:
312  size_t m_column_count; /* TODO: change to point to metadata */
313 };
314 
315 class Server_side_cursor;
316 
317 /**
318  Prepared_statement: a statement that can contain placeholders.
319 */
320 
323 
324  public:
332 
333  /*
334  Uniquely identifies each statement object in thread scope; change during
335  statement lifetime.
336  */
337  const ulong id;
338 
339  LEX *lex; // parse tree descriptor
340 
341  /**
342  The query associated with this statement.
343  */
345 
346  /* Performance Schema interface for a prepared statement. */
348 
349  private:
351 
353  bool with_log;
354  LEX_CSTRING m_name; /* name for named prepared statements */
355  /**
356  Name of the current (default) database.
357 
358  If there is the current (default) database, "db" contains its name. If
359  there is no current (default) database, "db" is NULL and "db_length" is
360  0. In other words, "db", "db_length" must either be NULL, or contain a
361  valid database name.
362 
363  @note this attribute is set and alloced by the slave SQL thread (for
364  the THD of that thread); that thread is (and must remain, for now) the
365  only responsible for freeing this member.
366  */
368 
369  /**
370  The memory root to allocate parsed tree elements (instances of Item,
371  SELECT_LEX and other classes).
372  */
374 
375  public:
376  Prepared_statement(THD *thd_arg);
377  virtual ~Prepared_statement();
378  bool set_name(const LEX_CSTRING &name);
379  const LEX_CSTRING &name() const { return m_name; }
380  void close_cursor();
381  bool is_in_use() const { return flags & (uint)IS_IN_USE; }
382  bool is_sql_prepare() const { return flags & (uint)IS_SQL_PREPARE; }
384  bool prepare(const char *packet, size_t packet_length);
385  bool execute_loop(String *expanded_query, bool open_cursor);
386  bool execute_server_runnable(Server_runnable *server_runnable);
387 #ifdef HAVE_PSI_PS_INTERFACE
389 #endif
390  /* Destroy this statement */
391  void deallocate();
392  bool set_parameters(String *expanded_query, bool has_new_types,
393  PS_PARAM *parameters);
394  bool set_parameters(String *expanded_query);
395 
396  private:
397  void cleanup_stmt();
398  void setup_set_params();
399  bool set_db(const LEX_CSTRING &db_length);
400 
401  bool execute(String *expanded_query, bool open_cursor);
402  bool reprepare();
406  bool insert_params(String *query, PS_PARAM *parameters);
407 };
408 
409 #endif // SQL_PREPARE_H
Ed_result_set * get_result_sets()
Definition: sql_prepare.h:261
bool execute(String *expanded_query, bool open_cursor)
Execute a prepared statement.
Definition: sql_prepare.cc:3067
size_t size() const
Definition: sql_prepare.h:305
Ed_row(Ed_column *column_array_arg, size_t column_count_arg)
Definition: sql_prepare.h:307
void mysqld_stmt_close(THD *thd, Prepared_statement *stmt)
Delete a prepared statement from memory.
Definition: sql_prepare.cc:2056
unsigned char uchar
Definition: my_inttypes.h:51
uint mysql_errno() const
Definition: sql_error.h:380
Ed_connection(THD *thd)
Construct a new "execute direct" connection.
Definition: sql_prepare.cc:3322
One result set record.
Definition: sql_prepare.h:296
Prepared_statement: a statement that can contain placeholders.
Definition: sql_prepare.h:321
Definition: mysql_lex_string.h:34
virtual ~Prepared_statement()
Destroy this prepared statement, cleaning up all used memory and resources.
Definition: sql_prepare.cc:2345
One result set column.
Definition: sql_prepare.h:290
size_t get_field_count() const
Definition: sql_prepare.h:154
Definition: sql_lex.h:3192
const Ed_column & operator[](const unsigned int column_index) const
Definition: sql_prepare.h:298
size_t m_column_count
Definition: sql_prepare.h:312
static char * query
Definition: myisam_ftdump.cc:44
Definition: sql_prepare.h:322
virtual ~Server_runnable()
Definition: sql_prepare.cc:2218
void mysql_sql_stmt_execute(THD *thd)
SQLCOM_EXECUTE implementation.
Definition: sql_prepare.cc:1953
Some integer typedefs for easier portability.
Ed_row * get_fields()
Definition: sql_prepare.h:146
char last_error[MYSQL_ERRMSG_SIZE]
Definition: sql_prepare.h:331
Definition: sql_prepare.h:322
Prepared_statement(THD *thd_arg)
Definition: sql_prepare.cc:2288
Ed_result_set – a container with result set rows.
Definition: sql_prepare.h:142
flag_values
Definition: sql_prepare.h:322
const char * message_text() const
Definition: sql_error.h:375
bool execute_server_runnable(Server_runnable *server_runnable)
Definition: sql_prepare.cc:2876
bool is_invalidated() const
Definition: sql_prepare.h:88
LEX_CSTRING m_db
Name of the current (default) database.
Definition: sql_prepare.h:367
Ed_result_set * m_current_rset
Definition: sql_prepare.h:276
bool is_sql_prepare() const
Definition: sql_prepare.h:382
Definition: mysql_lex_string.h:39
MEM_ROOT main_mem_root
The memory root to allocate parsed tree elements (instances of Item, SELECT_LEX and other classes)...
Definition: sql_prepare.h:373
void mysqld_stmt_reset(THD *thd, Prepared_statement *stmt)
Reset a prepared statement in case there was a recoverable error.
Definition: sql_prepare.cc:2030
Server_side_cursor * cursor
Definition: sql_prepare.h:328
Definition: com_data.h:106
Definition: com_data.h:53
Execute a fragment of server code in an isolated context, so that it doesn&#39;t leave any effect on THD...
Definition: sql_prepare.h:122
size_t m_column_count
Definition: sql_prepare.h:169
bool mysql_stmt_precheck(THD *thd, const COM_DATA *com_data, enum enum_server_command cmd, Prepared_statement **stmt)
Searches for the statement with the specified id and validates it.
Definition: sql_prepare.cc:1488
Using this class is fraught with peril, and you need to be very careful when doing so...
Definition: sql_string.h:161
void mysqld_stmt_prepare(THD *thd, const char *query, uint length, Prepared_statement *stmt)
COM_STMT_PREPARE handler.
Definition: sql_prepare.cc:1445
Common definition between mysql server & client.
Ed_result_set * m_next_rset
Definition: sql_prepare.h:172
Placeholder (&#39;?&#39;) of prepared statement.
Definition: item.h:3659
PSI_prepared_stmt * get_PS_prepared_stmt()
Definition: sql_prepare.h:388
Server_side_cursor – an interface for materialized implementation of cursors.
Definition: sql_cursor.h:51
bool set_name(const LEX_CSTRING &name)
Definition: sql_prepare.cc:2373
#define DBUG_ASSERT(A)
Definition: my_dbug.h:197
bool execute_loop(String *expanded_query, bool open_cursor)
Execute a prepared statement.
Definition: sql_prepare.cc:2750
Protocol_local: a helper class to intercept the result of the data written to the network...
Definition: sql_prepare.cc:239
void cleanup_stmt()
Definition: sql_prepare.cc:2364
bool reinit_stmt_before_use(THD *thd, LEX *lex)
Reinit prepared statement/stored procedure before execution.
Definition: sql_prepare.cc:1738
void reset_reprepare_observer()
Definition: sql_prepare.h:89
void mysqld_stmt_fetch(THD *thd, Prepared_statement *stmt, ulong num_rows)
COM_STMT_FETCH handler: fetches requested amount of rows from cursor.
Definition: sql_prepare.cc:1989
Definition: sql_class.h:229
Query_arena m_arena
Definition: sql_prepare.h:325
void add_result_set(Ed_result_set *ed_result_set)
A helper method that is called only during execution.
Definition: sql_prepare.cc:3410
struct PSI_prepared_stmt PSI_prepared_stmt
Definition: psi_statement_bits.h:80
bool set_db(const LEX_CSTRING &db_length)
Remember the current database.
Definition: sql_prepare.cc:2390
void deallocate()
Common part of DEALLOCATE PREPARE and mysqld_stmt_close.
Definition: sql_prepare.cc:3285
Definition: sql_prepare.h:176
Stores status of the currently executed statement.
Definition: sql_error.h:268
Ed_connection & operator=(Ed_connection &)
Ed_result_set & operator=(Ed_result_set &)
Defines various enable/disable and HAVE_ macros related to the performance schema instrumentation sys...
Definition: item.h:668
#define MYSQL_ERRMSG_SIZE
Max length of a error message.
Definition: mysql_com.h:833
unsigned int uint
Definition: uca-dump.cc:29
#define final(a, b, c)
Definition: hash.c:109
const LEX_CSTRING & name() const
Definition: sql_prepare.h:379
Performance schema instrumentation interface.
LEX_CSTRING m_query_string
The query associated with this statement.
Definition: sql_prepare.h:344
Ed_column * m_column_array
Definition: sql_prepare.h:311
bool m_invalidated
Definition: sql_prepare.h:92
Ed_result_set(List< Ed_row > *rows_arg, Ed_row *fields, size_t column_count, MEM_ROOT *mem_root_arg)
Initialize an instance of Ed_result_set.
Definition: sql_prepare.cc:3306
Ed_row * m_fields
Definition: sql_prepare.h:171
const char * get_last_error() const
The following three members are only valid if execute_direct() or move_to_next_result() returned an e...
Definition: sql_prepare.h:253
LEX_CSTRING m_name
Definition: sql_prepare.h:354
~Ed_result_set()
We don&#39;t call member destructors, they all are POD types.
Definition: sql_prepare.h:152
enum_server_command
A list of all MySQL protocol commands.
Definition: my_command.h:47
void swap_prepared_statement(Prepared_statement *copy)
Replace the original prepared statement with a prepared copy.
Definition: sql_prepare.cc:3004
LEX * lex
Definition: sql_prepare.h:339
bool validate_metadata(Prepared_statement *copy)
Validate statement result set metadata (if the statement returns a result set).
Definition: sql_prepare.cc:2980
THD * m_thd
Execute direct interface does not support multi-statements, only multi-results.
Definition: sql_prepare.h:274
void set_sql_prepare()
Definition: sql_prepare.h:383
bool prepare(const char *packet, size_t packet_length)
Parse statement text, validate the statement, and prepare it for execution.
Definition: sql_prepare.cc:2429
Definition: query_result.h:187
bool execute_direct(LEX_STRING sql_text)
Execute one SQL statement.
Definition: sql_prepare.cc:3347
bool is_in_use() const
Definition: sql_prepare.h:381
Diagnostics_area m_diagnostics_area
Definition: sql_prepare.h:266
void mysqld_stmt_execute(THD *thd, Prepared_statement *stmt, bool has_new_types, ulong execute_flags, PS_PARAM *parameters)
COM_STMT_EXECUTE handler: execute a previously prepared statement.
Definition: sql_prepare.cc:1889
bool insert_params_from_vars(List< LEX_STRING > &varnames, String *query)
Assign prepared statement parameters from user variables.
Definition: sql_prepare.cc:885
uint elements
Definition: sql_list.h:135
unsigned int get_last_errno() const
Definition: sql_prepare.h:257
THD * thd
Definition: sql_prepare.h:326
MEM_ROOT m_mem_root
Definition: sql_prepare.h:168
bool reprepare()
Reprepare this prepared statement.
Definition: sql_prepare.cc:2923
void close_cursor()
Definition: sql_prepare.cc:2310
Ed_result_set * m_rsets
Definition: sql_prepare.h:275
bool set_parameters(String *expanded_query, bool has_new_types, PS_PARAM *parameters)
Assign parameter values either from variables, in case of SQL PS or from the execute packet...
Definition: sql_prepare.cc:2681
void free_old_result()
Free all result sets of the previous statement, if any, and reset warnings and errors.
Definition: sql_prepare.cc:3332
Definition: sql_cmd_dml.h:32
bool select_like_stmt_cmd_test(THD *thd, class Sql_cmd_dml *cmd, ulong setup_tables_done_option)
COM_DATA cmd
Definition: test_session_info.cc:95
Query_result_send * result
Definition: sql_prepare.h:350
~Ed_connection()
Definition: sql_prepare.h:263
unsigned int size() const
Definition: sql_prepare.h:145
List< Ed_row > * m_rows
Definition: sql_prepare.h:170
uint flags
Definition: sql_prepare.h:352
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:77
Definition: table.h:2468
const Ed_column * get_column(const unsigned int column_index) const
Definition: sql_prepare.h:301
void mysql_sql_stmt_prepare(THD *thd)
SQLCOM_PREPARE implementation.
Definition: sql_prepare.cc:1663
void mysql_stmt_get_longdata(THD *thd, Prepared_statement *stmt, uint param_number, uchar *longdata, ulong length)
Handle long data in pieces from client.
Definition: sql_prepare.cc:2117
void mysql_sql_stmt_close(THD *thd)
SQLCOM_DEALLOCATE implementation.
Definition: sql_prepare.cc:2079
unsigned long ulong
Definition: my_inttypes.h:48
bool mysql_test_show(Prepared_statement *stmt, TABLE_LIST *tables)
Validate SHOW statement.
Definition: sql_prepare.cc:969
bool length(const dd::Spatial_reference_system *srs, const Geometry *g1, double *length, bool *null) noexcept
Computes the length of linestrings and multilinestrings.
Definition: length.cc:75
bool with_log
Definition: sql_prepare.h:353
bool insert_params(String *query, PS_PARAM *parameters)
Routines to assign parameters from data supplied by the client.
Definition: sql_prepare.cc:753
An interface that is used to take an action when the locking module notices that a table version has ...
Definition: sql_prepare.h:79
Item_param ** param_array
Definition: sql_prepare.h:327
const ulong id
Definition: sql_prepare.h:337
void setup_set_params()
Definition: sql_prepare.cc:2317
This file follows Google coding style, except for the name MEM_ROOT (which is kept for historical rea...
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:778
virtual bool execute_server_code(THD *thd)=0
PSI_prepared_stmt * m_prepared_stmt
Definition: sql_prepare.h:347
uint last_errno
Definition: sql_prepare.h:330
bool report_error(THD *thd)
Check if a change of metadata is OK.
Definition: sql_prepare.cc:2194
uint param_count
Definition: sql_prepare.h:329