Commit 54f235a4 authored by Leo Gordon's avatar Leo Gordon
Browse files

core-independent version of DBAdaptor. new() clean from URLFactory and...

core-independent version of DBAdaptor. new() clean from URLFactory and self-reference, but still plays nice with optional Registry
parent c7e762b5
......@@ -6,12 +6,7 @@
=head1 SYNOPSIS
$db = Bio::EnsEMBL::Hive::DBSQL::DBAdaptor->new(
-user => 'root',
-dbname => 'pog',
-host => 'caldy',
-driver => 'mysql',
);
my $db = Bio::EnsEMBL::Hive::DBSQL::DBAdaptor->new( -url => 'mysql://my_username:my_password@my_hostname:3306/my_hive_database' );
=head1 DESCRIPTION
......@@ -41,31 +36,32 @@ package Bio::EnsEMBL::Hive::DBSQL::DBAdaptor;
use strict;
use Bio::EnsEMBL::Registry;
use Bio::EnsEMBL::Utils::Argument ('rearrange');
use Bio::EnsEMBL::Utils::Exception ('throw');
use Bio::EnsEMBL::Hive::URLFactory;
use Bio::EnsEMBL::Hive::DBSQL::DBConnection;
use Bio::EnsEMBL::Hive::DBSQL::SqlSchemaAdaptor;
use base ('Bio::EnsEMBL::DBSQL::DBAdaptor');
sub new {
my ($class, @args) = @_;
my ($url, $reg_conf, $reg_type, $reg_alias, $no_sql_schema_version_check)
= rearrange(['URL', 'REG_CONF', 'REG_TYPE', 'REG_ALIAS', 'NO_SQL_SCHEMA_VERSION_CHECK'], @args);
my ($dbconn, $url, $reg_conf, $reg_type, $reg_alias, $species, $no_sql_schema_version_check)
= rearrange(['DBCONN', 'URL', 'REG_CONF', 'REG_TYPE', 'REG_ALIAS', 'SPECIES', 'NO_SQL_SCHEMA_VERSION_CHECK'], @args);
$url .= ';nosqlvc=1' if($url && $no_sql_schema_version_check);
my $self;
if($url) {
$self = Bio::EnsEMBL::Hive::URLFactory->fetch($url)
or die "Unable to connect to DBA using url='$url'\n";
$dbconn = Bio::EnsEMBL::Hive::DBSQL::DBConnection->new(-url => $url)
or die "Unable to create a DBC using url='$url'";
} elsif($reg_alias) {
Bio::EnsEMBL::Registry->load_all($reg_conf) if($reg_conf);
require Bio::EnsEMBL::Registry;
Bio::EnsEMBL::Registry->load_all($reg_conf); # if undefined, default reg_conf will be used
$reg_type ||= 'hive';
......@@ -73,11 +69,14 @@ sub new {
or die "Unable to connect to DBA using reg_conf='$reg_conf', reg_type='$reg_type', reg_alias='$reg_alias'\n";
if($reg_type ne 'hive') { # ensure we are getting a Hive adaptor even from a non-Hive Registry entry:
$self = Bio::EnsEMBL::Hive::DBSQL::DBAdaptor->new( -dbconn => $self->dbc(), -no_sql_schema_version_check => $no_sql_schema_version_check );
$dbconn = $self->dbc;
$self = undef;
}
} else {
$self = $class->SUPER::new(@args)
or die "Unable to connect to DBA using parameters (".join(', ', @args).")\n"
}
if($dbconn && !$self) {
$self = bless {}, $class;
$self->dbc( $dbconn );
}
unless($no_sql_schema_version_check) {
......@@ -105,7 +104,6 @@ sub new {
} elsif($db_sql_schema_version < $code_sql_schema_version) {
my $new_patches = Bio::EnsEMBL::Hive::DBSQL::SqlSchemaAdaptor->get_sql_schema_patches( $db_sql_schema_version, $dbc->driver )
|| die "DB($url) sql_schema_version mismatch: the database's version is '$db_sql_schema_version' but the code is already '$code_sql_schema_version'.\n"
."Unfortunately we cannot patch the database; you may have to create a new database or agree to run older code\n";
......@@ -122,17 +120,21 @@ sub new {
}
}
if($species) { # [compatibility with core code] store the DBAdaptor in Registry:
require Bio::EnsEMBL::Registry;
Bio::EnsEMBL::Registry->add_DBAdaptor( $species, 'hive', $self );
}
return $self;
}
sub dbc {
my $self = shift @_;
my $self = shift;
my $dbc = $self->SUPER::dbc( @_ );
bless $dbc, 'Bio::EnsEMBL::Hive::DBSQL::DBConnection' if( $dbc );
$self->{'_dbc'} = bless shift, 'Bio::EnsEMBL::Hive::DBSQL::DBConnection' if(@_);
return $dbc;
return $self->{'_dbc'};
}
......@@ -161,27 +163,63 @@ sub hive_use_param_stack { # getter only, not setter
sub get_available_adaptors {
my %pairs = (
# Core adaptors extended with Hive stuff:
'MetaContainer' => 'Bio::EnsEMBL::Hive::DBSQL::MetaContainer',
# "new" Hive adaptors (sharing the same fetching/storing code inherited from the BaseAdaptor class) :
'Accumulator' => 'Bio::EnsEMBL::Hive::DBSQL::AccumulatorAdaptor',
'Analysis' => 'Bio::EnsEMBL::Hive::DBSQL::AnalysisAdaptor',
'AnalysisCtrlRule' => 'Bio::EnsEMBL::Hive::DBSQL::AnalysisCtrlRuleAdaptor',
'AnalysisData' => 'Bio::EnsEMBL::Hive::DBSQL::AnalysisDataAdaptor',
'AnalysisJob' => 'Bio::EnsEMBL::Hive::DBSQL::AnalysisJobAdaptor',
'AnalysisStats' => 'Bio::EnsEMBL::Hive::DBSQL::AnalysisStatsAdaptor',
'DataflowRule' => 'Bio::EnsEMBL::Hive::DBSQL::DataflowRuleAdaptor',
'ResourceDescription' => 'Bio::EnsEMBL::Hive::DBSQL::ResourceDescriptionAdaptor',
'ResourceClass' => 'Bio::EnsEMBL::Hive::DBSQL::ResourceClassAdaptor',
'LogMessage' => 'Bio::EnsEMBL::Hive::DBSQL::LogMessageAdaptor',
'Meta' => 'Bio::EnsEMBL::Hive::DBSQL::MetaAdaptor',
'MetaContainer' => 'Bio::EnsEMBL::Hive::DBSQL::MetaContainer',
'NakedTable' => 'Bio::EnsEMBL::Hive::DBSQL::NakedTableAdaptor',
'Analysis' => 'Bio::EnsEMBL::Hive::DBSQL::AnalysisAdaptor',
'ResourceClass' => 'Bio::EnsEMBL::Hive::DBSQL::ResourceClassAdaptor',
'ResourceDescription' => 'Bio::EnsEMBL::Hive::DBSQL::ResourceDescriptionAdaptor',
'Queen' => 'Bio::EnsEMBL::Hive::Queen',
'AnalysisData' => 'Bio::EnsEMBL::Hive::DBSQL::AnalysisDataAdaptor',
'Accumulator' => 'Bio::EnsEMBL::Hive::DBSQL::AccumulatorAdaptor',
'Meta' => 'Bio::EnsEMBL::Hive::DBSQL::MetaAdaptor',
# "old" Hive adaptors (having their own fetching/storing code) :
'AnalysisJob' => 'Bio::EnsEMBL::Hive::DBSQL::AnalysisJobAdaptor',
'AnalysisStats' => 'Bio::EnsEMBL::Hive::DBSQL::AnalysisStatsAdaptor',
);
return \%pairs;
}
sub get_adaptor {
my $self = shift;
my $type = shift;
my $signature = join(':', $type, @_);
unless( $self->{'_cached_adaptor'}{$signature} ) {
my $adaptor_package_name = $self->get_available_adaptors()->{$type}
or throw("Could not find a module corresponding to '$type'");
eval "require $adaptor_package_name"
or throw("Could not load or compile module '$adaptor_package_name'");
$self->{'_cached_adaptor'}{$signature} = $adaptor_package_name->new( $self, @_ );
}
return $self->{'_cached_adaptor'}{$signature};
}
sub DESTROY { } # to simplify AUTOLOAD
sub AUTOLOAD {
our $AUTOLOAD;
my $type;
if ( $AUTOLOAD =~ /^.*::get_(\w+)Adaptor$/ ) {
$type = $1;
} elsif ( $AUTOLOAD =~ /^.*::get_(\w+)$/ ) {
$type = $1;
} else {
die "DBAdaptor::AUTOLOAD: Could not interpret the method: $AUTOLOAD";
}
my $self = shift;
return $self->get_adaptor($type, @_);
}
1;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment