File: join_type.t

package info (click to toggle)
libdbix-class-perl 0.082844-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,320 kB
  • sloc: perl: 27,215; sql: 322; sh: 29; makefile: 16
file content (50 lines) | stat: -rw-r--r-- 1,521 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use warnings;
use strict;

use Test::More;
use lib qw(t/lib);
use DBICTest ':DiffSQL';

my $schema = DBICTest->init_schema();


# a regular belongs_to prefetch
my $cds = $schema->resultset('CD')->search ({}, { prefetch => 'artist' } );

my $nulls = {
  hashref => {},
  arrayref => [],
  undef => undef,
};

# make sure null-prefetches do not screw with the final sql:
for my $type (keys %$nulls) {
  is_same_sql_bind (
    $cds->search({}, { prefetch => { artist => $nulls->{$type} } })->as_query,
    '( SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track,
              artist.artistid, artist.name, artist.rank, artist.charfield
        FROM cd me
        JOIN artist artist
          ON artist.artistid = me.artist
    )', [],
    "same sql with null $type prefetch"
  );
}

# make sure left join is carried only starting from the first has_many
is_same_sql_bind (
  $cds->search({}, { prefetch => { artist => { cds => 'artist' } } })->as_query,
  '(
    SELECT  me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track,
            artist.artistid, artist.name, artist.rank, artist.charfield,
            cds.cdid, cds.artist, cds.title, cds.year, cds.genreid, cds.single_track,
            artist_2.artistid, artist_2.name, artist_2.rank, artist_2.charfield
      FROM cd me
      JOIN artist artist ON artist.artistid = me.artist
      LEFT JOIN cd cds ON cds.artist = artist.artistid
      LEFT JOIN artist artist_2 ON artist_2.artistid = cds.artist
  )',
  [],
);

done_testing;