Commit 5e8a3a94 authored by Andreas Kusalananda Kähäri's avatar Andreas Kusalananda Kähäri
Browse files

Merge from HEAD.

parent 8b57dba4
......@@ -286,6 +286,42 @@ sub _straight_join {
}
=head2 bind_param_generic_fetch
Arg [1] : (optional) scalar $param
This is the parameter to bind
Arg [2] : (optional) int $sql_type
Type of the parameter (from DBI (:sql_types))
Example : $adaptor->bind_param_generic_fetch($stable_id,SQL_VARCHAR);
$adaptor->generic_fetch();
Description: When using parameters for the query, will call the bind_param to avoid
some security issues. If there are no arguments, will return the bind_parameters
ReturnType : listref
Exceptions: if called with one argument
=cut
sub bind_param_generic_fetch{
my $self = shift;
my $param = shift;
my $sql_type = shift;
if (defined $param && !defined $sql_type){
throw("Need to specify sql_type for parameter $param\n");
}
elsif (defined $param && defined $sql_type){
#both paramters have been entered, push it to the bind_param array
push @{$self->{'_bind_param_generic_fetch'}},[$param,$sql_type];
}
elsif (!defined $param && !defined $sql_type){
#when there are no arguments, return the array
return $self->{'_bind_param_generic_fetch'};
}
}
=head2 generic_fetch
Arg [1] : (optional) string $constraint
......@@ -307,7 +343,6 @@ sub _straight_join {
sub generic_fetch {
my ($self, $constraint, $mapper, $slice) = @_;
my @tabs = $self->_tables;
my $columns = join(', ', $self->_columns());
......@@ -371,6 +406,18 @@ sub generic_fetch {
# printf(STDERR "SQL:\n%s\n", $sql);
my $sth = $db->dbc->prepare($sql);
my $bind_parameters = $self->bind_param_generic_fetch();
if (defined $bind_parameters){
#if we have bind the parameters, call the DBI to bind them
my $i = 1;
foreach my $param (@{$bind_parameters}){
$sth->bind_param($i,$param->[0],$param->[1]);
$i++;
}
#after binding parameters, undef for future queries
$self->{'_bind_param_generic_fetch'} = ();
}
# print STDERR $sql,"\n";
$sth->execute;
my $res = $self->_objs_from_sth($sth, $mapper, $slice);
$sth->finish();
......@@ -407,7 +454,8 @@ sub fetch_by_dbID{
#construct a constraint like 't1.table1_id = 123'
my @tabs = $self->_tables;
my ($name, $syn) = @{$tabs[0]};
my $constraint = "${syn}.${name}_id = $id";
$self->bind_param_generic_fetch($id,SQL_INTEGER);
my $constraint = "${syn}.${name}_id = ?";
#Should only be one
my ($feat) = @{$self->generic_fetch($constraint)};
......
......@@ -67,12 +67,45 @@ sub new {
#initialize an LRU cache
my %cache;
tie(%cache, 'Bio::EnsEMBL::Utils::Cache', $SLICE_FEATURE_CACHE_SIZE);
tie( %cache, 'Bio::EnsEMBL::Utils::Cache',
{ Debug => 0, MaxCount => $SLICE_FEATURE_CACHE_SIZE } );
$self->{'_slice_feature_cache'} = \%cache;
return $self;
}
=head2 clear_cache
Args : None
Example : my $sa =
$registry->get_adaptor( 'Mus musculus', 'Core',
'Slice' );
my $ga =
$registry->get_adaptor( 'Mus musculus', 'Core',
'Gene' );
my $slice =
$sa->fetch_by_region( 'Chromosome', '1', 1e8,
1.05e8 );
my $genes = $ga->fetch_all_by_Slice($slice);
$ga->clear_cache();
Description : Empties the feature cache associated with this
feature adaptor.
Return type : None
Exceptions : None
Caller : General
Status : At risk (under development)
=cut
sub clear_cache {
my ($self) = @_;
$self->{'_slice_feature_cache'} = ();
}
=head2 fetch_all_by_Slice
Arg [1] : Bio::EnsEMBL::Slice $slice
......
......@@ -219,18 +219,43 @@ sub connect {
'odbc_cursortype' => 2});
};
}
elsif ( $self->driver() eq "Sybase" ) {
$dsn = "DBI:" . $self->driver() .
":server=" . $self->host() .
";database=" . $self->dbname() .
";tdsLevel=CS_TDS_495";
eval{ $dbh = DBI->connect($dsn,
$self->username(),
$self->password(),
{'LongTruncOk' => 1,
'RaiseError' => 1,
'PrintError' => 0});
};
}
else{
$dsn = "DBI:" . $self->driver() .
":database=". $self->dbname() .
";host=" . $self->host() .
";port=" . $self->port();
":database=". $self->dbname() .
";host=" . $self->host() .
";port=" . $self->port();
eval{ $dbh = DBI->connect($dsn,
$self->username(),
$self->password(),
{'RaiseError' => 1});
};
}
};
}
if(!$dbh || $@ || !$dbh->ping()) {
warn("Could not connect to database " . $self->dbname() .
" as user " . $self->username() .
......
......@@ -1406,6 +1406,94 @@ sub fetch_all_by_description {
return \@results;
} ## end sub fetch_all_by_description
=head2 fetch_all_by_source
Arg [1] : string source to search for. Include % etc in this string
if you want to use SQL patterns
Example : @unigene_refs = @{$db_entry_adaptor->fetch_all_by_source("%unigene%")};
Description: Retrieves DBEntrys that match the source name.
Returntype : ref to array of Bio::EnsEMBL::DBSQL::DBEntry
Exceptions : None.
Caller : General
Status : At Risk
=cut
sub fetch_all_by_source {
my ( $self, $source ) = @_;
my @results = ();
my $sql =
"SELECT xref.xref_id, xref.dbprimary_acc, xref.display_label,
xref.version, xref.description,
exDB.dbprimary_acc_linkable, exDB.display_label_linkable, exDB.priority,
exDB.db_name, exDB.db_display_name, exDB.db_release, es.synonym,
xref.info_type, xref.info_text, exDB.type, exDB.secondary_db_name,
exDB.secondary_db_table
FROM (xref, external_db exDB)
LEFT JOIN external_synonym es on es.xref_id = xref.xref_id
WHERE exDB.db_name like ?
AND xref.external_db_id = exDB.external_db_id";
my $sth = $self->prepare($sql);
$sth->bind_param( 1, $source, SQL_VARCHAR );
$sth->execute();
my $max_rows = 1000;
while ( my $rowcache = $sth->fetchall_arrayref( undef, $max_rows ) ) {
while ( my $arrayref = shift( @{$rowcache} ) ) {
my ( $dbID, $dbprimaryId,
$displayid, $version,
$desc, $primary_id_linkable,
$display_id_linkable, $priority,
$dbname, $db_display_name,
$release, $synonym,
$info_type, $info_text,
$type, $secondary_db_name,
$secondary_db_table
) = @$arrayref;
my $exDB =
Bio::EnsEMBL::DBEntry->new(
-adaptor => $self,
-dbID => $dbID,
-primary_id => $dbprimaryId,
-display_id => $displayid,
-version => $version,
-release => $release,
-dbname => $dbname,
-primary_id_linkable => $primary_id_linkable,
-display_id_linkable => $display_id_linkable,
-priority => $priority,
-db_display_name => $db_display_name,
-info_type => $info_type,
-info_text => $info_text,
-type => $type,
-secondary_db_name => $secondary_db_name,
-secondary_db_table => $secondary_db_table
);
if ($desc) { $exDB->description($desc) }
if ($synonym) { $exDB->add_synonym($synonym) }
push @results, $exDB;
} ## end while ( my $arrayref = shift...
} ## end while ( my $rowcache = $sth...
$sth->finish();
return \@results;
} ## end sub fetch_all_by_source
=head2 fetch_all_synonyms
Arg [1] : dbID of DBEntry to fetch synonyms for. Used in lazy loading of synonyms.
......@@ -1441,6 +1529,33 @@ sub fetch_all_synonyms {
}
=head2 get_db_name_from_external_db_id
Arg [1] : external_dbid of database to get the database_name
Example : my $db_name = $db_entry_adaptor->get_db_name_from_external_db_id(1100);
Description: Gets the database name for a certain external_db_id
Returntype : scalar
Exceptions : None.
Caller : General
Status : At Risk
=cut
sub get_db_name_from_external_db_id{
my $self = shift;
my $external_db_id = shift;
my $sth = $self->prepare("SELECT db_name FROM external_db WHERE external_db_id = ?");
$sth->bind_param(1, $external_db_id, SQL_INTEGER);
$sth->execute();
my ($db_name) = $sth->fetchrow_array();
$sth->finish();
return $db_name;
}
=head2 geneids_by_extids
Description: DEPRECATED use list_gene_ids_by_extids instead
......
......@@ -125,7 +125,9 @@ sub _final_clause {
sub fetch_by_stable_id {
my ($self, $stable_id) = @_;
my $constraint = "esi.stable_id = '$stable_id' AND e.is_current = 1";
my $constraint = "esi.stable_id = ? AND e.is_current = 1";
$self->bind_param_generic_fetch($stable_id,SQL_VARCHAR);
my ($exon) = @{ $self->generic_fetch($constraint) };
return $exon;
......@@ -150,7 +152,9 @@ sub fetch_by_stable_id {
sub fetch_all_versions_by_stable_id {
my ($self, $stable_id) = @_;
my $constraint = "esi.stable_id = '$stable_id'";
my $constraint = "esi.stable_id = ?";
$self->bind_param_generic_fetch($stable_id,SQL_VARCHAR);
return $self->generic_fetch($constraint);
}
......
......@@ -174,7 +174,8 @@ sub fetch_by_display_label {
my $self = shift;
my $label = shift;
my $constraint = "x.display_label = '$label' AND g.is_current = 1";
my $constraint = "x.display_label = ? AND g.is_current = 1";
$self->bind_param_generic_fetch($label,SQL_VARCHAR);
my ($gene) = @{ $self->generic_fetch($constraint) };
return $gene;
......@@ -203,7 +204,8 @@ sub fetch_by_display_label {
sub fetch_by_stable_id {
my ($self, $stable_id) = @_;
my $constraint = "gsi.stable_id = '$stable_id' AND g.is_current = 1";
my $constraint = "gsi.stable_id = ? AND g.is_current = 1";
$self->bind_param_generic_fetch($stable_id,SQL_VARCHAR);
my ($gene) = @{ $self->generic_fetch($constraint) };
return $gene;
......@@ -232,7 +234,8 @@ sub fetch_by_stable_id {
sub fetch_all_by_biotype {
my ($self, $biotype) = @_;
my $constraint = "g.biotype = '$biotype' and g.is_current = 1" ;
my $constraint = "g.biotype = ? and g.is_current = 1" ;
$self->bind_param_generic_fetch($biotype,SQL_VARCHAR);
my @genes = @{ $self->generic_fetch($constraint) };
return \@genes ;
}
......@@ -257,8 +260,8 @@ sub fetch_all_by_biotype {
sub fetch_all_versions_by_stable_id {
my ($self, $stable_id) = @_;
my $constraint = "gsi.stable_id = '$stable_id'";
my $constraint = "gsi.stable_id = ?";
$self->bind_param_generic_fetch($stable_id,SQL_VARCHAR);
return $self->generic_fetch($constraint);
}
......
......@@ -64,7 +64,9 @@ sub fetch_by_name {
my $self = shift;
my $name = shift;
my $result = $self->generic_fetch("oa.name = '$name'");
$self->bind_param_generic_fetch($name,SQL_VARCHAR);
my $result = $self->generic_fetch("oa.name = ?");
if (scalar @$result > 1) {
warning("Array $name is not unique in the database, but only one result has been returned");
......@@ -92,7 +94,8 @@ sub fetch_all_by_type {
my $constraint;
if (scalar @types == 1) {
$constraint = qq( oa.type = '$types[0]' );
$constraint = qq( oa.type = ? );
$self->bind_param_generic_fetch($types[0],SQL_VARCHAR);
} else {
$constraint = join q(','), @types;
$constraint = qq( oa.type IN ('$constraint') );
......
......@@ -71,7 +71,8 @@ sub fetch_all_by_Probe {
throw('fetch_all_by_Probe requires a stored Bio::EnsEMBL::OligoProbe object');
}
return $self->generic_fetch( 'of.oligo_probe_id = ' . $probe->dbID() );
$self->bind_param_generic_fetch($probe->dbID(),SQL_INTEGER);
return $self->generic_fetch( 'of.oligo_probe_id = ?' );
}
=head2 fetch_all_by_probeset
......@@ -94,7 +95,8 @@ sub fetch_all_by_probeset {
throw('fetch_all_by_probeset requires a probeset argument');
}
return $self->generic_fetch( "op.probeset = '$probeset'" );
$self->bind_param_generic_fetch($probeset,SQL_VARCHAR);
return $self->generic_fetch( "op.probeset = ?" );
}
=head2 fetch_all_by_Slice_arrayname
......
......@@ -120,7 +120,8 @@ sub fetch_all_by_probeset {
my $self = shift;
my $probeset = shift;
return $self->generic_fetch("op.probeset = '$probeset'");
$self->bind_param_generic_fetch($probeset,SQL_VARCHAR);
return $self->generic_fetch("op.probeset = ?");
}
=head2 fetch_all_by_Array
......@@ -150,7 +151,8 @@ sub fetch_all_by_Array {
return [];
}
return $self->generic_fetch("op.oligo_array_id = $array_id");
$self->bind_param_generic_fetch($array_id, SQL_INTEGER);
return $self->generic_fetch("op.oligo_array_id = ?");
}
=head2 fetch_by_OligoFeature
......
......@@ -109,7 +109,8 @@ sub fetch_by_stable_id {
my $syn = $self->_tables()->[1];
my $pts = $self->generic_fetch("$syn.display_label = '$stable_id'");
$self->bind_param_generic_fetch($stable_id,SQL_VARCHAR);
my $pts = $self->generic_fetch("$syn.display_label = ?");
return (@$pts) ? $pts->[0] : undef;
}
......
......@@ -86,9 +86,9 @@ sub fetch_all_by_translation_id {
my $sth = $self->prepare
("SELECT protein_feature_id, p.seq_start, p.seq_end, p.analysis_id, " .
" p.score, p.perc_ident, p.evalue, p.hit_start, p.hit_end, " .
" p.hit_id, x.display_label, i.interpro_ac " .
" p.hit_name, x.display_label, i.interpro_ac " .
"FROM protein_feature p " .
"LEFT JOIN interpro AS i ON p.hit_id = i.id " .
"LEFT JOIN interpro AS i ON p.hit_name = i.id " .
"LEFT JOIN xref AS x ON x.dbprimary_acc = i.interpro_ac " .
"WHERE p.translation_id = ?");
......@@ -150,10 +150,10 @@ sub fetch_by_dbID{
my $sth = $self->prepare(
"SELECT p.seq_start, p.seq_end, p.analysis_id, " .
" p.score, p.perc_ident, p.evalue, " .
" p.hit_start, p.hit_end, p.hit_id, " .
" p.hit_start, p.hit_end, p.hit_name, " .
" x.display_label, i.interpro_ac " .
"FROM protein_feature p " .
"LEFT JOIN interpro AS i ON p.hit_id = i.id " .
"LEFT JOIN interpro AS i ON p.hit_name = i.id " .
"LEFT JOIN xref AS x ON x.dbprimary_acc = i.interpro_ac " .
"WHERE p.protein_feature_id = ?");
......@@ -245,7 +245,7 @@ sub store {
" analysis_id = ?, ".
" hit_start = ?, ".
" hit_end = ?, ".
" hit_id = ?, ".
" hit_name = ?, ".
" score = ?, ".
" perc_ident = ?, ".
" evalue = ?");
......
......@@ -130,7 +130,10 @@ sub _left_join {
sub fetch_by_stable_id {
my ($self, $stable_id) = @_;
my $constraint = "tsi.stable_id = '$stable_id' AND t.is_current = 1";
my $constraint = "tsi.stable_id = ? AND t.is_current = 1";
$self->bind_param_generic_fetch($stable_id,SQL_VARCHAR);
my ($transcript) = @{ $self->generic_fetch($constraint) };
return $transcript;
......@@ -155,7 +158,9 @@ sub fetch_by_stable_id {
sub fetch_all_versions_by_stable_id {
my ($self, $stable_id) = @_;
my $constraint = "tsi.stable_id = '$stable_id'";
my $constraint = "tsi.stable_id = ?";
$self->bind_param_generic_fetch($stable_id,SQL_VARCHAR);
return $self->generic_fetch($constraint);
}
......@@ -474,7 +479,10 @@ sub fetch_by_display_label {
my $self = shift;
my $label = shift;
my $constraint = "x.display_label = '$label' AND t.is_current = 1";
my $constraint = "x.display_label = ? AND t.is_current = 1";
$self->bind_param_generic_fetch($label,SQL_VARCHAR);
my ($transcript) = @{ $self->generic_fetch($constraint) };
return $transcript;
......@@ -823,7 +831,7 @@ sub get_Interpro_by_transid {
WHERE tsi.stable_id = ?
AND tl.transcript_id = tsi.transcript_id
AND tl.translation_id = pf.translation_id
AND i.id = pf.hit_id
AND i.id = pf.hit_name
AND i.interpro_ac = x.dbprimary_acc
AND tsi.transcript_id = t.transcript_id
AND t.is_current = 1
......
......@@ -320,7 +320,8 @@ sub fetch_all_by_type {
unless($type) {
throw("type argument is required");
}
$self->generic_fetch("uo.type = \'$type\'");
$self->bind_param_generic_fetch($type,SQL_VARCHAR);
$self->generic_fetch("uo.type = ?");
}
......@@ -345,7 +346,8 @@ sub fetch_all_by_analysis {
unless($analysis) {
throw("analysis argument is required");
}
my $constraint = "uo.analysis_id = ".$analysis->dbID;
$self->bind_param_generic_fetch($analysis->dbID,SQL_INTEGER);
my $constraint = "uo.analysis_id = ?";
if(defined($dbname)){
my $db_id =0;
my $sth = $self->prepare('select external_db_id from external_db where db_name like "'.
......@@ -356,9 +358,9 @@ sub fetch_all_by_analysis {
if(!defined($db_id) or $db_id == 0){
throw("$dbname could not be found in the external database table\n");
}
$constraint .= " AND uo.external_db_id = $db_id";
$self->bind_param_generic_fetch($db_id,SQL_INTEGER);
$constraint .= " AND uo.external_db_id = ?";
}
#print $constraint."\n";
$self->generic_fetch($constraint);
}
......@@ -383,7 +385,9 @@ sub fetch_by_identifier {
unless($identifier) {
throw("identifier argument is required");
}
my $constraint = 'uo.identifier like "'.$identifier.'"';
$self->bind_param_generic_fetch($identifier,SQL_VARCHAR);
my $constraint = 'uo.identifier like ?';
if(defined($dbname)){
my $db_id =0;
my $sth = $self->prepare('select external_db_id from external_db where db_name like "'.
......@@ -394,9 +398,9 @@ sub fetch_by_identifier {
if(!defined($db_id) or $db_id == 0){
throw("$dbname could not be found in the external database table\n");
}
$constraint .= " AND uo.external_db_id = $db_id";
$self->bind_param_generic_fetch($db_id,SQL_INTEGER);
$constraint .= " AND uo.external_db_id = ?";
}
# print $constraint."\n";
return $self->generic_fetch($constraint);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment