Broad Network


Command Packet for PurePerl MySQL API

Developing a PurePerl MySQL API – Part 3

Writing a Perl Module

Foreword: In this part of the series I explain the command packet code for PurePerl MySQL API.

By: Chrysanthus Date Published: 28 Jan 2015

Introduction

This is part 3 of my series, Developing a PurePerl MySQL API. In this part of the series I talk about the MySQL protocol command packet. PurePerl stands for Pure Perl, meaning Perl software without any C software underneath. You should have read the previous part of the series before reaching here, as this is a continuation.

A packet is a string of bytes. It begins with a 4-byte header segment followed by a body segment. In this article I talk only about the body segment since I have already talked about the header segment, which is common to all the packets, in the previous series.

The Command Packet
Once the login (authentication phase) is complete, the client begins sending commands to the server using command packets. The body of the command packet is simple: it consists of two fields. The first field is 1 byte which is a decimal code (equivalent) indicating the instruction. For example, to change the database has one number; a normal query has a different number. The second field is called the argument. The argument can be the SQL statement without the ending semicolon. It can even be of zero length. For the compressed packet it is the argument that is compressed. For the uncompressed packet the argument is not compressed. The decimal numbers and their argument descriptions are given in the following table:
Table 3.1. Client Commands
Code
numeric
value
Argument DescriptionCommand Description
0No argument.Never sent by a client. Reserved for internal use.
1No argument.Tells the server to end the session.
2A string containing the name of the database.Tells the server to change the default database for the session to the one specified by the argument.
3A string containing the query.Tells the server to run the query.
4A string containing the name of the table.Tells the server to return a list of fields for the specified table. This is an obsolete command still supported on the server for compatibility with old clients. Newer clients use the SHOW FIELDS query.
5A string containing the name of the databaseTells the server to create a database with the specified name. This is an obsolete command still supported on the server for compatibility with old clients. Newer clients use the CREATE DATABASE query.
6A string containing the name of the database.Tells the server to drop the database with the specified name. This is an obsolete command still supported on the server for compatibility with old clients. Newer clients use the DROP DATABASE query.
7A byte containing the bit mask of reloading operations.Tells the server to refresh the table cache, rotate the logs, reread the access control tables, clear the host name lookup cache, reset the status variables to 0, clear the replication master logs, or reset the replication slave depending on the options in the bit mask. Issued by the client API call mysql_refresh( ).
8No argument.Tells the server to shut down.
9No argument.Tells the server to send back a string containing a brief status report. Issued by the client API call status( ).
10No argument.Tells the server to send back a report on the status of all running threads. This is an obsolete command still supported on the server for compatibility with old clients. Newer clients use the SHOW PROCESSLIST query.
11No argument.Never sent by a client. Used for internal purposes.
12A 4-byte integer with the low byte first containing the MySQL ID of the thread to be terminated.Tells the server to terminate the thread identified by the argument. This is an obsolete command still supported on the server for compatibility with old clients. Newer clients use the KILL query.
13No argument.Tells the server to dump some debugging information into its error log. Issued by the client API call mysql_dump_debug_info( ).
14No argument.Tells the server to respond with an OK packet. If the server is alive and reachable, it will. Issued by the client API call ping( ).
15No argument.Never sent by a client. Used for internal purposes.
16No argument.Never sent by a client. Used for internal purposes.
17A byte sequence in the following format: zero-terminated user name, encrypted password, zero-terminated default database name.Tells the server the client wants to change the user associated with this session.
18A byte sequence in the following format: 4-byte integer for the offset, 2- byte integer for the flags, 4-byte integer for the slave server ID, and a string for the log name. All integers are formatted with the low byte first.Tells the server to send a continuous feed of the replication master log events starting at the specified offset in the specified log. Used by the replication slave, and in the mysqlbinlog command-line utility.
19A byte sequence in the following format: 1 byte for database name length, database name, 1 byte for table name length, table name.Tells the server to send the table definition and data to the client in raw format. Used when a replication slave receives a LOAD DATA FROM MASTER query.
20No argument.Never sent by a client. Used for internal purposes.
21A byte sequence in the following format: a 4-byte integer for the server ID, then a sequence of 1 byte-length prefixed strings in the following order: slave host name, slave user to connect as, slave user password. Then a 2-byte slave user port, 4-byte replication recovery rank, and another 4-byte field that is currently unused. All integers have the low byte first.Tells the replication master server to register the slave using the information supplied in the argument. This command is a remnant of the started fail-safe replication project. It was introduced in the early version 4.0, but not much has changed since. It is possible that this command might get removed in the future versions.
22A string containing the statement.Tells the server to prepare the statement specified by the argument. Issued by the client API call mysql_stmt_prepare( ). New in version 4.1.
23A byte sequence in the following format: 4-byte statement ID, 1 byte for flags, and 4-byte iteration count. All integers have the low byte first.Tells the server to execute the statement referenced by the statement ID. Issued by the client API call mysql_stmt_ execute( ). New in version 4.1.
24A byte sequence in the following format: 4 byte statement ID, 2 byte parameter number, parameter string. Both integers have the low byte first.Tells the server the packet contains the data for one bound parameter in a prepared statement. Used to avoid unnecessary copying of a large amount of data when the value of the bound parameter is very long. Issued by the client API call mysql_stmt_send_long_data( ). New in version 4.1.
254-byte statement ID with the low byte first.Tells the server to close the prepared statement specified by the statement ID. Issued by the client API call mysql_stmt_close( ). New in version 4.1.
264-byte statement ID with the low byte first.Tells the server to discard the current parameter values in the prepared statement specified by the statement ID that may have been set with COM_ LONG_DATA. Issued by the client API call mysql_stmt_reset( ). New in version 4.1.
272-byte code for the option, low byte first.Tells the server to enable or disable the option specified by the code. At this point, seems to be used only to enable or disable the support of multiple statements in one query string. Issued by the client API call mysql_set_server_option( ). New in version 4.1.
28No argument.Never sent by a client. Used for internal purposes.

Tasks
This version of the API does not implement replication features. It does not implement prepare statement features. It does not implement the support of multiple statements in one query string. It does not also implement the end_session and change_user features. From the table, the tasks to implement are as follows – one function for each task:

- Tell the server to change the default database for the session to the one specified by the argument – Decimal code, 2 – Argument: database name.
- Tell the server to run the query – Decimal code, 3 – Argument: query string without semicolon.
- Tell the server to shut down – Decimal code, 8 – Argument: No argument.
- Tell the server to send back a string containing a brief status report - Decimal code, 9 – Argument: No argument.
- Tells the server to respond with an OK packet. If the server is alive and reachable, it will - Decimal code, 14 – Argument: No argument.

So there are five command functions, for this version. In the next part of the series the command packet package (module) will be developed, and the function names will be given – one function per task.

Chrys

Related Links

Internet Sockets and Perl
Perl pack and unpack Functions
Writing MySQL Protocol Packets in PurePerl
Developing a PurePerl MySQL API
Using the PurePerl MySQL API
More Related Links
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course

BACK NEXT

Comments

Become the Writer's Fan
Send the Writer a Message