Bio-Genex

 view release on metacpan or  search on metacpan

DBUtils/DBUtils.pm.in  view on Meta::CPAN

    $sql .= join(', ', @{$args{COLUMNS}});
    
    # and the table names
    unless (ref($args{COLUMNS}) eq 'ARRAY') {
      warn "create_select_sql: FROM must be an array reference of table names";
      return undef;
    }
    $sql .= ' FROM ';
    $sql .= join(', ', @{$args{FROM}});
    
    # and a where clause if requested
    if (exists $args{WHERE} && $args{WHERE} ne '') {
      $sql .= ' WHERE ' . $args{WHERE};
    }
    
    # and a where clause if requested
    if (exists $args{LIMIT} && $args{LIMIT} ne '') {
      $sql .= ' LIMIT ' . $args{LIMIT};
    }
  } else {
    carp "Bio::Genex::DBUtils::create_select_sql: Driver ", $dbh->dbms(), " not implemented";
  }

  return $sql;
}
#----------------------------------------------------------------------

=item lookup_usf_id($dbh,$sequence_feature_name,$approx)

Given a sequence feature name, this function returns the primary key
in the DB for that sequence feature.

If the optional C<$approx> flag is specified, then C<lookup_usf_id()>
will use wildcarding around C<$sequenc_feature_name> so the name does not have
to be an exact match.

=cut


sub lookup_usf_id {
  my ($dbh,$seq_feat,$approx) = @_;
  
  return lookup_id($dbh,'UserSequenceFeature','usf_name','usf_pk',
		   $seq_feat,$approx);;
}
#----------------------------------------------------------------------

=item assert_dbh($dbh)

This method ensures that $dbh is a valid instance of class
Bio::Genex::Connect, otherwise it triggers an exception.

=cut

sub assert_dbh {
  my $dbh = shift;
  return if ref($dbh) && $dbh->isa('Bio::Genex::Connect');

  # Oops, someone blew it, give them a useful error message
  # first figure out who called us
  my @caller = caller(1);
  my $subroutine = $caller[3];

  if (ref($dbh) && $dbh->isa('DBI::db')) {
    croak "Invalid DB handle. Cannot use DBI::connect()\nto create a DB handle for $subroutine,\nmust create DB handle using Bio::Genex::current_connection() or Bio::Genex::_connect()";
  } else {
    croak "$subroutine expected a database handle, but got: '$dbh'";
  }
}

=item assert_table_defined($dbh,$table_name)

This ensures that $table_name exists in $dbh, otherwise it triggers an
exception. This method caches its results for all tables it finds for
$dbh. This significantly speeds up all future calls for all tables.

=cut


sub assert_table_defined
{
  my ($dbh,$table) = @_;
  assert_dbh($dbh);
  if ($dbh->dbms() eq 'Pg') {
    $table = lc($table);	# this probably only works for postgres
  }
  # retrieve the table name cache for this DB, and use the cache
  my $cache = $dbh->tables();
  return if exists $cache->{$table}; # use the cache first

  # otherwise we need to access the DB
  assert_dbh($dbh);

  # verify presence of this table
  my $found = 0;
  my $modified = 0;
  my $sth = $dbh->table_info || die $DBI::errstr;
  while (my ($name,$type) = ($sth->fetchrow_array)[2..3]) {
    die $DBI::errstr if $sth->err;

    # we only want tables
    next unless $type eq 'TABLE';

    $modified = 1;

    # store this table in the cache for later
    $cache->{$name} = 1;
    if ($name eq $table) {
      $found = 1;
      last;
    }
  }
  $sth->finish;
  $dbh->tables($cache) if $modified; # update the cache, if modified

  die "Table $table not found" unless $found;
}

sub assert_blasthit {
  my $blasthit = shift;
  croak "Not a valid BlastHits entry" unless 



( run in 2.323 seconds using v1.01-cache-2.11-cpan-75ffa21a3d4 )