URLFactory.pm 5.78 KB
Newer Older
1
=pod 
2 3

=head1 NAME
4

5
    Bio::EnsEMBL::Hive::URLFactory
6 7

=head1 SYNOPSIS
8

9 10 11 12 13 14 15
    $url_string1 = 'mysql://ensadmin:<pass>@ecs2:3362/compara_hive_23c';                 # type=hive by default
    $url_string2 = 'mysql://ensadmin:<pass>@ecs2:3362/ensembl_compara_22_1;type=compara'
    $url_string3 = 'mysql://ensadmin:<pass>@ecs2:3362/ensembl_core_homo_sapiens_22_34;type=core'

    $hive_dba    = Bio::EnsEMBL::Hive::URLFactory->fetch($url_string1);
    $compara_dba = Bio::EnsEMBL::Hive::URLFactory->fetch($url_string2);
    $core_dba    = Bio::EnsEMBL::Hive::URLFactory->fetch($url_string3);
16 17

=head1 DESCRIPTION  
18

19 20
    Module to parse URL strings and return EnsEMBL objects.
    At the moment, DBAdaptors as well as Analyses, Jobs and NakedTables are supported.
21

22 23
=head1 LICENSE

24
    Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
Matthieu Muffato's avatar
Matthieu Muffato committed
25
    Copyright [2016-2018] EMBL-European Bioinformatics Institute
26 27 28 29 30 31 32 33 34 35

    Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software distributed under the License
    is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and limitations under the License.

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


43
    # global instance to cache connections and limit the number of open DB connections:
44
my $_URLFactory_global_instance;
45 46 47 48

package Bio::EnsEMBL::Hive::URLFactory;

use strict;
49 50 51
use Bio::EnsEMBL::Utils::Argument;
use Bio::EnsEMBL::Utils::Exception;

52
use Bio::EnsEMBL::Hive::Utils::URL;
53
use Bio::EnsEMBL::Hive::DBSQL::DBAdaptor;
54 55
use Bio::EnsEMBL::Hive::Extensions;
use Bio::EnsEMBL::Hive::Accumulator;
56
use Bio::EnsEMBL::Hive::NakedTable;
57

Leo Gordon's avatar
bug fix  
Leo Gordon committed
58 59
#use Data::Dumper;

60 61 62 63 64 65 66
sub new {
    my $class = shift @_;

    unless($_URLFactory_global_instance) {
        $_URLFactory_global_instance = bless {}, $class;
    }
    return $_URLFactory_global_instance;
67 68 69
}

sub DESTROY {
70 71 72 73 74
    my ($obj) = @_;

    foreach my $key (keys(%$_URLFactory_global_instance)) {
        $_URLFactory_global_instance->{$key} = undef;
    }
75
}
76 77

=head2 fetch
78

79
  Arg[1]     : string $url
80
  Example    :  $url = 'mysql://user:pass@host:3306/dbname/table_name?tparam_name=tparam_value;type=compara;discon=1'
81 82 83 84
                my $object = Bio::EnsEMBL::Hive::URLFactory->fetch($url);
  Description: parses the URL, connects to appropriate DBAdaptor,
               determines appropriate object_adaptor, fetches the object
  Returntype : blessed instance of the object refered to or a DBAdaptor if simple URL
85 86
  Exceptions : none
  Caller     : ?
87

88
=cut
89

90 91 92 93
sub fetch {
    my $class       = shift @_;
    my $url         = shift @_ or return;
    my $default_dba = shift @_;
94

95
    Bio::EnsEMBL::Hive::URLFactory->new();  # make sure global instance is created
96

97
    if(my $parsed_url = Bio::EnsEMBL::Hive::Utils::URL::parse( $url )) {
98

99 100 101
        my $dba = ($parsed_url->{'dbconn_part'} =~ m{^\w*:///$} )
            ? $default_dba
            : $class->create_cached_dba( @$parsed_url{qw(driver user pass host port dbname conn_params)} );
102

103 104 105
        my $table_name      = $parsed_url->{'table_name'};
        my $tparam_name     = $parsed_url->{'tparam_name'};
        my $tparam_value    = $parsed_url->{'tparam_value'};
106

107 108 109
        if(not $table_name) {
        
            return $dba;
110

111
        } elsif($table_name eq 'analysis') {
112

113
            return $dba->get_AnalysisAdaptor->fetch_by_url_query($tparam_name, $tparam_value);
114

115
        } elsif($table_name eq 'job') {
116

117
            return $dba->get_AnalysisJobAdaptor->fetch_by_url_query($tparam_name, $tparam_value);
118

119 120 121 122 123 124 125 126
        } elsif($table_name eq 'accu') {

            return Bio::EnsEMBL::Hive::Accumulator->new(
                    -adaptor            => $dba->get_AccumulatorAdaptor,
                    -struct_name        => $tparam_name,
                    -signature_template => $tparam_value,
            );

127
        } else {
128

129 130 131 132 133
            return Bio::EnsEMBL::Hive::NakedTable->new(
                -adaptor    => $dba->get_NakedTableAdaptor,
                -table_name => $table_name,
                $tparam_value ? (-insertion_method => $tparam_value) : ()
            );
134 135 136
        }
    }
    return;
137 138
}

139
sub create_cached_dba {
140
    my ($class, $driver, $user, $pass, $host, $port, $dbname, $conn_params) = @_;
141 142 143 144 145 146 147

    if($driver eq 'mysql') {
        $user ||= 'ensro';
        $pass ||= '';
        $host ||= '';
        $port ||= 3306;
    }
148

149 150 151
    my $type    = $conn_params->{'type'};
    my $discon  = $conn_params->{'discon'};
    my $nosqlvc = $conn_params->{'nosqlvc'};
152

153
    my $connectionKey = "$driver://$user:$pass\@$host:$port/$dbname;$type";
154 155 156 157 158 159 160 161 162 163 164 165 166 167
    my $dba = $_URLFactory_global_instance->{$connectionKey};

    unless($dba) {

        my $module = {
            'hive'     => 'Bio::EnsEMBL::Hive::DBSQL::DBAdaptor',
            'compara'  => 'Bio::EnsEMBL::Compara::DBSQL::DBAdaptor',
            'core'     => 'Bio::EnsEMBL::DBSQL::DBAdaptor',
            'pipeline' => 'Bio::EnsEMBL::Pipeline::DBSQL::DBAdaptor',
        }->{$type};

        eval "require $module";

        $_URLFactory_global_instance->{$connectionKey} = $dba = $module->new (
168
            -driver => $driver,
169 170
            -host   => $host,
            -port   => $port,
171 172
            -user   => $user,
            -pass   => $pass,
173 174
            -dbname => $dbname,
            -species => $dbname,
175
            -disconnect_when_inactive => $discon,
176
            -no_sql_schema_version_check => $nosqlvc,
177 178 179 180
        );
    }
    return $dba;
}
181

182
1;