10 use DBIC::SqlMakerTest;
12 my $schema = DBICTest->init_schema();
14 # The tag Blue is assigned to cds 1 2 3 and 5
15 # The tag Cheesy is assigned to cds 2 4 and 5
17 # This combination should make some interesting group_by's
20 my $in_rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Cheesy' ] });
24 sub { my $crs = shift->count_rs; isa_ok ($crs, 'DBIx::Class::ResultSetColumn'); $crs->next }
26 $rs = $schema->resultset('Tag')->search({ tag => 'Blue' });
27 is($get_count->($rs), 4, 'Count without DISTINCT');
29 $rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Cheesy' ] }, { group_by => 'tag' });
30 is($get_count->($rs), 2, 'Count with single column group_by');
32 $rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Cheesy' ] }, { group_by => 'cd' });
33 is($get_count->($rs), 5, 'Count with another single column group_by');
35 $rs = $schema->resultset('Tag')->search({ tag => 'Blue' }, { group_by => [ qw/tag cd/ ]});
36 is($get_count->($rs), 4, 'Count with multiple column group_by');
38 $rs = $schema->resultset('Tag')->search({ tag => 'Blue' }, { distinct => 1 });
39 is($get_count->($rs), 4, 'Count with single column distinct');
41 $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } });
42 is($get_count->($rs), 7, 'Count with IN subquery');
44 $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { group_by => 'tag' });
45 is($get_count->($rs), 2, 'Count with IN subquery with outside group_by');
47 $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { distinct => 1 });
48 is($get_count->($rs), 7, 'Count with IN subquery with outside distinct');
50 $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { distinct => 1, select => 'tag' }),
51 is($get_count->($rs), 2, 'Count with IN subquery with outside distinct on a single column');
53 $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->search({}, { group_by => 'tag' })->get_column('tag')->as_query } });
54 is($get_count->($rs), 7, 'Count with IN subquery with single group_by');
56 $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->search({}, { group_by => 'cd' })->get_column('tag')->as_query } });
57 is($get_count->($rs), 7, 'Count with IN subquery with another single group_by');
59 $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->search({}, { group_by => [ qw/tag cd/ ] })->get_column('tag')->as_query } });
60 is($get_count->($rs), 7, 'Count with IN subquery with multiple group_by');
62 $rs = $schema->resultset('Tag')->search({ tag => \"= 'Blue'" });
63 is($get_count->($rs), 4, 'Count without DISTINCT, using literal SQL');
65 $rs = $schema->resultset('Tag')->search({ tag => \" IN ('Blue', 'Cheesy')" }, { group_by => 'tag' });
66 is($get_count->($rs), 2, 'Count with literal SQL and single group_by');
68 $rs = $schema->resultset('Tag')->search({ tag => \" IN ('Blue', 'Cheesy')" }, { group_by => 'cd' });
69 is($get_count->($rs), 5, 'Count with literal SQL and another single group_by');
71 $rs = $schema->resultset('Tag')->search({ tag => \" IN ('Blue', 'Cheesy')" }, { group_by => [ qw/tag cd/ ] });
72 is($get_count->($rs), 7, 'Count with literal SQL and multiple group_by');
74 $rs = $schema->resultset('Tag')->search({ tag => 'Blue' }, { '+select' => { max => 'tagid' }, distinct => 1 });
75 is($get_count->($rs), 4, 'Count with +select aggreggate');
77 $rs = $schema->resultset('Tag')->search({}, { select => 'length(me.tag)', distinct => 1 });
78 is($get_count->($rs), 3, 'Count by distinct function result as select literal');
82 sub { my $row = $schema->resultset('Tag')->search({}, { select => { distinct => [qw/tag cd/] } })->first },
83 qr/select => { distinct => \.\.\. } syntax is not supported for multiple columns/,
84 'throw on unsupported syntax'
87 # make sure distinct+func works
89 my $rs = $schema->resultset('Artist')->search(
94 '+select' => [ { count => 'cds.cdid', -as => 'amount_of_cds' } ],
95 '+as' => [qw/num_cds/],
96 order_by => { -desc => 'amount_of_cds' },
103 SELECT me.artistid, me.name, me.rank, me.charfield, COUNT( cds.cdid ) AS amount_of_cds
104 FROM artist me LEFT JOIN cd cds ON cds.artist = me.artistid
105 GROUP BY me.artistid, me.name, me.rank, me.charfield
106 ORDER BY amount_of_cds DESC
111 is ($rs->next->get_column ('num_cds'), 3, 'Function aliased correctly');
114 # These two rely on the database to throw an exception. This might not be the case one day. Please revise.
115 dies_ok(sub { my $count = $schema->resultset('Tag')->search({}, { '+select' => \'tagid AS tag_id', distinct => 1 })->count }, 'expecting to die');