| 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 | ▶ | use v5.26; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use warnings qw( FATAL utf8 ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use open qw( :std :utf8 ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use Unicode::Normalize qw( NFC ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use Unicode::Collate; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use Encode qw( decode ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 4 | ▶ | if ( grep /\P{ASCII}/ => @ARGV ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 15 | 0 | @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 | ▶ | use autodie; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 4 | ▶ | use Carp qw( carp croak confess cluck ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 4 | ▶ | use English qw( -no_match_vars ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 4 | ▶ | use Data::Dumper qw( Dumper ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | # give a full stack dump on any untrapped exceptions | ||||||||||||||||||||||||||||
| 29 | 0 | ▶ | local $SIG{ __DIE__ } = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 30 | 0 | ▶ | confess "Uncaught exception: @_" unless $^S; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 4 | }; | |||||||||||||||||||||||||||
| 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 | 0 | ▶ | local $SIG{ __WARN__ } = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 37 | 0 | ▶ | if ( $^S ) { cluck "Trapped warning: @_" } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 0 | else { confess "Deadly warning: @_" } | |||||||||||||||||||||||||||
| 39 | 4 | }; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | # Useful common code | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 4 | ▶ | use parent qw( Pg::SQL::PrettyPrinter::Node ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 7 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 46 | 7 | my $class = shift; | |||||||||||||||||||||||||||
| 47 | 7 | my $self = $class->SUPER::new( @_ ); | |||||||||||||||||||||||||||
| 48 | 7 | bless $self, $class; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 7 | $self->objectify( 'larg' ); | |||||||||||||||||||||||||||
| 51 | 7 | $self->objectify( 'rarg' ); | |||||||||||||||||||||||||||
| 52 | 7 | ▶ | if ( $self->{ 'quals' } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 6 | $self->objectify( 'quals' ); | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | 7 | ▶ | if ( $self->{ 'usingClause' } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 1 | $self->objectify( 'usingClause' ); | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | 7 | return $self; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 14 | ▶ | sub join_type { | ||||||||||||||||||||||||||
|
Subroutine
join_type: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | 14 | my $self = shift; | |||||||||||||||||||||||||||
| 63 | 14 | ▶ | if ( !exists $self->{ '_join_type' } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 7 | my $join_type = $self->{ 'jointype' }; | |||||||||||||||||||||||||||
| 65 | 7 | $join_type =~ s/^JOIN_(.*)$/$1 JOIN/; | |||||||||||||||||||||||||||
| 66 | 7 | ▶ | $join_type = 'JOIN' if $join_type eq 'INNER JOIN'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 7 | ▶ | $join_type = "CROSS ${join_type}" if ( !exists $self->{ 'quals' } ) && ( !exists $self->{ 'usingClause' } ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $self->{"quals"} or exists $self->{"usingClause"}
MC/DC0%0/2
exists $self->{"quals"} or exists $self->{"usingClause"}
exists $self->{"quals"}
exists $self->{"usingClause"}
Truth table66%2/3
A or B
A= exists $self->{"quals"}B= exists $self->{"usingClause"}
| |||||||||||||||||||||||||||||
| 68 | 7 | $self->{ '_join_type' } = $join_type; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | 14 | return $self->{ '_join_type' }; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 7 | ▶ | sub as_text { | ||||||||||||||||||||||||||
|
Subroutine
as_text: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 74 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 7 | my $join_cond = ''; | |||||||||||||||||||||||||||
| 77 | 7 | ▶ | if ( $self->{ 'usingClause' } ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 78 | 1 | $join_cond = 'USING ( ' . join( ', ', map { $_->as_ident } @{ $self->{ 'usingClause' } } ) . ' )'; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | elsif ( $self->{ 'quals' } ) { | ||||||||||||||||||||||||||||
| 81 | 6 | $join_cond = 'ON ' . $self->{ 'quals' }->as_text; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | 7 | return sprintf '%s %s %s %s', $self->{ 'larg' }->as_text, $self->join_type, $self->{ 'rarg' }->as_text, $join_cond; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 7 | ▶ | sub pretty_print { | ||||||||||||||||||||||||||
|
Subroutine
pretty_print: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 87 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 7 | my $join_cond = ''; | |||||||||||||||||||||||||||
| 90 | 7 | ▶ | if ( $self->{ 'usingClause' } ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 91 | 1 | $join_cond = 'USING ( ' . join( ', ', map { $_->as_ident } @{ $self->{ 'usingClause' } } ) . ' )'; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | elsif ( $self->{ 'quals' } ) { | ||||||||||||||||||||||||||||
| 94 | 6 | my $Q = $self->{ 'quals' }; | |||||||||||||||||||||||||||
| 95 | 6 | my $q_text = $Q->as_text; | |||||||||||||||||||||||||||
| 96 | 6 | my $q_pretty = $Q->pretty_print; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # If join condition is multiline, indent it in nicer way. | ||||||||||||||||||||||||||||
| 99 | 6 | ▶ | if ( $q_pretty =~ /\n/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 2 | $join_cond = sprintf( "ON\n%s", $self->increase_indent( $q_pretty ) ); | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | else { | ||||||||||||||||||||||||||||
| 103 | 4 | ▶ | $join_cond = 'ON ' . $q_pretty if $q_pretty !~ /\n/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | 7 | return sprintf "%s\n%s %s %s", $self->{ 'larg' }->pretty_print, $self->join_type, $self->{ 'rarg' }->pretty_print, $join_cond; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 4 | 1; | |||||||||||||||||||||||||||