MySQL 9.1.0
Source Code Documentation
Prepared_statement_handle Class Reference

Prepared_statement_handle enables support for prepared statement execution. More...

#include <statement.h>

Inheritance diagram for Prepared_statement_handle:
[legend]

Public Member Functions

 Prepared_statement_handle (THD *thd, const char *query, uint length)
 
bool prepare ()
 Prepares the statement using m_query. More...
 
bool set_parameter (uint idx, bool is_null, enum_field_types type, bool is_unsigned, const void *data, unsigned long data_length, const char *name, unsigned long name_length)
 Set the parameter value in a prepared statement. More...
 
Item_paramget_parameter (size_t index)
 Get the parameter object. More...
 
bool execute () override
 Execute the statement that is prepared. More...
 
bool fetch ()
 Fetch rows from statement using cursor. More...
 
bool is_cursor_open ()
 Check if the statement uses cursor and it is open. More...
 
bool uses_cursor ()
 Check if the statement uses cursor. More...
 
bool reset ()
 Reset the statement parameters and cursors. More...
 
bool close ()
 Close the statement that is prepared. More...
 
uint get_param_count ()
 Get number of parameters used in the statement. More...
 
bool is_prepared_statement () override
 Convey that this is prepared statement. More...
 
bool is_executed_or_prepared () override
 Check if the statement has been executed or prepared. More...
 
virtual ~Prepared_statement_handle () override
 Virtual destroy for Prepared_statement_handle object. More...
 
- Public Member Functions inherited from Statement_handle
 Statement_handle (THD *thd, const char *query, size_t length)
 
bool is_error () const
 Check if error is reported. More...
 
LEX_CSTRING get_last_error ()
 Check if error is reported. More...
 
unsigned int get_last_errno () const
 Get the last mysql errno. More...
 
LEX_CSTRING get_mysql_state ()
 Get the mysql error state. More...
 
ulonglong warning_count () const
 Get number of warnings generated. More...
 
Warningget_warnings ()
 Get list of all warnings. More...
 
Result_setget_result_sets ()
 Get the next result sets generated while executing the statement. More...
 
Result_setget_current_result_set ()
 Get the current result set object. More...
 
void next_result_set ()
 Make the next result set the current result set. More...
 
void free_old_result ()
 Feel all the result collected so far, from query execution. More...
 
std::string_view get_query ()
 Get the query string being executed. More...
 
void set_capacity (size_t capacity)
 Set the capacity in bytes allowed for caching results. More...
 
size_t get_capacity ()
 Get the capacity in bytes allowed for caching results. More...
 
void set_use_thd_protocol (bool use_thd_protocol)
 Set thd protocol to enable result pass through. More...
 
bool is_using_thd_protocol () const
 Check if thd protocol is used. More...
 
void set_thd_protocol ()
 Set either Protocol_local_v2 when m_use_thd_protocol is not set or or classical protocol when m_use_thd_protocol is set to THD. More...
 
void reset_thd_protocol ()
 Reset THD protocol. More...
 
void set_expected_charset (const char *charset_name)
 Set the expected charset. More...
 
const char * get_expected_charset ()
 Get the expected charset. More...
 
size_t get_num_rows_per_fetch ()
 Get the num rows per fetch. More...
 
void set_num_rows_per_fetch (size_t num_rows_per_fetch)
 Set the num of rows to be retrieved per fetch. More...
 
virtual ~Statement_handle ()
 Destroy the Statement_handle object. More...
 

Static Public Attributes

static PSI_statement_info prepare_psi_info
 
static PSI_statement_info execute_psi_info
 
static PSI_statement_info fetch_psi_info
 
static PSI_statement_info reset_psi_info
 
static PSI_statement_info close_psi_info
 

Private Member Functions

template<typename Function >
bool run (Function exec_func, PSI_statement_info *psi_info)
 This is a wrapper function used to execute and operation on Prepared_statement. More...
 
bool internal_prepare ()
 
bool internal_execute ()
 
bool internal_fetch ()
 
bool internal_reset ()
 
bool internal_reset (bool invalidate_params)
 Reset the statement parameters and cursors. More...
 
bool internal_close ()
 
bool enable_cursor ()
 Method to enable cursor. More...
 
