buzgilla -> redmine
[bioperl-db.git] / lib / Bio / DB / BioSQL / Pg / TermAdaptorDriver.pm
blob6bb3b58021d47790daa5425e70676efe5613772e
1 # $Id$
3 # BioPerl module for Bio::DB::BioSQL::Pg::TermAdaptorDriver
5 # Please direct questions and support issues to <bioperl-l@bioperl.org>
7 # Cared for by Hilmar Lapp <hlapp at gmx.net>
9 # Copyright Hilmar Lapp
13 # (c) Hilmar Lapp, hlapp at gmx.net, 2003.
14 # (c) GNF, Genomics Institute of the Novartis Research Foundation, 2003.
16 # You may distribute this module under the same terms as perl itself.
17 # Refer to the Perl Artistic License (see the license accompanying this
18 # software package, or see http://www.perl.com/language/misc/Artistic.html)
19 # for the terms under which you may use, modify, and redistribute this module.
21 # THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
22 # WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
23 # MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
26 # POD documentation - main docs before the code
28 =head1 NAME
30 Bio::DB::BioSQL::Pg::TermAdaptorDriver - DESCRIPTION of Object
32 =head1 SYNOPSIS
34 Give standard usage here
36 =head1 DESCRIPTION
38 Describe the object here
40 =head1 FEEDBACK
42 =head2 Mailing Lists
44 User feedback is an integral part of the evolution of this and other
45 Bioperl modules. Send your comments and suggestions preferably to
46 the Bioperl mailing list. Your participation is much appreciated.
48 bioperl-l@bioperl.org - General discussion
49 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
51 =head2 Support
53 Please direct usage questions or support issues to the mailing list:
55 I<bioperl-l@bioperl.org>
57 rather than to the module maintainer directly. Many experienced and
58 reponsive experts will be able look at the problem and quickly
59 address it. Please include a thorough description of the problem
60 with code and data examples if at all possible.
62 =head2 Reporting Bugs
64 Report bugs to the Bioperl bug tracking system to help us keep track
65 of the bugs and their resolution. Bug reports can be submitted via
66 the web:
68 http://redmine.open-bio.org/projects/bioperl/
70 =head1 AUTHOR - Hilmar Lapp
72 Email hlapp at gmx.net
74 =head1 CONTRIBUTORS
76 Additional contributors names and emails here
78 =head1 APPENDIX
80 The rest of the documentation details each of the object methods.
81 Internal methods are usually preceded with a _
83 =cut
86 # Let the code begin...
89 package Bio::DB::BioSQL::Pg::TermAdaptorDriver;
90 use vars qw(@ISA);
91 use strict;
93 use Bio::DB::BioSQL::Pg::BasePersistenceAdaptorDriver;
95 @ISA = qw(Bio::DB::BioSQL::Pg::BasePersistenceAdaptorDriver);
98 =head2 remove_synonyms
100 Title : remove_synonyms
101 Usage :
102 Function: Removes all synonyms for an ontology term.
103 Example :
104 Returns : TRUE on success, and FALSE otherwise.
105 Args : The calling persistence adaptor.
107 The persistent term object for which to remove the synonyms
108 (a Bio::DB::PersistentObjectI compliant object with defined
109 primary key).
112 =cut
114 sub remove_synonyms{
115 my ($self,$adp,$obj) = @_;
117 # delete statement cached?
118 my $cachekey = "DELETE SYNONYMS";
119 my $sth = $adp->sth($cachekey);
120 # if not we need to build it
121 if(! $sth) {
122 # we need table name and foreign key
123 my $table = $self->table_name("TermSynonym");
124 my $fkname = $self->foreign_key_name($obj->obj);
125 # build, prepare, and cache the SQL statement
126 $sth = $self->_build_sth($adp, $cachekey,
127 "DELETE FROM $table WHERE $fkname = ?");
129 # bind parameters and execute insert
130 my $dbgmsg = "executing with values (".
131 $obj->primary_key().") (FK to ".ref($obj->obj).")";
132 $adp->debug("$cachekey: $dbgmsg\n");
133 my $rv = $sth->execute($obj->primary_key());
134 if(! $rv) {
135 $self->warn("failed to remove term synonyms (".ref($adp)
136 .") with values (".$obj->primary_key()
137 .") (FK to ".ref($obj->obj)."):\n".$sth->errstr());
139 return $rv;
142 =head2 store_synonym
144 Title : store_synonym
145 Usage :
146 Function: Stores a synonym for an ontology term.
147 Example :
148 Returns : TRUE on success, and FALSE otherwise.
149 Args : The calling persistence adaptor.
151 The persistent term object for which to store the synonym
152 (a Bio::DB::PersistentObjectI compliant object with defined
153 primary key).
155 The synonym to store (a scalar).
158 =cut
160 sub store_synonym{
161 my ($self,$adp,$obj,$syn) = @_;
163 # insert and look-up statements cached?
164 my $icachekey = "INSERT SYNONYM";
165 my $isth = $adp->sth($icachekey);
166 # if not we need to build them
167 if(! $isth) {
168 # we need table name, foreign key, and slot map
169 my $table = $self->table_name("TermSynonym");
170 my $fkname = $self->foreign_key_name($obj->obj);
171 my $colmap = $self->slot_attribute_map($table);
172 # build, prepare, and cache the SQL statements
173 $isth = $self->_build_sth($adp, $icachekey,
174 "INSERT INTO $table (".
175 join(", ", $colmap->{'synonym'}, $fkname).
176 ") VALUES (?, ?)");
178 # bind parameters and execute insert
179 my $dbgmsg = "executing with values ($syn, ".
180 $obj->primary_key().") (synonym, FK to ".ref($obj->obj).")";
181 $adp->debug("$icachekey: $dbgmsg\n");
182 my $rv = $isth->execute($syn, $obj->primary_key());
183 # in PostgreSQL, the UK failure is caught through a rule already,
184 # so if $rv evaluates to FALSE it must be the statement being bad
185 if (! $rv) {
186 $self->warn("failed to store term synonym (".ref($adp)
187 .") with values ($syn) (FK ".$obj->primary_key()
188 ." to ".ref($obj->obj)."):\n"
189 .$isth->errstr());
191 return $rv;
194 sub _build_sth{
195 my ($self,$adp,$cachekey,$sql) = @_;
196 # prepare and cache
197 $adp->debug("$cachekey: preparing: $sql\n");
198 my $sth = $adp->dbh->prepare($sql);
199 $self->throw("failed to prepare \"$sql\": ".$adp->dbh->errstr)
200 unless $sth;
201 $adp->sth($cachekey,$sth);
202 return $sth;
205 =head2 get_synonyms
207 Title : get_synonyms
208 Usage :
209 Function: Retrieves the synonyms for an ontology term and adds them
210 the term's synonyms.
211 Example :
212 Returns : TRUE on success, and FALSE otherwise.
213 Args : The calling persistence adaptor.
215 The persistent term object for which to retrieve the
216 synonyms (a Bio::DB::PersistentObjectI compliant object
217 with defined primary key).
220 =cut
222 sub get_synonyms{
223 my ($self,$adp,$obj) = @_;
225 # insert and look-up statements cached?
226 my $cachekey = "SELECT SYNONYMS";
227 my $sth = $adp->sth($cachekey);
228 # if not we need to build it
229 if(! $sth) {
230 # we need table name, foreign key, and slot map
231 my $table = $self->table_name("TermSynonym");
232 my $fkname = $self->foreign_key_name($obj->obj);
233 my $colmap = $self->slot_attribute_map($table);
234 # build, prepare, and cache the SQL statement
235 $sth = $self->_build_sth($adp, $cachekey,
236 "SELECT ".$colmap->{'synonym'}.
237 " FROM $table WHERE $fkname = ?");
239 # bind parameters and execute select
240 my $dbgmsg = "executing with values (".
241 $obj->primary_key().") (FK to ".ref($obj->obj).")";
242 $adp->debug("$cachekey: $dbgmsg\n");
243 my $rv = $sth->execute($obj->primary_key());
244 $self->warn("failed to execute $cachekey: ".$sth->errstr) unless $rv;
245 while(my $row = $sth->fetchrow_arrayref()) {
246 $obj->add_synonym($row->[0]);
248 return $rv;