MySQL 8.0.40
Source Code Documentation
protocol_classic.cc File Reference

Low level functions for storing data to be send to the MySQL client. More...

#include "sql/protocol_classic.h"
#include <openssl/ssl.h>
#include <string.h>
#include <algorithm>
#include <limits>
#include "decimal.h"
#include "lex_string.h"
#include "m_ctype.h"
#include "m_string.h"
#include "my_byteorder.h"
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_inttypes.h"
#include "my_loglevel.h"
#include "my_sys.h"
#include "my_time.h"
#include "mysql/com_data.h"
#include "mysql/psi/mysql_socket.h"
#include "mysqld_error.h"
#include "mysys_err.h"
#include "sql/field.h"
#include "sql/item.h"
#include "sql/item_func.h"
#include "sql/my_decimal.h"
#include "sql/mysqld.h"
#include "sql/session_tracker.h"
#include "sql/sql_class.h"
#include "sql/sql_error.h"
#include "sql/sql_lex.h"
#include "sql/sql_list.h"
#include "sql/sql_prepare.h"
#include "sql/system_variables.h"
#include "sql_string.h"
#include "template_utils.h"

Functions

static bool net_send_error_packet (THD *, uint, const char *, const char *)
 
static bool net_send_error_packet (NET *, uint, const char *, const char *, bool, ulong, const CHARSET_INFO *)
 
static bool write_eof_packet (THD *thd, NET *net, uint server_status, uint statement_warn_count)
 Format EOF packet according to the current protocol and write it to the network output buffer. More...
 
static ulong get_ps_param_len (enum enum_field_types type, uchar *packet, ulong packet_left_len, ulong *header_len, bool *err)
 Returns the length of the encoded data. More...
 
static bool ensure_packet_capacity (size_t length, String *packet)
 Ensures that the packet buffer has enough capacity to hold a string of the given length. More...
 
static bool net_store_data (const uchar *from, size_t length, String *packet)
 Store length and data in a network packet buffer. More...
 
static bool net_store_zero_padded_data (const char *data, size_t data_length, size_t padded_length, String *packet)
 Stores a string in the network buffer. More...
 
bool net_send_error (THD *thd, uint sql_errno, const char *err)
 Send a error string to client. More...
 
bool net_send_error (NET *net, uint sql_errno, const char *err)
 Send a error string to client using net struct. More...
 
static bool net_send_ok (THD *thd, uint server_status, uint statement_warn_count, ulonglong affected_rows, ulonglong id, const char *message, bool eof_identifier)
 Return OK to the client. More...
 
static bool net_send_eof (THD *thd, uint server_status, uint statement_warn_count)
 Send eof (= end of result set) to the client. More...
 
static ucharnet_store_length_fast (uchar *packet, size_t length)
 Faster net_store_length when we know that length is less than 65536. More...
 
ucharnet_store_data (uchar *to, const uchar *from, size_t length)
 
static bool parse_query_bind_params (THD *thd, uint param_count, PS_PARAM **out_parameters, unsigned char *out_has_new_types, unsigned long *out_parameter_count, Prepared_statement *stmt_data, uchar **inout_read_pos, size_t *inout_packet_left, bool receive_named_params, bool receive_parameter_set_count)
 
bool store (Protocol *prot, I_List< i_string > *str_list)
 Send a set of strings as one long string with ',' in between. More...
 
static bool store_integer (int64 value, bool unsigned_flag, uint32 zerofill, String *packet)
 Stores an integer in the protocol buffer for the text protocol. More...
 
static size_t floating_point_to_text (double value, uint32 decimals, my_gcvt_arg_type gcvt_arg_type, char *buffer)
 Converts a floating-point value to text for the text protocol. More...
 
static bool store_floating_point (double value, uint32 decimals, uint32 zerofill, my_gcvt_arg_type gcvt_arg_type, String *packet)
 Stores a floating-point value in the text protocol. More...
 
template<typename ToString >
static bool store_temporal (ToString to_string, String *packet)
 Stores a temporal value in the protocol buffer for the text protocol. More...
 
static ulong get_param_length (uchar *packet, ulong packet_left_len, ulong *header_len)
 Read the length of the parameter data and return it back to the caller. More...
 

Variables

static const unsigned int PACKET_BUFFER_EXTRA_ALLOC = 1024
 
