ensembl-hive  2.6
URLFactory.pm
Go to the documentation of this file.
1 =pod
2 
3 =head1 NAME
4 
6 
7 =head1 SYNOPSIS
8 
9  $url_string1 = 'mysql://ensadmin:<pass>@ecs2:3362/compara_hive_23c'; # type=hive by default
10  $url_string2 = 'mysql://ensadmin:<pass>@ecs2:3362/ensembl_compara_22_1;type=compara'
11  $url_string3 = 'mysql://ensadmin:<pass>@ecs2:3362/ensembl_core_homo_sapiens_22_34;type=core'
12 
13  $hive_dba = Bio::EnsEMBL::Hive::URLFactory->fetch($url_string1);
14  $compara_dba = Bio::EnsEMBL::Hive::URLFactory->fetch($url_string2);
15  $core_dba = Bio::EnsEMBL::Hive::URLFactory->fetch($url_string3);
16 
17 =head1 DESCRIPTION
18 
19  Module to parse URL strings and return EnsEMBL objects.
20  At the moment, DBAdaptors as well as Analyses, Jobs and NakedTables are supported.
21 
22 =head1 LICENSE
23 
24  Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
25  Copyright [2016-2024] EMBL-European Bioinformatics Institute
26 
27  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
28  You may obtain a copy of the License at
29 
30  http://www.apache.org/licenses/LICENSE-2.0
31 
32  Unless required by applicable law or agreed to in writing, software distributed under the License
33  is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34  See the License for the specific language governing permissions and limitations under the License.
35 
36 =head1 CONTACT
37 
38  Please subscribe to the Hive mailing list: http://listserver.ebi.ac.uk/mailman/listinfo/ehive-users to discuss Hive-related questions or to be notified of our updates
39 
40 =cut
41 
42 package Bio::EnsEMBL::Hive::URLFactory;
43 
44 use strict;
45 use warnings;
46 
50 
51 #use Data::Dumper;
52 
53 
54  # global instance to cache connections and limit the number of open DB connections:
55 my $_URLFactory_global_instance;
56 
57 
58 sub new {
59  my $class = shift @_;
60 
61  unless($_URLFactory_global_instance) {
62  $_URLFactory_global_instance = bless {}, $class;
63  }
64  return $_URLFactory_global_instance;
65 }
66 
67 sub DESTROY {
68  my ($obj) = @_;
69 
70  foreach my $key (keys(%$_URLFactory_global_instance)) {
71  $_URLFactory_global_instance->{$key} = undef;
72  }
73 }
74 
75 =head2 fetch
76 
77  Arg[1] : string $url
78  Example : $url = 'mysql://user:pass@host:3306/dbname/table_name?tparam_name=tparam_value;type=compara;disconnect_when_inactive=1'
79  my $object = Bio::EnsEMBL::Hive::URLFactory->fetch($url);
80  Description: parses the URL, connects to appropriate DBAdaptor,
81  determines appropriate object_adaptor, fetches the object
82  Returntype : blessed instance of the object refered to or a DBAdaptor if simple URL
83  Exceptions : none
84  Caller : ?
85 
86 =cut
87 
88 sub fetch {
89  my $class = shift @_;
90  my $url = shift @_ or return;
91  my $default_dba = shift @_;
92 
93  Bio::EnsEMBL::Hive::URLFactory->new(); # make sure global instance is created
94 
95  if(my $parsed_url = Bio::EnsEMBL::Hive::Utils::URL::parse( $url )) {
96 
97  my $table_name = $parsed_url->{'table_name'};
98  my $tparam_name = $parsed_url->{'tparam_name'};
99  my $tparam_value = $parsed_url->{'tparam_value'};
100 
101  unless($table_name=~/^(analysis|job|accu)$/) { # do not check schema version version when performing table dataflow:
102  $parsed_url->{'conn_params'}{'no_sql_schema_version_check'} = 1;
103  }
104 
105  my $dba = ($parsed_url->{'dbconn_part'} =~ m{^\w*:
106  ? $default_dba
107  : $class->create_cached_dba( @$parsed_url{qw(dbconn_part driver user pass host port dbname conn_params)} );
108 
109 
110  if(not $table_name) {
111 
112  return $dba;
113 
114  } elsif($table_name eq 'analysis') {
115 
116  return $dba->get_AnalysisAdaptor->fetch_by_url_query($tparam_name, $tparam_value);
117 
118  } elsif($table_name eq 'job') {
119 
120  return $dba->get_AnalysisJobAdaptor->fetch_by_url_query($tparam_name, $tparam_value);
121 
122  } elsif($table_name eq 'accu') {
123 
125  $dba ? (adaptor => $dba->get_AccumulatorAdaptor) : (),
126  accu_name => $tparam_name,
127  accu_address => $tparam_value,
128  );
129 
130  } else {
131 
133  $dba ? (adaptor => $dba->get_NakedTableAdaptor( 'table_name' => $table_name ) ) : (),
134  table_name => $table_name,
135  $tparam_value ? (insertion_method => $tparam_value) : (),
136  );
137  }
138  } else {
139  warn "Could not parse URL '$url'\n";
140  }
141  return;
142 }
143 
144 
145 sub create_cached_dba {
146  my ($class, $dbconn_part, $driver, $user, $pass, $host, $port, $dbname, $conn_params) = @_;
147 
148  my $type = $conn_params->{'type'};
149  my $disconnect_when_inactive = $conn_params->{'disconnect_when_inactive'};
150  my $no_sql_schema_version_check = $conn_params->{'no_sql_schema_version_check'};
151 
152  my $connectionKey = $driver.'://'.($user//'').':'.($pass//'').'@'.($host//'').':'.($port//'').'/'.($dbname//'').';'.$type;
153  my $dba = $_URLFactory_global_instance->{$connectionKey};
154 
155  unless($dba) {
156 
157  my $module = {
158  'hive' => 'Bio::EnsEMBL::Hive::DBSQL::DBAdaptor',
159  'compara' => 'Bio::EnsEMBL::Compara::DBSQL::DBAdaptor',
160  'core' => 'Bio::EnsEMBL::DBSQL::DBAdaptor',
161  'pipeline' => 'Bio::EnsEMBL::Pipeline::DBSQL::DBAdaptor',
162  }->{$type};
163 
164  eval "require $module";
165 
166  $_URLFactory_global_instance->{$connectionKey} = $dba =
167  $type eq 'hive'
168  ? $module->new(
169  -url => $dbconn_part,
170  -disconnect_when_inactive => $disconnect_when_inactive,
171  -no_sql_schema_version_check=> $no_sql_schema_version_check,
172  ) : $module->new(
173  -driver => $driver,
174  -host => $host,
175  -port => $port,
176  -user => $user,
177  -pass => $pass,
178  -dbname => $dbname,
179  -species => $dbname,
180  -disconnect_when_inactive => $disconnect_when_inactive,
181  );
182  }
183  return $dba;
184 }
185 
186 1;
Bio::EnsEMBL::Hive::Utils::URL
Definition: URL.pm:11
Bio::EnsEMBL::Hive::NakedTable
Definition: NakedTable.pm:10
Bio::EnsEMBL::Hive::Utils::URL::parse
public parse()
EnsEMBL
Definition: Filter.pm:1
Bio::EnsEMBL::Hive::URLFactory::new
public new()
Bio::EnsEMBL::Hive::Version
Definition: Version.pm:19
Bio::EnsEMBL::Hive::URLFactory
Definition: URLFactory.pm:19
Bio::EnsEMBL::Hive::Storable::new
public Bio::EnsEMBL::Hive::Storable new()
Bio::EnsEMBL::Hive::URLFactory::fetch
public Blessed fetch()
Bio::EnsEMBL::Hive::Accumulator
Definition: Accumulator.pm:11
Bio::EnsEMBL::Hive
Definition: Hive.pm:38