9 use DBICTest ':DiffSQL';
11 my $schema = DBICTest->init_schema();
13 # The tag Blue is assigned to cds 1 2 3 and 5
14 # The tag Cheesy is assigned to cds 2 4 and 5
16 # This combination should make some interesting group_by's
19 my $in_rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Cheesy' ] });
23 sub { my $crs = shift->count_rs; isa_ok ($crs, 'DBIx::Class::ResultSetColumn'); $crs->next }
25 $rs = $schema->resultset('Tag')->search({ tag => 'Blue' });
26 is($get_count->($rs), 4, 'Count without DISTINCT');
28 $rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Cheesy' ] }, { group_by => 'tag' });
29 is($get_count->($rs), 2, 'Count with single column group_by');
31 $rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Cheesy' ] }, { group_by => 'cd' });
32 is($get_count->($rs), 5, 'Count with another single column group_by');
34 $rs = $schema->resultset('Tag')->search({ tag => 'Blue' }, { group_by => [ qw/tag cd/ ]});
35 is($get_count->($rs), 4, 'Count with multiple column group_by');
37 $rs = $schema->resultset('Tag')->search({ tag => 'Blue' }, { distinct => 1 });
38 is($get_count->($rs), 4, 'Count with single column distinct');
40 $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } });
41 is($get_count->($rs), 7, 'Count with IN subquery');
43 $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { group_by => 'tag' });
44 is($get_count->($rs), 2, 'Count with IN subquery with outside group_by');
46 $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { distinct => 1 });
47 is($get_count->($rs), 7, 'Count with IN subquery with outside distinct');
49 $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { distinct => 1, select => 'tag' }),
50 is($get_count->($rs), 2, 'Count with IN subquery with outside distinct on a single column');
52 $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->search({}, { group_by => 'tag' })->get_column('tag')->as_query } });
53 is($get_count->($rs), 7, 'Count with IN subquery with single group_by');
55 $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->search({}, { group_by => 'cd' })->get_column('tag')->as_query } });
56 is($get_count->($rs), 7, 'Count with IN subquery with another single group_by');
58 $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->search({}, { group_by => [ qw/tag cd/ ] })->get_column('tag')->as_query } });
59 is($get_count->($rs), 7, 'Count with IN subquery with multiple group_by');
61 $rs = $schema->resultset('Tag')->search({ tag => \"= 'Blue'" });
62 is($get_count->($rs), 4, 'Count without DISTINCT, using literal SQL');
64 $rs = $schema->resultset('Tag')->search({ tag => \" IN ('Blue', 'Cheesy')" }, { group_by => 'tag' });
65 is($get_count->($rs), 2, 'Count with literal SQL and single group_by');
67 $rs = $schema->resultset('Tag')->search({ tag => \" IN ('Blue', 'Cheesy')" }, { group_by => 'cd' });
68 is($get_count->($rs), 5, 'Count with literal SQL and another single group_by');
70 $rs = $schema->resultset('Tag')->search({ tag => \" IN ('Blue', 'Cheesy')" }, { group_by => [ qw/tag cd/ ] });
71 is($get_count->($rs), 7, 'Count with literal SQL and multiple group_by');
73 $rs = $schema->resultset('Tag')->search({ tag => 'Blue' }, { '+select' => { max => 'tagid' }, distinct => 1 });
74 is($get_count->($rs), 4, 'Count with +select aggreggate');
76 $rs = $schema->resultset('Tag')->search({}, { select => [\'length(me.tag)'], distinct => 1 });
77 is($get_count->($rs), 3, 'Count by distinct function result as select literal');
81 sub { my $row = $schema->resultset('Tag')->search({}, { select => { distinct => [qw/tag cd/] } })->first },
82 qr/\Qselect => { distinct => ... } syntax is not supported for multiple columns/,
83 'throw on unsupported syntax'
86 # make sure distinct+func works
88 my $rs = $schema->resultset('Artist')->search(
93 '+select' => [ { count => 'cds.cdid', -as => 'amount_of_cds' } ],
94 '+as' => [qw/num_cds/],
95 order_by => { -desc => 'amount_of_cds' },
102 SELECT me.artistid, me.name, me.rank, me.charfield, COUNT( cds.cdid ) AS amount_of_cds
103 FROM artist me LEFT JOIN cd cds ON cds.artist = me.artistid
104 GROUP BY me.artistid, me.name, me.rank, me.charfield
105 ORDER BY amount_of_cds DESC
110 is ($rs->next->get_column ('num_cds'), 3, 'Function aliased correctly');
113 # and check distinct has_many join count
115 my $rs = $schema->resultset('Artist')->search(
116 { 'cds.title' => { '!=', 'fooooo' } },
120 '+select' => [ { count => 'cds.cdid', -as => 'amount_of_cds' } ],
121 '+as' => [qw/num_cds/],
122 order_by => { -desc => 'amount_of_cds' },
129 SELECT me.artistid, me.name, me.rank, me.charfield, COUNT( cds.cdid ) AS amount_of_cds
132 ON cds.artist = me.artistid
134 GROUP BY me.artistid, me.name, me.rank, me.charfield
135 ORDER BY amount_of_cds DESC
139 sqlt_datatype => 'varchar',
140 dbic_colname => 'cds.title',
147 $rs->count_rs->as_query,
151 SELECT me.artistid, me.name, me.rank, me.charfield
154 ON cds.artist = me.artistid
156 GROUP BY me.artistid, me.name, me.rank, me.charfield
161 sqlt_datatype => 'varchar',
162 dbic_colname => 'cds.title',
168 is ($rs->next->get_column ('num_cds'), 3, 'Function aliased correctly');
171 # These two rely on the database to throw an exception. This might not be the case one day. Please revise.
172 dies_ok(sub { my $count = $schema->resultset('Tag')->search({}, { '+select' => \'tagid AS tag_id', distinct => 1 })->count }, 'expecting to die');