bool create_parameter_buffers ()
 Create a parameter buffers. More...
 

Private Attributes

Prepared_statementm_stmt {nullptr}
 
PS_PARAMm_parameters {nullptr}
 
MEM_ROOT m_parameter_mem_root
 
ulong * m_parameter_buffer_max {nullptr}
 
bool m_bound_new_parameter_types {true}
 

Additional Inherited Members

- Protected Member Functions inherited from Statement_handle
void send_statement_status ()
 Send statement execution status after execute(). More...
 
void add_result_set (Result_set *result_set)
 
void set_result_set (Result_set *result_set)
 
void copy_warnings ()
 Copy the warnings generated for the query from the diagnostics area. More...
 
 Statement_handle (const Statement_handle &)=delete
 
 Statement_handle (Statement_handle &&)=delete
 
Statement_handleoperator= (const Statement_handle &)=delete
 
Statement_handleoperator= (Statement_handle &&)=delete
 
- Protected Attributes inherited from Statement_handle
std::string m_query
 
MEM_ROOT m_warning_mem_root
 
Warningm_warnings
 
ulonglong m_warnings_count = 0
 
Diagnostics_aream_diagnostics_area
 
THDm_thd
 
Result_setm_result_sets
 
Result_setm_current_rset
 
bool m_use_thd_protocol = false
 
size_t m_num_rows_per_fetch = 1
 
CHARSET_INFOm_expected_charset
 
Protocol_local_v2 m_protocol
 
Protocolm_saved_protocol {nullptr}
 

Detailed Description

Prepared_statement_handle enables support for prepared statement execution.

Supports parameters and cursors.

Constructor & Destructor Documentation

◆ Prepared_statement_handle()

Prepared_statement_handle::Prepared_statement_handle ( THD thd,
const char *  query,
uint  length 
)
inline

◆ ~Prepared_statement_handle()

virtual Prepared_statement_handle::~Prepared_statement_handle ( )
inlineoverridevirtual

Virtual destroy for Prepared_statement_handle object.

Member Function Documentation

◆ close()

bool Prepared_statement_handle::close ( void  )

Close the statement that is prepared.

Returns
true
false

◆ create_parameter_buffers()

bool Prepared_statement_handle::create_parameter_buffers ( )
private

Create a parameter buffers.

Returns
true on failure
false on success

◆ enable_cursor()

bool Prepared_statement_handle::enable_cursor ( )
private

Method to enable cursor.

Returns
true if cursor can be used for a statement.
false if cursor can not be used for a statement.

◆ execute()

bool Prepared_statement_handle::execute ( )
overridevirtual

Execute the statement that is prepared.

If a statement is not yet prepared, we fail.

If a statement was already in EXECUTED state, we close the cursor and execute the statement again.

Returns
true
false

Implements Statement_handle.

◆ fetch()

bool Prepared_statement_handle::fetch ( )

Fetch rows from statement using cursor.

Not all statement uses cursor. Check is_cursor_open() and then invoke this call.

Attempt to call fetch rows without preparing or executing the statement will cause failure.

Attempt to call fetch() without cursor in use, will cause failure.

Returns
true
false

◆ get_param_count()

uint Prepared_statement_handle::get_param_count ( )
inline

Get number of parameters used in the statement.

This should be called after preparing a statement.

Returns
uint

◆ get_parameter()

Item_param * Prepared_statement_handle::get_parameter ( size_t  index)

Get the parameter object.

Parameters
indexof the parameter
Returns
Item_param*

◆ internal_close()

bool Prepared_statement_handle::internal_close ( )
private

◆ internal_execute()

bool Prepared_statement_handle::internal_execute ( )
private

◆ internal_fetch()

bool Prepared_statement_handle::internal_fetch ( )
private

◆ internal_prepare()

bool Prepared_statement_handle::internal_prepare ( )
private

◆ internal_reset() [1/2]

bool Prepared_statement_handle::internal_reset ( )
private

◆ internal_reset() [2/2]

bool Prepared_statement_handle::internal_reset ( bool  invalidate_params)
private

Reset the statement parameters and cursors.

Parameters
invalidate_paramsWhen set to true parameters are invalidated.
Returns
true on failure.
false on execute.

