![]() |
MySQL 9.6.0
Source Code Documentation
|
Prepared_statement: a statement that can contain placeholders. More...
#include <sql_prepare.h>
Public Types | |
| enum | enum_param_pack_type { PACKED , UNPACKED } |
Public Member Functions | |
| Prepared_statement (THD *thd_arg) | |
| ~Prepared_statement () | |
| Destroy this prepared statement, cleaning up all used memory and resources. More... | |
| void | psi_instrumentation (THD *thd, uint digest_prefix_token, bool copy) |
| Performance schema instrumentation for execute / deallocate. More... | |
| void | set_display_query_string (const char *display_query_string, size_t display_query_string_length) |
| void | get_display_query_string (const char **display_query_string_ptr, size_t *display_query_string_length_ptr) const |
| void | set_digest (const sql_digest_storage *digest) |
| const sql_digest_storage * | get_digest () const |
| bool | set_name (const LEX_CSTRING &name) |
| const LEX_CSTRING & | name () const |
| ulong | id () const |
| bool | is_in_use () const |
| bool | is_sql_prepare () const |
| void | set_sql_prepare (bool prepare=true) |
| void | deallocate (THD *thd) |
| Common part of DEALLOCATE PREPARE and mysqld_stmt_close. More... | |
| bool | prepare (THD *thd, const char *packet, size_t packet_length, Item_param **orig_param_array) |
| Parse statement text, validate the statement, and prepare it for execution. More... | |
| bool | execute_loop (THD *thd, String *expanded_query, bool open_cursor) |
| Execute a prepared statement. More... | |
| bool | execute_server_runnable (THD *thd, Server_runnable *server_runnable) |
| PSI_prepared_stmt * | get_PS_prepared_stmt () |
| bool | set_parameters (THD *thd, String *expanded_query, bool has_new_types, PS_PARAM *parameters) |
| Assign parameter values from the execute packet. More... | |
| bool | set_parameters (THD *thd, String *expanded_query, bool has_new_types, PS_PARAM *parameters, enum enum_param_pack_type param_pack_type) |
| Assign parameter values from the execute packet. More... | |
| bool | set_parameters (THD *thd, String *expanded_query) |
| Assign parameter values from specified variables. More... | |
| void | trace_parameter_types (THD *thd) |
| void | close_cursor () |
Public Attributes | |
| Query_arena | m_arena |
| Memory allocation arena, for permanent allocations to statement. More... | |
| Item_param ** | m_param_array {nullptr} |
| Array of parameters used for statement, may be NULL if there are none. More... | |
| Server_side_cursor * | m_cursor {nullptr} |
| Pointer to cursor, may be NULL if statement never used with a cursor. More... | |
| const Protocol * | m_active_protocol {nullptr} |
| Used to check that the protocol is stable during execution. More... | |
| uint | m_param_count {0} |
| Number of parameters expected for statement. More... | |
| uint | m_last_errno {0} |
| char | m_last_error [MYSQL_ERRMSG_SIZE] |
| const ulong | m_id |
| Uniquely identifies each statement object in thread scope; change during statement lifetime. More... | |
| LEX * | m_lex {nullptr} |
| LEX_CSTRING | m_query_string {NULL_CSTR} |
| The query string associated with this statement. More... | |
| LEX_CSTRING | m_display_query_string {NULL_CSTR} |
| The query display string associated with this statement. More... | |
| PSI_prepared_stmt * | m_prepared_stmt {nullptr} |
| Performance Schema interface for a prepared statement. More... | |
Private Member Functions | |
| bool | prepare_query (THD *thd) |
| Perform semantic analysis of query and send a response packet to client. More... | |
| void | cleanup_stmt (THD *thd) |
| void | setup_stmt_logging (THD *thd) |
| bool | check_parameter_types () |
| Check resolved parameter types versus actual parameter types. More... | |
| void | copy_parameter_types (Item_param **from_param_array) |
| Copy parameter metada data from parameter array into current prepared stmt. More... | |
| bool | set_db (const LEX_CSTRING &db_length) |
| Remember the current database. More... | |
| bool | execute (THD *thd, String *expanded_query, bool open_cursor) |
| Execute a prepared statement that has already been prepared. More... | |
| bool | reprepare (THD *thd) |
| Reprepare this prepared statement. More... | |
| bool | validate_metadata (THD *thd, Prepared_statement *copy) |
| Validate statement result set metadata (if the statement returns a result set). More... | |
| void | swap_prepared_statement (Prepared_statement *copy) |
| Swap the MEM_ROOT allocated data of two prepared statements. More... | |
| bool | insert_parameters_from_vars (THD *thd, List< LEX_STRING > &varnames, String *query) |
| Assign prepared statement parameters from user variables. More... | |
| bool | insert_parameters (THD *thd, String *query, bool has_new_types, PS_PARAM *parameters, enum enum_param_pack_type param_pack_type) |
| Assign parameter values from data supplied by the client. More... | |
Private Attributes | |
| bool | m_used_as_cursor {false} |
| True if statement is used with cursor, false if used in regular execution. More... | |
| Query_result * | m_regular_result {nullptr} |
| Query result used when statement is used in regular execution. More... | |
| Query_result * | m_cursor_result {nullptr} |
| Query result used when statement is used with a cursor. More... | |
| Query_result * | m_aux_result {nullptr} |
| Auxiliary query result object, saved for proper destruction. More... | |
| bool | m_is_sql_prepare {false} |
| Flag that specifies preparation state. More... | |
| bool | m_in_use {false} |
| Flag that prevents recursive invocation of prepared statements. More... | |
| bool | m_with_log {false} |
| bool | m_first_execution {true} |
| LEX_CSTRING | m_name {NULL_CSTR} |
| Name of the prepared statement. More... | |
| LEX_CSTRING | m_db {NULL_CSTR} |
| Name of the current (default) database. More... | |
| MEM_ROOT | m_mem_root |
| The memory root to allocate parsed tree elements (instances of Item, Query_block and other classes). More... | |
| sql_digest_storage | m_digest {} |
| DIGEST and DIGEST_TEXT of the prepared statement. More... | |
| unsigned char * | m_token_array {nullptr} |
| Token array used to store DIGEST and DIGEST_TEXT. More... | |
| size_t | m_token_array_length {0} |
| Length of m_token_array. More... | |
Prepared_statement: a statement that can contain placeholders.
|
explicit |
| Prepared_statement::~Prepared_statement | ( | ) |
Destroy this prepared statement, cleaning up all used memory and resources.
This is called from deallocate() to handle COM_STMT_CLOSE and DEALLOCATE PREPARE or when THD ends and all prepared statements are freed.
|
private |
Check resolved parameter types versus actual parameter types.
Assumes that parameter values have been assigned to the parameters.
|
private |
| void Prepared_statement::close_cursor | ( | ) |
|
private |
Copy parameter metada data from parameter array into current prepared stmt.
| from_param_array | Parameter array to copy from. |
| void Prepared_statement::deallocate | ( | THD * | thd | ) |
Common part of DEALLOCATE PREPARE and mysqld_stmt_close.
Execute a prepared statement that has already been prepared.
| thd | current thread. |
| expanded_query | A query for binlogging which has all parameter markers ('?') replaced with their actual values. |
| open_cursor | True if an attempt to open a cursor should be made. Currently used only in the binary protocol. |
Postconditions.
Execute a prepared statement.
Re-prepare it a limited number of times if necessary.
Try to execute a prepared statement. If there is a metadata validation error, prepare a new copy of the prepared statement, swap the old and the new statements, and try again. If there is a validation error again, repeat the above, but perform not more than a maximum number of times. Reprepare_observer ensures that a prepared statement execution is retried not more than a maximum number of times.
| thd | current thread. |
| expanded_query | Query string. |
| open_cursor | Flag to specify if a cursor should be used. |
| true | error: either statement execution is retried for a maximum number of times or some general error. |
| false | successfully executed the statement, perhaps after having reprepared it a few times. |
| bool Prepared_statement::execute_server_runnable | ( | THD * | thd, |
| Server_runnable * | server_runnable | ||
| ) |
|
inline |
| void Prepared_statement::get_display_query_string | ( | const char ** | display_query_string_ptr, |
| size_t * | display_query_string_length_ptr | ||
| ) | const |
|
inline |
|
inline |
|
private |
Assign parameter values from data supplied by the client.
If required, generate a valid non-parameterized query for logging.
| thd | current thread. |
| query | The query with parameter markers replaced with values supplied by user that were used to execute the query. |
| has_new_types | if true, new types of actual parameters are provided, otherwise use the parameters from previous execution. |
| parameters | Array of actual parameter values. Contains parameter types if has_new_types is true. |
| pack_type | UNPACKED means that the parameter value buffer points to MYSQL_TIME* |
mysqld_stmt_prepare("UPDATE t1 SET a=a*1.25 WHERE a=?")
--> general logs gets [Prepare] UPDATE t1 SET a*1.25 WHERE a=?"
mysqld_stmt_execute(stmt);
--> general and binary logs get
[Execute] UPDATE t1 SET a*1.25 WHERE a=1"If a statement has been prepared using SQL syntax:
PREPARE stmt FROM "UPDATE t1 SET a=a*1.25 WHERE a=?"
--> general log gets [Query] PREPARE stmt FROM "UPDATE ..."
EXECUTE stmt USING @a
--> general log gets [Query] EXECUTE stmt USING @a;
|
private |
Assign prepared statement parameters from user variables.
If m_with_log is set, also construct query string for binary log.
| thd | Current thread. |
| varnames | List of variables. Caller must ensure that number of variables in the list is equal to number of statement parameters |
| query | The query with parameter markers replaced with corresponding user variables that were used to execute the query. |
|
inline |
|
inline |
|
inline |
| bool Prepared_statement::prepare | ( | THD * | thd, |
| const char * | query_str, | ||
| size_t | query_length, | ||
| Item_param ** | orig_param_array | ||
| ) |
Parse statement text, validate the statement, and prepare it for execution.
You should not change global THD state in this function, if at all possible: it may be called from any context, e.g. when executing a COM_* command, and SQLCOM_* command, or a stored procedure.
| thd | thread handle |
| query_str | Statement text |
| query_length | Length of statement string |
| orig_param_array | Array containing pointers to parameter items that contain data type information for query. This is used during reprepare. = NULL: Derive parameter metadata from query only. |
|
private |
Perform semantic analysis of query and send a response packet to client.
This function
| thd | current thread |
| false | success, statement metadata is sent to client |
| true | error, error message is set in THD (but not sent) |
| void Prepared_statement::psi_instrumentation | ( | THD * | thd, |
| uint | digest_prefix_token, | ||
| bool | copy | ||
| ) |
Performance schema instrumentation for execute / deallocate.
|
private |
Reprepare this prepared statement.
Performs a light reset of the Prepared_statement object (resets its MEM_ROOT and clears its MEM_ROOT allocated members), and calls prepare() on it again.
The resetting of the MEM_ROOT and clearing of the MEM_ROOT allocated members is performed by a swap operation (swap_prepared_statement()) on this Prepared_statement and a newly created, intermediate Prepared_statement. This both clears the data from this object and stores a backup of the original data in the intermediate object. If the repreparation fails, the original data is swapped back into this Prepared_statement.
| thd | current thread. |
| true | an error occurred. Possible errors include incompatibility of new and old result set metadata |
| false | success, the statement has been reprepared |
|
private |
Remember the current database.
We must reset/restore the current database during execution of a prepared statement since it affects execution environment: privileges, @character_set_database, and other.
| void Prepared_statement::set_digest | ( | const sql_digest_storage * | digest | ) |
| void Prepared_statement::set_display_query_string | ( | const char * | display_query_string, |
| size_t | display_query_string_length | ||
| ) |
| bool Prepared_statement::set_name | ( | const LEX_CSTRING & | name | ) |
Assign parameter values from specified variables.
| thd | current thread |
| expanded_query | a container with the original SQL statement. '?' placeholders will be replaced with their values in case of success. The result is used for logging and replication |
| bool Prepared_statement::set_parameters | ( | THD * | thd, |
| String * | expanded_query, | ||
| bool | has_new_types, | ||
| PS_PARAM * | parameters | ||
| ) |
Assign parameter values from the execute packet.
| thd | current thread |
| expanded_query | a container with the original SQL statement. '?' placeholders will be replaced with their values in case of success. The result is used for logging and replication |
| has_new_types | flag used to signal that new types are provided. |
| parameters | prepared statement's parsed parameters. |
| bool Prepared_statement::set_parameters | ( | THD * | thd, |
| String * | expanded_query, | ||
| bool | has_new_types, | ||
| PS_PARAM * | parameters, | ||
| enum enum_param_pack_type | param_pack_type | ||
| ) |
Assign parameter values from the execute packet.
| thd | current thread |
| expanded_query | a container with the original SQL statement. '?' placeholders will be replaced with their values in case of success. The result is used for logging and replication |
| has_new_types | flag used to signal that new types are provided. |
| parameters | prepared statement's parsed parameters. |
| param_pack_type | parameters pack type. |
|
inline |
|
private |
|
private |
Swap the MEM_ROOT allocated data of two prepared statements.
This is a private helper that is used as part of statement reprepare. It is used in the beginning of reprepare() to clear the MEM_ROOT of the statement before the new preparation, while keeping the data available as some of it is needed later in the repreparation. It is also used for restoring the original data from the copy, should the repreparation fail.
The operation is symmetric. It can be used both for saving an original statement into a backup, and for restoring the original state of the statement from the backup.
| void Prepared_statement::trace_parameter_types | ( | THD * | thd | ) |
|
private |
Validate statement result set metadata (if the statement returns a result set).
Currently we only check that the number of columns of the result set did not change. This is a helper method used during re-prepare.
| thd | current thread. |
| copy | the re-prepared prepared statement to verify the metadata of |
| true | error, ER_PS_REBIND is reported |
| false | statement return no or compatible metadata |
If this is an SQL prepared statement or EXPLAIN, return false – the metadata of the original SELECT, if any, has not been sent to the client.
Column counts mismatch, update the client
Used to check that the protocol is stable during execution.
| Query_arena Prepared_statement::m_arena |
Memory allocation arena, for permanent allocations to statement.
|
private |
Auxiliary query result object, saved for proper destruction.
| Server_side_cursor* Prepared_statement::m_cursor {nullptr} |
Pointer to cursor, may be NULL if statement never used with a cursor.
|
private |
Query result used when statement is used with a cursor.
|
private |
Name of the current (default) database.
If there is the current (default) database, "db" contains its name. If there is no current (default) database, "db" is NULL and "db_length" is 0. In other words, "db", "db_length" must either be NULL, or contain a valid database name.
|
private |
DIGEST and DIGEST_TEXT of the prepared statement.
| LEX_CSTRING Prepared_statement::m_display_query_string {NULL_CSTR} |
The query display string associated with this statement.
|
private |
| const ulong Prepared_statement::m_id |
Uniquely identifies each statement object in thread scope; change during statement lifetime.
|
private |
Flag that prevents recursive invocation of prepared statements.
|
private |
Flag that specifies preparation state.
| uint Prepared_statement::m_last_errno {0} |
| char Prepared_statement::m_last_error[MYSQL_ERRMSG_SIZE] |
|
private |
The memory root to allocate parsed tree elements (instances of Item, Query_block and other classes).
|
private |
Name of the prepared statement.
| Item_param** Prepared_statement::m_param_array {nullptr} |
Array of parameters used for statement, may be NULL if there are none.
| uint Prepared_statement::m_param_count {0} |
Number of parameters expected for statement.
| PSI_prepared_stmt* Prepared_statement::m_prepared_stmt {nullptr} |
Performance Schema interface for a prepared statement.
| LEX_CSTRING Prepared_statement::m_query_string {NULL_CSTR} |
The query string associated with this statement.
|
private |
Query result used when statement is used in regular execution.
|
private |
Token array used to store DIGEST and DIGEST_TEXT.
|
private |
Length of m_token_array.
|
private |
True if statement is used with cursor, false if used in regular execution.
|
private |