| 1 | package Pg::SQL::PrettyPrinter::Node::SubLink; | ||||||||||||||||||
| 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 | 10 | ▶ | sub new { | ||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||
| 46 | 10 | my $class = shift; | |||||||||||||||||
| 47 | 10 | my $self = $class->SUPER::new( @_ ); | |||||||||||||||||
| 48 | 10 | bless $self, $class; | |||||||||||||||||
| 49 | 10 | my @known_types_array = qw( ALL_SUBLINK ANY_SUBLINK EXPR_SUBLINK ARRAY_SUBLINK EXISTS_SUBLINK ); | |||||||||||||||||
| 50 | 10 | my %known_types; | |||||||||||||||||
| 51 | |||||||||||||||||||
| 52 | # Builds %known_types, where each key is elements form @known_types_array, and the value is the same as key. | ||||||||||||||||||
| 53 | 10 | @known_types{ @known_types_array } = @known_types_array; | |||||||||||||||||
| 54 | |||||||||||||||||||
| 55 | 10 | ▶ | croak( 'Unknown subselect type: ' . $self->{ 'subLinkType' } ) unless exists $known_types{ $self->{ 'subLinkType' } }; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 56 | |||||||||||||||||||
| 57 | 10 | $self->objectify( 'subselect' ); | |||||||||||||||||
| 58 | 10 | ▶ | if ( $self->{ 'subLinkType' } =~ m{\A(?:ALL|ANY)_SUBLINK\z} ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 59 | 3 | $self->objectify( 'testexpr', 'operName' ); | |||||||||||||||||
| 60 | } | ||||||||||||||||||
| 61 | 10 | ▶ | if ( exists $self->{ 'operName' } ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 62 | 2 | ▶ | croak( "Can't handle operName with more than 1 element: " . Dumper( $self ) ) if 1 != scalar @{ $self->{ 'operName' } }; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 63 | } | ||||||||||||||||||
| 64 | |||||||||||||||||||
| 65 | 10 | return $self; | |||||||||||||||||
| 66 | } | ||||||||||||||||||
| 67 | |||||||||||||||||||
| 68 | 9 | ▶ | sub as_text { | ||||||||||||||||
|
Subroutine
as_text: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||
| 69 | 9 | my $self = shift; | |||||||||||||||||
| 70 | 9 | my $subselect_text = $self->{ 'subselect' }->as_text; | |||||||||||||||||
| 71 | |||||||||||||||||||
| 72 | 9 | ▶ | if ( $self->{ 'subLinkType' } =~ m{\A(ANY|ALL)_SUBLINK\z} ) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 73 | 3 | my $type = $1; | |||||||||||||||||
| 74 | 3 | ▶ | if ( exists $self->{ 'operName' } ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 75 | 2 | my $opname = $self->{ 'operName' }->[ 0 ]->{ 'str' }; | |||||||||||||||||
| 76 | 2 | return sprintf( '%s %s %s( %s )', $self->{ 'testexpr' }->as_text, $opname, $type, $subselect_text ); | |||||||||||||||||
| 77 | } | ||||||||||||||||||
| 78 | else { | ||||||||||||||||||
| 79 | 1 | return sprintf( '%s IN ( %s )', $self->{ 'testexpr' }->as_text, $subselect_text ); | |||||||||||||||||
| 80 | } | ||||||||||||||||||
| 81 | } | ||||||||||||||||||
| 82 | elsif ( $self->{ 'subLinkType' } eq 'ARRAY_SUBLINK' ) { | ||||||||||||||||||
| 83 | 1 | return sprintf( 'ARRAY( %s )', $subselect_text ); | |||||||||||||||||
| 84 | } | ||||||||||||||||||
| 85 | elsif ( $self->{ 'subLinkType' } eq 'EXISTS_SUBLINK' ) { | ||||||||||||||||||
| 86 | 1 | return sprintf( 'EXISTS ( %s )', $subselect_text ); | |||||||||||||||||
| 87 | } | ||||||||||||||||||
| 88 | 4 | return sprintf( '( %s )', $self->{ 'subselect' }->as_text ); | |||||||||||||||||
| 89 | } | ||||||||||||||||||
| 90 | |||||||||||||||||||
| 91 | 8 | ▶ | sub pretty_print { | ||||||||||||||||
|
Subroutine
pretty_print: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||
| 92 | 8 | my $self = shift; | |||||||||||||||||
| 93 | 8 | my $subselect_text = $self->increase_indent( $self->{ 'subselect' }->pretty_print ); | |||||||||||||||||
| 94 | |||||||||||||||||||
| 95 | 8 | my @lines = (); | |||||||||||||||||
| 96 | 8 | ▶ | if ( $self->{ 'subLinkType' } =~ m{\A(ANY|ALL)_SUBLINK\z} ) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 97 | 3 | my $type = $1; | |||||||||||||||||
| 98 | 3 | ▶ | if ( exists $self->{ 'operName' } ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 99 | 2 | my $opname = $self->{ 'operName' }->[ 0 ]->{ 'str' }; | |||||||||||||||||
| 100 | 2 | push @lines, sprintf( '%s %s %s(', $self->{ 'testexpr' }->pretty_print, $opname, $type ); | |||||||||||||||||
| 101 | 2 | push @lines, $subselect_text; | |||||||||||||||||
| 102 | 2 | push @lines, ')'; | |||||||||||||||||
| 103 | } | ||||||||||||||||||
| 104 | else { | ||||||||||||||||||
| 105 | 1 | push @lines, $self->{ 'testexpr' }->pretty_print . ' IN ('; | |||||||||||||||||
| 106 | 1 | push @lines, $subselect_text; | |||||||||||||||||
| 107 | 1 | push @lines, ')'; | |||||||||||||||||
| 108 | } | ||||||||||||||||||
| 109 | } | ||||||||||||||||||
| 110 | elsif ( $self->{ 'subLinkType' } eq 'ARRAY_SUBLINK' ) { | ||||||||||||||||||
| 111 | 1 | push @lines, 'ARRAY('; | |||||||||||||||||
| 112 | 1 | push @lines, $subselect_text; | |||||||||||||||||
| 113 | 1 | push @lines, ')'; | |||||||||||||||||
| 114 | } | ||||||||||||||||||
| 115 | elsif ( $self->{ 'subLinkType' } eq 'EXISTS_SUBLINK' ) { | ||||||||||||||||||
| 116 | 1 | push @lines, 'EXISTS ('; | |||||||||||||||||
| 117 | 1 | push @lines, $subselect_text; | |||||||||||||||||
| 118 | 1 | push @lines, ')'; | |||||||||||||||||
| 119 | } | ||||||||||||||||||
| 120 | else { | ||||||||||||||||||
| 121 | 3 | push @lines, '('; | |||||||||||||||||
| 122 | 3 | push @lines, $subselect_text; | |||||||||||||||||
| 123 | 3 | push @lines, ')'; | |||||||||||||||||
| 124 | } | ||||||||||||||||||
| 125 | 8 | return join( "\n", @lines ); | |||||||||||||||||
| 126 | } | ||||||||||||||||||
| 127 | |||||||||||||||||||
| 128 | 4 | 1; | |||||||||||||||||