MySQL 8.4.0
Source Code Documentation
sql_prepare.h File Reference
#include <assert.h>
#include <stddef.h>
#include <sys/types.h>
#include <new>
#include "lex_string.h"
#include "my_alloc.h"
#include "my_command.h"
#include "my_inttypes.h"
#include "my_psi_config.h"
#include "mysql/components/services/bits/psi_statement_bits.h"
#include "mysql_com.h"
#include "sql/sql_class.h"
#include "sql/sql_error.h"
#include "sql/sql_list.h"
#include "sql/statement/statement_runnable.h"

Go to the source code of this file.

Classes

class  Reprepare_observer
 An interface that is used to take an action when the locking module notices that a table version has changed since the last execution. More...
 
class  Prepared_statement
 Prepared_statement: a statement that can contain placeholders. More...
 

Functions

void rewrite_query_if_needed (THD *thd)
 Rewrite the current query (to obfuscate passwords etc.) if needed (i.e. More...
 
void log_execute_line (THD *thd)
 Unless we're doing dynamic SQL, write the current query to the general query log if it's open. More...
 
bool ask_to_reprepare (THD *thd)
 Requests for repreparation of statement. More...
 
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. More...
 
void mysqld_stmt_prepare (THD *thd, const char *query, uint length, Prepared_statement *stmt)
 COM_STMT_PREPARE handler. More...
 
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. More...
 
void mysqld_stmt_close (THD *thd, Prepared_statement *stmt)
 Delete a prepared statement from memory. More...
 
void mysql_sql_stmt_prepare (THD *thd)
 SQLCOM_PREPARE implementation. More...
 
void mysql_sql_stmt_execute (THD *thd)
 SQLCOM_EXECUTE implementation. More...
 
void mysql_sql_stmt_close (THD *thd)
 SQLCOM_DEALLOCATE implementation. More...
 
void mysqld_stmt_fetch (THD *thd, Prepared_statement *stmt, ulong num_rows)
 COM_STMT_FETCH handler: fetches requested amount of rows from cursor. More...
 
void mysqld_stmt_reset (THD *thd, Prepared_statement *stmt)
 Reset a prepared statement in case there was a recoverable error. More...
 
void mysql_stmt_get_longdata (THD *thd, Prepared_statement *stmt, uint param_number, uchar *longdata, ulong length)
 Handle long data in pieces from client. More...
 
bool select_like_stmt_cmd_test (THD *thd, class Sql_cmd_dml *cmd, ulong setup_tables_done_option)
 
void reset_stmt_parameters (Prepared_statement *stmt)
 Clears parameters from data left from previous execution or long data. More...
 

Function Documentation

◆ ask_to_reprepare()

bool ask_to_reprepare ( THD thd)

Requests for repreparation of statement.

Returns
true if request has been placed.

◆ log_execute_line()

void log_execute_line ( THD thd)

Unless we're doing dynamic SQL, write the current query to the general query log if it's open.

If we have a rewritten version of the query, use that instead of the "raw" one.

Side-effect: query may be written to general log if it's open.

Parameters
thdthread handle

◆ mysql_sql_stmt_close()

void mysql_sql_stmt_close ( THD thd)

SQLCOM_DEALLOCATE implementation.

Close an SQL prepared statement. As this can be called from Dynamic SQL, we should be careful to not close a statement that is currently being executed.

OK packet is sent in case of success, otherwise an error message is set in THD.

◆ mysql_sql_stmt_execute()

void mysql_sql_stmt_execute ( THD thd)

SQLCOM_EXECUTE implementation.

Execute prepared statement using parameter values from lex->prepared_stmt_params and send result to the client using text protocol. This is called from mysql_execute_command and therefore should behave like an ordinary query (e.g. not change global THD data, such as warning count, server status, etc). This function uses text protocol to send a possible result set.

In case of success, OK (or result set) packet is sent to the client, otherwise an error is set in THD.

Parameters
thdthread handle

◆ mysql_sql_stmt_prepare()

void mysql_sql_stmt_prepare ( THD thd)

SQLCOM_PREPARE implementation.

Prepare an SQL prepared statement. This is called from mysql_execute_command and should therefore behave like an ordinary query (e.g. should not reset any global THD data).

In case of success, OK packet is sent to the client, otherwise an error message is set in THD.

Parameters
thdthread handle

◆ mysql_stmt_get_longdata()

void mysql_stmt_get_longdata ( THD thd,
Prepared_statement stmt,
uint  param_number,
uchar str,
ulong  length 
)

Handle long data in pieces from client.

Get a part of a long data. To make the protocol efficient, we are not sending any return packets here. If something goes wrong, then we will send the error on 'execute' We assume that the client takes care of checking that all parts are sent to the server. (No checking that we get a 'end of column' in the server is performed).

Parameters
thdThread handle
stmtPointer to Prepared_statement
param_numberNumber of parameters
strString to append
lengthLength of string (including end \0)

◆ mysql_stmt_precheck()

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.

Parameters
thd[in] thread handle
com_data[in] command data
cmd[in] command type to be executed
stmt[out] pointer to Prepared_statement to store it if found

◆ mysqld_stmt_close()

void mysqld_stmt_close ( THD thd,
Prepared_statement stmt 
)

Delete a prepared statement from memory.

Note
we don't send any reply to this command.

◆ mysqld_stmt_execute()

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.

If there are any parameters, then replace parameter markers with the data supplied from the client, and then execute the statement. This function uses binary protocol to send a possible result set to the client.

In case of success OK packet or a result set is sent to the client, otherwise an error message is set in THD.

Parameters
thdcurrent thread
stmtprepared statement
has_new_typestrue if parsed parameters have data types defined, otherwise types from last execution will be used
execute_flagsflag used to decide if a cursor should be used
parametersprepared statement's parsed parameters

◆ mysqld_stmt_fetch()

void mysqld_stmt_fetch ( THD thd,
Prepared_statement stmt,
ulong  num_rows 
)

COM_STMT_FETCH handler: fetches requested amount of rows from cursor.

Parameters
thdThread handle.
stmtPointer to the prepared statement.
num_rowsNumber of rows to fetch.

◆ mysqld_stmt_prepare()

void mysqld_stmt_prepare ( THD thd,
const char *  query,
uint  length,
Prepared_statement stmt 
)

COM_STMT_PREPARE handler.

Given a query string with parameter markers, create a prepared statement from it and send PS info back to the client.

If parameter markers are found in the query, then store the information using Item_param along with maintaining a list in lex->param_array, so that a fast and direct retrieval can be made without going through all field items.

In case of success a new statement id and metadata is sent to the client, otherwise an error message is set in THD.

Parameters
thdthread handle
queryquery to be prepared
lengthquery string length, including ignored trailing NULL or quote char.
stmtPrepared_statement to be used for preparation.
Note
This function parses the query and sends the total number of parameters and resultset metadata information back to client (if any), without executing the query i.e. without any log/disk writes. This allows the queries to be re-executed without re-parsing during execute.

◆ mysqld_stmt_reset()

void mysqld_stmt_reset ( THD thd,
Prepared_statement stmt 
)

Reset a prepared statement in case there was a recoverable error.

This function resets statement to the state it was right after prepare. It can be used to:

  • clear an error happened during mysqld_stmt_send_long_data
  • cancel long data stream for all placeholders without having to call mysqld_stmt_execute.
  • close an open cursor Sends 'OK' packet in case of success (statement was reset) or 'ERROR' packet (unrecoverable error/statement not found/etc).
Parameters
thdThread handle
stmtPointer to the Prepared_statement

◆ reset_stmt_parameters()

void reset_stmt_parameters ( Prepared_statement stmt)

Clears parameters from data left from previous execution or long data.

Parameters
stmtprepared statement for which parameters should be reset

◆ rewrite_query_if_needed()

void rewrite_query_if_needed ( THD thd)

Rewrite the current query (to obfuscate passwords etc.) if needed (i.e.

only if we'll be writing the query to any of our logs).

Side-effect: thd->rewritten_query() may be populated with a rewritten query. If the query is not of a rewritable type, thd->rewritten_query() will be empty.

Parameters
thdthread handle

◆ select_like_stmt_cmd_test()

bool select_like_stmt_cmd_test ( THD thd,
class Sql_cmd_dml cmd,
ulong  setup_tables_done_option 
)