13 This
object represents the handle
for a Hive system enabled database
17 Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
18 Copyright [2016-2024] EMBL-European Bioinformatics Institute
20 Licensed under the Apache License, Version 2.0 (the
"License"); you may not use
this file except in compliance with the License.
21 You may obtain a copy of the License at
25 Unless required by applicable law or agreed to in writing, software distributed under the License
26 is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27 See the License
for the specific language governing permissions and limitations under the License.
31 Please subscribe to the
Hive mailing list: http:
36 package Bio::EnsEMBL::Hive::DBSQL::DBAdaptor;
41 use Scalar::Util qw(weaken);
59 my $default_reg_type =
'hive';
65 my ($dbc, $url, $reg_conf, $reg_type, $reg_alias, $species, $no_sql_schema_version_check)
66 =
delete @flags{qw(-dbconn -url -reg_conf -reg_type -reg_alias -species -no_sql_schema_version_check)};
68 if ($url && $no_sql_schema_version_check) {
69 #check to see if the url has been quoted. If so, move the quote
70 #after the no_sql_schema_version_check
71 $url =~ s/([\
'\"]?)$/;no_sql_schema_version_check=1$1/;
74 if($reg_conf or $reg_alias) { # need to initialize Registry even if $reg_conf is not really given
75 require Bio::EnsEMBL::Registry;
76 # if undefined, default reg_conf will be used. If missing, will throw
77 Bio::EnsEMBL::Registry->load_all($reg_conf, undef, undef, undef, 'throw_if_missing
');
83 $dbc = Bio::EnsEMBL::Hive::DBSQL::DBConnection->new(-url => $url, %flags)
84 or die "Unable to create a DBC using url='$url
'";
88 if($reg_alias=~/^(\w+):(\w+)$/) {
89 ($reg_type, $reg_alias) = ($1, $2);
92 unless($reg_type) { # if no $reg_type explicitly given, try to guess:
93 my $dbas = Bio::EnsEMBL::Registry->get_all_DBAdaptors(-species => $reg_alias);
95 if( scalar(@$dbas) == 1 ) {
98 warn "The registry contains multiple entries for '$reg_alias
', please prepend the reg_alias with the desired type\n";
102 unless($self) { # otherwise (or if not found) try a specific $reg_type
103 $reg_type ||= $default_reg_type;
104 $self = Bio::EnsEMBL::Registry->get_DBAdaptor($reg_alias, $reg_type)
105 or die "Unable to connect to DBA using reg_conf='$reg_conf
', reg_type='$reg_type
', reg_alias='$reg_alias
'\n";
108 if( $self and !$self->isa($class) ) { # if we found a non-Hive Registry entry, detach the $dbc and build a Hive dba around it:
115 $self = bless {}, $class;
119 unless($no_sql_schema_version_check) {
121 my $dbc = $self->dbc();
123 # Make a safe URL without affecting EHIVE_PASS, which could have been set by the user
124 my $safe_url = $dbc->url('EHIVE_TMP_PASSWORD
');
125 $safe_url =~ s/EHIVE_TMP_PASSWORD/EHIVE_PASS/;
127 my $code_sql_schema_version = Bio::EnsEMBL::Hive::DBSQL::SqlSchemaAdaptor->get_code_sql_schema_version()
128 || die "DB($safe_url) Could not establish code_sql_schema_version, please check that 'EHIVE_ROOT_DIR
' environment variable is set correctly";
130 my $db_sql_schema_version = eval { $self->get_MetaAdaptor->fetch_by_meta_key( 'hive_sql_schema_version
' )->{'meta_value
'}; };
133 if($@ =~ /hive_meta.*doesn't exist/) {
135 die
"\nDB($safe_url) The 'hive_meta' table does not seem to exist in the database yet.\nPlease patch the database up to sql_schema_version '$code_sql_schema_version' and try again.\n";
139 die
"DB($safe_url) $@";
142 } elsif(!$db_sql_schema_version) {
144 die
"\nDB($safe_url) The 'hive_meta' table does not contain 'hive_sql_schema_version' entry.\nPlease investigate.\n";
146 } elsif($db_sql_schema_version < $code_sql_schema_version) {
149 || die
"DB($safe_url) sql_schema_version mismatch: the database's version is '$db_sql_schema_version' but the code is already '$code_sql_schema_version'.\n"
150 .
"Unfortunately we cannot patch the database; you may have to create a new database or agree to run older code\n";
152 my $sql_patcher_command =
"$ENV{'EHIVE_ROOT_DIR'}/scripts/db_cmd.pl -url $safe_url";
154 die
"DB($safe_url) sql_schema_version mismatch: the database's version is '$db_sql_schema_version' but the code is already '$code_sql_schema_version'.\n"
155 .
"Please upgrade the database by applying the following patches:\n\n"
156 .join(
"\n",
map { ($_=~/\.\w*sql\w*$/) ?
"\t$sql_patcher_command < $_" :
"$_ -url $safe_url" } @$new_patches)
157 .
"\n\nand try again.\n";
159 } elsif($code_sql_schema_version < $db_sql_schema_version) {
161 die
"DB($safe_url) sql_schema_version mismatch: the database's version is '$db_sql_schema_version', but your code is still '$code_sql_schema_version'.\n"
162 .
"Please update the code and try again.\n";
168 $self->{
'_species'} = $species;
176 sub species { # a stub to please
Registry code
178 return $self->{
'_species'}
181 sub group { # a stub to please Registry code
182 return $default_reg_type;
189 $self->{
'_dbc'} = bless shift,
'Bio::EnsEMBL::Hive::DBSQL::DBConnection' if(@_);
191 return $self->{
'_dbc'};
198 $self->{
'_hive_pipeline'} = shift @_;
200 unless ($self->{
'_hive_pipeline'}) {
203 return $self->{
'_hive_pipeline'};
207 our %adaptor_type_2_package_name = (
208 'Accumulator' =>
'Bio::EnsEMBL::Hive::DBSQL::AccumulatorAdaptor',
209 'Analysis' =>
'Bio::EnsEMBL::Hive::DBSQL::AnalysisAdaptor',
210 'AnalysisCtrlRule' =>
'Bio::EnsEMBL::Hive::DBSQL::AnalysisCtrlRuleAdaptor',
211 'AnalysisData' =>
'Bio::EnsEMBL::Hive::DBSQL::AnalysisDataAdaptor',
212 'AnalysisJob' =>
'Bio::EnsEMBL::Hive::DBSQL::AnalysisJobAdaptor',
213 'AnalysisStats' =>
'Bio::EnsEMBL::Hive::DBSQL::AnalysisStatsAdaptor',
214 'Beekeeper' =>
'Bio::EnsEMBL::Hive::DBSQL::BeekeeperAdaptor',
215 'DataflowRule' =>
'Bio::EnsEMBL::Hive::DBSQL::DataflowRuleAdaptor',
216 'DataflowTarget' =>
'Bio::EnsEMBL::Hive::DBSQL::DataflowTargetAdaptor',
217 'LogMessage' =>
'Bio::EnsEMBL::Hive::DBSQL::LogMessageAdaptor',
218 'Meta' =>
'Bio::EnsEMBL::Hive::DBSQL::MetaAdaptor',
219 'PipelineWideParameters'=>
'Bio::EnsEMBL::Hive::DBSQL::PipelineWideParametersAdaptor',
220 'NakedTable' =>
'Bio::EnsEMBL::Hive::DBSQL::NakedTableAdaptor',
221 'ResourceClass' =>
'Bio::EnsEMBL::Hive::DBSQL::ResourceClassAdaptor',
222 'ResourceDescription' =>
'Bio::EnsEMBL::Hive::DBSQL::ResourceDescriptionAdaptor',
223 'Role' =>
'Bio::EnsEMBL::Hive::DBSQL::RoleAdaptor',
224 'Semaphore' =>
'Bio::EnsEMBL::Hive::DBSQL::SemaphoreAdaptor',
225 'Queen' =>
'Bio::EnsEMBL::Hive::Queen',
228 'Job' =>
'Bio::EnsEMBL::Hive::DBSQL::AnalysisJobAdaptor',
229 'Worker' =>
'Bio::EnsEMBL::Hive::Queen',
230 'MetaParameters' =>
'Bio::EnsEMBL::Hive::DBSQL::MetaAdaptor',
234 sub get_available_adaptors {
236 return \%adaptor_type_2_package_name;
240 sub parse_underscored_id_name {
241 my ($self, $underscored_id_name) = @_;
243 my ($is_an_id, $foo_id_method_name, $foo_obj_method_name);
245 my @syll = split(/_/, $underscored_id_name);
246 if($syll[scalar(@syll)-1] eq
'id') {
248 ($is_an_id, $foo_id_method_name, $foo_obj_method_name) = ( 1, $underscored_id_name, join(
'_', @syll) );
250 ($is_an_id, $foo_id_method_name, $foo_obj_method_name) = ( 0, $underscored_id_name .
'_id' , $underscored_id_name );
253 my $AdaptorType =
''; # will be growing from right to left
255 $AdaptorType = ucfirst(pop @syll) . $AdaptorType;
256 if(exists( $self->get_available_adaptors->{ $AdaptorType })) {
257 return ($AdaptorType, $is_an_id, $foo_id_method_name, $foo_obj_method_name);
260 return; # could not parse
266 my $AdaptorType = shift;
268 my $adaptor_package_name = $self->get_available_adaptors()->{$AdaptorType}
269 or
throw(
"Could not find a module corresponding to '$AdaptorType'");
271 my $signature = join(
':', $adaptor_package_name, @_);
273 unless( $self->{
'_cached_adaptor'}{$signature} ) {
275 eval
"require $adaptor_package_name"
276 or
throw(
"Could not load or compile module '$adaptor_package_name' because $@");
278 $self->{
'_cached_adaptor'}{$signature} = $adaptor_package_name->new( $self, @_ );
281 return $self->{
'_cached_adaptor'}{$signature};
285 sub DESTROY { } # to simplify AUTOLOAD
291 if ( $AUTOLOAD =~ /^.*::get_(\w+)Adaptor$/ ) {
293 } elsif ( $AUTOLOAD =~ /^.*::get_(\w+)$/ ) {
296 throw(
"DBAdaptor::AUTOLOAD: Could not interpret the method: $AUTOLOAD" );
301 return $self->get_adaptor($type, @_);