From 9a1f8e72a836f67523754b565492b43d778b6f3c Mon Sep 17 00:00:00 2001
From: Leo Gordon <lg4@ebi.ac.uk>
Date: Tue, 21 Jan 2014 09:42:20 +0000
Subject: [PATCH] removed dependency on Core's DBSQL::BaseAdaptor and Storable
 by introducing own lightweight Hive::Storable

---
 modules/Bio/EnsEMBL/Hive/Analysis.pm          |   2 +-
 modules/Bio/EnsEMBL/Hive/AnalysisJob.pm       |   2 +-
 modules/Bio/EnsEMBL/Hive/AnalysisStats.pm     |   2 +-
 modules/Bio/EnsEMBL/Hive/DBSQL/BaseAdaptor.pm |  56 ++++++++-
 .../Bio/EnsEMBL/Hive/DBSQL/MetaContainer.pm   |   2 +-
 modules/Bio/EnsEMBL/Hive/DataflowRule.pm      |   2 +-
 modules/Bio/EnsEMBL/Hive/ResourceClass.pm     |   2 +-
 modules/Bio/EnsEMBL/Hive/Storable.pm          | 118 ++++++++++++++++++
 modules/Bio/EnsEMBL/Hive/Worker.pm            |   2 +-
 9 files changed, 179 insertions(+), 9 deletions(-)
 create mode 100644 modules/Bio/EnsEMBL/Hive/Storable.pm

diff --git a/modules/Bio/EnsEMBL/Hive/Analysis.pm b/modules/Bio/EnsEMBL/Hive/Analysis.pm
index 2dfec22ce..740b98cd3 100644
--- a/modules/Bio/EnsEMBL/Hive/Analysis.pm
+++ b/modules/Bio/EnsEMBL/Hive/Analysis.pm
@@ -41,7 +41,7 @@ use Bio::EnsEMBL::Utils::Argument ('rearrange');
 
 use Bio::EnsEMBL::Hive::Utils ('stringify');
 
