EnsEMBL/DBSQL/BaseMetaContainer.pm untested

filter: Statementstmt 0.0% 0 / 114 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 10 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 176 CC 16 SCAR 25.0
CC
16
Cov
0%
single_value_by_key
30.0
get_schema_version
12.0
list_value_by_key
12.0
CRAP 12.1
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::BaseMetaContainer - Encapsulates all generic access
34 to database meta information
35  
36 =head1 SYNOPSIS
37  
38 my $meta_container = $db_adaptor->get_MetaContainer();
39  
40 my @mapping_info =
41 @{ $meta_container->list_value_by_key('assembly.mapping') };
42  
43 =head1 DESCRIPTION
44  
45 An object that encapsulates access to db meta data
46  
47 =head1 METHODS
48  
49 =cut
50  
51 package Bio::EnsEMBL::DBSQL::BaseMetaContainer;
52 $Bio::EnsEMBL::DBSQL::BaseMetaContainer::VERSION = '114.0.0_50'; # TRIAL
53 $Bio::EnsEMBL::DBSQL::BaseMetaContainer::VERSION = '114.0.050';
54 use vars qw(@ISA);
55 use strict;
56  
57 use Bio::EnsEMBL::DBSQL::BaseAdaptor;
58 use Bio::EnsEMBL::Utils::Exception qw(throw warning);
59  
60 @ISA = qw(Bio::EnsEMBL::DBSQL::BaseAdaptor);
61  
62 # new() is inherited from Bio::EnsEMBL::DBSQL::BaseAdaptor
63  
64 =head2 get_schema_version
65  
66 Arg [1] : none
67 Example : $schema_ver = $meta_container->get_schema_version();
68 Description: Retrieves the schema version from the database meta table
69 Returntype : int
70 Exceptions : none
71 Caller : ?
72 Status : Medium risk
73  
74 =cut
75  
76 sub get_schema_version {
77 my $self = shift;
78  
79 my $arrRef = $self->list_value_by_key('schema_version');
80  
81 if (@$arrRef) {
82 my ($ver) = ( $arrRef->[0] =~ /^\s*(\d+)\s*$/ );
83 if ( !defined($ver) ) { # old style format
84 return 0;
85 }
86 return $ver * 1; #multiply by 1 to get this into a number
87 } else {
88 warning(
89 sprintf(
90 "Please insert meta_key 'schema_version' "
91 . "in meta table on core database '%s'\n",
92 $self->dbc()->dbname() ) );
93 }
94  
95 return 0;
96 }
97  
98  
99 =head2 list_value_by_key
100  
101 Arg [1] : string $key
102 the key to obtain values from the meta table with
103 Example : my @values = @{ $meta_container->list_value_by_key($key) };
104 Description: gets a value for a key. Can be anything
105 Returntype : listref of strings
106 Exceptions : none
107 Caller : ?
108 Status : Stable
109  
110 =cut
111  
112 sub list_value_by_key {
113 my ( $self, $key ) = @_;
114  
115 $self->{'cache'} ||= {};
116  
117 if ( exists $self->{'cache'}->{$key} ) {
118 return $self->{'cache'}->{$key};
119 }
120  
121 my $sth;
122  
123 if ( !$self->_species_specific_key($key) ) {
124 $sth =
125 $self->prepare( "SELECT meta_value "
126 . "FROM meta "
127 . "WHERE meta_key = ? "
128 . "AND species_id IS NULL "
129 . "ORDER BY meta_id" );
130 } else {
131 $sth =
132 $self->prepare( "SELECT meta_value "
133 . "FROM meta "
134 . "WHERE meta_key = ? "
135 . "AND species_id = ? "
136 . "ORDER BY meta_id" );
137 $sth->bind_param( 2, $self->species_id(), SQL_INTEGER );
138 }
139  
140 $sth->bind_param( 1, $key, SQL_VARCHAR );
141 $sth->execute();
142  
143 my @result;
144 while ( my $arrRef = $sth->fetchrow_arrayref() ) {
145 push( @result, $arrRef->[0] );
146 }
147  
148 $sth->finish();
149 $self->{'cache'}->{$key} = \@result;
150  
151 return \@result;
152 } ## end sub list_value_by_key
153  
154 =head2 single_value_by_key
155  
156 Arg [1] : string $key
157 the key to obtain values from the meta table with
158 Arg [2] : boolean $warn
159 If true will cause the code to warn the non-existence of a value
160 Example : my $value = $mc->single_value_by_key($key);
161 Description: Gets a value for a key. Can be anything
162 Returntype : Scalar
163 Exceptions : Raised if more than 1 meta item is returned
164  
165 =cut
166  
167 sub single_value_by_key {
168 my ($self, $key, $warn) = @_;
169 my $results = $self->list_value_by_key($key);
170 if(defined $results) {
171 my $count = scalar(@{$results});
172 if($count == 1) {
173 my ($value) = @{$results};
174 return $value;
175 }
176 elsif($count == 0) {
177 if($warn) {
178 my $group = $self->db()->group();
179 my $msg = sprintf(qq{Please insert meta_key '%s' in meta table at %s db\n}, $key, $group);
180 warning($msg);
181 }
182 }
183 else {
184 my $values = join(q{,}, @{$results});
185 throw sprintf(q{Found the values [%s] for the key '%s'}, $values, $key);
186 }
187 }
188 return;
189 } ## end sub single_value_by_key
190  
191 =head2 store_key_value
192  
193 Arg [1] : string $key
194 a key under which $value should be stored
195 Arg [2] : string $value
196 the value to store in the meta table
197 Example : $meta_container->store_key_value($key, $value);
198 Description: stores a value in the meta container, accessable by a key
199 Returntype : none
200 Exceptions : Thrown if the key/value already exists.
201 Caller : ?
202 Status : Stable
203  
204 =cut
205  
206 sub store_key_value {
207 my ( $self, $key, $value ) = @_;
208  
209 if ( $self->key_value_exists( $key, $value ) ) {
210 warn( "Key-value pair '$key'-'$value' "
211 . "already exists in the meta table; "
212 . "not storing duplicate" );
213 return;
214 }
215  
216 my $sth;
217  
218 if ( !$self->_species_specific_key($key) ) {
219 $sth = $self->prepare(
220 'INSERT INTO meta (meta_key, meta_value, species_id) '
221 . 'VALUES(?, ?, \N)' );
222 } else {
223 $sth = $self->prepare(
224 'INSERT INTO meta (meta_key, meta_value, species_id) '
225 . 'VALUES (?, ?, ?)' );
226 $sth->bind_param( 3, $self->species_id(), SQL_INTEGER );
227 }
228  
229 $sth->bind_param( 1, $key, SQL_VARCHAR );
230 $sth->bind_param( 2, $value, SQL_VARCHAR );
231 $sth->execute();
232  
233 $self->{'cache'} ||= {};
234  
235 delete $self->{'cache'}->{$key};
236 } ## end sub store_key_value
237  
238 =head2 update_key_value
239  
240 Arg [1] : string $key
241 a key under which $value should be updated
242 Arg [2] : string $value
243 the value to update in the meta table
244 Example : $meta_container->update_key_value($key, $value);
245 Description: update a value in the meta container, accessable by a key
246 Returntype : none
247 Exceptions : none
248 Caller : ?
249 Status : Stable
250  
251 =cut
252  
253 sub update_key_value {
254 my ( $self, $key, $value ) = @_;
255  
256 my $sth;
257  
258 if ( !$self->_species_specific_key($key) ) {
259 $sth =
260 $self->prepare( 'UPDATE meta SET meta_value = ? '
261 . 'WHERE meta_key = ?'
262 . 'AND species_id IS NULL' );
263 } else {
264 $sth =
265 $self->prepare( 'UPDATE meta '
266 . 'SET meta_value = ? '
267 . 'WHERE meta_key = ? '
268 . 'AND species_id = ?' );
269 $sth->bind_param( 3, $self->species_id(), SQL_INTEGER );
270 }
271  
272 $sth->bind_param( 1, $value, SQL_VARCHAR );
273 $sth->bind_param( 2, $key, SQL_VARCHAR );
274 $sth->execute();
275  
276 } ## end sub update_key_value
277  
278  
279 =head2 delete_key
280  
281 Arg [1] : string $key
282 The key which should be removed from the database.
283 Example : $meta_container->delete_key('sequence.compression');
284 Description: Removes all rows from the meta table which have a meta_key
285 equal to $key.
286 Returntype : none
287 Exceptions : none
288 Caller : dna_compress script, general
289 Status : Stable
290  
291 =cut
292  
293 sub delete_key {
294 my ( $self, $key ) = @_;
295  
296 my $sth;
297  
298 if ( !$self->_species_specific_key($key) ) {
299 $sth =
300 $self->prepare( 'DELETE FROM meta '
301 . 'WHERE meta_key = ?'
302 . 'AND species_id IS NULL' );
303 } else {
304 $sth =
305 $self->prepare( 'DELETE FROM meta '
306 . 'WHERE meta_key = ? '
307 . 'AND species_id = ?' );
308 $sth->bind_param( 2, $self->species_id(), SQL_INTEGER );
309 }
310  
311 $sth->bind_param( 1, $key, SQL_VARCHAR );
312 $sth->execute();
313  
314 delete $self->{'cache'}->{$key};
315 }
316  
317 =head2 delete_key_value
318  
319 Arg [1] : string $key
320 The key which should be removed from the database.
321 Arg [2] : string $value
322 The value to be removed.
323 Example : $meta_container->delete_key('patch', 'patch_39_40_b.sql|xref_unique_constraint');
324 Description: Removes all rows from the meta table which have a meta_key
325 equal to $key, AND a meta_value equal to $value.
326 Returntype : none
327 Exceptions : none
328 Caller : general
329 Status : Stable
330  
331 =cut
332  
333 sub delete_key_value {
334 my ( $self, $key, $value ) = @_;
335  
336 my $sth;
337  
338 if ( !$self->_species_specific_key($key) ) {
339 $sth =
340 $self->prepare( 'DELETE FROM meta '
341 . 'WHERE meta_key = ? '
342 . 'AND meta_value = ?'
343 . 'AND species_id IS NULL' );
344 } else {
345 $sth =
346 $self->prepare( 'DELETE FROM meta '
347 . 'WHERE meta_key = ? '
348 . 'AND meta_value = ? '
349 . 'AND species_id = ?' );
350 $sth->bind_param( 3, $self->species_id(), SQL_INTEGER );
351 }
352  
353 $sth->bind_param( 1, $key, SQL_VARCHAR );
354 $sth->bind_param( 2, $value, SQL_VARCHAR );
355 $sth->execute();
356  
357 delete $self->{'cache'}->{$key};
358 } ## end sub delete_key_value
359  
360 =head2 key_value_exists
361  
362 Arg [1] : string $key
363 the key to check
364 Arg [2] : string $value
365 the value to check
366 Example : if ($meta_container->key_value_exists($key, $value)) ...
367 Description: Return true (1) if a particular key/value pair exists,
368 false (0) otherwise.
369 Returntype : boolean
370 Exceptions : none
371 Caller : ?
372 Status : Stable
373  
374 =cut
375  
376 sub key_value_exists {
377 my ( $self, $key, $value ) = @_;
378  
379 my $sth;
380  
381 if ( !$self->_species_specific_key($key) ) {
382 $sth =
383 $self->prepare( 'SELECT meta_value '
384 . 'FROM meta '
385 . 'WHERE meta_key = ? '
386 . 'AND meta_value = ?'
387 . 'AND species_id IS NULL' );
388 } else {
389 $sth =
390 $self->prepare( 'SELECT meta_value '
391 . 'FROM meta '
392 . 'WHERE meta_key = ? '
393 . 'AND meta_value = ? '
394 . 'AND species_id = ?' );
395 $sth->bind_param( 3, $self->species_id(), SQL_INTEGER );
396 }
397  
398 $sth->bind_param( 1, $key, SQL_VARCHAR );
399 $sth->bind_param( 2, $value, SQL_VARCHAR );
400 $sth->execute();
401  
402 return 0 unless $sth->fetchrow_arrayref();
403  
404 return 1;
405  
406 } ## end sub key_value_exists
407  
408 # This utility method determines whether the key is a species-specific
409 # meta key or not. If the key is either 'patch' or 'schema_version'
410 # or 'schema_type', then it is not species-specific.
411  
412 # FIXME variation team messed up in release 65 and added the ploidy
413 # entry without species_id - this will be corrected for release 66,
414 # for now, I've added it to the list of allowed non-species specific
415  
416 sub _species_specific_key {
417 my ( $self, $key ) = @_;
418  
419 return ( $key ne 'patch'
420 && $key ne 'schema_version'
421 && $key ne 'schema_type');
422 }
423  
424 1;