◆ is_cursor_open()

bool Prepared_statement_handle::is_cursor_open ( )
inline

Check if the statement uses cursor and it is open.

If the API is called without preparing the statement will result in 'false'

Returns
true if cursor is in use
false if cursor not in use.

◆ is_executed_or_prepared()

bool Prepared_statement_handle::is_executed_or_prepared ( )
inlineoverridevirtual

Check if the statement has been executed or prepared.

Returns
true if executed or prepared
false otherwise

Implements Statement_handle.

◆ is_prepared_statement()

bool Prepared_statement_handle::is_prepared_statement ( )
inlineoverridevirtual

Convey that this is prepared statement.

Returns
Always returns true

Implements Statement_handle.

◆ prepare()

bool Prepared_statement_handle::prepare ( )

Prepares the statement using m_query.

If the statement is already in executing mode, close the cursor deallocate the previous statement and start preparing new statement using m_query.

Returns
true if fails.
false if succeeds.

◆ reset()

bool Prepared_statement_handle::reset ( void  )

Reset the statement parameters and cursors.

Invoking this API will close the cursor in use. This is invoked generally before executing the statement for the second time, after prepare.

Returns
true
false

◆ run()

template<typename Function >
bool Prepared_statement_handle::run ( Function  exec_func,
PSI_statement_info psi_info 
)
private

This is a wrapper function used to execute and operation on Prepared_statement.

This takes case of using relevant protocol, diagnostic area, backup the current query arena before executing the prepared statement operation.

Template Parameters
Functiontype of function to run
Parameters
exec_funcfunction to run
psi_infoPSI_statement_info instance.
Returns
true
false

◆ set_parameter()

bool Prepared_statement_handle::set_parameter ( uint  idx,
bool  is_null,
enum_field_types  type,
bool  is_unsigned,
const void *  data,
unsigned long  data_length,
const char *  name,
unsigned long  name_length 
)

Set the parameter value in a prepared statement.

Parameters
idxIndex of '?' in prepared statement.
is_nullSet parameter to NULL value.
typeSet the parameters field type.
is_unsignedMark parameter as unsigned.
dataPointer to buffer containing the value.
data_lengthLength of buffer 'data'
nameName of parameter (mostly unused)
name_lengthLength of 'name'
Returns
true if fails.
false if succeeds.

◆ uses_cursor()

bool Prepared_statement_handle::uses_cursor ( )
inline

Check if the statement uses cursor.

If the API is called without executing the statement will result in 'false'

Returns
true if statement uses cursor.
false if statement does not uses cursor.

Member Data Documentation

◆ close_psi_info

PSI_statement_info Prepared_statement_handle::close_psi_info
static
Initial value:
= {
0, "close", 0, "Prepared Statement Handle: CLOSE Statement"}

◆ execute_psi_info

PSI_statement_info Prepared_statement_handle::execute_psi_info
static
Initial value:
= {
0, "execute", 0, "Prepared Statement Handle: EXECUTE Statement"}

◆ fetch_psi_info

PSI_statement_info Prepared_statement_handle::fetch_psi_info
static
Initial value:
= {
0, "fetch", 0, "Prepared Statement Handle: FETCH cursor"}

◆ m_bound_new_parameter_types

bool Prepared_statement_handle::m_bound_new_parameter_types {true}
private

◆ m_parameter_buffer_max

ulong* Prepared_statement_handle::m_parameter_buffer_max {nullptr}
private

◆ m_parameter_mem_root

MEM_ROOT Prepared_statement_handle::m_parameter_mem_root
private

◆ m_parameters

PS_PARAM* Prepared_statement_handle::m_parameters {nullptr}
private

◆ m_stmt

Prepared_statement* Prepared_statement_handle::m_stmt {nullptr}
private

◆ prepare_psi_info

PSI_statement_info Prepared_statement_handle::prepare_psi_info
static
Initial value:
= {
0, "prepare", 0, "Prepared Statement Handle: PREPARE Statement"}

◆ reset_psi_info

PSI_statement_info Prepared_statement_handle::reset_psi_info
static
Initial value:
= {
0, "reset", 0, "Prepared Statement Handle: RESET Statement"}

The documentation for this class was generated from the following files: