From e4fbcf736c8c2eaa7b8463c8cf0ff3758d91ccbb Mon Sep 17 00:00:00 2001 From: Leo Gordon <lg4@ebi.ac.uk> Date: Thu, 3 Oct 2013 12:11:01 +0100 Subject: [PATCH] count_all_by... now also supports multicolumn filtering using _AND_ (with examples in t/ ) --- modules/Bio/EnsEMBL/Hive/DBSQL/BaseAdaptor.pm | 30 ++++++++++++------- t/fetch_and_count_by_multiple_columns.pl | 26 ++++++++++++++++ 2 files changed, 46 insertions(+), 10 deletions(-) create mode 100755 t/fetch_and_count_by_multiple_columns.pl diff --git a/modules/Bio/EnsEMBL/Hive/DBSQL/BaseAdaptor.pm b/modules/Bio/EnsEMBL/Hive/DBSQL/BaseAdaptor.pm index f5c305f95..0e9aa25bd 100644 --- a/modules/Bio/EnsEMBL/Hive/DBSQL/BaseAdaptor.pm +++ b/modules/Bio/EnsEMBL/Hive/DBSQL/BaseAdaptor.pm @@ -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 diff --git a/t/fetch_and_count_by_multiple_columns.pl b/t/fetch_and_count_by_multiple_columns.pl new file mode 100755 index 000000000..6ce78c6eb --- /dev/null +++ b/t/fetch_and_count_by_multiple_columns.pl @@ -0,0 +1,26 @@ +#!/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"; + -- GitLab