-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBase.pm
63 lines (52 loc) · 1.47 KB
/
Base.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package OrthoMCLEngine::Main::Base;
use strict;
use DBI;
sub new {
my ($class, $configFile, $loghandle) = @_;
my $self = {};
bless($self,$class);
$self->parseConfigFile($configFile, $loghandle);
return $self;
}
sub parseConfigFile {
my ($self, $configFile, $loghandle) = @_;
open(F, $configFile) || die "Can't open config file '$configFile'\n";
$self->{configFile} = $configFile;
while(<F>) {
chomp;
s/\s+$//;
next if /^\#/;
/^(\w+)\=(.+)/ || die "illegal line in config file '$_'\n";
my $key=$1;
my $val=$2;
$self->{config}->{$key} = $val;
if ($loghandle) {
$val = '********' if $key eq 'dbPassword';
print $loghandle localtime() . " configuration: $key=$val\n";
}
}
}
sub getConfig {
my ($self, $prop) = @_;
die "can't find property $prop in config file" unless $self->{config}->{$prop};
return $self->{config}->{$prop};
}
sub getDbh {
my ($self) = @_;
if (!$self->{dbh}) {
my $dbVendor = $self->getConfig("dbVendor");
if ($dbVendor eq 'oracle') {
require DBD::Oracle;
} elsif ($dbVendor eq 'mysql') {
require DBD::mysql;
} else {
die "config file '$self->{configFile}' has invalid value '$dbVendor' for dbVendor property\n";
}
$self->{dbh} = DBI->connect($self->getConfig("dbConnectString"),
$self->getConfig("dbLogin"),
$self->getConfig("dbPassword"),
{PrintError => 0, RaiseError => 1}) or die DBI::errstr;
}
return $self->{dbh};
}
1;