My Project
|
#include "my_global.h"
#include "sql_priv.h"
#include "unireg.h"
#include "sql_class.h"
#include "set_var.h"
#include "sql_prepare.h"
#include "sql_parse.h"
#include "sql_base.h"
#include "sql_cache.h"
#include "sql_view.h"
#include "sql_delete.h"
#include "sql_select.h"
#include "sql_insert.h"
#include "sql_update.h"
#include "sql_db.h"
#include "sql_acl.h"
#include "sql_cursor.h"
#include "sp_head.h"
#include "sp.h"
#include "sp_cache.h"
#include "sql_handler.h"
#include "probes_mysql.h"
#include <mysql_com.h>
#include "lock.h"
#include "opt_trace.h"
#include "sql_analyse.h"
#include "sql_rewrite.h"
#include "transaction.h"
#include "sql_audit.h"
#include <algorithm>
Classes | |
class | Select_fetch_protocol_binary |
class | Prepared_statement |
class | Execute_sql_statement |
class | Protocol_local |
Functions | |
bool | is_param_null (const uchar *pos, ulong param_no) |
bool | is_param_long_data_type (Item_param *param) |
void | mysqld_stmt_prepare (THD *thd, const char *packet, uint packet_length) |
void | mysql_sql_stmt_prepare (THD *thd) |
void | reinit_stmt_before_use (THD *thd, LEX *lex) |
void | mysqld_stmt_execute (THD *thd, char *packet_arg, uint packet_length) |
void | mysql_sql_stmt_execute (THD *thd) |
void | mysqld_stmt_fetch (THD *thd, char *packet, uint packet_length) |
void | mysqld_stmt_reset (THD *thd, char *packet, uint packet_length) |
void | mysqld_stmt_close (THD *thd, char *packet, uint packet_length) |
void | mysql_sql_stmt_close (THD *thd) |
void | mysql_stmt_get_longdata (THD *thd, char *packet, ulong packet_length) |
This file contains the implementation of prepared statements.
When one prepares a statement:
[STMT_ID:4] [Column_count:2] [Param_count:2] [Params meta info (stubs only for now)] (if Param_count > 0) [Columns meta info] (if Column_count > 0)
During prepare the tables used in a statement are opened, but no locks are acquired. Table opening will block any DDL during the operation, and we do not need any locks as we neither read nor modify any data during prepare. Tables are closed after prepare finishes.
When one executes a statement:
[COM_STMT_EXECUTE:1] [STMT_ID:4] [NULL_BITS:(param_count+7)/8)] [TYPES_SUPPLIED_BY_CLIENT(0/1):1] [[length]data] [[length]data] .. [[length]data].
During execution of prepared statement tables are opened and locked the same way they would for normal (non-prepared) statement execution. Tables are unlocked and closed after the execution.
When one supplies long data for a placeholder:
bool is_param_long_data_type | ( | Item_param * | param | ) | [inline] |
Check whether this parameter data type is compatible with long data. Used to detect whether a long data stream has been supplied to a incompatible data type.
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.
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.
thd | thread handle |
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).
thd | thread handle |
void mysql_stmt_get_longdata | ( | THD * | thd, |
char * | packet, | ||
ulong | packet_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).
thd | Thread handle |
packet | String to append |
packet_length | Length of string (including end \0) |
void mysqld_stmt_close | ( | THD * | thd, |
char * | packet, | ||
uint | packet_length | ||
) |
Delete a prepared statement from memory.
void mysqld_stmt_execute | ( | THD * | thd, |
char * | packet_arg, | ||
uint | packet_length | ||
) |
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.
thd | current thread |
packet_arg | parameter types and data, if any |
packet_length | packet length, including the terminator character. |
void mysqld_stmt_fetch | ( | THD * | thd, |
char * | packet, | ||
uint | packet_length | ||
) |
COM_STMT_FETCH handler: fetches requested amount of rows from cursor.
thd | Thread handle |
packet | Packet from client (with stmt_id & num_rows) |
packet_length | Length of packet |
void mysqld_stmt_prepare | ( | THD * | thd, |
const char * | packet, | ||
uint | packet_length | ||
) |
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.
packet | query to be prepared |
packet_length | query string length, including ignored trailing NULL or quote char. |
void mysqld_stmt_reset | ( | THD * | thd, |
char * | packet, | ||
uint | packet_length | ||
) |
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:
thd | Thread handle |
packet | Packet with stmt id |
packet_length | length of data in packet |
void reinit_stmt_before_use | ( | THD * | thd, |
LEX * | lex | ||
) |
Reinit prepared statement/stored procedure before execution.