static uchar eof_buff [1] = {(uchar)254}
 

Detailed Description

Low level functions for storing data to be send to the MySQL client.

The actual communication is handled by the net_xxx functions in net_serv.cc

Function Documentation

◆ ensure_packet_capacity()

static bool ensure_packet_capacity ( size_t  length,
String packet 
)
static

Ensures that the packet buffer has enough capacity to hold a string of the given length.

Parameters
lengththe length of the string
[in,out]packetthe buffer
Returns
true if memory could not be allocated, false on success

◆ floating_point_to_text()

static size_t floating_point_to_text ( double  value,
uint32  decimals,
my_gcvt_arg_type  gcvt_arg_type,
char *  buffer 
)
static

Converts a floating-point value to text for the text protocol.

Parameters
valuethe floating point value
decimalsthe precision of the value
gcvt_arg_typethe type of the floating-point value
buffera buffer large enough to hold FLOATING_POINT_BUFFER characters plus a terminating zero character
Returns
the length of the text representation of the value

◆ get_param_length()

static ulong get_param_length ( uchar packet,
ulong  packet_left_len,
ulong *  header_len 
)
static

Read the length of the parameter data and return it back to the caller.

Parameters
packeta pointer to the data
packet_left_lenremaining packet length
header_lensize of the header stored at the beginning of the packet and used to specify the length of the data.
Returns
Length of data piece.

◆ get_ps_param_len()

static ulong get_ps_param_len ( enum enum_field_types  type,
uchar packet,
ulong  packet_left_len,
ulong *  header_len,
bool *  err 
)
static

Returns the length of the encoded data.

Parameters
[in]typeparameter data type
[in]packetnetwork buffer
[in]packet_left_lennumber of bytes left in packet
[out]header_lenthe size of the header(bytes to be skipped)
[out]errboolean to store if an error occurred

◆ net_send_eof()

static bool net_send_eof ( THD thd,
uint  server_status,
uint  statement_warn_count 
)
static

Send eof (= end of result set) to the client.

See EOF_Packet packet for the structure of the packet.

note The warning count will not be sent if 'no_flush' is set as we don't want to report the warning count until all data is sent to the client.

Parameters
thdThread handler
server_statusThe server status
statement_warn_countTotal number of warnings
Return values
falseThe message was successfully sent
trueAn error occurred and the message wasn't sent properly

◆ net_send_error() [1/2]

bool net_send_error ( NET net,
uint  sql_errno,
const char *  err 
)

Send a error string to client using net struct.

This is used initial connection handling code.

Parameters
netLow-level net struct
sql_errnoThe error code to send
errA pointer to the error message
Return values
falseThe message was sent to the client
trueAn error occurred and the message wasn't sent properly

◆ net_send_error() [2/2]

bool net_send_error ( THD thd,
uint  sql_errno,
const char *  err 
)

Send a error string to client.

Design note:

net_printf_error and net_send_error are low-level functions that shall be used only when a new connection is being established or at server startup.

For SIGNAL/RESIGNAL and GET DIAGNOSTICS functionality it's critical that every error that can be intercepted is issued in one place only, my_message_sql.

Parameters
thdThread handler
sql_errnoThe error code to send
errA pointer to the error message
Return values
falseThe message was sent to the client
trueAn error occurred and the message wasn't sent properly

◆ net_send_error_packet() [1/2]

static bool net_send_error_packet ( NET net,
uint  sql_errno,
const char *  err,
const char *  sqlstate,
bool  bootstrap,
ulong  client_capabilities,
const CHARSET_INFO character_set_results 
)
static
Parameters
netLow-level NET struct
sql_errnoThe error code to send
errA pointer to the error message
sqlstateSQL state
bootstrapServer is started in bootstrap mode
client_capabilitiesClient capabilities flag
character_set_resultsChar set info
Return values
falseThe message was successfully sent
trueAn error occurred and the messages wasn't sent properly

See also ERR_Packet

◆ net_send_error_packet() [2/2]

static bool net_send_error_packet ( THD thd,
uint  sql_errno,
const char *  err,
const char *  sqlstate 
)
static
Parameters
thdThread handler
sql_errnoThe error code to send
errA pointer to the error message
sqlstateSQL state
Return values
falseThe message was successfully sent
trueAn error occurred and the messages wasn't sent properly

See also ERR_Packet

