File Coverage

blib/lib/Pg/SQL/PrettyPrinter/Node/JoinExpr.pm
Criterion Covered Total %
statement 75 75 100.0
branch 15 18 83.3
condition 2 3 66.6
subroutine 18 18 100.0
pod 4 4 100.0
total 114 118 96.6


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 3     3   2222 use v5.26;
  3         13  
5 3     3   17 use strict;
  3         8  
  3         90  
6 3     3   16 use warnings;
  3         7  
  3         109  
7 3     3   15 use warnings qw( FATAL utf8 );
  3         7  
  3         113  
8 3     3   17 use utf8;
  3         8  
  3         18  
9 3     3   87 use open qw( :std :utf8 );
  3         9  
  3         33  
10 3     3   440 use Unicode::Normalize qw( NFC );
  3         16  
  3         205  
11 3     3   21 use Unicode::Collate;
  3         8  
  3         100  
12 3     3   17 use Encode qw( decode );
  3         7  
  3         213  
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 3     3   666 use autodie;
  3         7  
  3         22  
24 3     3   16815 use Carp qw( carp croak confess cluck );
  3         12  
  3         250  
25 3     3   22 use English qw( -no_match_vars );
  3         7  
  3         22  
26 3     3   1183 use Data::Dumper qw( Dumper );
  3         11  
  3         737  
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 3     3   24 use parent qw( Pg::SQL::PrettyPrinter::Node );
  3         14  
  3         23  
44              
45             sub new {
46 5     5 1 144 my $class = shift;
47 5         26 my $self = $class->SUPER::new( @_ );
48 5         13 bless $self, $class;
49              
50 5         22 $self->objectify( 'larg' );
51 5         17 $self->objectify( 'rarg' );
52 5 100       18 if ( $self->{ 'quals' } ) {
53 4         15 $self->objectify( 'quals' );
54             }
55 5 100       20 if ( $self->{ 'usingClause' } ) {
56 1         4 $self->objectify( 'usingClause' );
57             }
58 5         150 return $self;
59             }
60              
61             sub join_type {
62 10     10 1 19 my $self = shift;
63 10 100       24 if ( !exists $self->{ '_join_type' } ) {
64 5         11 my $join_type = $self->{ 'jointype' };
65 5         42 $join_type =~ s/^JOIN_(.*)$/$1 JOIN/;
66 5 100       20 $join_type = 'JOIN' if $join_type eq 'INNER JOIN';
67 5 50 66     21 $join_type = "CROSS ${join_type}" if ( !exists $self->{ 'quals' } ) && ( !exists $self->{ 'usingClause' } );
68 5         16 $self->{ '_join_type' } = $join_type;
69             }
70 10         29 return $self->{ '_join_type' };
71             }
72              
73             sub as_text {
74 5     5 1 13 my $self = shift;
75              
76 5         15 my $join_cond = '';
77 5 100       101 if ( $self->{ 'usingClause' } ) {
    50          
78 1         2 $join_cond = 'USING ( ' . join( ', ', map { $_->as_ident } @{ $self->{ 'usingClause' } } ) . ' )';
  1         4  
  1         3  
79             }
80             elsif ( $self->{ 'quals' } ) {
81 4         21 $join_cond = 'ON ' . $self->{ 'quals' }->as_text;
82             }
83 5         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 5     5 1 10 my $self = shift;
88              
89 5         13 my $join_cond = '';
90 5 100       27 if ( $self->{ 'usingClause' } ) {
    50          
91 1         2 $join_cond = 'USING ( ' . join( ', ', map { $_->as_ident } @{ $self->{ 'usingClause' } } ) . ' )';
  1         5  
  1         4  
92             }
93             elsif ( $self->{ 'quals' } ) {
94 4         15 $join_cond = 'ON ' . $self->{ 'quals' }->pretty_print;
95             }
96 5         30 return sprintf "%s\n%s %s %s", $self->{ 'larg' }->pretty_print, $self->join_type, $self->{ 'rarg' }->pretty_print, $join_cond;
97             }
98              
99             1;