49 lines
1.2 KiB
Perl
49 lines
1.2 KiB
Perl
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;
|