######################
# Set Error Trapping #
######################
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
use warnings;
use strict;
##################
# Use DBI Module #
##################
use DBI();
##########################
# Set Package Name Space #
##########################
package SQL;
################
# Start Module #
################
BEGIN {
# Invoke Exporter
use Exporter;
# Set Variables
our (@ISA, @EXPORT);
@ISA = qw(Exporter);
# Define global vars and subs to be exported
@EXPORT = qw( &getSQL &insSQL &updSQL &delSQL &cntSQL &sumSQL $DSN $FILEDSN $IMN $HLP);
}
########################################################################
########################### GLOBAL SUBROUTINES #########################
########################################################################
###############################################
############## Get SQL Routine ################
###############################################
sub getSQL {
#_0 = Table
#_1 = Columns
#_2 = Where
#_3 = Order By
# Define Record Set Array & Hash
my @rs;
#Build SQL Statement
my $sel = "SELECT $_[1] FROM $_[0] WHERE $_[2]";
# Check for ORDER BY
if($_[3]){$sel .= " ORDER BY $_[3]";}
# Open DB Connection
my $db = DBI->connect("DBI:mysql:YOUR_DSN:localhost","YOUR_ID","YOUR_PASSWORD",{'RaiseError' => 1});
# Run SQL Command
my $sth = $db->prepare("$sel") || die "Error in SQL : $sel";
$sth->execute();
# Loop SQL Record Set
while (my $ref = $sth->fetchrow_hashref()) {
# Build Array of Hashes with SQL Data
$rs[@rs] = \%$ref;
}
# Close DB Connection
$sth->finish();
# Disconnect from the database.
$db->disconnect();
# Return Record Set Array of Hashes
@rs;
}
##################################################
############## Update SQL Routine ################
##################################################
sub updSQL {
#_0 = Table
#_1 = Values
#_2 = Where
#Build SQL Statement
my $sel = "UPDATE $_[0] SET $_[1] WHERE $_[2]";
# Open DB Connection
my $db = DBI->connect("DBI:mysql:YOUR_DSN:localhost","YOUR_ID","YOUR_PASSWORD",{'RaiseError' => 1});
# Run SQL Command
my $sth = $db->prepare("$sel");
$sth->execute();
my $rowcount = $sth->rows;
# Close DB Connection
$sth->finish();
# Disconnect from the database.
$db->disconnect();
# Return number of rows updated
$rowcount;
}
##################################################
############## Insert SQL Routine ################
##################################################
sub insSQL {
#_0 = Table
#_1 = Columns
#_2 = Values
#Build SQL Statement
my $sel = "INSERT INTO $_[0] ($_[1]) VALUES ($_[2])";
# Open DB Connection
my $db = DBI->connect("DBI:mysql:YOUR_DSN:localhost","YOUR_ID","YOUR_PASSWORD",{'RaiseError' => 1});
# Run SQL Command
my $sth = $db->prepare("$sel");
$sth->execute();
my $rowcount = $sth->rows;
# Close DB Connection
$sth->finish();
# Disconnect from the database.
$db->disconnect();
# Return number of rows inserted
$rowcount;
}
##################################################
############## Delete SQL Routine ################
##################################################
sub delSQL {
#_0 = Table
#_1 = Where
#Build SQL Statement
my $sel = "DELETE FROM $_[0] WHERE $_[1]";
# Open DB Connection
my $db = DBI->connect("DBI:mysql:YOUR_DSN:localhost","YOUR_ID","YOUR_PASSWORD",{'RaiseError' => 1});
# Run SQL Command
my $sth = $db->prepare("$sel");
$sth->execute();
my $rowcount = $sth->rows;
# Close DB Connection
$sth->finish();
# Disconnect from the database.
$db->disconnect();
# Return number of rows deleted
$rowcount;
}
#################################################
############## COUNT SQL Routine ################
#################################################
sub cntSQL {
#_0 = Table
#_1 = Where
# Define Record Set Array & Hash
my @rs;
#Build SQL Statement
my $sel = "SELECT COUNT(1) as COUNT FROM $_[0] WHERE $_[1]";
# Open DB Connection
my $db = DBI->connect("DBI:mysql:YOUR_DSN:localhost","YOUR_ID","YOUR_PASSWORD",{'RaiseError' => 1});
# Run SQL Command
my $sth = $db->prepare("$sel");
$sth->execute();
# Loop SQL Record Set
while (my $ref = $sth->fetchrow_hashref()) {
# Build Array of Hashes with SQL Data
$rs[@rs] = \%$ref;
}
# Close DB Connection
$sth->finish();
# Disconnect from the database.
$db->disconnect();
# Return Count
$rs[0]->{'COUNT'};
}
###############################################
############## SUM SQL Routine ################
###############################################
sub sumSQL {
#_0 = Table
#_1 = Column
#_2 = Where
# Define Record Set Array & Hash
my @rs;
#Build SQL Statement
my $sel = "SELECT SUM($_[1]) as MYTOT FROM $_[0] WHERE $_[2]";
# Open DB Connection
my $db = DBI->connect("DBI:mysql:YOUR_DSN:localhost","YOUR_ID","YOUR_PASSWORD",{'RaiseError' => 1});
# Run SQL Command
my $sth = $db->prepare("$sel");
$sth->execute();
# Loop SQL Record Set
while (my $ref = $sth->fetchrow_hashref()) {
# Build Array of Hashes with SQL Data
$rs[@rs] = \%$ref;
}
# Close DB Connection
$sth->finish();
# Disconnect from the database.
$db->disconnect();
# Return Count
$rs[0]->{'MYTOT'};
}
###########################
# END OF MODULE RETURN 1; #
###########################
1;