0
|
1 =head1 LICENSE
|
|
2
|
|
3 Copyright (c) 1999-2012 The European Bioinformatics Institute and
|
|
4 Genome Research Limited. All rights reserved.
|
|
5
|
|
6 This software is distributed under a modified Apache license.
|
|
7 For license details, please see
|
|
8
|
|
9 http://www.ensembl.org/info/about/code_licence.html
|
|
10
|
|
11 =head1 CONTACT
|
|
12
|
|
13 Please email comments or questions to the public Ensembl
|
|
14 developers list at <dev@ensembl.org>.
|
|
15
|
|
16 Questions may also be sent to the Ensembl help desk at
|
|
17 <helpdesk@ensembl.org>.
|
|
18
|
|
19 =cut
|
|
20
|
|
21 =head1 NAME
|
|
22
|
|
23 Bio::EnsEMBL::DBSQL::ProxyDBConnection - Database connection wrapper allowing
|
|
24 for one backing connection to be used for multiple DBs
|
|
25
|
|
26 =head1 SYNOPSIS
|
|
27
|
|
28 my $dbc = Bio::EnsEMBL::DBSQL::DBConnection->new(-HOST => 'host', -PORT => 3306, -USER => 'user');
|
|
29 my $p_h_dbc = Bio::EnsEMBL::DBSQL::ProxyDBConnection->new(-DBC => $dbc, -DBNAME => 'human');
|
|
30 my $p_m_dbc = Bio::EnsEMBL::DBSQL::ProxyDBConnection->new(-DBC => $dbc, -DBNAME => 'mouse');
|
|
31
|
|
32 =head1 DESCRIPTION
|
|
33
|
|
34 This class is used to maintain one active connection to a database whilst it
|
|
35 appears to be working against multiple schemas. It does this by checking the
|
|
36 currently connected database before performing any query which could require
|
|
37 a database change such as prepare.
|
|
38
|
|
39 This class is only intended for internal use so please do not use unless
|
|
40 you are aware of what it will do and what the consequences of its usage are.
|
|
41
|
|
42 =head1 METHODS
|
|
43
|
|
44 =cut
|
|
45
|
|
46 package Bio::EnsEMBL::DBSQL::ProxyDBConnection;
|
|
47
|
|
48 use strict;
|
|
49 use warnings;
|
|
50
|
|
51 use Bio::EnsEMBL::Utils::Exception qw/warning/;
|
|
52 use Bio::EnsEMBL::Utils::Argument qw/rearrange/;
|
|
53
|
|
54 use base qw/Bio::EnsEMBL::Utils::Proxy/;
|
|
55
|
|
56 sub new {
|
|
57 my ($class, @args) = @_;
|
|
58 my ($dbc, $dbname) = rearrange([qw/DBC DBNAME/], @args);
|
|
59 my $self = $class->SUPER::new($dbc);
|
|
60 $self->dbname($dbname);
|
|
61 return $self;
|
|
62 }
|
|
63
|
|
64 =head2 switch_database
|
|
65
|
|
66 Description : Performs a switch of the backing DBConnection if the currently
|
|
67 connected database is not the same as the database this proxy
|
|
68 wants to connect to. It currently supports MySQL, Oracle and
|
|
69 Postges switches is untested with all bar MySQL. If it
|
|
70 cannot do a live DB/schema switch then it will disconnect
|
|
71 the connection and then wait for the next process to
|
|
72 connect therefore switching the DB.
|
|
73 Exceptions : None but will warn if you attempt to switch a DB with
|
|
74 active kids attached to the proxied database handle.
|
|
75
|
|
76 =cut
|
|
77
|
|
78 sub switch_database {
|
|
79 my ($self) = @_;
|
|
80 my $proxy = $self->__proxy();
|
|
81 my $backing_dbname = $proxy->dbname();
|
|
82 my $dbname = $self->dbname();
|
|
83
|
|
84 my $switch = 0;
|
|
85 if(defined $dbname) {
|
|
86 if(defined $backing_dbname) {
|
|
87 $switch = ($dbname ne $backing_dbname) ? 1 : 0;
|
|
88 }
|
|
89 else {
|
|
90 $switch = 1;
|
|
91 }
|
|
92 }
|
|
93 else {
|
|
94 $switch = 1 if defined $backing_dbname;
|
|
95 }
|
|
96
|
|
97 if($switch) {
|
|
98 $proxy->dbname($dbname);
|
|
99 if($proxy->connected()) {
|
|
100 my $kids = $proxy->db_handle()->{Kids};
|
|
101 my $driver = lc($proxy->driver());
|
|
102 #Edit to add other DB switching strategies on a per driver basis
|
|
103 if($driver eq 'mysql') {
|
|
104 $proxy->do('use '.$dbname);
|
|
105 }
|
|
106 elsif($driver eq 'oracle') {
|
|
107 $proxy->do('ALTER SESSION SET CURRENT_SCHEMA = '.$dbname);
|
|
108 }
|
|
109 elsif($driver eq 'pg') {
|
|
110 $proxy->do('set search_path to '.$dbname);
|
|
111 }
|
|
112 else {
|
|
113 if($kids > 0) {
|
|
114 warning "Attempting a database switch from '$backing_dbname' to '$dbname' with $kids active handle(s). Check your logic or do not use a ProxyDBConnection";
|
|
115 }
|
|
116 $proxy->disconnect_if_idle();
|
|
117 }
|
|
118 }
|
|
119 }
|
|
120
|
|
121 return $switch;
|
|
122 }
|
|
123
|
|
124 sub dbname {
|
|
125 my ($self, $dbname) = @_;
|
|
126 $self->{'dbname'} = $dbname if defined $dbname;
|
|
127 return $self->{'dbname'};
|
|
128 }
|
|
129
|
|
130 my %SWITCH_METHODS = map { $_ => 1 } qw/
|
|
131 connect
|
|
132 db_handle
|
|
133 do
|
|
134 prepare
|
|
135 reconnect
|
|
136 work_with_db_handle
|
|
137 /;
|
|
138
|
|
139 sub __resolver {
|
|
140 my ($self, $package, $method) = @_;
|
|
141 if($self->__proxy()->can($method)) {
|
|
142 if($SWITCH_METHODS{$method}) {
|
|
143 return sub {
|
|
144 my ($local_self, @args) = @_;
|
|
145 $local_self->switch_database();
|
|
146 return $local_self->__proxy()->$method(@args);
|
|
147 };
|
|
148 }
|
|
149 else {
|
|
150 return sub {
|
|
151 my ($local_self, @args) = @_;
|
|
152 return $local_self->__proxy()->$method(@args);
|
|
153 };
|
|
154 }
|
|
155 }
|
|
156 return;
|
|
157 }
|
|
158
|
|
159 1;
|