My Project
Classes | Functions
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 "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)

Detailed Description

This file contains the implementation of prepared statements.

When one prepares a statement:

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:

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:


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.

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
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
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
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)
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.
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.
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
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.
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
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.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines