sub _new
{ my ($self, @param) = @_;
$self->SUPER::_new(@param);
$self->attr('dbh', undef);
$self->openConnection(); } |
sub dbh
{ my ($self, $dbh) = @_;
if ($dbh) {
$self->set('dbh',$dbh);
}
return $self->get('dbh'); } |
sub dbhDC
{
my ($self, $dbh) = @_;
$dbh = $self->get('dbh');
$dbh->disconnect(); $self->set('dbh',undef);
}
1; } |
sub openConnection
{ my $self = shift;
unless (defined($self->host ) && defined ($self->databaseType)
&& defined($self->database) && defined($self->user)) {
BioMart::Exception::Database->throw("Database Specifications must contain at least the database type (".$self->databaseType.") host (".$self->host."), database (".$self->database."), and user (".$self->user.")");
}
my $dsn;
if ($self->databaseType eq 'oracle'){
$dsn=sprintf(ORACLEDSNTEMPLATE, $self->host, $self->database,
$self->port);
} elsif ($self->databaseType eq 'mysql') {
$dsn=sprintf(MYSQLDNSTEMPLATE, $self->database, $self->host,
$self->port);
} elsif ($self->databaseType eq 'postgres') {
$dsn=sprintf(POSTGRESTEMPLATE, $self->database, $self->host,
$self->port);
} elsif ($self->databaseType eq 'odbc') {
$dsn=sprintf(ODBCDNSTEMPLATE, $self->database);
} else {
warn("unsupported RDBMS type:\" $self->databaseType\" - please use the correct name or supported RDBMS ...... skipping connection");
}
$self->dsn($dsn);
my $dbh;
eval {
$dbh = DBI->connect(
$dsn,
$self->user,
$self->password,
{InactiveDestroy => 1, RaiseError => 1, PrintError => 1}
);
};
if($@ || !$dbh) {
BioMart::Exception::Database->throw("Could not connect to ".$self->databaseType." database ".$self->database.": ".$@);
}
$self->dbh($dbh); } |