5d17e9941f351e97f07def8e40715d372e8ffb33
[dbsrgits/DBIx-Class.git] / t / sqlmaker / legacy_joins.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest ':DiffSQL';
7
8 use DBIx::Class::SQLMaker;
9 my $sa = DBIx::Class::SQLMaker->new;
10
11 my @j = (
12     { child => 'person' },
13     [ { father => 'person' }, { 'father.person_id' => 'child.father_id' }, ],
14     [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
15 );
16 my $match = 'person child JOIN person father ON ( father.person_id = '
17           . 'child.father_id ) JOIN person mother ON ( mother.person_id '
18           . '= child.mother_id )'
19           ;
20 is_same_sql(
21   $sa->_recurse_from(@j),
22   $match,
23   'join 1 ok'
24 );
25
26 my @j2 = (
27     { mother => 'person' },
28     [   [   { child => 'person' },
29             [   { father             => 'person' },
30                 { 'father.person_id' => 'child.father_id' }
31             ]
32         ],
33         { 'mother.person_id' => 'child.mother_id' }
34     ],
35 );
36 $match = 'person mother JOIN (person child JOIN person father ON ('
37        . ' father.person_id = child.father_id )) ON ( mother.person_id = '
38        . 'child.mother_id )'
39        ;
40 is_same_sql(
41   $sa->_recurse_from(@j2),
42   $match,
43   'join 2 ok'
44 );
45
46 my @j3 = (
47     { child => 'person' },
48     [ { father => 'person', -join_type => 'inner' }, { 'father.person_id' => 'child.father_id' }, ],
49     [ { mother => 'person', -join_type => 'inner'  }, { 'mother.person_id' => 'child.mother_id' } ],
50 );
51 $match = 'person child INNER JOIN person father ON ( father.person_id = '
52           . 'child.father_id ) INNER JOIN person mother ON ( mother.person_id '
53           . '= child.mother_id )'
54           ;
55
56 is_same_sql(
57   $sa->_recurse_from(@j3),
58   $match,
59   'join 3 (inner join) ok'
60 );
61
62 my @j4 = (
63     { mother => 'person' },
64     [   [   { child => 'person', -join_type => 'left' },
65             [   { father             => 'person', -join_type => 'right' },
66                 { 'father.person_id' => 'child.father_id' }
67             ]
68         ],
69         { 'mother.person_id' => 'child.mother_id' }
70     ],
71 );
72 $match = 'person mother LEFT JOIN (person child RIGHT JOIN person father ON ('
73        . ' father.person_id = child.father_id )) ON ( mother.person_id = '
74        . 'child.mother_id )'
75        ;
76 is_same_sql(
77   $sa->_recurse_from(@j4),
78   $match,
79   'join 4 (nested joins + join types) ok'
80 );
81
82 my @j5 = (
83     { child => 'person' },
84     [ { father => 'person' }, { 'father.person_id' => \'!= child.father_id' }, ],
85     [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
86 );
87 $match = 'person child JOIN person father ON ( father.person_id != '
88           . 'child.father_id ) JOIN person mother ON ( mother.person_id '
89           . '= child.mother_id )'
90           ;
91 is_same_sql(
92   $sa->_recurse_from(@j5),
93   $match,
94   'join 5 (SCALAR reference for ON statement) ok'
95 );
96
97 done_testing;