File Coverage

blib/lib/Pg/SQL/PrettyPrinter/Node/JoinExpr.pm
Criterion Covered Total %
statement 80 80 100.0
branch 18 22 81.8
condition 2 3 66.6
subroutine 18 18 100.0
pod 4 4 100.0
total 122 127 96.0


line stmt bran cond sub pod time code
1             package Pg::SQL::PrettyPrinter::Node::JoinExpr;
2              
3             # UTF8 boilerplace, per http://stackoverflow.com/questions/6162484/why-does-modern-perl-avoid-utf-8-by-default/
4 4     4   5953 use v5.26;
  4         16  
5 4     4   19 use strict;
  4         9  
  4         96  
6 4     4   13 use warnings;
  4         7  
  4         227  
7 4     4   17 use warnings qw( FATAL utf8 );
  4         8  
  4         201  
8 4     4   20 use utf8;
  4         8  
  4         27  
9 4     4   167 use open qw( :std :utf8 );
  4         8  
  4         28  
10 4     4   592 use Unicode::Normalize qw( NFC );
  4         22  
  4         317  
11 4     4   26 use Unicode::Collate;
  4         9  
  4         193  
12 4     4   24 use Encode qw( decode );
  4         7  
  4         669  
13              
14             if ( grep /\P{ASCII}/ => @ARGV ) {
15             @ARGV = map { decode( 'UTF-8', $_ ) } @ARGV;
16             }
17              
18             # If there is __DATA__,then uncomment next line:
19             # binmode( DATA, ':encoding(UTF-8)' );
20             # UTF8 boilerplace, per http://stackoverflow.com/questions/6162484/why-does-modern-perl-avoid-utf-8-by-default/
21              
22             # Useful common code
23 4     4   29 use autodie;
  4         7  
  4         34  
24 4     4   20009 use Carp qw( carp croak confess cluck );
  4         9  
  4         344  
25 4     4   29 use English qw( -no_match_vars );
  4         7  
  4         32  
26 4     4   1744 use Data::Dumper qw( Dumper );
  4         9  
  4         958  
27              
28             # give a full stack dump on any untrapped exceptions
29             local $SIG{ __DIE__ } = sub {
30             confess "Uncaught exception: @_" unless $^S;
31             };
32              
33             # now promote run-time warnings into stackdumped exceptions
34             # *unless* we're in an try block, in which
35             # case just generate a clucking stackdump instead
36             local $SIG{ __WARN__ } = sub {
37             if ( $^S ) { cluck "Trapped warning: @_" }
38             else { confess "Deadly warning: @_" }
39             };
40              
41             # Useful common code
42              
43 4     4   33 use parent qw( Pg::SQL::PrettyPrinter::Node );
  4         8  
  4         73  
44              
45             sub new {
46 7     7 1 144 my $class = shift;
47 7         26 my $self = $class->SUPER::new( @_ );
48 7         14 bless $self, $class;
49              
50 7         33 $self->objectify( 'larg' );
51 7         18 $self->objectify( 'rarg' );
52 7 100       18 if ( $self->{ 'quals' } ) {
53 6         13 $self->objectify( 'quals' );
54             }
55 7 100       21 if ( $self->{ 'usingClause' } ) {
56 1         3 $self->objectify( 'usingClause' );
57             }
58 7         12 return $self;
59             }
60              
61             sub join_type {
62 14     14 1 22 my $self = shift;
63 14 100       28 if ( !exists $self->{ '_join_type' } ) {
64 7         12 my $join_type = $self->{ 'jointype' };
65 7         92 $join_type =~ s/^JOIN_(.*)$/$1 JOIN/;
66 7 100       26 $join_type = 'JOIN' if $join_type eq 'INNER JOIN';
67 7 50 66     23 $join_type = "CROSS ${join_type}" if ( !exists $self->{ 'quals' } ) && ( !exists $self->{ 'usingClause' } );
68 7         19 $self->{ '_join_type' } = $join_type;
69             }
70 14         43 return $self->{ '_join_type' };
71             }
72              
73             sub as_text {
74 7     7 1 14 my $self = shift;
75              
76 7         38 my $join_cond = '';
77 7 100       25 if ( $self->{ 'usingClause' } ) {
    50          
78 1         2 $join_cond = 'USING ( ' . join( ', ', map { $_->as_ident } @{ $self->{ 'usingClause' } } ) . ' )';
  1         4  
  1         146  
79             }
80             elsif ( $self->{ 'quals' } ) {
81 6         21 $join_cond = 'ON ' . $self->{ 'quals' }->as_text;
82             }
83 7         24 return sprintf '%s %s %s %s', $self->{ 'larg' }->as_text, $self->join_type, $self->{ 'rarg' }->as_text, $join_cond;
84             }
85              
86             sub pretty_print {
87 7     7 1 10 my $self = shift;
88              
89 7         12 my $join_cond = '';
90 7 100       26 if ( $self->{ 'usingClause' } ) {
    50          
91 1         2 $join_cond = 'USING ( ' . join( ', ', map { $_->as_ident } @{ $self->{ 'usingClause' } } ) . ' )';
  1         4  
  1         37  
92             }
93             elsif ( $self->{ 'quals' } ) {
94 6         9 my $Q = $self->{ 'quals' };
95 6         16 my $q_text = $Q->as_text;
96 6         46 my $q_pretty = $Q->pretty_print;
97              
98             # If join condition is multiline, indent it in nicer way.
99 6 100       16 if ( $q_pretty =~ /\n/ ) {
100 2         8 $join_cond = sprintf( "ON\n%s", $self->increase_indent( $q_pretty ) );
101             }
102             else {
103 4 50       14 $join_cond = 'ON ' . $q_pretty if $q_pretty !~ /\n/;
104             }
105             }
106 7         21 return sprintf "%s\n%s %s %s", $self->{ 'larg' }->pretty_print, $self->join_type, $self->{ 'rarg' }->pretty_print, $join_cond;
107             }
108              
109             1;