54 lines
1.4 KiB
Perl
54 lines
1.4 KiB
Perl
package My::parser::db;
|
|
use strict;
|
|
use warnings;
|
|
use DBI;
|
|
|
|
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 $self->{'config'}->get_dbh();
|
|
}
|
|
|
|
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) {
|
|
my $i = 0;
|
|
$self->{'config'}->{'logger'}->log('Connecting to DB');
|
|
while(1) {
|
|
last if($dbh = DBI->connect("DBI:mysql:database=$db;host=$host",$usr,$pwd, { PrintError => 1, mysql_auto_reconnect=>0, AutoCommit => 1 }));
|
|
sleep($i);
|
|
$i++;
|
|
unless ($i % 10) {
|
|
$self->{'config'}->{'logger'}->log('Cannot connect to DB, reconnect retry '.$i);
|
|
}
|
|
}
|
|
$config->set_dbh($dbh);
|
|
#$self->{'config'}->{'logger'}->log("Sucsessfully connected to db"); FIXME add debug to config?
|
|
return $config->get_dbh();
|
|
} else {
|
|
$self->{'config'}->{'logger'}->log("Unable to connect to db, not enough parameters");
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
1;
|