From: Peter Rabbitson Date: Sun, 28 Jun 2009 11:00:03 +0000 (+0000) Subject: The prefetch+group_by is a complex problem - branch X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=dca04b1fa6473b375e94544facf6c009c052720c;p=dbsrgits%2FDBIx-Class-Historic.git The prefetch+group_by is a complex problem - branch --- diff --git a/t/prefetch/grouped.t b/t/prefetch/grouped.t deleted file mode 100644 index c517741..0000000 --- a/t/prefetch/grouped.t +++ /dev/null @@ -1,75 +0,0 @@ -#!/usr/bin/perl - -use strict; -use warnings; -use Test::More 'no_plan'; - -use lib qw(t/lib); -use DBICTest; -use DBIC::SqlMakerTest; -use DBIC::DebugObj; - -#plan tests => 6; - -my $schema = DBICTest->init_schema( deploy_args => { add_drop_table => 1 } ); - -## CD has_many Tracks -## Track belongs_to CD - -my $cd_count = $schema->resultset('CD')->count; -is($cd_count, 5, 'Plain CDs count'); -# print STDERR "CDs: $cd_count\n"; - -my $cd_tracks = $schema->resultset('CD')->related_resultset('tracks')->count; -is($cd_tracks, 15, 'Tracks for CDs'); -# print STDERR "CD Tracks: $cd_tracks\n"; - -my $count_per_cd = $schema->resultset('Track')-> - search({}, - { 'select' => [ 'me.cd', { count => 'me.trackid', } ], - 'as' => [ 'cd', 'cd_count' ], - 'group_by' => ['me.cd'], -# [ 'me.cd', 'cd_count' ], - }); - -while (my $cnt = $count_per_cd->next) { - is($cnt->get_column('cd_count'), 3, 'CDs per track ' . $cnt->get_column('cd')); -# print STDERR "CD: " . $cnt->get_column('cd') . " count: " . $cnt->get_column('cd_count') . "\n"; -} - -$count_per_cd->reset; -while (my $trcnt = $count_per_cd->next) { -# print STDERR "CD: " . $trcnt->get_column('cd') . " count: " . $trcnt->get_column('cd_count') . "\n"; - ok($trcnt->cd->title, 'Title for CD, fetched (' . $trcnt->get_column('cd') . ')'); -# print STDERR "CD title: " . $trcnt->cd->title . "\n"; -} - - -## This is the working sql for group/prefetch combo -# my $stuff = $schema->storage->dbh_do( -# sub { -# my ($storage, $dbh) = @_; -# $dbh->selectall_arrayref("SELECT me.cd, me.count, cd.cdid, cd.artist, cd.title, cd.year, cd.genreid, cd.single_track FROM (SELECT me.cd, COUNT( me.trackid ) as count FROM track me GROUP BY me.cd) AS me JOIN cd cd ON cd.cdid = me.cd"); -# }, -#); - -# print STDERR Data::Dumper::Dumper($stuff); - -{ - my ($sql, @bind); - $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind)); - $schema->storage->debug(1); - - my $count_per_prefetched = $count_per_cd->search({}, { prefetch => 'cd' }); - is($count_per_prefetched->all, 5, 'Prefetched count with groupby'); -# print STDERR "Tracks/CDs with prefetch, count: " . $count_per_prefetched->count . "\n"; - - is_same_sql_bind ( - $sql, - \@bind, - "SELECT me.cd, me.count, cd.cdid, cd.artist, cd.title, cd.year, cd.genreid, cd.single_track FROM (SELECT me.cd, COUNT( me.trackid ) as count FROM track me GROUP BY me.cd) AS me JOIN cd cd ON cd.cdid = me.cd", - [ ], - ); -# } - -}