EnsEMBL/DBSQL/MiscSetAdaptor.pm untested

filter: Statementstmt 0.0% 0 / 96 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 6 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 136 CC 9 SCAR 24.2
CC
9
Cov
0%
store
30.0
update
12.0
fetch_by_dbID
6.0
CRAP 11.3
1 =head1 LICENSE
2  
3 See the NOTICE file distributed with this work for additional information
4 regarding copyright ownership.
5  
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9  
10 http://www.apache.org/licenses/LICENSE-2.0
11  
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17  
18 =cut
19  
20  
21 =head1 CONTACT
22  
23 Please email comments or questions to the public Ensembl
24 developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.
25  
26 Questions may also be sent to the Ensembl help desk at
27 <http://www.ensembl.org/Help/Contact>.
28  
29 =cut
30  
31 =head1 NAME
32  
33 Bio::EnsEMBL::DBSQL::MiscSetAdaptor - Provides database interaction for
34 Bio::EnsEMBL::MiscSet objects.
35  
36 =head1 SYNOPSIS
37  
38 my $msa = $registry->get_adaptor( 'Human', 'Core', 'MiscSet' );
39  
40 my $misc_set = $msa->fetch_by_dbID(1234);
41  
42 $misc_set = $msa->fetch_by_code('clone');
43  
44 =head1 DESCRIPTION
45  
46 This class provides database interactivity for MiscSet objects.
47 MiscSets are used to classify MiscFeatures into groups.
48  
49 =head1 METHODS
50  
51 =cut
52  
53 package Bio::EnsEMBL::DBSQL::MiscSetAdaptor;
54 $Bio::EnsEMBL::DBSQL::MiscSetAdaptor::VERSION = '114.0.0_50'; # TRIAL
55 $Bio::EnsEMBL::DBSQL::MiscSetAdaptor::VERSION = '114.0.050';
56 use strict;
57 use warnings;
58  
59 use Bio::EnsEMBL::MiscSet;
60 use Bio::EnsEMBL::DBSQL::BaseAdaptor;
61  
62 use Bio::EnsEMBL::Utils::Exception qw(throw warning);
63  
64 use vars qw(@ISA);
65  
66 @ISA = qw(Bio::EnsEMBL::DBSQL::BaseAdaptor);
67  
68  
69 =head2 new
70  
71 Arg [...] : Superclass args. See Bio::EnsEMBL::DBSQL::BaseAdaptor
72 Description: Instantiates a Bio::EnsEMBL::DBSQL::MiscSetAdaptor and
73 caches the contents of the MiscSet table.
74 Returntype : Bio::EnsEMBL::MiscSet
75 Exceptions : none
76 Caller : MiscFeatureAdaptor
77 Status : Stable
78  
79 =cut
80  
81  
82 sub new {
83 my $class = shift;
84  
85 my $self = $class->SUPER::new(@_);
86  
87 $self->{'_id_cache'} = {};
88 $self->{'_code_cache'} = {};
89  
90 # cache the entire contents of the misc set table
91 # the table is small and it removes the need to repeatedly query the
92 # table or join to the table
93  
94 $self->fetch_all();
95  
96 return $self;
97 }
98  
99  
100  
101  
102 =head2 fetch_all
103  
104 Arg [1] : none
105 Example : foreach my $ms (@{$msa->fetch_all()}) {
106 print $ms->code(), ' ', $ms->name(), "\n";
107 }
108 Description: Retrieves every MiscSet defined in the DB.
109 NOTE: In a multi-species database, this method will
110 return all the entries matching the search criteria, not
111 just the ones associated with the current species.
112 Returntype : listref of Bio::EnsEMBL::MiscSets
113 Exceptions : none
114 Caller : general
115 Status : Stable
116  
117 =cut
118  
119 sub fetch_all {
120 my $self = shift;
121  
122 my $sth = $self->prepare
123 ('SELECT misc_set_id, code, name, description, max_length FROM misc_set');
124  
125 $sth->execute();
126  
127 my ($dbID, $code, $name, $desc, $max_len);
128 $sth->bind_columns(\$dbID, \$code, \$name, \$desc, \$max_len);
129  
130 my @all;
131  
132 while($sth->fetch()) {
133 my $ms = Bio::EnsEMBL::MiscSet->new
134 (-DBID => $dbID,
135 -ADAPTOR => $self,
136 -CODE => $code,
137 -NAME => $name,
138 -DESCRIPTION => $desc,
139 -LONGEST_FEATURE => $max_len);
140  
141 $self->{'_id_cache'}->{$dbID} = $ms;
142 $self->{'_code_cache'}->{lc($code)} = $ms;
143 push @all, $ms;
144 }
145  
146 $sth->finish();
147  
148 return \@all;
149 }
150  
151  
152  
153 =head2 fetch_by_dbID
154  
155 Arg [1] : int $dbID
156 The internal identifier of the misc set to retrieve
157 Example : my $ms = $msa->fetch_by_dbID($dbID);
158 Description: Retrieves a misc set via its internal identifier
159 Returntype : Bio::EnsEMBL::MiscSet
160 Exceptions : none
161 Caller : general
162 Status : Stable
163  
164 =cut
165  
166 sub fetch_by_dbID {
167 my $self = shift;
168 my $dbID = shift;
169  
170 if(!$self->{'_id_cache'}->{$dbID}) {
171 # on a cache miss reread the whole table and reload the cache
172 $self->fetch_all();
173 }
174  
175 return $self->{'_id_cache'}->{$dbID};
176 }
177  
178  
179  
180 =head2 fetch_by_code
181  
182 Arg [1] : string $code
183 The unique code of the MiscSet to retrieve
184 Example : my $ms = $msa->fetch_by_code('clone');
185 Description: Retrieves a MiscSet via its code
186 Returntype : Bio::EnsEMBL::MiscSet
187 Exceptions : none
188 Caller : general
189 Status : Stable
190  
191 =cut
192  
193 sub fetch_by_code {
194 my $self = shift;
195 my $code = shift;
196  
197 if(!$self->{'_code_cache'}->{lc($code)}) {
198 # on cache miss, reread whole table and reload cache
199 $self->fetch_all();
200 }
201  
202 return $self->{'_code_cache'}->{lc($code)};
203 }
204  
205  
206  
207 =head2 store
208  
209 Arg [1] : list of MiscSets @mist_sets
210 Example : $misc_set_adaptor->store(@misc_sets);
211 Description: Stores a list of MiscSets in the database, and sets the
212 dbID and adaptor attributes of the stored sets.
213 Returntype : none
214 Exceptions : throw on incorrect arguments
215 warning if a feature is already stored in this database
216 Caller : MiscFeatureAdaptor::store
217 Status : Stable
218  
219 =cut
220  
221 sub store {
222 my $self = shift;
223 my @misc_sets = @_;
224  
225 # we use 'insert ignore' so that inserts can occur safely on the farm
226 # otherwise 2 processes could try to insert at the same time and one
227 # would fail
228  
229 my $insert_ignore = $self->insert_ignore_clause();
230 my $sth = $self->prepare(
231 qq{${insert_ignore} INTO misc_set (
232 code,
233 name,
234 description,
235 max_length
236 ) VALUES (?, ?, ?, ?)
237 });
238  
239 my $db = $self->db();
240  
241 SET:
242 foreach my $ms (@misc_sets) {
243 if(!ref($ms) || !$ms->isa('Bio::EnsEMBL::MiscSet')) {
244 throw("List of MiscSet arguments expected.");
245 }
246  
247 if($ms->is_stored($db)) {
248 warning("MiscSet [".$ms->dbID."] is already stored in this database.");
249 next SET;
250 }
251  
252 $sth->bind_param(1,$ms->code,SQL_VARCHAR);
253 $sth->bind_param(2,$ms->name,SQL_VARCHAR);
254 $sth->bind_param(3,$ms->description,SQL_LONGVARCHAR);
255 $sth->bind_param(4,$ms->longest_feature,SQL_INTEGER);
256  
257 my $num_inserted = $sth->execute();
258  
259 my $dbID;
260  
261 if($num_inserted == 0) {
262 # insert failed because set with this code already exists
263 my $sth2 = $self->prepare("SELECT misc_set_id from misc_set " .
264 "WHERE code = ?");
265 $sth2->bind_param(1,$ms->code,SQL_VARCHAR);
266 $sth2->execute();
267  
268 ($dbID) = $sth2->fetchrow_array();
269  
270 if($sth2->rows() != 1) {
271 throw("Could not retrieve or store MiscSet, code=[".$ms->code."]\n".
272 "Wrong database user/permissions?");
273 }
274 } else {
275 $dbID = $self->last_insert_id('misc_set_id', undef, 'misc_set');
276 }
277  
278 $ms->dbID($dbID);
279 $ms->adaptor($self);
280  
281 # update the internal caches
282 $self->{'_id_cache'}->{$dbID} = $ms;
283 $self->{'_code_cache'}->{lc($ms->code())} = $ms;
284 }
285  
286 return;
287 }
288  
289 =head2 update
290  
291 Arg [1] : Bio::EnsEMBL::MiscSet $miscset
292 Example : $adaptor->update($miscset)
293 Description: Updates this misc_set in the database
294 Returntype : int 1 if update is performed, undef if it is not
295 Exceptions : throw if arg is not an misc_set object
296 Caller : ?
297 Status : Stable
298  
299 =cut
300  
301 sub update {
302 my $self = shift;
303 my $m = shift;
304  
305 if (!ref($m) || !$m->isa('Bio::EnsEMBL::MiscSet')) {
306 throw("Expected Bio::EnsEMBL::MiscSet argument.");
307 }
308  
309 if(!$m->is_stored($self->db())) {
310 return undef;
311 }
312  
313 my $sth = $self->prepare("UPDATE misc_set ".
314 "SET code =?, name =?, description = ?, max_length = ? ".
315 "WHERE misc_set_id = ?");
316  
317 $sth->bind_param(1,$m->code,SQL_VARCHAR);
318 $sth->bind_param(2,$m->name,SQL_VARCHAR);
319 $sth->bind_param(3,$m->description,SQL_VARCHAR);
320 $sth->bind_param(4,$m->longest_feature,SQL_INTEGER);
321 $sth->bind_param(5,$m->dbID,SQL_INTEGER);
322  
323 $sth->execute();
324 $sth->finish();
325  
326 # update the internal caches
327 $self->{'_id_cache'}->{$m->dbID} = $m;
328 $self->{'_code_cache'}->{lc($m->code())} = $m;
329 }
330  
331 1;