Broad Network


Compressed SQL Statements with PurePerl MySQL API

Using the PurePerl MySQL API– Part 12

Foreword: In this part of the series I explain how to use PurePerl MySQL API to compress a SQL statement and send to the MySQL server.

By: Date Published: 28 Aug 2017

Introduction

This is part 12 of my series, Using the PurePerl MySQL API. In the previous part of the series, it was the data value in a cell (column) of a database table that was compressed. That is expected when the PurePerl MySQL API (client) is in the same computer as the MySQL server. If both are in different computers and possibly distant apart, then the client (PurePerl MySQL API) should compress the whole SQL statement and send to the MySQL server. The MySQL server will decompress the SQL statement before carrying out the query. The compressed statement may still require a cell value to be compressed (before being inserted) - you will try that on your own. In this part of the series I explain how to use PurePerl MySQL API to compress a SQL statement and send to the MySQL server.

Compressing an INSERT Statement
The following code compresses an INSERT statement and send to MySQL server:

#!C:/Perl/bin/perl5.18.2.exe
use Mysql;
use strict;

        if (!Mysql::connect("root", "secret", "localhost", 3306, 'compress'))
         {
                print "$Mysql::Error_msg";
         }
        else
         {
                if (Mysql::query("USE PetStore") != 1)
                 {
                        print $Mysql::Error_msg, "\n";
                 }
                else
                 {

                        my $ins = "INSERT INTO pet (name, owner, species, sex, birth, death) VALUES ('Pin','Peop','brancher','m','2017-04-21',NULL)";
                        if (Mysql::query($ins) != 1)
                         {
                                print $Mysql::Error_msg, "\n";
                         }
                 }
         }

         Mysql::close();

This code is similar to other code samples, except for the presence of the argument, 'compress' in the connection expression (above).

Compressing a SELECT Statement
The following code compresses a SELECT statement and send:

#!C:/Perl/bin/perl5.18.2.exe
use Mysql;
use strict;

        if (!Mysql::connect("root", "secret", "localhost", 3306, 'compress'))
            {
                print "$Mysql::Error_msg";
            }
        else
            {
                if (Mysql::query("USE PetStore") != 1)
                    {
                        print $Mysql::Error_msg, "\n";
                    }
                else
                    {
                        my $sel = "select * from pet where name='Pin'";
                        if (Mysql::query($sel) != 1)
                            {
                                print $Mysql::Error_msg, "\n";
                            }
                        else
                            {
                                for (my $i=0; $i<$Mysql::No_of_Rows; ++$i)
                                    {
                                       print $Mysql::Result[$i]{'name'},  ', ';
                                       print $Mysql::Result[$i]{'owner'},  ', ';
                                       print $Mysql::Result[$i]{'species'},  ', ';
                                       print $Mysql::Result[$i]{'sex'},  ', ';
                                       print $Mysql::Result[$i]{'birth'},  ', ';
                                       print $Mysql::Result[$i]{'death'},  ', ';
                                       print "\n";        
                                    }
                            }
                     }
            }

         Mysql::close();

Note the use of the 'compress' argument in the connection expression, which enabes the SELECT statement to be compressed (before being sent over the network).

Compressing an DELETE Statement
The following code compresses a DELETE statement and send:

#!C:/Perl/bin/perl5.18.2.exe
use Mysql;
use strict;

        if (!Mysql::connect("root", "secret", "localhost", 3306, 'compress'))
         {
                print "$Mysql::Error_msg";
         }
        else
         {
                if (Mysql::query("USE PetStore") != 1)
                 {
                        print $Mysql::Error_msg, "\n";
                 }
                else
                 {
                        my $sel = "delete from pet where name='Pin'";
                        if (Mysql::query($sel) != 1)
                         {
                                print $Mysql::Error_msg, "\n";
                         }
                 }
         }

         Mysql::close();

This code is similar to other code samples, except for the presence of the argument, 'compress' in the connection expression. Any compression of a SQL statement, needs this argument.

Note: The API does not yet support a combination of INSERT, SELECT and DELETE.

That is it for this part of the series. We stop here and continue in the next part.

Chrys

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 Follower
Send the Writer a Message