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   8844 use v5.26;
  4         27  
5 4     4   24 use strict;
  4         10  
  4         139  
6 4     4   20 use warnings;
  4         9  
  4         362  
7 4     4   25 use warnings qw( FATAL utf8 );
  4         68  
  4         288  
8 4     4   27 use utf8;
  4         9  
  4         37  
9 4     4   289 use open qw( :std :utf8 );
  4         8  
  4         40  
10 4     4   825 use Unicode::Normalize qw( NFC );
  4         12  
  4         410  
11 4     4   29 use Unicode::Collate;
  4         9  
  4         248  
12 4     4   32 use Encode qw( decode );
  4         18  
  4         793  
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   31 use autodie;
  4         9  
  4         48  
24 4     4   37725 use Carp qw( carp croak confess cluck );
  4         10  
  4         667  
25 4     4   41 use English qw( -no_match_vars );
  4         13  
  4         65  
26 4     4   2414 use Data::Dumper qw( Dumper );
  4         12  
  4         1406  
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   47 use parent qw( Pg::SQL::PrettyPrinter::Node );
  4         7  
  4         39  
44              
45             sub new {
46 10     10 1 364 my $class = shift;
47 10         50 my $self = $class->SUPER::new( @_ );
48 10         22 bless $self, $class;
49 10         75 my @known_types_array = qw( ALL_SUBLINK ANY_SUBLINK EXPR_SUBLINK ARRAY_SUBLINK EXISTS_SUBLINK );
50 10         24 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         86 @known_types{ @known_types_array } = @known_types_array;
54              
55 10 50       69 croak( 'Unknown subselect type: ' . $self->{ 'subLinkType' } ) unless exists $known_types{ $self->{ 'subLinkType' } };
56              
57 10         56 $self->objectify( 'subselect' );
58 10 100       61 if ( $self->{ 'subLinkType' } =~ m{\A(?:ALL|ANY)_SUBLINK\z} ) {
59 3         13 $self->objectify( 'testexpr', 'operName' );
60             }
61 10 100       33 if ( exists $self->{ 'operName' } ) {
62 2 50       5 croak( "Can't handle operName with more than 1 element: " . Dumper( $self ) ) if 1 != scalar @{ $self->{ 'operName' } };
  2         9  
63             }
64              
65 10         77 return $self;
66             }
67              
68             sub as_text {
69 9     9 1 19 my $self = shift;
70 9         38 my $subselect_text = $self->{ 'subselect' }->as_text;
71              
72 9 100       65 if ( $self->{ 'subLinkType' } =~ m{\A(ANY|ALL)_SUBLINK\z} ) {
    100          
    100          
73 3         15 my $type = $1;
74 3 100       12 if ( exists $self->{ 'operName' } ) {
75 2         7 my $opname = $self->{ 'operName' }->[ 0 ]->{ 'str' };
76 2         13 return sprintf( '%s %s %s( %s )', $self->{ 'testexpr' }->as_text, $opname, $type, $subselect_text );
77             }
78             else {
79 1         4 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         3 return sprintf( 'EXISTS ( %s )', $subselect_text );
87             }
88 4         17 return sprintf( '( %s )', $self->{ 'subselect' }->as_text );
89             }
90              
91             sub pretty_print {
92 8     8 1 17 my $self = shift;
93 8         37 my $subselect_text = $self->increase_indent( $self->{ 'subselect' }->pretty_print );
94              
95 8         21 my @lines = ();
96 8 100       67 if ( $self->{ 'subLinkType' } =~ m{\A(ANY|ALL)_SUBLINK\z} ) {
    100          
    100          
97 3         15 my $type = $1;
98 3 100       28 if ( exists $self->{ 'operName' } ) {
99 2         8 my $opname = $self->{ 'operName' }->[ 0 ]->{ 'str' };
100 2         12 push @lines, sprintf( '%s %s %s(', $self->{ 'testexpr' }->pretty_print, $opname, $type );
101 2         30 push @lines, $subselect_text;
102 2         9 push @lines, ')';
103             }
104             else {
105 1         4 push @lines, $self->{ 'testexpr' }->pretty_print . ' IN (';
106 1         3 push @lines, $subselect_text;
107 1         2 push @lines, ')';
108             }
109             }
110             elsif ( $self->{ 'subLinkType' } eq 'ARRAY_SUBLINK' ) {
111 1         3 push @lines, 'ARRAY(';
112 1         3 push @lines, $subselect_text;
113 1         2 push @lines, ')';
114             }
115             elsif ( $self->{ 'subLinkType' } eq 'EXISTS_SUBLINK' ) {
116 1         9 push @lines, 'EXISTS (';
117 1         3 push @lines, $subselect_text;
118 1         3 push @lines, ')';
119             }
120             else {
121 3         10 push @lines, '(';
122 3         26 push @lines, $subselect_text;
123 3         8 push @lines, ')';
124             }
125 8         49 return join( "\n", @lines );
126             }
127              
128             1;