[Bast-commits] r5849 - in DBIx-Class/0.08/branches/count_distinct: lib/DBIx/Class t

arcanez at dev.catalyst.perl.org arcanez at dev.catalyst.perl.org
Mon Mar 30 20:40:05 BST 2009


Author: arcanez
Date: 2009-03-30 20:40:05 +0100 (Mon, 30 Mar 2009)
New Revision: 5849

Modified:
   DBIx-Class/0.08/branches/count_distinct/lib/DBIx/Class/ResultSet.pm
   DBIx-Class/0.08/branches/count_distinct/t/count_distinct.t
Log:
 * add more tests
 * remove old cruft
 * remove old note


Modified: DBIx-Class/0.08/branches/count_distinct/lib/DBIx/Class/ResultSet.pm
===================================================================
--- DBIx-Class/0.08/branches/count_distinct/lib/DBIx/Class/ResultSet.pm	2009-03-30 18:50:27 UTC (rev 5848)
+++ DBIx-Class/0.08/branches/count_distinct/lib/DBIx/Class/ResultSet.pm	2009-03-30 19:40:05 UTC (rev 5849)
@@ -1117,12 +1117,6 @@
 with to find the number of elements. If passed arguments, does a search
 on the resultset and counts the results of that.
 
-Note: When using C<count> with C<group_by>, L<DBIx::Class> emulates C<GROUP BY>
-using C<COUNT( DISTINCT( columns ) )>. Some databases (notably SQLite) do
-not support C<DISTINCT> with multiple columns. If you are using such a
-database, you should only use columns from the main table in your C<group_by>
-clause.
-
 =cut
 
 sub count {
@@ -1146,20 +1140,7 @@
   my $attrs = { %{$self->_resolved_attrs} };
 
   if (my $group_by = $attrs->{group_by}) {
-    delete $attrs->{having};
     delete $attrs->{order_by};
-    my @distinct = (ref $group_by ?  @$group_by : ($group_by));
-    # todo: try CONCAT for multi-column pk
-    my @pk = $self->result_source->primary_columns;
-    if (@pk == 1) {
-      my $alias = $attrs->{alias};
-      foreach my $column (@distinct) {
-        if ($column =~ qr/^(?:\Q${alias}.\E)?$pk[0]$/) {
-          @distinct = ($column);
-          last;
-        }
-      }
-    }
 
     $attrs->{select} = $group_by; 
     $attrs->{from} = [ { 'mesub' => (ref $self)->new($self->result_source, $attrs)->cursor->as_query } ];

Modified: DBIx-Class/0.08/branches/count_distinct/t/count_distinct.t
===================================================================
--- DBIx-Class/0.08/branches/count_distinct/t/count_distinct.t	2009-03-30 18:50:27 UTC (rev 5848)
+++ DBIx-Class/0.08/branches/count_distinct/t/count_distinct.t	2009-03-30 19:40:05 UTC (rev 5849)
@@ -12,16 +12,45 @@
 
 eval "use DBD::SQLite";
 plan skip_all => 'needs DBD::SQLite for testing' if $@;
-plan tests => 5;
+plan tests => 13;
 
+my $in_rs = $schema->resultset("Tag")->search({ tag => [ 'Blue', 'Shiny' ] });
+
 cmp_ok($schema->resultset("Tag")->count({ tag => 'Blue' }),
-           '==', 9, 'Count without DISTINCT ok');
+           '==', 4, 'Count without DISTINCT');
 
 cmp_ok($schema->resultset("Tag")->count({ tag => [ 'Blue', 'Shiny' ] }, { group_by => 'tag' }),
-           '==', 2, 'Count with single column group_by ok');
+           '==', 2, 'Count with single column group_by');
 
 cmp_ok($schema->resultset("Tag")->count({ tag => 'Blue' }, { group_by => [ qw/tag cd/ ]}), 
-           '==', 4, 'Count with multiple column group_by ok');
+           '==', 4, 'Count with multiple column group_by');
 
 cmp_ok($schema->resultset("Tag")->count({ tag => 'Blue' }, { distinct => 1 }),
-           '==', 4, "Count with single column distinct ok");
+           '==', 4, "Count with single column distinct");
+
+cmp_ok($schema->resultset("Tag")->count({ tag => { -in => $in_rs->get_column('tag')->as_query } }),
+           '==', 4, "Count with IN subquery");
+
+cmp_ok($schema->resultset("Tag")->count({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { group_by => 'tag' }),
+           '==', 1, "Count with IN subquery with outside group_by");
+
+cmp_ok($schema->resultset("Tag")->count({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { distinct => 1 }),
+           '==', 4, "Count with IN subquery with outside distinct");
+
+cmp_ok($schema->resultset("Tag")->count({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { distinct => 1, select => 'tag' }),
+           '==', 1, "Count with IN subquery with outside distinct on a single column");
+
+cmp_ok($schema->resultset("Tag")->count({ tag => { -in => $in_rs->search({}, { group_by => 'tag' })->get_column('tag')->as_query } }),
+           '==', 4, "Count with IN subquery with single group_by");
+
+cmp_ok($schema->resultset("Tag")->count({ tag => { -in => $in_rs->search({}, { group_by => [ qw/tag cd/ ] })->get_column('tag')->as_query } }),
+           '==', 4, "Count with IN subquery with multiple group_by");
+
+cmp_ok($schema->resultset("Tag")->count({ tag => \"= 'Blue'" }),
+           '==', 4, "Count without DISTINCT, using literal SQL");
+
+cmp_ok($schema->resultset("Tag")->count({ tag => \" IN ('Blue', 'Shiny')" }, { group_by => 'tag' }),
+           '==', 2, "Count with literal SQL and single group_by");
+
+cmp_ok($schema->resultset("Tag")->count({ tag => \" IN ('Blue', 'Shiny')" }, { group_by => [ qw/tag cd/ ] }),
+           '==', 6, "Count with literal SQL and multiple group_by");




More information about the Bast-commits mailing list