arrow-left

All pages
gitbookPowered by GitBook
1 of 10

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

COM_STMT_BULK_EXECUTE

This command executes a bulk insert for a previously prepared statement, using a compact binary format for efficiency.

Executes a bulk insert of a previously prepared statement.

A command that returns a result set returns an error (Error packet).

hashtag
Direction

Client to server.

hashtag
Fields

  • 0xfa : COM_STMT_BULK_EXECUTE header.

  • statement id.

  • .

hashtag
Flags

hashtag
Bulk Flags

Flag
Value
Details

hashtag
Parameter Type Flag

hashtag
Parameter Indicator

hashtag
Response

The server can answer with 3 different responses:

  • 0xff: if any errors occur.

  • 0x00: when query execution works without result set.

  • A result set containing affected rows and auto-increment IDs when bulk flag SEND_UNIT_RESULTS is set.

This page is licensed: CC BY-SA / Gnu FDL

COM_STMT_CLOSE

This command deallocates a prepared statement on the server, freeing up associated resources.

Closes a previously prepared statement.

hashtag
Direction

Client to server.

hashtag
Implemented by

  • .

  • .

hashtag
Fields

  • 0x19 COM_STMT_CLOSE header.

  • statement id.

hashtag
Example

hashtag
Response

No response from server.

This page is licensed: CC BY-SA / Gnu FDL

COM_STMT_FETCH

This command fetches rows from an existing result set of a prepared statement that was executed with a cursor.

Fetch rows from a prepared statement.

A COM_STMT_EXECUTE with a non-zero cursor flag must have been successfully executed before any COM_STMT_FETCH commands can be executed.

hashtag
Fields

  • 0x1C COM_STMT_FETCH header.

  • statement id.

  • number of rows to fetch.

hashtag
Response

Returns one or more followed by an .

This page is licensed: CC BY-SA / Gnu FDL

int<1>
int<4>
int<4>
binary result set rows
EOF packet
spinner

3 - Binary Protocol (Prepared Statements)

Understand the binary protocol for prepared statements. This section details how prepared statements are exchanged efficiently between client and server, optimizing performance and security.

int<1>
int<4>
spinner
mysql_stmt_close()
mysql_stmt_prepare()
05 00 00 00 19 04 00 00 00

