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 3     3   2306 use v5.26;
  3         73  
5 3     3   22 use strict;
  3         6  
  3         64  
6 3     3   15 use warnings;
  3         7  
  3         82  
7 3     3   15 use warnings qw( FATAL utf8 );
  3         13  
  3         101  
8 3     3   28 use utf8;
  3         7  
  3         20  
9 3     3   83 use open qw( :std :utf8 );
  3         5  
  3         14  
10 3     3   406 use Unicode::Normalize qw( NFC );
  3         6  
  3         198  
11 3     3   22 use Unicode::Collate;
  3         6  
  3         101  
12 3     3   17 use Encode qw( decode );
  3         6  
  3         206  
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   702 use autodie;
  3         7  
  3         45  
24 3     3   16887 use Carp qw( carp croak confess cluck );
  3         15  
  3         278  
25 3     3   22 use English qw( -no_match_vars );
  3         8  
  3         17  
26 3     3   1164 use Data::Dumper qw( Dumper );
  3         11  
  3         700  
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         7  
  3         21  
44              
45             sub new {
46 10     10 1 280 my $class = shift;
47 10         58 my $self = $class->SUPER::new( @_ );
48 10         20 bless $self, $class;
49 10         33 my @known_types_array = qw( ALL_SUBLINK ANY_SUBLINK EXPR_SUBLINK ARRAY_SUBLINK EXISTS_SUBLINK );
50 10         16 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         43 @known_types{ @known_types_array } = @known_types_array;
54              
55 10 50       42 croak( 'Unknown subselect type: ' . $self->{ 'subLinkType' } ) unless exists $known_types{ $self->{ 'subLinkType' } };
56              
57 10         42 $self->objectify( 'subselect' );
58 10 100       42 if ( $self->{ 'subLinkType' } =~ m{\A(?:ALL|ANY)_SUBLINK\z} ) {
59 3         12 $self->objectify( 'testexpr', 'operName' );
60             }
61 10 100       29 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         41 return $self;
66             }
67              
68             sub as_text {
69 9     9 1 20 my $self = shift;
70 9         42 my $subselect_text = $self->{ 'subselect' }->as_text;
71              
72 9 100       53 if ( $self->{ 'subLinkType' } =~ m{\A(ANY|ALL)_SUBLINK\z} ) {
    100          
    100          
73 3         11 my $type = $1;
74 3 100       10 if ( exists $self->{ 'operName' } ) {
75 2         7 my $opname = $self->{ 'operName' }->[ 0 ]->{ 'str' };
76 2         8 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         7 return sprintf( 'ARRAY( %s )', $subselect_text );
84             }
85             elsif ( $self->{ 'subLinkType' } eq 'EXISTS_SUBLINK' ) {
86 1         5 return sprintf( 'EXISTS ( %s )', $subselect_text );
87             }
88 4         15 return sprintf( '( %s )', $self->{ 'subselect' }->as_text );
89             }
90              
91             sub pretty_print {
92 8     8 1 19 my $self = shift;
93 8         35 my $subselect_text = $self->increase_indent( $self->{ 'subselect' }->pretty_print );
94              
95 8         26 my @lines = ();
96 8 100       46 if ( $self->{ 'subLinkType' } =~ m{\A(ANY|ALL)_SUBLINK\z} ) {
    100          
    100          
97 3         8 my $type = $1;
98 3 100       12 if ( exists $self->{ 'operName' } ) {
99 2         8 my $opname = $self->{ 'operName' }->[ 0 ]->{ 'str' };
100 2         9 push @lines, sprintf( '%s %s %s(', $self->{ 'testexpr' }->pretty_print, $opname, $type );
101 2         6 push @lines, $subselect_text;
102 2         5 push @lines, ')';
103             }
104             else {
105 1         53 push @lines, $self->{ 'testexpr' }->pretty_print . ' IN (';
106 1         3 push @lines, $subselect_text;
107 1         7 push @lines, ')';
108             }
109             }
110             elsif ( $self->{ 'subLinkType' } eq 'ARRAY_SUBLINK' ) {
111 1         3 push @lines, 'ARRAY(';
112 1         2 push @lines, $subselect_text;
113 1         2 push @lines, ')';
114             }
115             elsif ( $self->{ 'subLinkType' } eq 'EXISTS_SUBLINK' ) {
116 1         2 push @lines, 'EXISTS (';
117 1         2 push @lines, $subselect_text;
118 1         2 push @lines, ')';
119             }
120             else {
121 3         7 push @lines, '(';
122 3         7 push @lines, $subselect_text;
123 3         8 push @lines, ')';
124             }
125 8         38 return join( "\n", @lines );
126             }
127              
128             1;