use Treefam::DBConnection;
my $dbc = new Treefam::DBConnection (
-database => 'treefam_3',
-host => 'vegasrv.sanger.ac.uk',
-user => 'anonymous',
-port => 3308 );
OR to read configuration file:
my $dbc = Treefam::DBConnection->new(-file=>file);
OR to use default configuration from Treefam::Config
my $dbc = Treefam::DBConnection->new();
my $tree_handle = $dbc->get_TreeHandle();
my $tree = $tree_handle->get_by_gene($geneID);
None available.
sub new
{
my $class = shift;
my %dbc = @_;
if (!defined($dbc{'-database'})) {
if (defined($dbc{'-file'}) && -e $dbc{'-file'}) {
unless (my $return = do $dbc{'-file'}) {
croak "couldn't parse $dbc{'-file'}: $@" if $@;
croak "couldn't do $dbc{'-file'}: $!" unless defined $return;
croak "couldn't run $dbc{'-file'}" unless $return;
}
}
$dbc{'-database'} = $TFDBNAME;
$dbc{'-host'} = $TFDBHOST;
$dbc{'-user'} = $TFDBUSER;
$dbc{'-password'} = $TFDBPASS;
$dbc{'-port'} = $TFDBPORT;
}
my $db = $dbc{'-database'};
die "ERROR: No database selected\n" unless ($db);
unless ($dbc{'-database'}=~/\w_$APIVERSION/) {
warn "\nWARNING: API version doesn't match database version for $dbc{'-database'}. Some things won't work.\n\n";
sleep(1); }
my $host = $dbc{'-host'};
my $port = $dbc{'-port'} if defined($dbc{'-port'});
my $user_name = $dbc{'-user'};
my $password = $dbc{'-password'} || "";
my $dsn="DBI:mysql:$db:$host:$port";
my $self = {};
bless ($self, $class);
my $dbh;
$SIG{ALRM} = sub { die "timeout"; };
alarm(10); eval {
$dbh= DBI->connect ($dsn, $user_name, $password, {RaiseError=> 1, PrintError=> 0});
};
if (!$dbh || $@) {
croak "Could not connect to database $db on host $host: $DBI::errstr\n";
}
alarm(0);
$self->{'database'} = $dbc{'-database'};
$self->{'database_handle'} = $dbh;
return $self; } |