◆ net_send_ok()

static bool net_send_ok ( THD thd,
uint  server_status,
uint  statement_warn_count,
ulonglong  affected_rows,
ulonglong  id,
const char *  message,
bool  eof_identifier 
)
static

Return OK to the client.

See OK_Packet for the OK packet structure.

Parameters
thdThread handler
server_statusThe server status
statement_warn_countTotal number of warnings
affected_rowsNumber of rows changed by statement
idAuto_increment id for first row (if used)
messageMessage to send to the client (Used by mysql_status)
eof_identifierwhen true [FE] will be set in OK header else [00] will be used
Return values
falseThe message was successfully sent
trueAn error occurred and the messages wasn't sent properly

◆ net_store_data() [1/2]

static bool net_store_data ( const uchar from,
size_t  length,
String packet 
)
inlinestatic

Store length and data in a network packet buffer.

Parameters
fromthe data to store
lengththe length of the data
[in,out]packetthe buffer
Returns
true if there is not enough memory, false on success

◆ net_store_data() [2/2]

uchar * net_store_data ( uchar to,
const uchar from,
size_t  length 
)

◆ net_store_length_fast()

static uchar * net_store_length_fast ( uchar packet,
size_t  length 
)
static

Faster net_store_length when we know that length is less than 65536.

We keep a separate version for that range because it's widely used in libmysql.

uint is used as argument type because of MySQL type conventions:

  • uint for 0..65536
  • ulong for 0..4294967296
  • ulonglong for bigger numbers.

◆ net_store_zero_padded_data()

static bool net_store_zero_padded_data ( const char *  data,
size_t  data_length,
size_t  padded_length,
String packet 
)
static

Stores a string in the network buffer.

The string is padded with zeros if it is shorter than the specified padded length.

Parameters
datathe string to store
data_lengththe length of the string
padded_lengththe length of the zero-padded string
[in,out]packetthe network buffer

◆ parse_query_bind_params()

static bool parse_query_bind_params ( THD thd,
uint  param_count,
PS_PARAM **  out_parameters,
unsigned char *  out_has_new_types,
unsigned long *  out_parameter_count,
Prepared_statement stmt_data,
uchar **  inout_read_pos,
size_t *  inout_packet_left,
bool  receive_named_params,
bool  receive_parameter_set_count 
)
static

◆ store()

bool store ( Protocol prot,
I_List< i_string > *  str_list 
)

Send a set of strings as one long string with ',' in between.

◆ store_floating_point()

static bool store_floating_point ( double  value,
uint32  decimals,
uint32  zerofill,
my_gcvt_arg_type  gcvt_arg_type,
String packet 
)
static

Stores a floating-point value in the text protocol.

Parameters
valuethe floating point value
decimalsthe precision of the value
zerofillthe length up to which the value should be zero-padded, or 0 if no zero-padding should be used
gcvt_arg_typethe type of the floating-point value
packetthe destination buffer
Returns
false on success, true on error

◆ store_integer()

static bool store_integer ( int64  value,
bool  unsigned_flag,
uint32  zerofill,
String packet 
)
static

Stores an integer in the protocol buffer for the text protocol.

Parameters
valuethe integer value to convert to a string
unsigned_flagtrue if the integer is unsigned
zerofillthe length up to which the value should be zero-padded
packetthe destination buffer
Returns
false on success, true on error

◆ store_temporal()

template<typename ToString >
static bool store_temporal ( ToString  to_string,
String packet 
)
static

Stores a temporal value in the protocol buffer for the text protocol.

Parameters
to_stringthe function that converts the temporal value to a string
packetthe destination buffer
Returns
false on success, true on error

◆ write_eof_packet()

static bool write_eof_packet ( THD thd,
NET net,
uint  server_status,
uint  statement_warn_count 
)
static

Format EOF packet according to the current protocol and write it to the network output buffer.

See also ERR_Packet

Parameters
thdThe thread handler
netThe network handler
server_statusThe server status
statement_warn_countThe number of warnings
Return values
falseThe message was sent successfully
trueAn error occurred and the messages wasn't sent properly

Variable Documentation

◆ eof_buff

uchar eof_buff[1] = {(uchar)254}
static

◆ PACKET_BUFFER_EXTRA_ALLOC

const unsigned int PACKET_BUFFER_EXTRA_ALLOC = 1024
static