MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sql_prepare.cc File Reference
#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 <algorithm>
Include dependency graph for sql_prepare.cc:

Go to the source code of this file.

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)

Detailed Description

This file contains the implementation of prepared statements.

When one prepares a statement:

  • Server gets the query from client with command 'COM_STMT_PREPARE'; in the following format: [COM_STMT_PREPARE:1] [query]
  • Parse the query and recognize any parameter markers '?' and store its information list in lex->param_list
  • Allocate a new statement for this prepare; and keep this in 'thd->stmt_map'.
  • Without executing the query, return back to client the total number of parameters along with result-set metadata information (if any) in the following format:
        [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:

  • Server gets the command 'COM_STMT_EXECUTE' to execute the previously prepared query. If there are any parameter markers, then the client will send the data in the following format:
        [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].
    (Note: Except for string/binary types; all other types will not be supplied with length field)
  • If it is a first execute or types of parameters were altered by client, then setup the conversion routines.
  • Assign parameter items from the supplied data.
  • Execute the query without re-parsing and send back the results to client

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:

  • Server gets the long data in pieces with command type 'COM_STMT_SEND_LONG_DATA'.
  • The packet recieved will have the format as: [COM_STMT_SEND_LONG_DATA:1][STMT_ID:4][parameter_number:2][data]
  • data from the packet is appended to the long data value buffer for this placeholder.
  • It's up to the client to stop supplying data chunks at any point. The server doesn't care; also, the server doesn't notify the client whether it got the data or not; if there is any error, then it will be returned at statement execute.

Definition in file sql_prepare.cc.

Function Documentation

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.

Definition at line 841 of file sql_prepare.cc.

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.

Returns
none: OK packet is sent in case of success, otherwise an error message is set in THD

Definition at line 2935 of file sql_prepare.cc.

Here is the call graph for this function:

Here is the caller graph for this function:

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.

Parameters
thdthread handle
Returns
none: in case of success, OK (or result set) packet is sent to the client, otherwise an error is set in THD

Definition at line 2730 of file sql_prepare.cc.

Here is the caller graph for this function:

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).

Parameters
thdthread handle
Returns
none: in case of success, OK packet is sent to the client, otherwise an error message is set in THD

Definition at line 2425 of file sql_prepare.cc.

Here is the call graph for this function:

Here is the caller graph for this function:

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).

Parameters
thdThread handle
packetString to append
packet_lengthLength of string (including end \0)

Definition at line 2969 of file sql_prepare.cc.

Here is the caller graph for this function:

void mysqld_stmt_close ( THD *  thd,
char *  packet,
uint  packet_length 
)

Delete a prepared statement from memory.

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

Definition at line 2891 of file sql_prepare.cc.

Here is the call graph for this function:

Here is the caller graph for this function:

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.

Parameters
thdcurrent thread
packet_argparameter types and data, if any
packet_lengthpacket length, including the terminator character.
Returns
none: in case of success OK packet or a result set is sent to the client, otherwise an error message is set in THD.

Definition at line 2657 of file sql_prepare.cc.

Here is the call graph for this function:

Here is the caller graph for this function:

void mysqld_stmt_fetch ( THD *  thd,
char *  packet,
uint  packet_length 
)

COM_STMT_FETCH handler: fetches requested amount of rows from cursor.

Parameters
thdThread handle
packetPacket from client (with stmt_id & num_rows)
packet_lengthLength of packet

Definition at line 2769 of file sql_prepare.cc.

Here is the call graph for this function:

Here is the caller graph for this function:

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.

Parameters
packetquery to be prepared
packet_lengthquery string length, including ignored trailing NULL or quote char.
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.
Returns
none: in case of success a new statement id and metadata is sent to the client, otherwise an error message is set in THD.

Definition at line 2278 of file sql_prepare.cc.

Here is the call graph for this function:

Here is the caller graph for this function:

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:

  • 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
packetPacket with stmt id
packet_lengthlength of data in packet

Definition at line 2840 of file sql_prepare.cc.

Here is the call graph for this function:

Here is the caller graph for this function:

void reinit_stmt_before_use ( THD *  thd,
LEX *  lex 
)

Reinit prepared statement/stored procedure before execution.

Todo:
When the new table structure is ready, then have a status bit to indicate the table is altered, and re-do the setup_* and open the tables back.

Definition at line 2490 of file sql_prepare.cc.