ensembl-hive  2.7.0
ObjectAdaptor.pm
Go to the documentation of this file.
1 =pod
2 
3 =head1 NAME
4 
6 
7 =head1 SYNOPSIS
8 
9  $object_adaptor = $dba->get_SpecificObjectAdaptor;
10  $object_adaptor = $specific_object->adaptor;
11 
12 =head1 DESCRIPTION
13 
14  This module defines a parent class for all specific object adaptors.
15  It is not supposed to be instantiated directly.
16 
17 =head1 LICENSE
18 
19  See the NOTICE file distributed with this work for additional information
20  regarding copyright ownership.
21 
22  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
23  You may obtain a copy of the License at
24 
25  http://www.apache.org/licenses/LICENSE-2.0
26 
27  Unless required by applicable law or agreed to in writing, software distributed under the License
28  is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29  See the License for the specific language governing permissions and limitations under the License.
30 
31 =head1 CONTACT
32 
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 =cut
36 
37 
38 package Bio::EnsEMBL::Hive::DBSQL::ObjectAdaptor;
39 
40 use strict;
41 use warnings;
42 
43 use base ('Bio::EnsEMBL::Hive::DBSQL::BaseAdaptor');
44 
45 
46 sub object_class {
47  die "Please define object_class() in your specific adaptor class to return the class name of your intended object";
48 }
49 
50 
51 sub slicer { # take a slice of the object (if only we could inline in Perl!)
52  my ($self, $object, $fields) = @_;
53 
54  my $autoinc_id = $self->autoinc_id();
55  my $overflow_limit = $self->overflow_limit();
56 
57  return [ map { ($_ eq $autoinc_id)
58  ? $object->dbID()
59  : eval { my $value = $object->$_();
60  my $ol = $overflow_limit->{$_};
61  (defined($ol) and length($value)>$ol)
62  ? $self->db->get_AnalysisDataAdaptor()->store_if_needed( $value )
63  : $value
64  }
65  } @$fields ];
66 }
67 
68 
69 sub objectify { # turn the hashref into an object (if only we could inline in Perl!)
70  my ($self, $hashref) = @_;
71 
72  my $autoinc_id = $self->autoinc_id();
73 
74  my $object = $self->object_class()->new( 'adaptor' => $self, map { ( ($_ eq $autoinc_id) ? 'dbID' : $_ ) => $hashref->{$_} } keys %$hashref );
75  $object->hive_pipeline($self->db->hive_pipeline) if $self->db->hive_pipeline and $object->can('hive_pipeline');
76  return $object;
77 }
78 
79 
80 sub mark_stored {
81  my ($self, $object, $dbID) = @_;
82 
83  if($self->autoinc_id()) {
84  $object->dbID($dbID);
85  }
86  $object->adaptor($self);
87 }
88 
89 
90 sub keys_to_columns {
91  my ($self, $object) = @_;
92 
93  my $autoinc_id = $self->autoinc_id();
94  my $sorted_keys = [ sort grep { ($_ ne $autoinc_id) and defined($object->$_()) } keys %{ $self->column_set() } ];
95 
96  return ( $sorted_keys, join(', ', @$sorted_keys) );
97 }
98 
99 
100 =head2 refresh
101 
103  Description: reload the object from the database
104  Returntype : The object with the same reference but with reloaded data.
105 
106 =cut
107 
108 sub refresh {
109  my ($self, $object) = @_;
110 
111  my $new_object = $self->fetch_by_dbID( $object->dbID ); # fetch into a separate object
112 
113  my $orig_hive_pipeline = exists $object->{'_hive_pipeline'} && $object->hive_pipeline;
114  %$object = %$new_object; # copy the data over
115  $object->hive_pipeline($orig_hive_pipeline) if $orig_hive_pipeline;
116 
117  return $object;
118 }
119 
120 1;
121 
map
public map()
Bio::EnsEMBL::Hive::Version
Definition: Version.pm:19
Bio::EnsEMBL::Hive::Cacheable::hive_pipeline
public hive_pipeline()
Bio::EnsEMBL::Hive::DBSQL::ObjectAdaptor
Definition: ObjectAdaptor.pm:18
Bio::EnsEMBL::Hive::Cacheable
Definition: Cacheable.pm:6
Bio::EnsEMBL::Hive
Definition: Hive.pm:38
Bio::EnsEMBL::Hive::Storable
Definition: Storable.pm:20