Bio::EnsEMBL::DBSQL BaseMetaContainer
SummaryIncluded librariesPackage variablesSynopsisDescriptionGeneral documentationMethods
Toolbar
WebCvsRaw content
Summary
Bio::EnsEMBL::DBSQL::BaseMetaContainer - Encapsulates all generic access
to database meta information
Package variables
No package variables defined.
Included modules
Bio::EnsEMBL::DBSQL::BaseAdaptor
Bio::EnsEMBL::Utils::Exception qw ( throw deprecate warning )
Inherit
Bio::EnsEMBL::DBSQL::BaseAdaptor
Synopsis
  my $meta_container = $db_adaptor->get_MetaContainer();
my @mapping_info = @{ $meta_container->list_value_by_key('assembly.mapping') };
Description
  An object that encapsulates access to db meta data
Methods
_species_specific_key
No description
Code
delete_keyDescriptionCode
delete_key_valueDescriptionCode
get_schema_versionDescriptionCode
key_value_existsDescriptionCode
list_value_by_keyDescriptionCode
store_key_valueDescriptionCode
update_key_valueDescriptionCode
Methods description
delete_keycode    nextTop
  Arg [1]    : string $key
The key which should be removed from the database.
Example : $meta_container->delete_key('sequence.compression');
Description: Removes all rows from the meta table which have a meta_key
equal to $key.
Returntype : none
Exceptions : none
Caller : dna_compress script, general
Status : Stable
delete_key_valuecodeprevnextTop
  Arg [1]    : string $key
The key which should be removed from the database.
Arg [2] : string $value
The value to be removed.
Example : $meta_container->delete_key('patch', 'patch_39_40_b.sql|xref_unique_constraint');
Description: Removes all rows from the meta table which have a meta_key
equal to $key, AND a meta_value equal to $value.
Returntype : none
Exceptions : none
Caller : general
Status : Stable
get_schema_versioncodeprevnextTop
  Arg [1]    : none
Example : $schema_ver = $meta_container->get_schema_version();
Description: Retrieves the schema version from the database meta table
Returntype : int
Exceptions : none
Caller : ?
Status : Medium risk
key_value_existscodeprevnextTop
  Arg [1]    : string $key
the key to check
Arg [2] : string $value
the value to check
Example : if ($meta_container->key_value_exists($key, $value)) ...
Description: Return true (1) if a particular key/value pair exists,
false (0) otherwise.
Returntype : boolean
Exceptions : none
Caller : ?
Status : Stable
list_value_by_keycodeprevnextTop
  Arg [1]    : string $key
the key to obtain values from the meta table with
Example : my @values = $meta_container->list_value_by_key($key);
Description: gets a value for a key. Can be anything
Returntype : listref of strings
Exceptions : none
Caller : ?
Status : Stable
store_key_valuecodeprevnextTop
  Arg [1]    : string $key
a key under which $value should be stored
Arg [2] : string $value
the value to store in the meta table
Example : $meta_container->store_key_value($key, $value);
Description: stores a value in the meta container, accessable by a key
Returntype : none
Exceptions : Thrown if the key/value already exists.
Caller : ?
Status : Stable
update_key_valuecodeprevnextTop
  Arg [1]    : string $key
a key under which $value should be updated
Arg [2] : string $value
the value to update in the meta table
Example : $meta_container->update_key_value($key, $value);
Description: update a value in the meta container, accessable by a key
Returntype : none
Exceptions : none
Caller : ?
Status : Stable
Methods code
_species_specific_keydescriptionprevnextTop
sub _species_specific_key {
  my ( $self, $key ) = @_;
  return ( $key ne 'patch' && $key ne 'schema_version' );
}

