Commit | Line | Data |
8a3fa4ae |
1 | use warnings; |
4a233f30 |
2 | use strict; |
8a3fa4ae |
3 | |
4 | use Test::More; |
8a3fa4ae |
5 | use lib qw(t/lib); |
a5a7bb73 |
6 | use DBICTest ':DiffSQL'; |
8a3fa4ae |
7 | |
8 | my $schema = DBICTest->init_schema(); |
9 | |
10 | |
11 | # a regular belongs_to prefetch |
12 | my $cds = $schema->resultset('CD')->search ({}, { prefetch => 'artist' } ); |
13 | |
14 | my $nulls = { |
15 | hashref => {}, |
16 | arrayref => [], |
17 | undef => undef, |
18 | }; |
19 | |
20 | # make sure null-prefetches do not screw with the final sql: |
21 | for my $type (keys %$nulls) { |
41a5d675 |
22 | is_same_sql_bind ( |
23 | $cds->search({}, { prefetch => { artist => $nulls->{$type} } })->as_query, |
24 | '( SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track, |
25 | artist.artistid, artist.name, artist.rank, artist.charfield |
26 | FROM cd me |
27 | JOIN artist artist |
28 | ON artist.artistid = me.artist |
29 | )', [], |
30 | "same sql with null $type prefetch" |
31 | ); |
8a3fa4ae |
32 | } |
33 | |
34 | # make sure left join is carried only starting from the first has_many |
35 | is_same_sql_bind ( |
36 | $cds->search({}, { prefetch => { artist => { cds => 'artist' } } })->as_query, |
37 | '( |
38 | SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track, |
39 | artist.artistid, artist.name, artist.rank, artist.charfield, |
40 | cds.cdid, cds.artist, cds.title, cds.year, cds.genreid, cds.single_track, |
41 | artist_2.artistid, artist_2.name, artist_2.rank, artist_2.charfield |
42 | FROM cd me |
43 | JOIN artist artist ON artist.artistid = me.artist |
44 | LEFT JOIN cd cds ON cds.artist = artist.artistid |
45 | LEFT JOIN artist artist_2 ON artist_2.artistid = cds.artist |
8a3fa4ae |
46 | )', |
47 | [], |
48 | ); |
49 | |
50 | done_testing; |