9 use DBIC::SqlMakerTest;
11 my $schema = DBICTest->init_schema();
14 # while cds.* will be selected anyway (prefetch implies it)
15 # only the requested me.name column will be fetched.
17 # reference sql with select => [...]
18 # SELECT me.name, cds.title, cds.cdid, cds.artist, cds.title, cds.year, cds.genreid, cds.single_track FROM ...
20 my $rs = $schema->resultset('Artist')->search(
21 { 'cds.title' => { '!=', 'Generic Manufactured Singles' } },
23 prefetch => [ qw/ cds / ],
24 order_by => [ { -desc => 'me.name' }, 'cds.title' ],
25 select => [qw/ me.name cds.title / ],
29 is ($rs->count, 2, 'Correct number of collapsed artists');
30 my ($we_are_goth) = $rs->all;
31 is ($we_are_goth->name, 'We Are Goth', 'Correct first artist');
32 is ($we_are_goth->cds->count, 1, 'Correct number of CDs for first artist');
33 is ($we_are_goth->cds->first->title, 'Come Be Depressed With Us', 'Correct cd for artist');
34 }, 'explicit prefetch on a keyless object works');
38 my $rs = $schema->resultset('CD')->search(
41 order_by => [ { -desc => 'me.year' } ],
44 my $years = [qw/ 2001 2001 1999 1998 1997/];
47 [ $rs->search->get_column('me.year')->all ],
49 'Expected years (at least one duplicate)',
53 for my $cd ($rs->all) {
54 my $data = { year => $cd->year, cdid => $cd->cdid };
55 for my $tr ($cd->tracks->all) {
56 push @{$data->{tracks}}, { $tr->get_columns };
58 @{$data->{tracks}} = sort { $a->{trackid} <=> $b->{trackid} } @{$data->{tracks}};
59 push @cds_and_tracks, $data;
62 my $pref_rs = $rs->search ({}, { columns => [qw/year cdid/], prefetch => 'tracks' });
64 my @pref_cds_and_tracks;
65 for my $cd ($pref_rs->all) {
66 my $data = { $cd->get_columns };
67 for my $tr ($cd->tracks->all) {
68 push @{$data->{tracks}}, { $tr->get_columns };
70 @{$data->{tracks}} = sort { $a->{trackid} <=> $b->{trackid} } @{$data->{tracks}};
71 push @pref_cds_and_tracks, $data;
75 \@pref_cds_and_tracks,
77 'Correct collapsing on non-unique primary object'
81 $pref_rs->search ({}, { order_by => [ { -desc => 'me.year' }, 'trackid' ] })->all_hri,
83 'Correct HRI collapsing on non-unique primary object'
86 }, 'weird collapse lives');
90 # test implicit prefetch as well
92 my $rs = $schema->resultset('CD')->search(
93 { title => 'Generic Manufactured Singles' },
96 select => [qw/ me.title artist.name / ],
101 is ($cd->title, 'Generic Manufactured Singles', 'CD title prefetched correctly');
102 isa_ok ($cd->artist, 'DBICTest::Artist');
103 is ($cd->artist->name, 'Random Boy Band', 'Artist object has correct name');
105 }, 'implicit keyless prefetch works');
110 $schema->resultset('Track')->search({}, { join => { cd => 'artist' }, '+columns' => 'artist.name' } )->next;
112 qr|\QInflation into non-existent relationship 'artist' of 'Track' requested, check the inflation specification (columns/as) ending in '...artist.name'|,
113 'Sensible error message on mis-specified "as"',
116 # check complex limiting prefetch without the join-able columns
118 my $pref_rs = $schema->resultset('Owners')->search({}, {
122 columns => 'name', # only the owner name, still prefetch all the books
129 SELECT me.name, books.id, books.source, books.owner, books.title, books.price
131 SELECT me.name, me.id
137 LEFT JOIN books books
138 ON books.owner = me.id
141 [ [ { sqlt_datatype => "integer" } => 3 ], [ { sqlt_datatype => "integer" } => 1 ] ],
142 'Expected SQL on complex limited prefetch with non-selected join condition',
154 title => "Best Recipe Cookbook",
157 'Expected result on complex limited prefetch with non-selected join condition'
160 my $empty_ordered_pref_rs = $pref_rs->search({}, {
161 columns => [], # nothing, we only prefetch the book data
162 order_by => 'me.name',
164 my $empty_ordered_pref_hri = [ {
170 title => "Best Recipe Cookbook",
175 $empty_ordered_pref_rs->as_query,
177 SELECT books.id, books.source, books.owner, books.title, books.price
179 SELECT me.id, me.name
185 LEFT JOIN books books
186 ON books.owner = me.id
189 [ [ { sqlt_datatype => "integer" } => 3 ], [ { sqlt_datatype => "integer" } => 1 ] ],
190 'Expected SQL on *ordered* complex limited prefetch with non-selected root data',
194 $empty_ordered_pref_rs->all_hri,
195 $empty_ordered_pref_hri,
196 'Expected result on *ordered* complex limited prefetch with non-selected root data'
199 $empty_ordered_pref_rs = $empty_ordered_pref_rs->search({}, {
200 order_by => [ \ 'LENGTH(me.name)', \ 'RANDOM()' ],
204 $empty_ordered_pref_rs->as_query,
206 SELECT books.id, books.source, books.owner, books.title, books.price
208 SELECT me.id, me.name
210 ORDER BY LENGTH(me.name), RANDOM()
214 LEFT JOIN books books
215 ON books.owner = me.id
216 ORDER BY LENGTH(me.name), RANDOM()
218 [ [ { sqlt_datatype => "integer" } => 3 ], [ { sqlt_datatype => "integer" } => 1 ] ],
219 'Expected SQL on *function-ordered* complex limited prefetch with non-selected root data',
223 $empty_ordered_pref_rs->all_hri,
224 $empty_ordered_pref_hri,
225 'Expected result on *function-ordered* complex limited prefetch with non-selected root data'