3 See the NOTICE file distributed with
this work
for additional information
4 regarding copyright ownership.
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
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.
23 Please email comments or questions to the
public Ensembl
24 developers list at <http:
26 Questions may also be sent to the Ensembl help desk at
34 to database meta information
38 my $meta_container = $db_adaptor->get_MetaContainer();
45 An
object that encapsulates access to db meta data
51 package Bio::EnsEMBL::DBSQL::BaseMetaContainer;
61 # new() is inherited from Bio::EnsEMBL::DBSQL::BaseAdaptor
63 =head2 get_schema_version
66 Example : $schema_ver = $meta_container->get_schema_version();
67 Description: Retrieves the schema version from the database meta table
75 sub get_schema_version {
78 my $arrRef = $self->list_value_by_key(
'schema_version');
81 my ($ver) = ( $arrRef->[0] =~ /^\s*(\d+)\s*$/ );
82 if ( !defined($ver) ) { # old style format
85 return $ver * 1; #multiply by 1 to get
this into a number
89 "Please insert meta_key 'schema_version' "
90 .
"in meta table on core database '%s'\n",
91 $self->dbc()->dbname() ) );
98 =head2 list_value_by_key
100 Arg [1] :
string $key
101 the key to obtain values from the meta table with
102 Example : my @values = @{ $meta_container->list_value_by_key($key) };
103 Description: gets a value
for a key. Can be anything
104 Returntype : listref of strings
111 sub list_value_by_key {
112 my ( $self, $key ) = @_;
114 $self->{
'cache'} ||= {};
116 if ( exists $self->{
'cache'}->{$key} ) {
117 return $self->{
'cache'}->{$key};
122 if ( !$self->_species_specific_key($key) ) {
124 $self->prepare(
"SELECT meta_value "
126 .
"WHERE meta_key = ? "
127 .
"AND species_id IS NULL "
128 .
"ORDER BY meta_id" );
131 $self->prepare(
"SELECT meta_value "
133 .
"WHERE meta_key = ? "
134 .
"AND species_id = ? "
135 .
"ORDER BY meta_id" );
136 $sth->bind_param( 2, $self->species_id(), SQL_INTEGER );
139 $sth->bind_param( 1, $key, SQL_VARCHAR );
143 while ( my $arrRef = $sth->fetchrow_arrayref() ) {
144 push( @result, $arrRef->[0] );
148 $self->{
'cache'}->{$key} = \@result;
151 } ## end sub list_value_by_key
153 =head2 single_value_by_key
155 Arg [1] :
string $key
156 the key to obtain values from the meta table with
157 Arg [2] :
boolean $warn
158 If
true will cause the code to warn the non-existence of a value
159 Example : my $value = $mc->single_value_by_key($key);
160 Description: Gets a value
for a key. Can be anything
162 Exceptions : Raised
if more than 1 meta item is returned
166 sub single_value_by_key {
167 my ($self, $key, $warn) = @_;
168 my $results = $self->list_value_by_key($key);
169 if(defined $results) {
170 my $count = scalar(@{$results});
172 my ($value) = @{$results};
177 my $group = $self->db()->group();
178 my $msg = sprintf(qq{Please insert meta_key
'%s' in meta table at %s db\n}, $key, $group);
183 my $values = join(q{,}, @{$results});
184 throw sprintf(q{Found the values [%s]
for the key
'%s'}, $values, $key);
188 } ## end sub single_value_by_key
190 =head2 store_key_value
192 Arg [1] :
string $key
193 a key under which $value should be stored
194 Arg [2] :
string $value
195 the value to store in the meta table
196 Example : $meta_container->store_key_value($key, $value);
197 Description: stores a value in the meta container, accessable by a key
199 Exceptions : Thrown
if the key/value already exists.
205 sub store_key_value {
206 my ( $self, $key, $value ) = @_;
208 if ( $self->key_value_exists( $key, $value ) ) {
209 warn(
"Key-value pair '$key'-'$value' "
210 .
"already exists in the meta table; "
211 .
"not storing duplicate" );
217 if ( !$self->_species_specific_key($key) ) {
218 $sth = $self->prepare(
219 'INSERT INTO meta (meta_key, meta_value, species_id) '
220 .
'VALUES(?, ?, \N)' );
222 $sth = $self->prepare(
223 'INSERT INTO meta (meta_key, meta_value, species_id) '
224 .
'VALUES (?, ?, ?)' );
225 $sth->bind_param( 3, $self->species_id(), SQL_INTEGER );
228 $sth->bind_param( 1, $key, SQL_VARCHAR );
229 $sth->bind_param( 2, $value, SQL_VARCHAR );
232 $self->{
'cache'} ||= {};
234 delete $self->{
'cache'}->{$key};
235 } ## end sub store_key_value
237 =head2 update_key_value
239 Arg [1] :
string $key
240 a key under which $value should be updated
241 Arg [2] :
string $value
242 the value to update in the meta table
243 Example : $meta_container->update_key_value($key, $value);
244 Description: update a value in the meta container, accessable by a key
252 sub update_key_value {
253 my ( $self, $key, $value ) = @_;
257 if ( !$self->_species_specific_key($key) ) {
259 $self->prepare(
'UPDATE meta SET meta_value = ? '
260 .
'WHERE meta_key = ?'
261 .
'AND species_id IS NULL' );
264 $self->prepare(
'UPDATE meta '
265 .
'SET meta_value = ? '
266 .
'WHERE meta_key = ? '
267 .
'AND species_id = ?' );
268 $sth->bind_param( 3, $self->species_id(), SQL_INTEGER );
271 $sth->bind_param( 1, $value, SQL_VARCHAR );
272 $sth->bind_param( 2, $key, SQL_VARCHAR );
275 } ## end sub update_key_value
280 Arg [1] :
string $key
281 The key which should be removed from the database.
282 Example : $meta_container->delete_key(
'sequence.compression');
283 Description: Removes all rows from the meta table which have a meta_key
287 Caller : dna_compress script, general
293 my ( $self, $key ) = @_;
297 if ( !$self->_species_specific_key($key) ) {
299 $self->prepare(
'DELETE FROM meta '
300 .
'WHERE meta_key = ?'
301 .
'AND species_id IS NULL' );
304 $self->prepare(
'DELETE FROM meta '
305 .
'WHERE meta_key = ? '
306 .
'AND species_id = ?' );
307 $sth->bind_param( 2, $self->species_id(), SQL_INTEGER );
310 $sth->bind_param( 1, $key, SQL_VARCHAR );
313 delete $self->{
'cache'}->{$key};
316 =head2 delete_key_value
318 Arg [1] :
string $key
319 The key which should be removed from the database.
320 Arg [2] :
string $value
321 The value to be removed.
322 Example : $meta_container->delete_key(
'patch',
'patch_39_40_b.sql|xref_unique_constraint');
323 Description: Removes all rows from the meta table which have a meta_key
324 equal to $key, AND a meta_value equal to $value.
332 sub delete_key_value {
333 my ( $self, $key, $value ) = @_;
337 if ( !$self->_species_specific_key($key) ) {
339 $self->prepare(
'DELETE FROM meta '
340 .
'WHERE meta_key = ? '
341 .
'AND meta_value = ?'
342 .
'AND species_id IS NULL' );
345 $self->prepare(
'DELETE FROM meta '
346 .
'WHERE meta_key = ? '
347 .
'AND meta_value = ? '
348 .
'AND species_id = ?' );
349 $sth->bind_param( 3, $self->species_id(), SQL_INTEGER );
352 $sth->bind_param( 1, $key, SQL_VARCHAR );
353 $sth->bind_param( 2, $value, SQL_VARCHAR );
356 delete $self->{
'cache'}->{$key};
357 } ## end sub delete_key_value
359 =head2 key_value_exists
361 Arg [1] :
string $key
363 Arg [2] :
string $value
365 Example :
if ($meta_container->key_value_exists($key, $value)) ...
366 Description: Return
true (1)
if a particular key/value pair exists,
375 sub key_value_exists {
376 my ( $self, $key, $value ) = @_;
380 if ( !$self->_species_specific_key($key) ) {
382 $self->prepare(
'SELECT meta_value '
384 .
'WHERE meta_key = ? '
385 .
'AND meta_value = ?'
386 .
'AND species_id IS NULL' );
389 $self->prepare(
'SELECT meta_value '
391 .
'WHERE meta_key = ? '
392 .
'AND meta_value = ? '
393 .
'AND species_id = ?' );
394 $sth->bind_param( 3, $self->species_id(), SQL_INTEGER );
397 $sth->bind_param( 1, $key, SQL_VARCHAR );
398 $sth->bind_param( 2, $value, SQL_VARCHAR );
401 return 0 unless $sth->fetchrow_arrayref();
405 } ## end sub key_value_exists
407 # This utility method determines whether the key is a species-specific
408 # meta key or not. If the key is either 'patch' or 'schema_version'
409 # or 'schema_type', then it is not species-specific.
411 # FIXME variation team messed up in release 65 and added the ploidy
412 # entry without species_id - this will be corrected for release 66,
413 # for now, I've added it to the list of allowed non-species specific
415 sub _species_specific_key {
416 my ( $self, $key ) = @_;
418 return ( $key ne
'patch'
419 && $key ne
'schema_version'
420 && $key ne
'schema_type');