Skip to content
Snippets Groups Projects
Commit e4fbcf73 authored by Leo Gordon's avatar Leo Gordon
Browse files

count_all_by... now also supports multicolumn filtering using _AND_ (with examples in t/ )

parent a67276fa
No related branches found
No related tags found
No related merge requests found
......@@ -407,13 +407,13 @@ sub AUTOLOAD {
my ($self) = @_;
my $column_set = $self->column_set();
my $filter_components = $filter_string && [ split('_and_', $filter_string) ];
my $filter_components = $filter_string && [ split(/_AND_/i, $filter_string) ];
foreach my $column_name ( @$filter_components ) {
unless($column_set->{$column_name}) {
die "unknown column '$column_name'";
}
}
my $key_components = $key_string && [ split('_and_', $key_string) ];
my $key_components = $key_string && [ split(/_AND_/i, $key_string) ];
foreach my $column_name ( @$key_components ) {
unless($column_set->{$column_name}) {
die "unknown column '$column_name'";
......@@ -434,19 +434,29 @@ sub AUTOLOAD {
);
};
goto &$AUTOLOAD; # restart the new method
} elsif($AUTOLOAD =~ /::count_all_by_(\w+)$/) {
my $filter_name = $1;
my $filter_string = $1;
my ($self) = @_;
my $column_set = $self->column_set();
if($column_set->{$filter_name}) {
# print "Setting up '$AUTOLOAD' method\n";
*$AUTOLOAD = sub { my ($self, $filter_value) = @_; return $self->count_all("$filter_name='$filter_value'"); };
goto &$AUTOLOAD; # restart the new method
} else {
die "unknown column '$filter_name'";
my $filter_components = $filter_string && [ split(/_AND_/i, $filter_string) ];
foreach my $column_name ( @$filter_components ) {
unless($column_set->{$column_name}) {
die "unknown column '$column_name'";
}
}
# print "Setting up '$AUTOLOAD' method\n";
*$AUTOLOAD = sub {
my $self = shift @_;
return $self->count_all(
join(' AND ', map { "$filter_components->[$_]='$_[$_]'" } 0..scalar(@$filter_components)-1),
);
};
goto &$AUTOLOAD; # restart the new method
} elsif($AUTOLOAD =~ /::remove_all_by_(\w+)$/) {
my $filter_name = $1;
......@@ -461,7 +471,7 @@ sub AUTOLOAD {
die "unknown column '$filter_name'";
}
} elsif($AUTOLOAD =~ /::update_(\w+)$/) {
my @columns_to_update = split('_and_', $1);
my @columns_to_update = split(/_AND_/i, $1);
# print "Setting up '$AUTOLOAD' method\n";
*$AUTOLOAD = sub { my ($self, $object) = @_; return $self->update($object, @columns_to_update); };
goto &$AUTOLOAD; # restart the new method
......
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
use Getopt::Long;
use Bio::EnsEMBL::Hive::DBSQL::DBAdaptor;
my $url;
GetOptions( 'url=s' => \$url );
die "Please specify the -url\n" unless($url);
$ENV{'EHIVE_ROOT_DIR'} = $ENV{'ENSEMBL_CVS_ROOT_DIR'}.'/ensembl-hive'; # I'm just being lazy. For the correct way to set this variable check eHive scripts
my $hive_dba = Bio::EnsEMBL::Hive::DBSQL::DBAdaptor->new( -url => $url );
print "Filtering on 1 column:\n";
print join("\n", map { "\t".$_->toString } @{ $hive_dba->get_DataflowRuleAdaptor->fetch_all_by_from_analysis_id(1) } )."\n";
print "Filtering on 2 columns:\n";
print join("\n", map { "\t".$_->toString } @{ $hive_dba->get_DataflowRuleAdaptor->fetch_all_by_from_analysis_id_AND_branch_code(1, 2) } )."\n";
print "Count(filter by 1 'from_analysis_id' column) ".$hive_dba->get_DataflowRuleAdaptor->count_all_by_from_analysis_id(1)."\n";
print "Count(filter by 1 'branch_code' column) ".$hive_dba->get_DataflowRuleAdaptor->count_all_by_branch_code(1)."\n";
print "Count(filter by 2 columns) ".$hive_dba->get_DataflowRuleAdaptor->count_all_by_from_analysis_id_AND_branch_code(1, 2)."\n";
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