Commit | Line | Data |
b3426cd2 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | use Test::More 'no_plan'; |
6 | |
7 | use lib qw(t/lib); |
8 | use DBICTest; |
9 | use DBIC::SqlMakerTest; |
10 | use DBIC::DebugObj; |
11 | |
12 | #plan tests => 6; |
13 | |
14 | my $schema = DBICTest->init_schema( deploy_args => { add_drop_table => 1 } ); |
15 | |
16 | ## CD has_many Tracks |
17 | ## Track belongs_to CD |
18 | |
19 | my $cd_count = $schema->resultset('CD')->count; |
20 | is($cd_count, 5, 'Plain CDs count'); |
21 | # print STDERR "CDs: $cd_count\n"; |
22 | |
23 | my $cd_tracks = $schema->resultset('CD')->related_resultset('tracks')->count; |
24 | is($cd_tracks, 15, 'Tracks for CDs'); |
25 | # print STDERR "CD Tracks: $cd_tracks\n"; |
26 | |
27 | my $count_per_cd = $schema->resultset('Track')-> |
28 | search({}, |
29 | { 'select' => [ 'me.cd', { count => 'me.trackid', } ], |
30 | 'as' => [ 'cd', 'cd_count' ], |
31 | 'group_by' => ['me.cd'], |
32 | # [ 'me.cd', 'cd_count' ], |
33 | }); |
34 | |
35 | while (my $cnt = $count_per_cd->next) { |
36 | is($cnt->get_column('cd_count'), 3, 'CDs per track ' . $cnt->get_column('cd')); |
37 | # print STDERR "CD: " . $cnt->get_column('cd') . " count: " . $cnt->get_column('cd_count') . "\n"; |
38 | } |
39 | |
40 | $count_per_cd->reset; |
41 | while (my $trcnt = $count_per_cd->next) { |
42 | # print STDERR "CD: " . $trcnt->get_column('cd') . " count: " . $trcnt->get_column('cd_count') . "\n"; |
43 | ok($trcnt->cd->title, 'Title for CD, fetched (' . $trcnt->get_column('cd') . ')'); |
44 | # print STDERR "CD title: " . $trcnt->cd->title . "\n"; |
45 | } |
46 | |
47 | |
48 | ## This is the working sql for group/prefetch combo |
49 | # my $stuff = $schema->storage->dbh_do( |
50 | # sub { |
51 | # my ($storage, $dbh) = @_; |
52 | # $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"); |
53 | # }, |
54 | #); |
55 | |
56 | # print STDERR Data::Dumper::Dumper($stuff); |
57 | |
58 | { |
59 | my ($sql, @bind); |
60 | $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind)); |
61 | $schema->storage->debug(1); |
62 | |
63 | my $count_per_prefetched = $count_per_cd->search({}, { prefetch => 'cd' }); |
64 | is($count_per_prefetched->all, 5, 'Prefetched count with groupby'); |
65 | # print STDERR "Tracks/CDs with prefetch, count: " . $count_per_prefetched->count . "\n"; |
66 | |
67 | is_same_sql_bind ( |
68 | $sql, |
69 | \@bind, |
70 | "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", |
71 | [ ], |
72 | ); |
73 | # } |
74 | |
75 | } |