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