1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
10 use DBICTest ':DiffSQL';
12 my $schema = DBICTest->init_schema();
15 # while cds.* will be selected anyway (prefetch implies it)
16 # only the requested me.name column will be fetched.
18 # reference sql with select => [...]
19 # SELECT me.name, cds.title, cds.cdid, cds.artist, cds.title, cds.year, cds.genreid, cds.single_track FROM ...
21 my $rs = $schema->resultset('Artist')->search(
22 { 'cds.title' => { '!=', 'Generic Manufactured Singles' } },
24 prefetch => [ qw/ cds / ],
25 order_by => [ { -desc => 'me.name' }, 'cds.title' ],
26 select => [qw/ me.name cds.title / ],
30 is ($rs->count, 2, 'Correct number of collapsed artists');
31 my ($we_are_goth) = $rs->all;
32 is ($we_are_goth->name, 'We Are Goth', 'Correct first artist');
33 is ($we_are_goth->cds->count, 1, 'Correct number of CDs for first artist');
34 is ($we_are_goth->cds->first->title, 'Come Be Depressed With Us', 'Correct cd for artist');
35 }, 'explicit prefetch on a keyless object works');
39 my $rs = $schema->resultset('CD')->search(
42 order_by => [ { -desc => 'me.year' } ],
45 my $years = [qw/ 2001 2001 1999 1998 1997/];
48 [ $rs->search->get_column('me.year')->all ],
50 'Expected years (at least one duplicate)',
54 for my $cd ($rs->all) {
55 my $data = { year => $cd->year, cdid => $cd->cdid };
56 for my $tr ($cd->tracks->all) {
57 push @{$data->{tracks}}, { $tr->get_columns };
59 @{$data->{tracks}} = sort { $a->{trackid} <=> $b->{trackid} } @{$data->{tracks}};
60 push @cds_and_tracks, $data;
63 my $pref_rs = $rs->search ({}, { columns => [qw/year cdid/], prefetch => 'tracks' });
65 my @pref_cds_and_tracks;
66 for my $cd ($pref_rs->all) {
67 my $data = { $cd->get_columns };
68 for my $tr ($cd->tracks->all) {
69 push @{$data->{tracks}}, { $tr->get_columns };
71 @{$data->{tracks}} = sort { $a->{trackid} <=> $b->{trackid} } @{$data->{tracks}};
72 push @pref_cds_and_tracks, $data;
76 \@pref_cds_and_tracks,
78 'Correct collapsing on non-unique primary object'
82 $pref_rs->search ({}, { order_by => [ { -desc => 'me.year' }, 'trackid' ] })->all_hri,
84 'Correct HRI collapsing on non-unique primary object'
87 }, 'weird collapse lives');
91 # test implicit prefetch as well
93 my $rs = $schema->resultset('CD')->search(
94 { title => 'Generic Manufactured Singles' },
97 select => [qw/ me.title artist.name / ],
102 is ($cd->title, 'Generic Manufactured Singles', 'CD title prefetched correctly');
103 isa_ok ($cd->artist, 'DBICTest::Artist');
104 is ($cd->artist->name, 'Random Boy Band', 'Artist object has correct name');
106 }, 'implicit keyless prefetch works');
111 $schema->resultset('Track')->search({}, { join => { cd => 'artist' }, '+columns' => 'artist.name' } )->next;
113 qr|\QInflation into non-existent relationship 'artist' of 'Track' requested, check the inflation specification (columns/as) ending in '...artist.name'|,
114 'Sensible error message on mis-specified "as"',
117 # check complex limiting prefetch without the join-able columns
119 my $pref_rs = $schema->resultset('Owners')->search({}, {
123 columns => 'name', # only the owner name, still prefetch all the books
130 SELECT me.name, books.id, books.source, books.owner, books.title, books.price
132 SELECT me.name, me.id
138 LEFT JOIN books books
139 ON books.owner = me.id
142 [ [ { sqlt_datatype => "integer" } => 3 ], [ { sqlt_datatype => "integer" } => 1 ] ],
143 'Expected SQL on complex limited prefetch with non-selected join condition',
155 title => "Best Recipe Cookbook",
158 'Expected result on complex limited prefetch with non-selected join condition'
161 my $empty_ordered_pref_rs = $pref_rs->search({}, {
162 columns => [], # nothing, we only prefetch the book data
163 order_by => 'me.name',
165 my $empty_ordered_pref_hri = [ {
171 title => "Best Recipe Cookbook",
176 $empty_ordered_pref_rs->as_query,
178 SELECT books.id, books.source, books.owner, books.title, books.price
180 SELECT me.id, me.name
186 LEFT JOIN books books
187 ON books.owner = me.id
190 [ [ { sqlt_datatype => "integer" } => 3 ], [ { sqlt_datatype => "integer" } => 1 ] ],
191 'Expected SQL on *ordered* complex limited prefetch with non-selected root data',
195 $empty_ordered_pref_rs->all_hri,
196 $empty_ordered_pref_hri,
197 'Expected result on *ordered* complex limited prefetch with non-selected root data'
200 $empty_ordered_pref_rs = $empty_ordered_pref_rs->search({}, {
201 order_by => [ \ 'LENGTH(me.name)', \ 'RANDOM()' ],
205 $empty_ordered_pref_rs->as_query,
207 SELECT books.id, books.source, books.owner, books.title, books.price
209 SELECT me.id, me.name
211 ORDER BY LENGTH(me.name), RANDOM()
215 LEFT JOIN books books
216 ON books.owner = me.id
217 ORDER BY LENGTH(me.name), RANDOM()
219 [ [ { sqlt_datatype => "integer" } => 3 ], [ { sqlt_datatype => "integer" } => 1 ] ],
220 'Expected SQL on *function-ordered* complex limited prefetch with non-selected root data',
224 $empty_ordered_pref_rs->all_hri,
225 $empty_ordered_pref_hri,
226 'Expected result on *function-ordered* complex limited prefetch with non-selected root data'