If (bulk_flag & SEND_TYPES_TO_SERVER):

  • For each parameter:

    • byte<1>: field type.

    • byte<1>: .

  • Until end of packet:

    • For each parameter (i.e param_count times):

      • byte<1>: parameter indicator.

      • If indicator == NONE:

        • byte<n> : binary parameter value.

  • IGNORE

    Value is default for insert, Is ignored for update

    SEND_UNIT_RESULTS

    64

    Return generated affected rows and auto-increment IDs as a result set (only when server supports MARIADB_CLIENT_BULK_UNIT_RESULTS capability).

    SEND_TYPES_TO_SERVER

    128

    Send types to server.

    128

    unsigned

    0

    NONE

    Value follow

    1

    NULL

    Value is null

    2

    DEFAULT

    For INSERT/UPDATE, value is default

    int<1>
    int<4>
    int<2>
    bulk flags
    ERR_Packet
    OK_packet
    spinner

    3

    parameter type flag

    COM_STMT_SEND_LONG_DATA

    This command sends long data, such as BLOB or TEXT values, in chunks for a specific parameter of a prepared statement.

    When data for a specific column is large, it can be sent separately to avoid the limitation of max_allowed_packet (see packet splitting).

    Multiple COM_STMT_SEND_LONG_DATA commands with the same column ID append the data. COM_STMT_SEND_LONG_DATA is sent before COM_STMT_EXECUTE.

    hashtag
    Fields

    • 0x18 COM_STMT_SEND_LONG_DATA header.

    • statement id.

    • parameter number.

    hashtag
    Response

    Server doesn't send response.

    This page is licensed: CC BY-SA / Gnu FDL

    data.

    int<1>
    int<4>
    int<2>
    spinner
    byte<EOF>

    COM_STMT_EXECUTE

    This command executes a prepared statement using parameter values provided in the binary protocol format.

    Executes a previously prepared statement.

    If specific data is large, it can be sent separately prior to this command (see COM_STMT_SEND_LONG_DATA).

    If a statement is re-executed without changing the bind types, the types do not need to be sent to the server again.

    hashtag
    Direction

    Client to server.

    hashtag
    Fields

    • 0x17 : COM_STMT_EXECUTE header.

    • .

    • flags.

    hashtag
    Statement ID

    Statement ID is the identifier of the prepared statement (from ).

    hashtag
    Specific "-1" Statement ID Value

    Value -1 (0xFFFFFFFF) can be used to indicate to use the last statement prepared on current connection if no COM_STMT_PREPARE has failed since.

    This permit pipelining :

    • Send COM_STMT_PREPARE + COM_STMT_EXECUTE with statement ID -1.

    • Read COM_STMT_PREPARE + COM_STMT_EXECUTE response.

    In case COM_STMT_PREPARE returns an error, COM_STMT_EXECUTE returns an error that statement ID -1 is unknown. This permits to avoid much of the network latency.

    hashtag
    Flag

    hashtag
    Cursors

    If the flags of the COM_STMT_EXECUTE request a cursor to be opened, the returned result only contains the column definitions and the EOF that terminates it, and the result set rows are fetched using separate COM_STMT_FETCH commands.

    Whether a cursor is actually opened is indicated by the SERVER_STATUS_CURSOR_EXISTS bit in the first EOF packet in the response to the COM_STMT_EXECUTE. If it is not set in, no cursor is opened and a normal result set is returned.

    hashtag
    Parameter Flag

    Parameter type flag byte:

    hashtag
    NULL-Bitmap

    The NULL-Bitmap indicates if parameters are null (one bit per parameter). If the parameter is NULL, the bit is set in the bitmap and the parameter value is not sent.

    The size in bytes of the NULL-bitmap can be calculated with: (parameter number + 7) / 8

    hashtag
    Binary Parameter Encoding

    The encoding of the COM_STMT_EXECUTE parameters are the same as the encoding of the .

    hashtag
    Response

    The server can answer with 3 different responses:

    • 0xff: if any errors occur.

    • 0x00: when query execution works without result set.

    • One (or more) , when query execution return rows (in case of SELECT query, for example).

    This page is licensed: CC BY-SA / Gnu FDL

    int<4> Iteration count (always 1).

  • If (param_count > 0)

    • byte<(param_count + 7)/8> null bitmap.

    • byte<1>: send type to server (0 / 1).

    • If (send type to server):

      • For each parameter:

        • byte<1>: .

        • byte<1>: .

    • For each parameter (for instance, param_count times):

      • If parameter is not null:

        • byte .

  • 0

    no cursor

    1

    read only

    2

    cursor for update

    4

    scrollable cursor

    128

    unsigned

    int<1>
    int<4>
    statement ID
    int<1>
    COM_STMT_PREPARE answer
    binary result sets
    ERR_Packet
    OK_packet
    result set
    spinner
    field type
    parameter flag
    binary parameter value

    Server Response Packets (Binary Protocol)

    This section details the structure of response packets sent by the server when using the binary protocol, particularly for result sets.

    The following response packets will be sent from the server to client in binary protocol only. They may be mixed with packets from the text protocol, like , PACKET_METADATA or .

    OK_PACKETarrow-up-right
    ERR_PACKETarrow-up-right
    PACKET_BINDATAchevron-right

    PACKET_BINDATA

    This packet type in the binary protocol transfers result set row data in a compact binary format, determined by the column types returned in metadata.

    In contrast to the text protocol, the binary protocol transfers data according to the format of the field types returned in PACKET_METADATA.

    Field type
    Representation

    MYSQL_TYPE_BIT

    str_LEC

    MYSQL_TYPE_BLOB

    hashtag
    Fields

    This page is licensed: CC BY-SA / Gnu FDL

    str_LEC

    MYSQL_TYPE_DATE MYSQL_TYPE_DATETIME MYSQL_TYPE_TIMESTAMP

    int_11 (default)int_7 (no microseconds)int_4 (no time values)int_0 (no values)

    MYSQL_TYPE_DECIMAL

    str_LEC

    MYSQL_TYPE_DOUBLE

    int_8

    MYSQL_TYPE_ENUM

    str_LEC

    MYSQL_TYPE_FLOAT

    int_4

    MYSQL_TYPE_GEOMETRY

    str_LEC

    MYSQL_TYPE_INT24

    int_4

    MYSQL_TYPE_JSON

    str_LEC

    MYSQL_TYPE_LONGLONG

    int_8

    MYSQL_TYPE_LONG_BLOB

    str_LEC

    MYSQL_TYPE_LONG

    int_4

    MYSQL_TYPE_MEDIUM_BLOB

    str_LEC

    MYSQL_TYPE_NEWDECIMAL

    str_LEC

    MYSQL_TYPE_NULL

    stored in bitmap

    MYSQL_TYPE_SET

    str_LEC

    MYSQL_TYPE_STRING

    str_LEC

    MYSQL_TYPE_SHORT

    int_2

    MYSQL_TYPE_TINY_BLOB

    str_LEC

    MYSQL_TYPE_TINY

    int_1

    MYSQL_TYPE_VARCHAR

    str_LEC

    MYSQL_TYPE_VAR_STRING

    str_LEC

    MYSQL_TYPE_YEAR

    int_4

    spinner
    (column_count + 7)/8   null bitmap
    while (!eof) {
      for (i=0; i < column_count; i++)
      {
        data (length depends on the data type)
      }
    }

    COM_STMT_RESET

    This command resets the data of a prepared statement on the server, clearing any buffers or previous parameter values.

    Resets a prepared statement on the client and server to state after preparing.

    hashtag
    Fields

    • int<1> 0x1A COM_STMT_RESET header.

    • Statement ID.

    hashtag
    Response

    or .

    hashtag
    Example

    This page is licensed: CC BY-SA / Gnu FDL

    int<4>
    ERR_Packet
    OK_Packet
    spinner
    05 00 00 00 1A 04 00 00 00

    COM_STMT_PREPARE

    This command prepares an SQL statement on the server, returning a statement ID and metadata about parameters and columns.

    Prepares a statement on the server.

    circle-info

    Not all statements can be prepared. See PREPARE for a list of statements that can be prepared.

    hashtag
    Implemented by

    hashtag
    Fields

    • 0x16 COM_STMT_PREPARE header

    • SQL Statement

    hashtag
    Example

    hashtag
    Response

    If something goes wrong, the server sends an . If the command succeeds, different packets are received:

    • .

    • If number of parameters (count of ? placeholders) > 0:

      • For each parameter:

    hashtag
    COM_STMT_PREPARE_OK

    • 0x00 COM_STMT_PREPARE_OK header.

    • statement ID.

    • number of columns in the returned result set (or 0 if statement does not return result set).

    This page is licensed: CC BY-SA / Gnu FDL

    column definition packet.

  • If !DEPRECATE_EOF eof_packet.

  • If prepared statement returns result set and number of result set columns > 0:

    • For each column:

      • column definition packet.

    • If !DEPRECATE_EOF .

  • int<2> number of prepared statement parameters ('?' placeholders).

  • string<1> -not used-

  • int<2> number of warnings.

  • 1F 00 00 00 16 53 45 4C 45 43 54 20 2A 20 46 52  .....SELECT * FR
    1F 4D 20 74 65 73 74 5F 62 69 6E 64 5F 72 65 73  OM test_bind_res
    75 6C 74                                         ult
    int<1>
    string<EOF>
    ERR_Packet
    COM_STMT_PREPARE_OK
    int<1>
    int<4>
    int<2>
    spinner
    mariadb_stmt_execute_direct()
    mysql_stmt_prepare()
    eof_packet