URLFactory.pm 5.62 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-2014] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
25 26 27 28 29 30 31 32 33 34

    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.

35
=head1 CONTACT
36

37
    Please contact ehive-users@ebi.ac.uk mailing list with questions/suggestions.
38

39
=cut
40 41


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

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

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

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

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

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

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

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

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

=head2 fetch
77

78
  Arg[1]     : string $url
79
  Example    :  $url = 'mysql://user:pass@host:3306/dbname/table_name?tparam_name=tparam_value;type=compara;discon=1'
80 81 82 83
                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
84 85
  Exceptions : none
  Caller     : ?
86

87
=cut
88

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

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

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

98 99 100
        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)} );
101

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

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

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

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

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

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

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

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

126
        } else {
127

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

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

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

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

152
    my $connectionKey = "$driver://$user:$pass\@$host:$port/$dbname;$type";
153 154 155 156 157 158 159 160 161 162 163 164 165 166
    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 (
167
            -driver => $driver,
168 169
            -host   => $host,
            -port   => $port,
170 171
            -user   => $user,
            -pass   => $pass,
172 173
            -dbname => $dbname,
            -species => $dbname,
174
            -disconnect_when_inactive => $discon,
175
            -no_sql_schema_version_check => $nosqlvc,
176 177 178 179
        );
    }
    return $dba;
}
180

181
1;