00001 00002 =head1 LICENSE 00003 00004 Copyright [1999-2014] EMBL-European Bioinformatics Institute 00005 00006 Licensed under the Apache License, Version 2.0 (the "License"); 00007 you may not use this file except in compliance with the License. 00008 You may obtain a copy of the License at 00009 00010 http://www.apache.org/licenses/LICENSE-2.0 00011 00012 Unless required by applicable law or agreed to in writing, software 00013 distributed under the License is distributed on an "AS IS" BASIS, 00014 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00015 See the License for the specific language governing permissions and 00016 limitations under the License. 00017 00018 =cut 00019 00020 =pod 00021 00022 =head1 NAME 00023 00024 Bio::EnsEMBL::MetaData::DBSQL::EventInfoAdaptor 00025 00026 =head1 SYNOPSIS 00027 00028 # metadata_db is an instance of MetaDataDBAdaptor 00029 my $adaptor = $metadata_db->get_EventInfoAdaptor(); 00030 my $events = $adaptor->fetch_events($info); 00031 00032 =head1 DESCRIPTION 00033 00034 Adaptor for storing and retrieving EventInfo objects from MySQL ensembl_metadata database 00035 00036 =head1 SEE ALSO 00037 00038 Bio::EnsEMBL::MetaData::EventInfo 00039 Bio::EnsEMBL::MetaData::DatabaseInfo 00040 Bio::EnsEMBL::MetaData::GenomeComparaInfo 00041 Bio::EnsEMBL::MetaData::GenomeInfo 00042 00043 =head1 AUTHOR 00044 00045 Dan Staines 00046 00047 =cut 00048 00049 package Bio::EnsEMBL::MetaData::DBSQL::EventInfoAdaptor; 00050 00051 use strict; 00052 use warnings; 00053 00054 use base qw/Bio::EnsEMBL::MetaData::DBSQL::BaseInfoAdaptor/; 00055 00056 use Carp qw(cluck croak); 00057 use Bio::EnsEMBL::Utils::Argument qw( rearrange ); 00058 use Bio::EnsEMBL::Utils::Exception qw( throw ); 00059 use Bio::EnsEMBL::MetaData::DataReleaseInfo; 00060 use List::MoreUtils qw(natatime); 00061 00062 =head1 METHODS 00063 =cut 00064 00065 my $tables = {"Bio::EnsEMBL::MetaData::GenomeInfo" => "genome", 00066 "Bio::EnsEMBL::MetaData::GenomeComparaInfo" => "compara_analysis", 00067 "Bio::EnsEMBL::MetaData::DatabaseInfo" => "data_release_database" 00068 }; 00069 =head2 store 00070 Arg : Bio::EnsEMBL::MetaData::DatabaseInfo 00071 Description: Store the supplied object 00072 Returntype : none 00073 Exceptions : none 00074 Caller : general 00075 Status : Stable 00076 =cut 00077 00078 sub store { 00079 my ( $self, $event ) = @_; 00080 my $table = $tables->{ ref( $event->subject() ) }; 00081 my $id = $table . '_id'; 00082 my $event_table = $table . '_event'; 00083 if(!defined $event->subject()->dbID()) { 00084 throw "Cannot store an event on an object that has not been stored first"; 00085 } 00086 if ( !defined $event->dbID() ) { 00087 $self->dbc()->sql_helper()->execute_update( 00088 -SQL => qq/insert into $event_table($id,type,source,details) 00089 values (?,?,?,?)/, 00090 -PARAMS => [ $event->subject()->dbID(), $event->type(), 00091 $event->source(), $event->details() ], 00092 -CALLBACK => sub { 00093 my ( $sth, $dbh, $rv ) = @_; 00094 $event->dbID( $dbh->{mysql_insertid} ); 00095 } ); 00096 $event->adaptor($self); 00097 $self->_store_cached_obj($event); 00098 } 00099 else { 00100 throw "Cannot store an event that has been stored already"; 00101 } 00102 return; 00103 } 00104 =head2 update 00105 Arg : Bio::EnsEMBL::MetaData::DatabaseInfo 00106 Description: Update the supplied object (must be previously stored) 00107 Returntype : none 00108 Exceptions : none 00109 Caller : general 00110 Status : Stable 00111 =cut 00112 sub update { 00113 my ( $self, $event ) = @_; 00114 throw "Cannot update an event"; 00115 } 00116 =head2 fetch_events 00117 Arg : Bio::EnsEMBL::MetaData::GenomeInfo 00118 or Bio::EnsEMBL::MetaData::GenomeComparaInfo 00119 or Bio::EnsEMBL::MetaData::DatabaseInfo 00120 Description: Retrieve events associated with the supplied object 00121 Returntype : Arrayref of Bio::EnsEMBL::MetaData::EventInfo 00122 Exceptions : none 00123 Caller : general 00124 Status : Stable 00125 =cut 00126 sub fetch_events { 00127 my ( $self, $subject ) = @_; 00128 my $table = $tables->{ ref($subject) }; 00129 my $id = $table . '_id'; 00130 my $event_table = $table . '_event'; 00131 my $event_table_id = $event_table . '_id'; 00132 return $self->dbc()->sql_helper()->execute( 00133 -SQL => qq/select $event_table_id, type, source, details, creation_time 00134 from $event_table where $id=? order by creation_time asc/, 00135 -CALLBACK => sub { 00136 my @row = @{ shift @_ }; 00137 return 00138 Bio::EnsEMBL::MetaData::EventInfo->new( -DBID => $row[0], 00139 -SUBJECT => $subject, 00140 -TYPE => $row[1], 00141 -SOURCE => $row[2], 00142 -DETAILS => $row[3], 00143 -TIMESTAMP => $row[4] ); 00144 }, 00145 -PARAMS => [ $subject->dbID() ] ); 00146 } 00147 00148 1;