File Coverage

blib/lib/Pg/SQL/PrettyPrinter/Node/SubLink.pm
Criterion Covered Total %
statement 89 89 100.0
branch 22 24 91.6
condition n/a
subroutine 17 17 100.0
pod 3 3 100.0
total 131 133 98.5


line stmt bran cond sub pod time code
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     4   4295 use v5.26;
  4         16  
5 4     4   19 use strict;
  4         8  
  4         84  
6 4     4   16 use warnings;
  4         6  
  4         196  
7 4     4   16 use warnings qw( FATAL utf8 );
  4         6  
  4         193  
8 4     4   20 use utf8;
  4         7  
  4         24  
9 4     4   141 use open qw( :std :utf8 );
  4         6  
  4         34  
10 4     4   479 use Unicode::Normalize qw( NFC );
  4         8  
  4         212  
11 4     4   17 use Unicode::Collate;
  4         6  
  4         116  
12 4     4   16 use Encode qw( decode );
  4         5  
  4         525  
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   24 use autodie;
  4         7  
  4         28  
24 4     4   18224 use Carp qw( carp croak confess cluck );
  4         9  
  4         372  
25 4     4   25 use English qw( -no_match_vars );
  4         7  
  4         53  
26 4     4   1239 use Data::Dumper qw( Dumper );
  4         9  
  4         727  
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   23 use parent qw( Pg::SQL::PrettyPrinter::Node );
  4         6  
  4         25  
44              
45             sub new {
46 10     10 1 187 my $class = shift;
47 10         33 my $self = $class->SUPER::new( @_ );
48 10         16 bless $self, $class;
49 10         22 my @known_types_array = qw( ALL_SUBLINK ANY_SUBLINK EXPR_SUBLINK ARRAY_SUBLINK EXISTS_SUBLINK );
50 10         11 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         40 @known_types{ @known_types_array } = @known_types_array;
54              
55 10 50       30 croak( 'Unknown subselect type: ' . $self->{ 'subLinkType' } ) unless exists $known_types{ $self->{ 'subLinkType' } };
56              
57 10         28 $self->objectify( 'subselect' );
58 10 100       36 if ( $self->{ 'subLinkType' } =~ m{\A(?:ALL|ANY)_SUBLINK\z} ) {
59 3         9 $self->objectify( 'testexpr', 'operName' );
60             }
61 10 100       22 if ( exists $self->{ 'operName' } ) {
62 2 50       3 croak( "Can't handle operName with more than 1 element: " . Dumper( $self ) ) if 1 != scalar @{ $self->{ 'operName' } };
  2         5  
63             }
64              
65 10         33 return $self;
66             }
67              
68             sub as_text {
69 9     9 1 11 my $self = shift;
70 9         23 my $subselect_text = $self->{ 'subselect' }->as_text;
71              
72 9 100       40 if ( $self->{ 'subLinkType' } =~ m{\A(ANY|ALL)_SUBLINK\z} ) {
    100          
    100          
73 3         8 my $type = $1;
74 3 100       7 if ( exists $self->{ 'operName' } ) {
75 2         5 my $opname = $self->{ 'operName' }->[ 0 ]->{ 'str' };
76 2         36 return sprintf( '%s %s %s( %s )', $self->{ 'testexpr' }->as_text, $opname, $type, $subselect_text );
77             }
78             else {
79 1         3 return sprintf( '%s IN ( %s )', $self->{ 'testexpr' }->as_text, $subselect_text );
80             }
81             }
82             elsif ( $self->{ 'subLinkType' } eq 'ARRAY_SUBLINK' ) {
83 1         3 return sprintf( 'ARRAY( %s )', $subselect_text );
84             }
85             elsif ( $self->{ 'subLinkType' } eq 'EXISTS_SUBLINK' ) {
86 1         2 return sprintf( 'EXISTS ( %s )', $subselect_text );
87             }
88 4         10 return sprintf( '( %s )', $self->{ 'subselect' }->as_text );
89             }
90              
91             sub pretty_print {
92 8     8 1 14 my $self = shift;
93 8         23 my $subselect_text = $self->increase_indent( $self->{ 'subselect' }->pretty_print );
94              
95 8         19 my @lines = ();
96 8 100       34 if ( $self->{ 'subLinkType' } =~ m{\A(ANY|ALL)_SUBLINK\z} ) {
    100          
    100          
97 3         7 my $type = $1;
98 3 100       7 if ( exists $self->{ 'operName' } ) {
99 2         5 my $opname = $self->{ 'operName' }->[ 0 ]->{ 'str' };
100 2         6 push @lines, sprintf( '%s %s %s(', $self->{ 'testexpr' }->pretty_print, $opname, $type );
101 2         4 push @lines, $subselect_text;
102 2         4 push @lines, ')';
103             }
104             else {
105 1         2 push @lines, $self->{ 'testexpr' }->pretty_print . ' IN (';
106 1         2 push @lines, $subselect_text;
107 1         2 push @lines, ')';
108             }
109             }
110             elsif ( $self->{ 'subLinkType' } eq 'ARRAY_SUBLINK' ) {
111 1         2 push @lines, 'ARRAY(';
112 1         2 push @lines, $subselect_text;
113 1         1 push @lines, ')';
114             }
115             elsif ( $self->{ 'subLinkType' } eq 'EXISTS_SUBLINK' ) {
116 1         2 push @lines, 'EXISTS (';
117 1         1 push @lines, $subselect_text;
118 1         3 push @lines, ')';
119             }
120             else {
121 3         5 push @lines, '(';
122 3         5 push @lines, $subselect_text;
123 3         6 push @lines, ')';
124             }
125 8         26 return join( "\n", @lines );
126             }
127              
128             1;