NakedTable.pm 3.38 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
=pod 

=head1 NAME

  Bio::EnsEMBL::Hive::NakedTable

=head1 DESCRIPTION

    A data container object that links together an adaptor, a table and a preferred insertion method (insert/insert-ignore/replace).
    This object is generated from specially designed datalow URLs.

12 13
=head1 LICENSE

14
    Copyright [1999-2014] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
15 16 17 18 19 20 21 22 23 24

    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.

25 26
=head1 CONTACT

27
  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
28 29 30

=cut

31

32 33 34
package Bio::EnsEMBL::Hive::NakedTable;

use strict;
35 36
use Scalar::Util ('weaken');

37
use Bio::EnsEMBL::Utils::Argument ('rearrange');
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

sub new {
    my $class = shift @_;

    my $self = bless {}, $class;

    my ($adaptor, $table_name, $insertion_method) = 
         rearrange([qw(adaptor table_name insertion_method) ], @_);

    $self->adaptor($adaptor)                    if(defined($adaptor));
    $self->table_name($table_name)              if(defined($table_name));
    $self->insertion_method($insertion_method)  if(defined($insertion_method));

    return $self;
}

54

55 56 57 58 59
sub adaptor {
    my $self = shift @_;

    if(@_) {
        $self->{'_adaptor'} = shift @_;
60
        weaken $self->{'_adaptor'};
61
    }
62

63 64 65
    return $self->{'_adaptor'};
}

66

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
sub table_name {
    my $self = shift @_;

    if(@_) {
        $self->{'_table_name'} = shift @_;
    }
    return $self->{'_table_name'};
}

sub insertion_method {
    my $self = shift @_;

    if(@_) {
        $self->{'_insertion_method'} = shift @_;
    }
82
    return $self->{'_insertion_method'} || 'INSERT_IGNORE';
83 84 85 86 87 88 89
}

sub url {
    my $self    = shift @_;
    my $ref_dba = shift @_;     # if reference dba is the same as 'our' dba, a shorter url can be generated

    if(my $adaptor = $self->adaptor) {
90
        my $conn_prefix = ($adaptor->db == $ref_dba) ? ':///' : $adaptor->db->dbc->url();
91 92 93 94 95 96 97
        return $conn_prefix .'/'. $self->table_name() . '?insertion_method=' . $self->insertion_method();
    } else {
        return;
    }
}

sub dataflow {
98
    my ( $self, $output_ids, $emitting_job ) = @_;
99

100 101 102 103 104 105 106
        # we have to do this the ugly way
        # because Registry code currently prevents us from passing arguments to adaptors' new() methods
        # (and by caching guarantees there is only one instance of each adaptor per DBAdaptor)
    my $adaptor = $self->adaptor();
    $adaptor->table_name( $self->table_name() );
    $adaptor->insertion_method( $self->insertion_method() );

107 108 109 110 111 112 113 114 115 116 117
    my @column_names = keys %{$self->adaptor->column_set};
    my @rows = ();

    foreach my $output_id (@$output_ids) {
        my %row_hash = ();
        foreach my $column (@column_names) {
            $row_hash{ $column } = $emitting_job->_param_possibly_overridden($column, $output_id);
        }
        push @rows, \%row_hash;
    }
    $adaptor->store( \@rows );
118 119 120 121 122
}


1;