File Coverage

lib/SQL/QueryBuilder/Flex/Join.pm
Criterion Covered Total %
statement 44 44 100.0
branch 10 14 71.4
condition 2 5 40.0
subroutine 10 10 100.0
pod 0 6 0.0
total 66 79 83.5


line stmt bran cond sub pod time code
1             package SQL::QueryBuilder::Flex::Join;
2              
3 24     24   121 use strict;
  24         38  
  24         744  
4 24     24   118 use warnings;
  24         39  
  24         540  
5 24     24   12656 use SQL::QueryBuilder::Flex::Exp;
  24         58  
  24         713  
6 24     24   160 use base 'SQL::QueryBuilder::Flex::Statement';
  24         44  
  24         20312  
7              
8             sub new {
9 9     9 0 30 my ($class, @options) = @_;
10 9         79 my $self = $class->SUPER::new(
11             type => undef,
12             table => undef,
13             alias => undef,
14             on => undef,
15             using => [],
16             @options,
17             );
18 9         32 return $self;
19             }
20              
21             sub table {
22 2     2 0 3 my ($self) = @_;
23 2         9 return $self->{table};
24             }
25              
26             sub alias {
27 3     3 0 15 my ($self) = @_;
28 3         16 return $self->{alias};
29             }
30              
31             sub on {
32 1     1 0 2 my ($self, $condition, @values) = @_;
33 1         6 die "The 'USING' clause is already defined"
34 1 50       2 if scalar(@{ $self->{using} });
35 1   33     13 my $cond_list = $self->{on} ||= SQL::QueryBuilder::Flex::Exp->new(
36             parent => $self,
37             );
38 1 50       7 return $condition
39             ? $cond_list->and($condition, @values)->parent()
40             : $cond_list
41             ;
42             }
43              
44             sub using {
45 8     8 0 18 my ($self, @columns) = @_;
46 8 50       39 die "The 'ON' clause is already defined"
47             if $self->{on};
48 8         13 push @{ $self->{using} }, @columns;
  8         19  
49 8         101 return $self;
50             }
51              
52             sub do_build {
53 7     7 0 17 my ($self, $writer, $indent) = @_;
54              
55 7   50     58 $indent ||= 0;
56              
57 7 100       24 if (ref $self->{table}) {
58 1         7 $writer->write(join(' ', $self->{type}, 'JOIN ('), $indent);
59 1         4 $self->{table}->build($writer, $indent + 1);
60 1         5 $writer->write(join(' AS ',')', $self->{alias}), $indent);
61             }
62             else {
63 6 100       28 my $table = $self->{alias}
64             ? join(' ', $self->{table}, $self->{alias})
65             : $self->{table}
66             ;
67 6         36 $writer->write(join(' ', $self->{type}, 'JOIN', $table), $indent);
68             }
69              
70 7 100       26 if ( $self->{on} ) {
  6 50       23  
71 1         11 $writer->write('ON', $indent);
72 1         9 $self->{on}->build($writer, $indent + 1);
73             }
74             elsif ( scalar(@{ $self->{using} }) ) {
75 6         12 $writer->write('USING ('. join(', ', @{ $self->{using} }). ')', $indent);
  6         28  
76             }
77              
78 7         153 return;
79             }
80              
81             1;