All the files
This commit is contained in:
48
lib/db.pm
Normal file
48
lib/db.pm
Normal file
@@ -0,0 +1,48 @@
|
||||
package My::parser::db;
|
||||
use strict;
|
||||
use warnings;
|
||||
use DBI;
|
||||
#use Scalar::Util qw(weaken);
|
||||
|
||||
sub new {
|
||||
my $class = shift;
|
||||
my $config = shift;
|
||||
my $rest = shift;
|
||||
return \$class if($rest);
|
||||
my $self = {};
|
||||
bless ($self, $class);
|
||||
|
||||
$self->{'config'} = $config;
|
||||
return $self;
|
||||
}
|
||||
|
||||
sub init {
|
||||
my $self = shift;
|
||||
my $parser = shift; ## not needed for this module as the db-connection is for all modules
|
||||
$self->connect || die "Could not connect to db";
|
||||
return 1;
|
||||
}
|
||||
|
||||
sub connect {
|
||||
my $self = shift;
|
||||
my $config = $self->{'config'};
|
||||
my $usr = $config->get_as_single_val('config','dbusr');
|
||||
my $pwd = $config->get_as_single_val('config','dbpwd');
|
||||
my $host = $config->get_as_single_val('config','dbhost');
|
||||
my $db = $config->get_as_single_val('config','db');
|
||||
my $dbh;
|
||||
if($usr && $pwd && $host && $db) {
|
||||
while(1) {
|
||||
last if($dbh = DBI->connect("DBI:mysql:database=$db;host=$host",$usr,$pwd, { PrintError => 1, mysql_auto_reconnect=>1, AutoCommit => 1 }));
|
||||
sleep(10);
|
||||
}
|
||||
$config->set_dbh($dbh);
|
||||
#$self->{'config'}->{'logger'}->log("Sucsessfully connected to db"); FIXME add debug to config?
|
||||
return 1;
|
||||
} else {
|
||||
$self->{'config'}->{'logger'}->log("Unable to connect to db, not enough parameters");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
1;
|
||||
Reference in New Issue
Block a user