MySQL  8.0.20
Source Code Documentation
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.
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.
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.
16  This program is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  GNU General Public License, version 2.0, for more details.
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 */
25 #include <stddef.h>
26 #include <sys/types.h>
27 #include <new>
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"
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;
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.
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.
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.
69  At most 1 instance of this class is active at a time, in which
70  case THD::m_reprepare_observer is not NULL.
72  @sa check_and_update_table_version() for details of the
73  version tracking algorithm
75  @sa Open_tables_state::m_reprepare_observer for the life cycle
76  of metadata observers.
77 */
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; }
91  private:
93 };
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);
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 */
123  public:
124  virtual bool execute_server_code(THD *thd) = 0;
125  virtual ~Server_runnable();
126 };
128 /**
129  Execute direct interface.
131  @todo Implement support for prelocked mode.
132 */
134 class Ed_row;
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 */
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; }
148  Ed_result_set(List<Ed_row> *rows_arg, Ed_row *fields, size_t column_count,
149  MEM_ROOT *mem_root_arg);
151  /** We don't call member destructors, they all are POD types. */
154  size_t get_field_count() const { return m_column_count; }
156  static void operator delete(void *, size_t) noexcept {
157  // Does nothing because m_mem_root is deallocated in the destructor
158  }
160  static void operator delete(
161  void *, MEM_ROOT *, const std::nothrow_t &)noexcept { /* never called */
162  }
164  private:
165  Ed_result_set(const Ed_result_set &); /* not implemented */
166  Ed_result_set &operator=(Ed_result_set &); /* not implemented */
167  private:
168  MEM_ROOT m_mem_root;
173  friend class Ed_connection;
174 };
177  public:
178  /**
179  Construct a new "execute direct" connection.
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.
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);
195  /**
196  Execute one SQL statement.
198  Until this method is executed, no other methods of
199  Ed_connection can be used. Life cycle of Ed_connection is:
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.
207  This method can be called repeatedly. Once it's invoked,
208  results of the previous execution are lost.
210  A result of execute_direct() can be either:
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.
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..
223  - an error, methods to retrieve error information can
224  be used.
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);
234  /**
235  Same as the previous, but takes an instance of Server_runnable
236  instead of SQL statement text.
238  @return execution status
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);
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 {
254  return m_diagnostics_area.message_text();
255  }
257  unsigned int get_last_errno() const {
258  return m_diagnostics_area.mysql_errno();
259  }
261  Ed_result_set *get_result_sets() { return m_rsets; }
263  ~Ed_connection() { free_old_result(); }
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;
279  private:
280  void free_old_result();
281  void add_result_set(Ed_result_set *ed_result_set);
283  private:
284  Ed_connection(const Ed_connection &); /* not implemented */
285  Ed_connection &operator=(Ed_connection &); /* not implemented */
286 };
288 /** One result set column. */
290 struct Ed_column final : public LEX_STRING {
291  /** Implementation note: destructor for this class is never called. */
292 };
294 /** One result set record. */
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; }
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) {}
310  private:
312  size_t m_column_count; /* TODO: change to point to metadata */
313 };
315 class Server_side_cursor;
317 /**
318  Prepared_statement: a statement that can contain placeholders.
319 */
321 class Prepared_statement final {
322  enum flag_values { IS_IN_USE = 1, IS_SQL_PREPARE = 2 };
324  public:
331  char last_error[MYSQL_ERRMSG_SIZE];
333  /*
334  Uniquely identifies each statement object in thread scope; change during
335  statement lifetime.
336  */
337  const ulong id;
339  LEX *lex; // parse tree descriptor
341  /**
342  The query associated with this statement.
343  */
346  /* Performance Schema interface for a prepared statement. */
349  private:
353  bool with_log;
354  LEX_CSTRING m_name; /* name for named prepared statements */
355  /**
356  Name of the current (default) database.
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.
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  */
369  /**
370  The memory root to allocate parsed tree elements (instances of Item,
371  SELECT_LEX and other classes).
372  */
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; }
383  void set_sql_prepare() { 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);
388  PSI_prepared_stmt *get_PS_prepared_stmt() { return m_prepared_stmt; }
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);
396  private:
397  void cleanup_stmt();
398  void setup_set_params();
399  bool set_db(const LEX_CSTRING &db_length);
401  bool execute(String *expanded_query, bool open_cursor);
402  bool reprepare();
403  bool validate_metadata(Prepared_statement *copy);
404  void swap_prepared_statement(Prepared_statement *copy);
405  bool insert_params_from_vars(List<LEX_STRING> &varnames, String *query);
406  bool insert_params(String *query, PS_PARAM *parameters);
407 };
409 #endif // SQL_PREPARE_H
Ed_result_set * get_result_sets()
Definition: sql_prepare.h:261
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.
unsigned char uchar
Definition: my_inttypes.h:51
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
One result set column.
Definition: sql_prepare.h:290
size_t get_field_count() const
Definition: sql_prepare.h:154
Definition: sql_lex.h:3303
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
void mysql_sql_stmt_execute(THD *thd)
SQLCOM_EXECUTE implementation.
Some integer typedefs for easier portability.
Ed_row * get_fields()
Definition: sql_prepare.h:146
Ed_result_set – a container with result set rows.
Definition: sql_prepare.h:142
Definition: sql_prepare.h:322
bool is_invalidated() const
Definition: sql_prepare.h:88
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.
Server_side_cursor * cursor
Definition: sql_prepare.h:328
static bool execute(MYSQL_STMT *stmt, char *packet, ulong length)
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.
Using this class is fraught with peril, and you need to be very careful when doing so...
Definition: sql_string.h:164
void mysqld_stmt_prepare(THD *thd, const char *query, uint length, Prepared_statement *stmt)
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:3929
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
#define DBUG_ASSERT(A)
Definition: my_dbug.h:199
Protocol_local: a helper class to intercept the result of the data written to the network...
bool reinit_stmt_before_use(THD *thd, LEX *lex)
Reinit prepared statement/stored procedure before execution.
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_class.h:226
static void prepare(pax_msg *p, pax_op op)
Definition: xcom_base.c:1383
Query_arena m_arena
Definition: sql_prepare.h:325
struct PSI_prepared_stmt PSI_prepared_stmt
Definition: psi_statement_bits.h:80
Definition: sql_prepare.h:176
Stores status of the currently executed statement.
Definition: sql_error.h:268
Defines various enable/disable and HAVE_ macros related to the performance schema instrumentation sys...
Definition: item.h:740
Max length of a error message.
Definition: mysql_com.h:833
unsigned int uint
#define final(a, b, c)
Definition: hash.c:109
case opt name
Definition: sslopt-case.h:32
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
static bool set_parameters(gis::srid_t srid, gis::srs::wkt_parser::Projected_cs *proj, std::vector< std::pair< int, double *>> *params)
Extract projection parameter values from the parse tree and assign them to variables.
bool m_invalidated
Definition: sql_prepare.h:92
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
Definition: sql_prepare.h:354
We don&#39;t call member destructors, they all are POD types.
Definition: sql_prepare.h:152
A list of all MySQL protocol commands.
Definition: my_command.h:47
LEX * lex
Definition: sql_prepare.h:339
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
Definition: query_result.h:187
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.
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
Ed_result_set * m_rsets
Definition: sql_prepare.h:275
Definition: sql_cmd_dml.h:32
bool select_like_stmt_cmd_test(THD *thd, class Sql_cmd_dml *cmd, ulong setup_tables_done_option)
Query_result_send * result
Definition: sql_prepare.h:350
Definition: sql_prepare.h:263
struct st_binary_log_transaction_compression_stats m_rows
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:2481
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.
void mysql_stmt_get_longdata(THD *thd, Prepared_statement *stmt, uint param_number, uchar *longdata, ulong length)
Handle long data in pieces from client.
void mysql_sql_stmt_close(THD *thd)
SQLCOM_DEALLOCATE implementation.
bool mysql_test_show(Prepared_statement *stmt, TABLE_LIST *tables)
Validate SHOW statement.
bool length(const dd::Spatial_reference_system *srs, const Geometry *g1, double *length, bool *null) noexcept
Computes the length of linestrings and multilinestrings.
bool with_log
Definition: sql_prepare.h:353
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 copy(Shards< COUNT > &dst, const Shards< COUNT > &src) noexcept
Copy the counters, overwrite destination.
Definition: ut0counter.h:336
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:765
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.
uint param_count
Definition: sql_prepare.h:329