File Coverage

blib/lib/ObjectDB/With.pm
Criterion Covered Total %
statement 6 48 12.5
branch 0 12 0.0
condition 0 6 0.0
subroutine 2 4 50.0
pod 0 2 0.0
total 8 72 11.1


line stmt bran cond sub pod time code
1             package ObjectDB::With;
2              
3 5     5   59949 use strict;
  5         15  
  5         123  
4 5     5   20 use warnings;
  5         7  
  5         2071  
5              
6             our $VERSION = '3.26';
7              
8             sub new {
9 0     0 0   my $class = shift;
10 0           my (%params) = @_;
11              
12 0           my $self = {};
13 0           bless $self, $class;
14              
15 0           $self->{meta} = $params{meta};
16 0           $self->{with} = $params{with};
17             $self->{with} = [ $self->{with} ]
18 0 0 0       if $self->{with} && ref $self->{with} ne 'ARRAY';
19              
20 0           my $joins = { join => [] };
21              
22 0           my %seen;
23 0 0         if (my @with = sort grep { defined } @{ $self->{with} || [] }) {
  0 0          
  0            
24 0           foreach my $with (@with) {
25 0           my $meta = $self->{meta};
26              
27 0           my $name = $with;
28 0           my $columns;
29 0 0         if (ref $name eq 'HASH') {
30 0           $name = $with->{name};
31 0           $columns = $with->{columns};
32             }
33              
34 0           my @parts = split /[.]/xms, $name;
35              
36 0           my $seen = q{};
37 0           my $parent_join = $joins;
38 0           foreach my $part (@parts) {
39 0           $seen .= q{.} . $part;
40              
41 0           my $rel = $meta->get_relationship($part);
42              
43 0 0         if ($seen{$seen}) {
44 0           $parent_join = $seen{$seen};
45 0           $meta = $rel->class->meta;
46 0           next;
47             }
48              
49 0           my $parent_as = $parent_join->{as};
50              
51 0 0         my $name_prefix = $parent_as ? $parent_as . '_' : '';
52             my @joins = $rel->to_source(
53             table => $parent_join->{as},
54 0           name_prefix => $name_prefix
55             );
56              
57 0           foreach my $join (@joins) {
58 0           push @{ $parent_join->{join} },
59             {
60             source => $join->{table},
61             rel_name => $join->{as},
62             as => $name_prefix . $join->{as},
63             on => $join->{constraint},
64             op => $join->{join},
65             columns => $columns || $join->{columns},
66 0   0       join => []
67             };
68             }
69              
70 0           $parent_join = $parent_join->{join}->[-1];
71 0           $seen{$seen} = $parent_join;
72              
73 0           $meta = $rel->class->meta;
74             }
75             }
76             }
77              
78 0           $self->{joins} = $joins->{join};
79              
80 0           return $self;
81             }
82              
83             sub to_joins {
84 0     0 0   my $self = shift;
85              
86 0           return $self->{joins};
87             }
88              
89             1;