| 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'; | ||
| 53 | use vars qw(@ISA); | ||
| 54 | use strict; | ||
| 55 | |||
| 56 | use Bio::EnsEMBL::DBSQL::BaseAdaptor; | ||
| 57 | use Bio::EnsEMBL::Utils::Exception qw(throw warning); | ||
| 58 | |||
| 59 | @ISA = qw(Bio::EnsEMBL::DBSQL::BaseAdaptor); | ||
| 60 | |||
| 61 | # new() is inherited from Bio::EnsEMBL::DBSQL::BaseAdaptor | ||
| 62 | |||
| 63 | =head2 get_schema_version | ||
| 64 | |||
| 65 | Arg [1] : none | ||
| 66 | Example : $schema_ver = $meta_container->get_schema_version(); | ||
| 67 | Description: Retrieves the schema version from the database meta table | ||
| 68 | Returntype : int | ||
| 69 | Exceptions : none | ||
| 70 | Caller : ? | ||
| 71 | Status : Medium risk | ||
| 72 | |||
| 73 | =cut | ||
| 74 | |||
| 75 | sub get_schema_version { | ||
| 76 | my $self = shift; | ||
| 77 | |||
| 78 | my $arrRef = $self->list_value_by_key('schema_version'); | ||
| 79 | |||
| 80 | if (@$arrRef) { | ||
| 81 | my ($ver) = ( $arrRef->[0] =~ /^\s*(\d+)\s*$/ ); | ||
| 82 | if ( !defined($ver) ) { # old style format | ||
| 83 | return 0; | ||
| 84 | } | ||
| 85 | return $ver * 1; #multiply by 1 to get this into a number | ||
| 86 | } else { | ||
| 87 | warning( | ||
| 88 | sprintf( | ||
| 89 | "Please insert meta_key 'schema_version' " | ||
| 90 | . "in meta table on core database '%s'\n", | ||
| 91 | $self->dbc()->dbname() ) ); | ||
| 92 | } | ||
| 93 | |||
| 94 | return 0; | ||
| 95 | } | ||
| 96 | |||
| 97 | |||
| 98 | =head2 list_value_by_key | ||
| 99 | |||
| 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 | ||
| 105 | Exceptions : none | ||
| 106 | Caller : ? | ||
| 107 | Status : Stable | ||
| 108 | |||
| 109 | =cut | ||
| 110 | |||
| 111 | sub list_value_by_key { | ||
| 112 | my ( $self, $key ) = @_; | ||
| 113 | |||
| 114 | $self->{'cache'} ||= {}; | ||
| 115 | |||
| 116 | if ( exists $self->{'cache'}->{$key} ) { | ||
| 117 | return $self->{'cache'}->{$key}; | ||
| 118 | } | ||
| 119 | |||
| 120 | my $sth; | ||
| 121 | |||
| 122 | if ( !$self->_species_specific_key($key) ) { | ||
| 123 | $sth = | ||
| 124 | $self->prepare( "SELECT meta_value " | ||
| 125 | . "FROM meta " | ||
| 126 | . "WHERE meta_key = ? " | ||
| 127 | . "AND species_id IS NULL " | ||
| 128 | . "ORDER BY meta_id" ); | ||
| 129 | } else { | ||
| 130 | $sth = | ||
| 131 | $self->prepare( "SELECT meta_value " | ||
| 132 | . "FROM meta " | ||
| 133 | . "WHERE meta_key = ? " | ||
| 134 | . "AND species_id = ? " | ||
| 135 | . "ORDER BY meta_id" ); | ||
| 136 | $sth->bind_param( 2, $self->species_id(), SQL_INTEGER ); | ||
| 137 | } | ||
| 138 | |||
| 139 | $sth->bind_param( 1, $key, SQL_VARCHAR ); | ||
| 140 | $sth->execute(); | ||
| 141 | |||
| 142 | my @result; | ||
| 143 | while ( my $arrRef = $sth->fetchrow_arrayref() ) { | ||
| 144 | push( @result, $arrRef->[0] ); | ||
| 145 | } | ||
| 146 | |||
| 147 | $sth->finish(); | ||
| 148 | $self->{'cache'}->{$key} = \@result; | ||
| 149 | |||
| 150 | return \@result; | ||
| 151 | } ## end sub list_value_by_key | ||
| 152 | |||
| 153 | =head2 single_value_by_key | ||
| 154 | |||
| 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 | ||
| 161 | Returntype : Scalar | ||
| 162 | Exceptions : Raised if more than 1 meta item is returned | ||
| 163 | |||
| 164 | =cut | ||
| 165 | |||
| 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}); | ||
| 171 | if($count == 1) { | ||
| 172 | my ($value) = @{$results}; | ||
| 173 | return $value; | ||
| 174 | } | ||
| 175 | elsif($count == 0) { | ||
| 176 | if($warn) { | ||
| 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); | ||
| 179 | warning($msg); | ||
| 180 | } | ||
| 181 | } | ||
| 182 | else { | ||
| 183 | my $values = join(q{,}, @{$results}); | ||
| 184 | throw sprintf(q{Found the values [%s] for the key '%s'}, $values, $key); | ||
| 185 | } | ||
| 186 | } | ||
| 187 | return; | ||
| 188 | } ## end sub single_value_by_key | ||
| 189 | |||
| 190 | =head2 store_key_value | ||
| 191 | |||
| 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 | ||
| 198 | Returntype : none | ||
| 199 | Exceptions : Thrown if the key/value already exists. | ||
| 200 | Caller : ? | ||
| 201 | Status : Stable | ||
| 202 | |||
| 203 | =cut | ||
| 204 | |||
| 205 | sub store_key_value { | ||
| 206 | my ( $self, $key, $value ) = @_; | ||
| 207 | |||
| 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" ); | ||
| 212 | return; | ||
| 213 | } | ||
| 214 | |||
| 215 | my $sth; | ||
| 216 | |||
| 217 | if ( !$self->_species_specific_key($key) ) { | ||
| 218 | $sth = $self->prepare( | ||
| 219 | 'INSERT INTO meta (meta_key, meta_value, species_id) ' | ||
| 220 | . 'VALUES(?, ?, \N)' ); | ||
| 221 | } else { | ||
| 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 ); | ||
| 226 | } | ||
| 227 | |||
| 228 | $sth->bind_param( 1, $key, SQL_VARCHAR ); | ||
| 229 | $sth->bind_param( 2, $value, SQL_VARCHAR ); | ||
| 230 | $sth->execute(); | ||
| 231 | |||
| 232 | $self->{'cache'} ||= {}; | ||
| 233 | |||
| 234 | delete $self->{'cache'}->{$key}; | ||
| 235 | } ## end sub store_key_value | ||
| 236 | |||
| 237 | =head2 update_key_value | ||
| 238 | |||
| 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 | ||
| 245 | Returntype : none | ||
| 246 | Exceptions : none | ||
| 247 | Caller : ? | ||
| 248 | Status : Stable | ||
| 249 | |||
| 250 | =cut | ||
| 251 | |||
| 252 | sub update_key_value { | ||
| 253 | my ( $self, $key, $value ) = @_; | ||
| 254 | |||
| 255 | my $sth; | ||
| 256 | |||
| 257 | if ( !$self->_species_specific_key($key) ) { | ||
| 258 | $sth = | ||
| 259 | $self->prepare( 'UPDATE meta SET meta_value = ? ' | ||
| 260 | . 'WHERE meta_key = ?' | ||
| 261 | . 'AND species_id IS NULL' ); | ||
| 262 | } else { | ||
| 263 | $sth = | ||
| 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 ); | ||
| 269 | } | ||
| 270 | |||
| 271 | $sth->bind_param( 1, $value, SQL_VARCHAR ); | ||
| 272 | $sth->bind_param( 2, $key, SQL_VARCHAR ); | ||
| 273 | $sth->execute(); | ||
| 274 | |||
| 275 | } ## end sub update_key_value | ||
| 276 | |||
| 277 | |||
| 278 | =head2 delete_key | ||
| 279 | |||
| 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 | ||
| 284 | equal to $key. | ||
| 285 | Returntype : none | ||
| 286 | Exceptions : none | ||
| 287 | Caller : dna_compress script, general | ||
| 288 | Status : Stable | ||
| 289 | |||
| 290 | =cut | ||
| 291 | |||
| 292 | sub delete_key { | ||
| 293 | my ( $self, $key ) = @_; | ||
| 294 | |||
| 295 | my $sth; | ||
| 296 | |||
| 297 | if ( !$self->_species_specific_key($key) ) { | ||
| 298 | $sth = | ||
| 299 | $self->prepare( 'DELETE FROM meta ' | ||
| 300 | . 'WHERE meta_key = ?' | ||
| 301 | . 'AND species_id IS NULL' ); | ||
| 302 | } else { | ||
| 303 | $sth = | ||
| 304 | $self->prepare( 'DELETE FROM meta ' | ||
| 305 | . 'WHERE meta_key = ? ' | ||
| 306 | . 'AND species_id = ?' ); | ||
| 307 | $sth->bind_param( 2, $self->species_id(), SQL_INTEGER ); | ||
| 308 | } | ||
| 309 | |||
| 310 | $sth->bind_param( 1, $key, SQL_VARCHAR ); | ||
| 311 | $sth->execute(); | ||
| 312 | |||
| 313 | delete $self->{'cache'}->{$key}; | ||
| 314 | } | ||
| 315 | |||
| 316 | =head2 delete_key_value | ||
| 317 | |||
| 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. | ||
| 325 | Returntype : none | ||
| 326 | Exceptions : none | ||
| 327 | Caller : general | ||
| 328 | Status : Stable | ||
| 329 | |||
| 330 | =cut | ||
| 331 | |||
| 332 | sub delete_key_value { | ||
| 333 | my ( $self, $key, $value ) = @_; | ||
| 334 | |||
| 335 | my $sth; | ||
| 336 | |||
| 337 | if ( !$self->_species_specific_key($key) ) { | ||
| 338 | $sth = | ||
| 339 | $self->prepare( 'DELETE FROM meta ' | ||
| 340 | . 'WHERE meta_key = ? ' | ||
| 341 | . 'AND meta_value = ?' | ||
| 342 | . 'AND species_id IS NULL' ); | ||
| 343 | } else { | ||
| 344 | $sth = | ||
| 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 ); | ||
| 350 | } | ||
| 351 | |||
| 352 | $sth->bind_param( 1, $key, SQL_VARCHAR ); | ||
| 353 | $sth->bind_param( 2, $value, SQL_VARCHAR ); | ||
| 354 | $sth->execute(); | ||
| 355 | |||
| 356 | delete $self->{'cache'}->{$key}; | ||
| 357 | } ## end sub delete_key_value | ||
| 358 | |||
| 359 | =head2 key_value_exists | ||
| 360 | |||
| 361 | Arg [1] : string $key | ||
| 362 | the key to check | ||
| 363 | Arg [2] : string $value | ||
| 364 | the value to check | ||
| 365 | Example : if ($meta_container->key_value_exists($key, $value)) ... | ||
| 366 | Description: Return true (1) if a particular key/value pair exists, | ||
| 367 | false (0) otherwise. | ||
| 368 | Returntype : boolean | ||
| 369 | Exceptions : none | ||
| 370 | Caller : ? | ||
| 371 | Status : Stable | ||
| 372 | |||
| 373 | =cut | ||
| 374 | |||
| 375 | sub key_value_exists { | ||
| 376 | my ( $self, $key, $value ) = @_; | ||
| 377 | |||
| 378 | my $sth; | ||
| 379 | |||
| 380 | if ( !$self->_species_specific_key($key) ) { | ||
| 381 | $sth = | ||
| 382 | $self->prepare( 'SELECT meta_value ' | ||
| 383 | . 'FROM meta ' | ||
| 384 | . 'WHERE meta_key = ? ' | ||
| 385 | . 'AND meta_value = ?' | ||
| 386 | . 'AND species_id IS NULL' ); | ||
| 387 | } else { | ||
| 388 | $sth = | ||
| 389 | $self->prepare( 'SELECT meta_value ' | ||
| 390 | . 'FROM meta ' | ||
| 391 | . 'WHERE meta_key = ? ' | ||
| 392 | . 'AND meta_value = ? ' | ||
| 393 | . 'AND species_id = ?' ); | ||
| 394 | $sth->bind_param( 3, $self->species_id(), SQL_INTEGER ); | ||
| 395 | } | ||
| 396 | |||
| 397 | $sth->bind_param( 1, $key, SQL_VARCHAR ); | ||
| 398 | $sth->bind_param( 2, $value, SQL_VARCHAR ); | ||
| 399 | $sth->execute(); | ||
| 400 | |||
| 401 | return 0 unless $sth->fetchrow_arrayref(); | ||
| 402 | |||
| 403 | return 1; | ||
| 404 | |||
| 405 | } ## end sub key_value_exists | ||
| 406 | |||
| 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. | ||
| 410 | |||
| 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 | ||
| 414 | |||
| 415 | sub _species_specific_key { | ||
| 416 | my ( $self, $key ) = @_; | ||
| 417 | |||
| 418 | return ( $key ne 'patch' | ||
| 419 | && $key ne 'schema_version' | ||
| 420 | && $key ne 'schema_type'); | ||
| 421 | } | ||
| 422 | |||
| 423 | 1; |