Prepared_statement: a statement that can contain placeholders.
More...
#include <sql_prepare.h>
Prepared_statement: a statement that can contain placeholders.
◆ flag_values
Enumerator |
---|
IS_IN_USE | |
IS_SQL_PREPARE | |
◆ Prepared_statement()
Prepared_statement::Prepared_statement |
( |
THD * |
thd_arg | ) |
|
◆ ~Prepared_statement()
Prepared_statement::~Prepared_statement |
( |
| ) |
|
|
virtual |
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.
◆ check_parameter_types()
bool Prepared_statement::check_parameter_types |
( |
| ) |
|
|
private |
Check resolved parameter types versus actual parameter types.
Assumes that parameter values have been assigned to the parameters.
- Returns
- true if parameter types are compatible, false otherwise.
◆ cleanup_stmt()
void Prepared_statement::cleanup_stmt |
( |
| ) |
|
|
private |
◆ close_cursor()
void Prepared_statement::close_cursor |
( |
| ) |
|
◆ copy_parameter_types()
void Prepared_statement::copy_parameter_types |
( |
Item_param ** |
from_param_array | ) |
|
|
private |
Copy parameter metada data from parameter array into current prepared stmt.
- Parameters
-
from_param_array | Parameter array to copy from. |
◆ deallocate()
void Prepared_statement::deallocate |
( |
| ) |
|
Common part of DEALLOCATE PREPARE and mysqld_stmt_close.
◆ execute()
bool Prepared_statement::execute |
( |
String * |
expanded_query, |
|
|
bool |
open_cursor |
|
) |
| |
|
private |
Execute a prepared statement.
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.
- Parameters
-
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. Currenlty used only in the binary protocol. |
- Note
- Preconditions, postconditions.
- See the comment for Prepared_statement::prepare().
- Return values
-
◆ execute_loop()
bool Prepared_statement::execute_loop |
( |
String * |
expanded_query, |
|
|
bool |
open_cursor |
|
) |
| |
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.
- Note
- We have to try several times in a loop since we release metadata locks on tables after prepared statement prepare. Therefore, a DDL statement may sneak in between prepare and execute of a new statement. If a prepared statement execution is retried for a maximum number of times then we give up.
- Parameters
-
expanded_query | Query string. |
open_cursor | Flag to specift if a cursor should be used. |
- Returns
- a bool value representing the function execution status.
- Return values
-
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. |
◆ execute_server_runnable()
bool Prepared_statement::execute_server_runnable |
( |
Server_runnable * |
server_runnable | ) |
|
◆ get_PS_prepared_stmt()
◆ insert_parameters()
bool Prepared_statement::insert_parameters |
( |
String * |
query, |
|
|
bool |
has_new_types, |
|
|
PS_PARAM * |
parameters |
|
) |
| |
|
private |
Assign parameter values from data supplied by the client.
If required, generate a valid non-parameterized query for logging.
- Parameters
-
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. |
- Returns
- false if success, true if error
- Note
- with_log is set when one of slow or general logs are open. Logging of prepared statements in all cases is performed by means of regular queries: if parameter data was supplied from C API, each placeholder in the query is replaced with its actual value; if we're logging a [Dynamic] SQL prepared statement, parameter markers are replaced with variable names. Example:
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;
◆ insert_parameters_from_vars()
bool Prepared_statement::insert_parameters_from_vars |
( |
List< LEX_STRING > & |
varnames, |
|
|
String * |
query |
|
) |
| |
|
private |
Assign prepared statement parameters from user variables.
If with_log is set, also construct query string for binary log.
- Parameters
-
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. |
- Returns
- false if success, true if error
◆ is_in_use()
bool Prepared_statement::is_in_use |
( |
| ) |
const |
|
inline |
◆ is_sql_prepare()
bool Prepared_statement::is_sql_prepare |
( |
| ) |
const |
|
inline |
◆ name()
◆ prepare()
bool Prepared_statement::prepare |
( |
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.
- Parameters
-
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. |
- Note
- Precondition: The caller must ensure that thd->change_list and thd->item_list is empty: this function will not back them up but will free in the end of its execution.
-
Postcondition: thd->mem_root contains unused memory allocated during validation.
◆ prepare_query()
bool Prepared_statement::prepare_query |
( |
| ) |
|
Perform semantic analysis of query and send a response packet to client.
This function
- opens all tables and checks privileges to them.
- validates semantics of statement columns and SQL functions by calling fix_fields.
- performs global transformations such as semi-join conversion.
- Return values
-
false | success, statement metadata is sent to client |
true | error, error message is set in THD (but not sent) |
◆ reprepare()
bool Prepared_statement::reprepare |
( |
| ) |
|
|
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.
- Return values
-
true | an error occurred. Possible errors include incompatibility of new and old result set metadata |
false | success, the statement has been reprepared |
◆ set_db()
bool Prepared_statement::set_db |
( |
const LEX_CSTRING & |
db_arg | ) |
|
|
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.
- Returns
- Returns an error if out of memory.
◆ set_name()
bool Prepared_statement::set_name |
( |
const LEX_CSTRING & |
name | ) |
|
◆ set_parameters() [1/2]
bool Prepared_statement::set_parameters |
( |
String * |
expanded_query | ) |
|
Assign parameter values from specified variables.
- Parameters
-
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 |
- Returns
- false if success, true if error (likely a conversion error or out of memory)
◆ set_parameters() [2/2]
bool Prepared_statement::set_parameters |
( |
String * |
expanded_query, |
|
|
bool |
has_new_types, |
|
|
PS_PARAM * |
parameters |
|
) |
| |
Assign parameter values from the execute packet.
- Parameters
-
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. |
- Returns
- false if success, true if error (likely a conversion error, out of memory, or malformed packet)
◆ set_sql_prepare()
void Prepared_statement::set_sql_prepare |
( |
| ) |
|
|
inline |
◆ setup_stmt_logging()
void Prepared_statement::setup_stmt_logging |
( |
| ) |
|
|
private |
◆ swap_prepared_statement()
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.
◆ trace_parameter_types()
void Prepared_statement::trace_parameter_types |
( |
| ) |
|
◆ validate_metadata()
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.
- Parameters
-
[in] | copy | the re-prepared prepared statement to verify the metadata of |
- Return values
-
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
◆ cursor
◆ flags
uint Prepared_statement::flags |
|
private |
◆ id
const ulong Prepared_statement::id |
◆ last_errno
uint Prepared_statement::last_errno |
◆ last_error
◆ lex
LEX* Prepared_statement::lex |
◆ m_active_protocol
Used to check that the protocol is stable during execution.
◆ m_arena
◆ m_db
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.
- Note
- this attribute is set and alloced by the slave SQL thread (for the THD of that thread); that thread is (and must remain, for now) the only responsible for freeing this member.
◆ m_name
◆ m_prepared_stmt
◆ m_query_string
The query associated with this statement.
◆ main_mem_root
MEM_ROOT Prepared_statement::main_mem_root |
|
private |
The memory root to allocate parsed tree elements (instances of Item, Query_block and other classes).
◆ param_array
◆ param_count
uint Prepared_statement::param_count |
◆ result
◆ thd
THD* Prepared_statement::thd |
◆ with_log
bool Prepared_statement::with_log |
|
private |
The documentation for this class was generated from the following files: