ObjectAdaptor.pm 3.53 KB
Newer Older
1 2 3 4
=pod

=head1 NAME

5
    Bio::EnsEMBL::Hive::DBSQL::ObjectAdaptor
6 7 8 9 10 11 12 13 14 15 16

=head1 SYNOPSIS

    $object_adaptor = $dba->get_SpecificObjectAdaptor;
    $object_adaptor = $specific_object->adaptor;

=head1 DESCRIPTION

    This module defines a parent class for all specific object adaptors.
    It is not supposed to be instantiated directly.

17 18
=head1 LICENSE

19
    Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
Brandon Walts's avatar
Brandon Walts committed
20
    Copyright [2016-2020] EMBL-European Bioinformatics Institute
21 22 23 24 25 26 27 28 29 30

    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.

31 32
=head1 CONTACT

33
    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
34 35 36 37 38 39 40

=cut


package Bio::EnsEMBL::Hive::DBSQL::ObjectAdaptor;

use strict;
41
use warnings;
42 43 44 45 46 47 48 49 50 51 52 53

use base ('Bio::EnsEMBL::Hive::DBSQL::BaseAdaptor');


sub object_class {
    die "Please define object_class() in your specific adaptor class to return the class name of your intended object";
}


sub slicer {    # take a slice of the object (if only we could inline in Perl!)
    my ($self, $object, $fields) = @_;

54 55 56 57 58 59 60 61 62 63 64 65
    my $autoinc_id      = $self->autoinc_id();
    my $overflow_limit  = $self->overflow_limit();

    return [ map { ($_ eq $autoinc_id)
                    ? $object->dbID()
                    : eval { my $value  = $object->$_();
                             my $ol     = $overflow_limit->{$_};
                             (defined($ol) and length($value)>$ol)
                                ? $self->db->get_AnalysisDataAdaptor()->store_if_needed( $value )
                                : $value
                      }
                 } @$fields ];
66 67 68 69 70 71
}


sub objectify { # turn the hashref into an object (if only we could inline in Perl!)
    my ($self, $hashref) = @_;

72 73
    my $autoinc_id = $self->autoinc_id();

74
    return $self->object_class()->new( 'adaptor' => $self, map { ( ($_ eq $autoinc_id) ? 'dbID' : $_ ) => $hashref->{$_} } keys %$hashref );
75 76 77 78 79 80 81 82 83 84 85 86 87
}


sub mark_stored {
    my ($self, $object, $dbID) = @_;

    if($self->autoinc_id()) {
        $object->dbID($dbID);
    }
    $object->adaptor($self);
}


88 89 90 91 92 93 94 95 96 97
sub keys_to_columns {
    my ($self, $object) = @_;

    my $autoinc_id  = $self->autoinc_id();
    my $sorted_keys = [ sort grep { ($_ ne $autoinc_id) and defined($object->$_()) } keys %{ $self->column_set() } ];

    return ( $sorted_keys, join(', ', @$sorted_keys) );
}


98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
=head2 refresh

  Arg [1]    : Bio::EnsEMBL::Hive::Storable and Bio::EnsEMBL::Hive::Cacheable object
  Description: reload the object from the database
  Returntype : The object with the same reference but with reloaded data.

=cut

sub refresh {
    my ($self, $object) = @_;

    my $new_object = $self->fetch_by_dbID( $object->dbID );     # fetch into a separate object

    my $orig_hive_pipeline = exists $object->{'_hive_pipeline'} && $object->hive_pipeline;
    %$object = %$new_object;                                    # copy the data over
    $object->hive_pipeline($orig_hive_pipeline) if $orig_hive_pipeline;

    return $object;
}
117

118 119
1;