Hi peeps,
I have a module i've written for doing SQL commands, I'm curious to learn a bit of OO design in perl and thought I might convert it to OO as a learning excersise.
But I need help!!!
Also would there be any advantage doing so, here is one of the routines for getting a count from a table....
module subroutine...
and in my normal perl script i call it like so...
I have similar routines for fetching updating , deleteing etc...
So I could call
so you see how I break it down by Table,Columns,Where,Order
Works great, I was thinking I could change it so it was OO designed, so maybe like this...
where Get,Sum,Cnt,Del,Ins are all the methods for the various SQL actions, or maybe have a $sql->Type("SQLType"); and then my @records = $sql->Run;
So would some one help me understand how I go about this, but more importantly is it worth it, what benefit would it give me?
I also currently have global vars which store the DSN be it a local system one, or you can use a file DSN, to change the current one used I do
What are peoples opinions on making this change, is it worth it?
"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."
"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
I have a module i've written for doing SQL commands, I'm curious to learn a bit of OO design in perl and thought I might convert it to OO as a learning excersise.
But I need help!!!
Also would there be any advantage doing so, here is one of the routines for getting a count from a table....
module subroutine...
Code:
###############################################
############## SUM SQL Routine ################
###############################################
sub sumSQL {
#_0 = Table
#_1 = Column
#_2 = Where
# Define Record Set Array
my @rs;
#Build SQL Statement
my $sel = "SELECT SUM($_[1]) as MYTOT FROM $_[0] WHERE $_[2]";
# Open DB Connection
my $db;
if(!$FILEDSN){
$db = new Win32::ODBC($DSN) || die "getSQL Error Connecting: " . Win32::ODBC::Error();
}
else{
$db = new Win32::ODBC("FILEDSN=$DSN;") || die "getSQL Error Connecting: " . Win32::ODBC::Error();
}
# Run SQL Command
if(!$db->Sql("$sel")) {
# Loop SQL Record Set
while($db->FetchRow()){
# Build Array of Hashes with SQL Data
my %dt = $db->DataHash();
$rs[@rs] = \%dt;
}
# Close DB Connection
$db->Close();
# Return Count
$rs[0]->{'MYTOT'};
}
else{die "Error in sumSQL ($sel)" . Win32::ODBC::Error();}
}
and in my normal perl script i call it like so...
Code:
my $cnt = &cntSQL("MyTableName","ColumnName = MyValue");
I have similar routines for fetching updating , deleteing etc...
So I could call
Code:
my @records = &getSQL("TableName","Column1,Column2,Column3","ColumnName1 = 'Value' AND ColumnName2 = 'Value'","ColumnName1 DESC");
so you see how I break it down by Table,Columns,Where,Order
Works great, I was thinking I could change it so it was OO designed, so maybe like this...
Code:
my $sql = new SQL;
$sql->Table("TableName");
$sql->Columns("Column1","Column2");
$sql->Where("Column1 = 'value' AND Column2 = 'value'");
$sql->Order("Column1 DESC");
my @records = $sql->Get;
where Get,Sum,Cnt,Del,Ins are all the methods for the various SQL actions, or maybe have a $sql->Type("SQLType"); and then my @records = $sql->Run;
So would some one help me understand how I go about this, but more importantly is it worth it, what benefit would it give me?
I also currently have global vars which store the DSN be it a local system one, or you can use a file DSN, to change the current one used I do
Code:
$sql::DSN = "new dsn details";
and if i wanted to switch to DSN being a fileDSN I use
$sql::FILEDSN = 1;
as it is default set to 0
What are peoples opinions on making this change, is it worth it?
"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."
"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!