-use base (  'Bio::EnsEMBL::Storable',       # inherit dbID(), adaptor() and new() methods
+use base (  'Bio::EnsEMBL::Hive::Storable',       # inherit dbID(), adaptor() and new() methods
          );
  
 
diff --git a/modules/Bio/EnsEMBL/Hive/AnalysisJob.pm b/modules/Bio/EnsEMBL/Hive/AnalysisJob.pm
index 8ca1dffdc..43f186860 100644
--- a/modules/Bio/EnsEMBL/Hive/AnalysisJob.pm
+++ b/modules/Bio/EnsEMBL/Hive/AnalysisJob.pm
@@ -44,7 +44,7 @@ use Bio::EnsEMBL::Hive::Utils ('stringify', 'destringify');
 use Bio::EnsEMBL::Hive::DBSQL::AnalysisJobAdaptor;
 use Bio::EnsEMBL::Hive::DBSQL::DataflowRuleAdaptor;
 
-use base (  'Bio::EnsEMBL::Storable',       # inherit dbID(), adaptor() and new() methods
+use base (  'Bio::EnsEMBL::Hive::Storable',       # inherit dbID(), adaptor() and new() methods
             'Bio::EnsEMBL::Hive::Params',   # inherit param management functionality
          );
 
diff --git a/modules/Bio/EnsEMBL/Hive/AnalysisStats.pm b/modules/Bio/EnsEMBL/Hive/AnalysisStats.pm
index f424579f8..6b96e5f0a 100644
--- a/modules/Bio/EnsEMBL/Hive/AnalysisStats.pm
+++ b/modules/Bio/EnsEMBL/Hive/AnalysisStats.pm
@@ -42,7 +42,7 @@ use Bio::EnsEMBL::Utils::Argument ('rearrange');
 use Bio::EnsEMBL::Utils::Exception ('throw');
 use Bio::EnsEMBL::Hive::Analysis;
 
-use base ( 'Bio::EnsEMBL::Storable' );  # inherit dbID(), adaptor() and new() methods
+use base ( 'Bio::EnsEMBL::Hive::Storable' );  # inherit dbID(), adaptor() and new() methods
 
 
     ## Minimum amount of time in msec that a worker should run before reporting
diff --git a/modules/Bio/EnsEMBL/Hive/DBSQL/BaseAdaptor.pm b/modules/Bio/EnsEMBL/Hive/DBSQL/BaseAdaptor.pm
index 1fa5ee4b2..b5f71fca5 100644
--- a/modules/Bio/EnsEMBL/Hive/DBSQL/BaseAdaptor.pm
+++ b/modules/Bio/EnsEMBL/Hive/DBSQL/BaseAdaptor.pm
@@ -39,8 +39,6 @@ use strict;
 no strict 'refs';   # needed to allow AUTOLOAD create new methods
 use DBI 1.6;        # the 1.6 functionality is important for detecting autoincrement fields and other magic.
 
-use base ('Bio::EnsEMBL::DBSQL::BaseAdaptor');
-
 
 sub default_table_name {
     die "Please define table_name either by setting it via table_name() method or by redefining default_table_name() in your adaptor class";
@@ -68,6 +66,60 @@ sub default_input_column_mapping {
     };
 }
 
+# ---------------------------------------------------------------------------
+
+sub new {
+    my ( $class, $dbobj ) = @_;
+
+    my $self = bless {}, $class;
+
+    if ( !defined $dbobj || !ref $dbobj ) {
+        throw("Don't have a db [$dbobj] for new adaptor");
+    }
+
+    if ( ref($dbobj) =~ /DBConnection$/ ) {
+        $self->dbc($dbobj);
+    } elsif( UNIVERSAL::can($dbobj, 'dbc') ) {
+        $self->dbc( $dbobj->dbc );
+        $self->db( $dbobj );
+    } else {
+        throw("I was given [$dbobj] for a new adaptor");
+    }
+
+    return $self;
+}
+
+
+sub db {
+    my $self = shift @_;
+
+    if(@_) {    # setter
+        $self->{_db} = shift @_;
+    }
+    return $self->{_db};
+}
+
+
+sub dbc {
+    my $self = shift @_;
+
+    if(@_) {    # setter
+        $self->{_dbc} = shift @_;
+    }
+    return $self->{_dbc};
+}
+
+
+sub prepare {
+    my ( $self, $sql ) = @_;
+
+    # Uncomment next line to cancel caching on the SQL side.
+    # Needed for timing comparisons etc.
+    #$sql =~ s/SELECT/SELECT SQL_NO_CACHE/i;
+
+    return $self->dbc->prepare($sql);
+}
+
 
 sub overflow_limit {
     my $self = shift @_;
diff --git a/modules/Bio/EnsEMBL/Hive/DBSQL/MetaContainer.pm b/modules/Bio/EnsEMBL/Hive/DBSQL/MetaContainer.pm
index 9706ee967..7580bc621 100644
--- a/modules/Bio/EnsEMBL/Hive/DBSQL/MetaContainer.pm
+++ b/modules/Bio/EnsEMBL/Hive/DBSQL/MetaContainer.pm
@@ -37,7 +37,7 @@ package Bio::EnsEMBL::Hive::DBSQL::MetaContainer;
 use strict;
 use Bio::EnsEMBL::Hive::Utils ('stringify', 'destringify');
 
-use base ('Bio::EnsEMBL::Hive::DBSQL::NakedTableAdaptor', 'Bio::EnsEMBL::DBSQL::BaseMetaContainer');
+use base ('Bio::EnsEMBL::Hive::DBSQL::NakedTableAdaptor');
 
 
 sub default_table_name {
diff --git a/modules/Bio/EnsEMBL/Hive/DataflowRule.pm b/modules/Bio/EnsEMBL/Hive/DataflowRule.pm
index dfcab628a..48e809b98 100644
--- a/modules/Bio/EnsEMBL/Hive/DataflowRule.pm
+++ b/modules/Bio/EnsEMBL/Hive/DataflowRule.pm
@@ -59,7 +59,7 @@ use Bio::EnsEMBL::Utils::Exception ('throw');
 use Bio::EnsEMBL::Hive::Utils ('stringify');
 use Bio::EnsEMBL::Hive::DBSQL::AnalysisAdaptor;
 
-use base (  'Bio::EnsEMBL::Storable',       # inherit dbID(), adaptor() and new() methods
+use base (  'Bio::EnsEMBL::Hive::Storable',       # inherit dbID(), adaptor() and new() methods
          );
 
 
diff --git a/modules/Bio/EnsEMBL/Hive/ResourceClass.pm b/modules/Bio/EnsEMBL/Hive/ResourceClass.pm
index 20df3ab67..4756ef78a 100644
--- a/modules/Bio/EnsEMBL/Hive/ResourceClass.pm
+++ b/modules/Bio/EnsEMBL/Hive/ResourceClass.pm
@@ -41,7 +41,7 @@ use strict;
 
 use Bio::EnsEMBL::Utils::Argument ('rearrange');
 
-use base (  'Bio::EnsEMBL::Storable',       # inherit dbID(), adaptor() and new() methods
+use base (  'Bio::EnsEMBL::Hive::Storable',       # inherit dbID(), adaptor() and new() methods
          );
  
 
diff --git a/modules/Bio/EnsEMBL/Hive/Storable.pm b/modules/Bio/EnsEMBL/Hive/Storable.pm
new file mode 100644
index 000000000..f08d088d3
--- /dev/null
+++ b/modules/Bio/EnsEMBL/Hive/Storable.pm
@@ -0,0 +1,118 @@
+=pod 
+
+=head1 NAME
+
+    Bio::EnsEMBL::Hive::Storable
+
+=head1 SYNOPSIS
+
+    my $dbID    = $storable_object->dbID();
+    my $adaptor = $storable_object->adaptor();
+
+=head1 DESCRIPTION
+
+    Storable is a base class for anything that can be stored.
+    It provides two getters/setters: dbID() and adaptor().
+
+=head1 LICENSE
+
+    Copyright [1999-2013] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
+
+    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.
+
+=head1 CONTACT
+
+    Please contact ehive-users@ebi.ac.uk mailing list with questions/suggestions.
+
+=head1 APPENDIX
+
+    The rest of the documentation details each of the object methods.
+    Internal methods are usually preceded with a _
+
+=cut
+
+
+package Bio::EnsEMBL::Hive::Storable;
+
+use strict;
+use warnings;
+
+use Bio::EnsEMBL::Utils::Argument qw(rearrange);
+use Scalar::Util qw(weaken);
+
+
+=head2 new
+
+  Arg [-ADAPTOR] : Bio::EnsEMBL::Hive::DBSQL::BaseAdaptor
+  Arg [-dbID]    : database internal id
+  Caller         : internal calls
+  Description    : create a new Storable object 
+  Returntype     : Bio::EnsEMBL::Hive::Storable
+  Exceptions     : Adaptor not a Bio::EnsEMBL::DBSQL::BaseAdaptor
+  Status         : Stable
+
+=cut
+
+sub new {
+    my $caller = shift;
+    my $class = ref($caller) || $caller;
+
+    my $self = bless {}, $class;
+
+    my ($adaptor, $dbID) = rearrange(['ADAPTOR', 'dbID'], @_);
+    $self->dbID( $dbID )        if defined( $dbID );
+    $self->adaptor($adaptor)    if defined( $adaptor );
+
+    return $self;
+}
+
+
+=head2 dbID
+
+  Arg [1]    : int $dbID
+  Description: getter/setter for the database internal id
+  Returntype : int
+  Exceptions : none
+  Caller     : general, set from adaptor on store
+  Status     : Stable
+
+=cut
+
+sub dbID {
+    my $self = shift;
+    $self->{'dbID'} = shift if(@_);
+    return $self->{'dbID'};
+}
+
+
+=head2 adaptor
+
+  Arg [1]    : Bio::EnsEMBL::DBSQL::BaseAdaptor $adaptor
+  Description: get/set for this objects Adaptor
+  Returntype : Bio::EnsEMBL::DBSQL::BaseAdaptor
+  Exceptions : none
+  Caller     : general, set from adaptor on store
+  Status     : Stable
+
+=cut
+
+sub adaptor {
+    my $self = shift;
+
+    if(@_) {
+        $self->{'adaptor'} = shift;
+        weaken( $self->{'adaptor'} ) if defined( $self->{'adaptor'} );
+    }
+
+    return $self->{'adaptor'};
+}
+
+1;
+
diff --git a/modules/Bio/EnsEMBL/Hive/Worker.pm b/modules/Bio/EnsEMBL/Hive/Worker.pm
index 780b1d40b..11ef740a1 100644
--- a/modules/Bio/EnsEMBL/Hive/Worker.pm
+++ b/modules/Bio/EnsEMBL/Hive/Worker.pm
@@ -89,7 +89,7 @@ use Bio::EnsEMBL::Hive::Utils::RedirectStack;
 use Bio::EnsEMBL::Hive::Utils::Stopwatch;
 use Bio::EnsEMBL::Hive::Utils ('stringify');
 
-use base (  'Bio::EnsEMBL::Storable',       # inherit dbID(), adaptor() and new() methods
+use base (  'Bio::EnsEMBL::Hive::Storable',       # inherit dbID(), adaptor() and new() methods
          );
 
 
-- 
GitLab