[Bps-public-commit] dbix-searchbuilder branch, unify-function-use-group-by, created. 1.59-42-g5bba4d6
Ruslan Zakirov
ruz at bestpractical.com
Thu Jun 2 08:10:24 EDT 2011
The branch, unify-function-use-group-by has been created
at 5bba4d6144f612af1338823043a8f688ac7c3a69 (commit)
- Log -----------------------------------------------------------------
commit 7a20a45257769e2415f3bbe518d06beec4d05cf1
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date: Thu Jun 2 02:23:54 2011 +0400
extend GroupBy's FUNCTION arg with CombineFunctionWithField
In old code when FUNCTION is provided then FIELD and ALIAS
arguments are ignored. CombineFunctionWithField behaves
in the old way if FIELD is empty.
diff --git a/lib/DBIx/SearchBuilder.pm b/lib/DBIx/SearchBuilder.pm
index 67ff120..7142d53 100755
--- a/lib/DBIx/SearchBuilder.pm
+++ b/lib/DBIx/SearchBuilder.pm
@@ -1159,7 +1159,7 @@ sub _GroupClause {
);
if ($rowhash{'FUNCTION'} ) {
$clause .= ($clause ? ", " : " ");
- $clause .= $rowhash{'FUNCTION'};
+ $clause .= $self->CombineFunctionWithField( %rowhash );
}
elsif ( ($rowhash{'ALIAS'}) and
commit de58ddf43c71d896f39e1c9c1c79f53ffafc2490
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date: Thu Jun 2 15:37:17 2011 +0400
basic tests for group by
diff --git a/t/01searches.t b/t/01searches.t
index 285035d..1fdf346 100644
--- a/t/01searches.t
+++ b/t/01searches.t
@@ -7,7 +7,7 @@ use Test::More;
BEGIN { require "t/utils.pl" }
our (@AvailableDrivers);
-use constant TESTS_PER_DRIVER => 105;
+use constant TESTS_PER_DRIVER => 110;
my $total = scalar(@AvailableDrivers) * TESTS_PER_DRIVER;
plan tests => $total;
@@ -183,8 +183,36 @@ SKIP: {
TODO: {
local $TODO = 'we leave order_by after clean slate, fixing this results in many RT failures';
is_deeply( $users_obj, $clean_obj, 'after CleanSlate looks like new object');
+ }
+
+ {
+ $users_obj = TestApp::Users->new( $handle );
+ $users_obj->UnLimit;
+ $users_obj->GroupBy({FUNCTION => 'Login'});
+ $users_obj->OrderBy(FIELD => 'Login', ORDER => 'desc');
+ $users_obj->Column(FIELD => 'Login');
+ is( $users_obj->Count, $count_all, "group by / order by finds right amount");
+ $first_rec = $users_obj->First;
+ isa_ok( $first_rec, 'DBIx::SearchBuilder::Record', 'First returns record object' );
+ is( $first_rec->Login, 'obra', 'login is correct' );
+ }
+ {
$users_obj = TestApp::Users->new( $handle );
+ $users_obj->UnLimit;
+ $users_obj->GroupBy({FUNCTION => 'SUBSTR(Login, 1, 1)', });
+ $users_obj->Column(FIELD => 'Login', FUNCTION => 'SUBSTR(Login, 1, 1)');
+ my @list = sort map $_->Login, @{ $users_obj->ItemsArrayRef };
+ is_deeply( \@list, [qw(a c g o)], 'correct values' );
}
+ {
+ $users_obj = TestApp::Users->new( $handle );
+ $users_obj->UnLimit;
+ $users_obj->GroupBy({FUNCTION => 'SUBSTR(?, 1, 1)', FIELD => 'Login'});
+ $users_obj->Column(FIELD => 'Login', FUNCTION => 'SUBSTR(?, 1, 1)');
+ my @list = sort map $_->Login, @{ $users_obj->ItemsArrayRef };
+ is_deeply( \@list, [qw(a c g o)], 'correct values' );
+ }
+ $users_obj = TestApp::Users->new( $handle );
# Let's play a little with ENTRYAGGREGATOR
# EA defaults to OR for the same field
commit 0b6be1f17f784d21e9b30f45c024fc5841865b63
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date: Thu Jun 2 15:38:21 2011 +0400
no need in variable outside the loop
diff --git a/lib/DBIx/SearchBuilder.pm b/lib/DBIx/SearchBuilder.pm
index 7142d53..53d21a7 100755
--- a/lib/DBIx/SearchBuilder.pm
+++ b/lib/DBIx/SearchBuilder.pm
@@ -1149,10 +1149,9 @@ sub _GroupClause {
my $self = shift;
return '' unless $self->{'group_by'};
- my $row;
my $clause;
- foreach $row ( @{$self->{'group_by'}} ) {
+ foreach my $row ( @{$self->{'group_by'}} ) {
my %rowhash = ( ALIAS => 'main',
FIELD => undef,
%$row
commit 6a14c3cedc15493e407dbdbb832400c7cb4788cb
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date: Thu Jun 2 15:42:27 2011 +0400
simplify operations with $clause, no functional changes
diff --git a/lib/DBIx/SearchBuilder.pm b/lib/DBIx/SearchBuilder.pm
index 53d21a7..c5fd236 100755
--- a/lib/DBIx/SearchBuilder.pm
+++ b/lib/DBIx/SearchBuilder.pm
@@ -1149,33 +1149,28 @@ sub _GroupClause {
my $self = shift;
return '' unless $self->{'group_by'};
- my $clause;
-
+ my $clause = '';
foreach my $row ( @{$self->{'group_by'}} ) {
my %rowhash = ( ALIAS => 'main',
FIELD => undef,
%$row
);
if ($rowhash{'FUNCTION'} ) {
- $clause .= ($clause ? ", " : " ");
+ $clause .= ', ' if $clause;
$clause .= $self->CombineFunctionWithField( %rowhash );
}
elsif ( ($rowhash{'ALIAS'}) and
($rowhash{'FIELD'}) ) {
+ $clause .= ', ' if $clause;
- $clause .= ($clause ? ", " : " ");
$clause .= $rowhash{'ALIAS'} . ".";
$clause .= $rowhash{'FIELD'};
}
}
- if ($clause) {
- return " GROUP BY" . $clause . " ";
- }
- else {
- return '';
- }
+ return '' unless $clause;
+ return " GROUP BY $clause ";
}
=head2 NewAlias
commit 5bba4d6144f612af1338823043a8f688ac7c3a69
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date: Thu Jun 2 16:09:19 2011 +0400
no need in %rowhash that is just copy of $row
diff --git a/lib/DBIx/SearchBuilder.pm b/lib/DBIx/SearchBuilder.pm
index c5fd236..c76e0ea 100755
--- a/lib/DBIx/SearchBuilder.pm
+++ b/lib/DBIx/SearchBuilder.pm
@@ -1151,21 +1151,13 @@ sub _GroupClause {
my $clause = '';
foreach my $row ( @{$self->{'group_by'}} ) {
- my %rowhash = ( ALIAS => 'main',
- FIELD => undef,
- %$row
- );
- if ($rowhash{'FUNCTION'} ) {
+ if ( $row->{'FUNCTION'} ) {
$clause .= ', ' if $clause;
- $clause .= $self->CombineFunctionWithField( %rowhash );
-
+ $clause .= $self->CombineFunctionWithField( %$row );
}
- elsif ( ($rowhash{'ALIAS'}) and
- ($rowhash{'FIELD'}) ) {
+ elsif ( $row->{'FIELD'} ) {
$clause .= ', ' if $clause;
-
- $clause .= $rowhash{'ALIAS'} . ".";
- $clause .= $rowhash{'FIELD'};
+ $clause .= ($row->{'ALIAS'}||'main') .'.'. $row->{'FIELD'};
}
}
-----------------------------------------------------------------------
More information about the Bps-public-commit
mailing list