$dbc = Bio::EnsEMBL::DBSQL::DBConnection->new(
-user => 'anonymous',
-dbname => 'snpd',
-host => 'ocs4',
-driver => 'Oracle',
-species => 'Homo Sapiens',
-group => 'glovar'
);
SQL statements should be created/executed through
this modules prepare() and do() methods.
$sth = $dbc->prepare( "SELECT something FROM yourtable" );
$sth->execute();
# do something with rows returned ...
$sth->finish();
sub connect
{ my $self = shift;
return if($self->connected);
$self->connected(1);
if(defined($self->db_handle) and $self->db_handle->ping) {
warning("unconnected db_handle is still pingable, reseting connected boolean\n");
}
my $dsn = "DBI:" . $self->driver . ":";
my $dbh;
eval {
$dbh = DBI->connect($dsn,
$self->username . "\@" . $self->dbname,
$self->password,
{ 'RaiseError' => 1, 'PrintError' => 0 });
};
if(!$dbh || $@ || !$dbh->ping) {
warn("Could not connect to database " . $self->dbname .
" as user " . $self->username .
" using [$dsn] as a locator:\n" . $DBI::errstr);
throw("Could not connect to database " . $self->dbname .
" as user " . $self->username .
" using [$dsn] as a locator:\n" . $DBI::errstr);
}
$self->db_handle($dbh);
}
1; } |
sub new
{ my $class = shift;
my ($db, $host, $driver, $user, $password, $port, $dbconn) =
rearrange([qw(DBNAME HOST DRIVER USER PASS PORT DBCONN )], @_);
my $self = {};
bless $self, $class;
if($dbconn) {
if($db || $host || $driver || $password || $port) {
throw("Cannot specify other arguments when -DBCONN argument used.");
}
$self->dbname($dbconn->dbname);
$self->username($dbconn->username);
$self->host($dbconn->host);
$self->password($dbconn->password);
$self->port($dbconn->port);
$self->driver($dbconn->driver);
} else {
$db || throw("-DBNAME argument is required.");
$user || throw("-USER argument is required.");
$driver ||= 'Oracle';
$self->username($user);
$self->host($host);
$self->dbname($db);
$self->password($password);
$self->driver($driver);
}
return $self; } |