Broad Network


The PurePerl MySQL API Variables and Functions Coding

Developing a PurePerl MySQL API – Part 9

Writing a Perl Module

Foreword: In this part of the series, I explain the functions and use of the variables in the PurePerl MySQL API module.

By: Chrysanthus Date Published: 28 Jan 2015

Introduction

This is part 9 of my series, Developing a PurePerl MySQL API. In this part of the series, I explain the functions and use of the variables in the PurePerl MySQL API module.  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.

The Variables
The variables are as follows, with their meanings:

our $Error_msg = "Error: ";
This will hold any error message developed from a query or some other command. The message is for the last issued command.
our $Message;
This will hold an OK message, if present; or any other message, if present. The message is for the last issued command.
our $No_Warnings;
This will hold the number of warnings the last issued command (or query) has generated. The OK and the EOF packets can have number of warnings.
our $No_Records_Changed;
This is the number of records changed by a command.
our $Auto_Increment_ID;
This is the last auto-incremented ID of an Insert command, where applicable.
our @Result;
This is an array of hashes that will hold the result set, for queries of the SELECT nature.

our $No_of_Columns;
The will hold the number of columns in a result set.
our $DB_Name;
This will hold the database name for a result set
our @Column_Properties;
This will hold column properties like table names and column names for a result set. It is a two-dimensional array, where one row is for one column.
our $No_of_Rows = 0;
This will hold the number of rows of a result set.

The Functions
The functions and their meanings are given below:

The connect() Function
This function connects the client software to the server software. I have already talked about this function in one of the previous parts of the series.

The select_db() Function
This function selects a new database for the client. I have already talked about this function in one of the previous parts of the series.

The query() Function
This function takes one argument. The argument is a string, which is a MySQL SQL statement without the ending semicolon. Very many different types of SQL statement can be the argument. The function sends a command to the server and receives one of three things: the OK packet, the Error packet or a Result Set. The function definition is long. You just have to see it when you download the whole library.

The status() Function
This function tells the server to send back a string containing a brief status report. I have already talked about this function in one of the previous parts of the series.

The ping() Function
This function reports whether the server is alive and reachable. I have already talked about this function in one of the previous parts of the series.

The shutdown() Function
When this command is issued, if the server is to shut down, it will send the EOF packet to the client. The code in the Mysql package is:

    sub shutdown
        {
            my $db_pkt = Command::command(8);
            #send the DB packet
            send (SOCK, $db_pkt, 0);
            #receive the reply
            my $db_reply;
            recv (SOCK, $db_reply, 200, 0);

            #determine and return 1 or 0
            my ($dummy,$OKByt) = unpack('A4A', $db_reply);
            my $zero_byte = pack('H2', "00");
            my $OKByt_16 = $zero_byte . $OKByt;
            my $OKByt_16_D = unpack('S>', $OKByt_16);
            if ($OKByt_16_D == 255)
                {
                    my %ha = Error::error($db_reply);
                    $Error_msg .= $ha{'error_msg'};
                    return 0;
                }
            if ($OKByt_16_D == 254)
                {
                    $Message = "The MySQL Server should shutdown.";
                    return 1;
                }
        }

The close() Function
Any socket opened has to be closed at the end of the session. Closing the socket at the client API, also closes the connection. In the Mysql package, the function to do this is:

    sub close
        {
         if (close (SOCK))
             {
                 return 1;
             }
            else
             {
                 $Error_msg .= $! ;
                 return 0;
             }
        }

End of Article and End of Series
We have come to the end of the article and the end of the series. I hope you appreciated the series. The next series to learn is on how to use the PurePerl MySQL API.

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

Comments

Become the Writer's Fan
Send the Writer a Message