8 use DBICTest ':DiffSQL';
10 my $schema = DBICTest->init_schema();
13 # while cds.* will be selected anyway (prefetch implies it)
14 # only the requested me.name column will be fetched.
16 # reference sql with select => [...]
17 # SELECT me.name, cds.title, cds.cdid, cds.artist, cds.title, cds.year, cds.genreid, cds.single_track FROM ...
19 my $rs = $schema->resultset('Artist')->search(
20 { 'cds.title' => { '!=', 'Generic Manufactured Singles' } },
22 prefetch => [ qw/ cds / ],
23 order_by => [ { -desc => 'me.name' }, 'cds.title' ],
24 select => [qw/ me.name cds.title / ],
28 is ($rs->count, 2, 'Correct number of collapsed artists');
29 my ($we_are_goth) = $rs->all;
30 is ($we_are_goth->name, 'We Are Goth', 'Correct first artist');
31 is ($we_are_goth->cds->count, 1, 'Correct number of CDs for first artist');
32 is ($we_are_goth->cds->first->title, 'Come Be Depressed With Us', 'Correct cd for artist');
33 }, 'explicit prefetch on a keyless object works');
37 my $rs = $schema->resultset('CD')->search(
40 order_by => [ { -desc => 'me.year' } ],
43 my $years = [qw/ 2001 2001 1999 1998 1997/];
46 [ $rs->search->get_column('me.year')->all ],
48 'Expected years (at least one duplicate)',
52 for my $cd ($rs->all) {
53 my $data = { year => $cd->year, cdid => $cd->cdid };
54 for my $tr ($cd->tracks->all) {
55 push @{$data->{tracks}}, { $tr->get_columns };
57 @{$data->{tracks}} = sort { $a->{trackid} <=> $b->{trackid} } @{$data->{tracks}};
58 push @cds_and_tracks, $data;
61 my $pref_rs = $rs->search ({}, { columns => [qw/year cdid/], prefetch => 'tracks' });
63 my @pref_cds_and_tracks;
64 for my $cd ($pref_rs->all) {
65 my $data = { $cd->get_columns };
66 for my $tr ($cd->tracks->all) {
67 push @{$data->{tracks}}, { $tr->get_columns };
69 @{$data->{tracks}} = sort { $a->{trackid} <=> $b->{trackid} } @{$data->{tracks}};
70 push @pref_cds_and_tracks, $data;
74 \@pref_cds_and_tracks,
76 'Correct collapsing on non-unique primary object'
80 $pref_rs->search ({}, { order_by => [ { -desc => 'me.year' }, 'trackid' ] })->all_hri,
82 'Correct HRI collapsing on non-unique primary object'
85 }, 'weird collapse lives');
89 # test implicit prefetch as well
91 my $rs = $schema->resultset('CD')->search(
92 { title => 'Generic Manufactured Singles' },
95 select => [qw/ me.title artist.name / ],
100 is ($cd->title, 'Generic Manufactured Singles', 'CD title prefetched correctly');
101 isa_ok ($cd->artist, 'DBICTest::Artist');
102 is ($cd->artist->name, 'Random Boy Band', 'Artist object has correct name');
104 }, 'implicit keyless prefetch works');
109 $schema->resultset('Track')->search({}, { join => { cd => 'artist' }, '+columns' => 'artist.name' } )->next;
111 qr|\QInflation into non-existent relationship 'artist' of 'Track' requested, check the inflation specification (columns/as) ending in '...artist.name'|,
112 'Sensible error message on mis-specified "as"',
115 # check complex limiting prefetch without the join-able columns
117 my $pref_rs = $schema->resultset('Owners')->search({}, {
121 columns => 'name', # only the owner name, still prefetch all the books
128 SELECT me.name, books.id, books.source, books.owner, books.title, books.price
130 SELECT me.name, me.id
136 LEFT JOIN books books
137 ON books.owner = me.id
140 [ [ { sqlt_datatype => "integer" } => 3 ], [ { sqlt_datatype => "integer" } => 1 ] ],
141 'Expected SQL on complex limited prefetch with non-selected join condition',
153 title => "Best Recipe Cookbook",
156 'Expected result on complex limited prefetch with non-selected join condition'
159 my $empty_ordered_pref_rs = $pref_rs->search({}, {
160 columns => [], # nothing, we only prefetch the book data
161 order_by => 'me.name',
163 my $empty_ordered_pref_hri = [ {
169 title => "Best Recipe Cookbook",
174 $empty_ordered_pref_rs->as_query,
176 SELECT books.id, books.source, books.owner, books.title, books.price
178 SELECT me.id, me.name
184 LEFT JOIN books books
185 ON books.owner = me.id
188 [ [ { sqlt_datatype => "integer" } => 3 ], [ { sqlt_datatype => "integer" } => 1 ] ],
189 'Expected SQL on *ordered* complex limited prefetch with non-selected root data',
193 $empty_ordered_pref_rs->all_hri,
194 $empty_ordered_pref_hri,
195 'Expected result on *ordered* complex limited prefetch with non-selected root data'
198 $empty_ordered_pref_rs = $empty_ordered_pref_rs->search({}, {
199 order_by => [ \ 'LENGTH(me.name)', \ 'RANDOM()' ],
203 $empty_ordered_pref_rs->as_query,
205 SELECT books.id, books.source, books.owner, books.title, books.price
207 SELECT me.id, me.name
209 ORDER BY LENGTH(me.name), RANDOM()
213 LEFT JOIN books books
214 ON books.owner = me.id
215 ORDER BY LENGTH(me.name), RANDOM()
217 [ [ { sqlt_datatype => "integer" } => 3 ], [ { sqlt_datatype => "integer" } => 1 ] ],
218 'Expected SQL on *function-ordered* complex limited prefetch with non-selected root data',
222 $empty_ordered_pref_rs->all_hri,
223 $empty_ordered_pref_hri,
224 'Expected result on *function-ordered* complex limited prefetch with non-selected root data'