1;
}
delete_keydescriptionprevnextTop
sub delete_key {
  my ( $self, $key ) = @_;

  my $sth;

  if ( ref($self) =~ /Compara|Funcgen|Variation/ ) {
    $sth = $self->prepare( 'DELETE FROM meta WHERE meta_key = ?' );
  } elsif ( !$self->_species_specific_key($key) ) {
    $sth =
      $self->prepare(   'DELETE FROM meta '
                      . 'WHERE meta_key = ?'
                      . 'AND species_id IS NULL' );
  } else {
    $sth =
      $self->prepare(   'DELETE FROM meta '
                      . 'WHERE meta_key = ? '
                      . 'AND species_id = ?' );
    $sth->bind_param( 2, $self->species_id(), SQL_INTEGER );
  }

  $sth->bind_param( 1, $key, SQL_VARCHAR );
  $sth->execute();

  delete $self->{'cache'}->{$key};
}
delete_key_valuedescriptionprevnextTop
sub delete_key_value {
  my ( $self, $key, $value ) = @_;

  my $sth;

  if ( ref($self) =~ /Compara|Funcgen|Variation/ ) {
    $sth =
      $self->prepare(   'DELETE FROM meta '
                      . 'WHERE meta_key = ? '
                      . 'AND meta_value = ?' );
  } elsif ( !$self->_species_specific_key($key) ) {
    $sth =
      $self->prepare(   'DELETE FROM meta '
                      . 'WHERE meta_key = ? '
                      . 'AND meta_value = ?'
                      . 'AND species_id IS NULL' );
  } else {
    $sth =
      $self->prepare(   'DELETE FROM meta '
                      . 'WHERE meta_key = ? '
                      . 'AND meta_value = ? '
                      . 'AND species_id = ?' );
    $sth->bind_param( 3, $self->species_id(), SQL_INTEGER );
  }

  $sth->bind_param( 1, $key,   SQL_VARCHAR );
  $sth->bind_param( 2, $value, SQL_VARCHAR );
  $sth->execute();

  delete $self->{'cache'}->{$key};
} ## end sub delete_key_value
}
get_schema_versiondescriptionprevnextTop
sub get_schema_version {
  my $self = shift;

  my $arrRef = $self->list_value_by_key('schema_version');

  if (@$arrRef) {
    my ($ver) = ( $arrRef->[0] =~ /^\s*(\d+)\s*$/ );
    if ( !defined($ver) ) {    # old style format
return 0; } return $ver; } else { warning( sprintf( "Please insert meta_key 'schema_version' " . "in meta table on core database '%s'\n", $self->dbc()->dbname() ) ); } return 0;
}
key_value_existsdescriptionprevnextTop
sub key_value_exists {
  my ( $self, $key, $value ) = @_;

  my $sth;

  if ( ref($self) =~ /Compara|Funcgen|Variation/ ) {
    $sth =
      $self->prepare(   'SELECT meta_value '
                      . 'FROM meta '
                      . 'WHERE meta_key = ? '
                      . 'AND meta_value = ?' );
  } elsif ( !$self->_species_specific_key($key) ) {
    $sth =
      $self->prepare(   'SELECT meta_value '
                      . 'FROM meta '
                      . 'WHERE meta_key = ? '
                      . 'AND meta_value = ?'
                      . 'AND species_id IS NULL' );
  } else {
    $sth =
      $self->prepare(   'SELECT meta_value '
                      . 'FROM meta '
                      . 'WHERE meta_key = ? '
                      . 'AND meta_value = ? '
                      . 'AND species_id = ?' );
    $sth->bind_param( 3, $self->species_id(), SQL_INTEGER );
  }

  $sth->bind_param( 1, $key,   SQL_VARCHAR );
  $sth->bind_param( 2, $value, SQL_VARCHAR );
  $sth->execute();

  while ( my $arrRef = $sth->fetchrow_arrayref() ) {
    if ( $arrRef->[0] eq $value ) {
      $sth->finish();
      return 1;
    }
  }

  return 0;
} ## end sub key_value_exists
# This utility method determines whether the key is a species-specific
# meta key or not. If the key is either 'patch' or 'schema_version',
# then it is not species-specific.
}
list_value_by_keydescriptionprevnextTop
sub list_value_by_key {
  my ( $self, $key ) = @_;

  $self->{'cache'} ||= {};

  if ( exists $self->{'cache'}->{$key} ) {
    return $self->{'cache'}->{$key};
  }

  my $sth;

  if ( ref($self) =~ /Compara|Funcgen|Variation/ ) {
    $sth =
      $self->prepare(   "SELECT meta_value "
                      . "FROM meta "
                      . "WHERE meta_key = ? "
                      . "ORDER BY meta_id" );
  } elsif ( !$self->_species_specific_key($key) ) {
    $sth =
      $self->prepare(   "SELECT meta_value "
                      . "FROM meta "
                      . "WHERE meta_key = ? "
                      . "AND species_id IS NULL "
                      . "ORDER BY meta_id" );
  } else {
    $sth =
      $self->prepare(   "SELECT meta_value "
                      . "FROM meta "
                      . "WHERE meta_key = ? "
                      . "AND species_id = ? "
                      . "ORDER BY meta_id" );
    $sth->bind_param( 2, $self->species_id(), SQL_INTEGER );
  }

  $sth->bind_param( 1, $key, SQL_VARCHAR );
  $sth->execute();

  my @result;
  while ( my $arrRef = $sth->fetchrow_arrayref() ) {
    push( @result, $arrRef->[0] );
  }

  $sth->finish();
  $self->{'cache'}->{$key} =\@ result;

  return\@ result;
} ## end sub list_value_by_key
}
store_key_valuedescriptionprevnextTop
sub store_key_value {
  my ( $self, $key, $value ) = @_;

  if ( $self->key_value_exists( $key, $value ) ) {
    warn(   "Key-value pair '$key'-'$value' "
          . "already exists in the meta table; "
          . "not storing duplicate" );
    return;
  }

  my $sth;

  if ( ref($self) =~ /Compara|Funcgen|Variation/ ) {
    $sth = $self->prepare(
                'INSERT INTO meta (meta_key, meta_value) VALUES(?, ?)');
  } elsif ( !$self->_species_specific_key($key) ) {
    $sth = $self->prepare(
                  'INSERT INTO meta (meta_key, meta_value, species_id) '
                    . 'VALUES(?, ?,\N )' );
  } else {
    $sth = $self->prepare(
                  'INSERT INTO meta (meta_key, meta_value, species_id) '
                    . 'VALUES (?, ?, ?)' );
    $sth->bind_param( 3, $self->species_id(), SQL_INTEGER );
  }

  $sth->bind_param( 1, $key,   SQL_VARCHAR );
  $sth->bind_param( 2, $value, SQL_VARCHAR );
  $sth->execute();

  $self->{'cache'} ||= {};

  delete $self->{'cache'}->{$key};
} ## end sub store_key_value
}
update_key_valuedescriptionprevnextTop
sub update_key_value {
  my ( $self, $key, $value ) = @_;

  my $sth;

  if ( ref($self) =~ /Compara|Funcgen|Variation/ ) {
    $sth = $self->prepare(
             'UPDATE meta SET meta_value = ? WHERE meta_key = ?' );
  } elsif ( !$self->_species_specific_key($key) ) {
    $sth =
      $self->prepare(   'UPDATE meta SET meta_value = ? '
                      . 'WHERE meta_key = ?'
                      . 'AND species_id IS NULL' );
  } else {
    $sth =
      $self->prepare(   'UPDATE meta '
                      . 'SET meta_value = ? '
                      . 'WHERE meta_key = ? '
                      . 'AND species_id = ?' );
    $sth->bind_param( 3, $self->species_id(), SQL_INTEGER );
  }

  $sth->bind_param( 1, $value, SQL_VARCHAR );
  $sth->bind_param( 2, $key,   SQL_VARCHAR );
  $sth->execute();

} ## end sub update_key_value
}
General documentation
LICENSETop
  Copyright (c) 1999-2009 The European Bioinformatics Institute and
Genome Research Limited. All rights reserved.
This software is distributed under a modified Apache license. For license details, please see /info/about/code_licence.html
CONTACTTop
  Please email comments or questions to the public Ensembl
developers list at <ensembl-dev@ebi.ac.uk>.
Questions may also be sent to the Ensembl help desk at <helpdesk@ensembl.org>.