Broad Network


Command Packet Code for PurePerl MySQL API

Developing a PurePerl MySQL API – Part 4

Writing a Perl Module

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

By: Chrysanthus Date Published: 28 Jan 2015

Introduction

This is part 4 of my series, Developing a PurePerl MySQL API. In this part of the series I explain the command packet code for the PurePerl MySQL API. The command packet code is used by all the functions of the API. All functions send a command to the server. These API command functions are separated from the command packet code, but are related to the code. 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.

Command Packet Code Tasks and Function Names
- Tell the server to change the default database for the session to the one specified by the argument – Decimal code, 2 – Argument: database name. The function name will be, select_db().
- Tell the server to run a query – Decimal code, 3 – Argument: query string without semicolon. The function name will be query();
- Tell the server to shut down – Decimal code, 8 – Argument: No argument. The function name will be, shutdown().
- Tell the server to send back a string containing a brief status report - Decimal code, 9 – Argument: No argument. The function will be called, status().
- Tells the server to respond with an OK packet. If the server is alive and reachable, it will - Decimal code, 14 – Argument: No argument. The function will be called, ping().

The Command Packet Code
The code is simple, it is:

package Command;
our $VERSION = "1.01";

use strict;

    sub command
        {
            my $com_code_Dec = $_[0];
            my $code16 = pack('S', $com_code_Dec);
            my $com_code_B = pack('A', $code16);

            my $com_arg  = $_[1];

            my $com_arg_len = length($com_arg);
            if (!$com_arg_len)
                {
                    $com_arg = "";
                    $com_arg_len = 0;
                }

            my $packet_body_str = $com_code_B . $com_arg;

            my $body_len = $com_arg_len + 1;
            #convert decimal $body_len to 3 bytes with low-byte first
            my $body_len_16 = pack( 'S<', $body_len);  
            my $body_len_24 = $body_len_16 . pack( 'H2', "00");  
            my $seq_no = pack('H2', "00");
            my $header = $body_len_24 . $seq_no;

            my $packet_string = $header . $packet_body_str;  

            return $packet_string;
        }

1;

Every Perl module should end with a 1 followed by a semicolon. That is why you have “1;” at the end of the code. The code has one function, which is not any of the functions mentioned above.

The first code segment converts the code decimal number into a byte. Next, the argument string is assigned to a variable. The argument length is then determined and the code byte and argument bytes are joined to form the body string.

In the segment that follows, the length of the body is determined. The number is then converted to 3 bytes with low-byte-first. In this segment, the packet sequence number is determined and joined to the body length in bytes to form the header segment for the packet.

The bytes for the header and body segments are then joined and returned.

The command packet code is a module. In the next part of the series, I develop the functions of the Mysql module. This module has the connect() function.

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