File Coverage

blib/lib/Pg/SQL/PrettyPrinter/Node/A_Indirection.pm
Criterion Covered Total %
statement 68 68 100.0
branch 12 16 75.0
condition n/a
subroutine 17 17 100.0
pod 3 3 100.0
total 100 104 96.1


line stmt bran cond sub pod time code
1             package Pg::SQL::PrettyPrinter::Node::A_Indirection;
2              
3             # UTF8 boilerplace, per http://stackoverflow.com/questions/6162484/why-does-modern-perl-avoid-utf-8-by-default/
4 3     3   4065 use v5.26;
  3         13  
5 3     3   17 use strict;
  3         4  
  3         105  
6 3     3   13 use warnings;
  3         5  
  3         171  
7 3     3   14 use warnings qw( FATAL utf8 );
  3         4  
  3         161  
8 3     3   49 use utf8;
  3         22  
  3         20  
9 3     3   96 use open qw( :std :utf8 );
  3         6  
  3         19  
10 3     3   402 use Unicode::Normalize qw( NFC );
  3         3  
  3         146  
11 3     3   14 use Unicode::Collate;
  3         5  
  3         110  
12 3     3   15 use Encode qw( decode );
  3         4  
  3         386  
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   15 use autodie;
  3         5  
  3         19  
24 3     3   13819 use Carp qw( carp croak confess cluck );
  3         5  
  3         301  
25 3     3   21 use English qw( -no_match_vars );
  3         6  
  3         26  
26 3     3   1155 use Data::Dumper qw( Dumper );
  3         7  
  3         662  
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   19 use parent qw( Pg::SQL::PrettyPrinter::Node );
  3         5  
  3         24  
44              
45             sub new {
46 6     6 1 114 my $class = shift;
47 6         15 my $self = $class->SUPER::new( @_ );
48 6         10 bless $self, $class;
49              
50 6         16 $self->objectify( 'arg', 'indirection' );
51              
52 6         12 return $self;
53             }
54              
55             sub as_text {
56 6     6 1 8 my $self = shift;
57 6         14 my $arg_as_text = $self->{ 'arg' }->as_text;
58 6 100       24 $arg_as_text = "( $arg_as_text )" unless $self->{ 'arg' }->isa( 'Pg::SQL::PrettyPrinter::Node::ColumnRef' );
59 6 100       8 if ( 1 == scalar @{ $self->{ 'indirection' } } ) {
  6         11  
60 5         8 my $ind = $self->{ 'indirection' }->[ 0 ];
61 5 50       18 return "${arg_as_text}.*" if $ind->isa( 'Pg::SQL::PrettyPrinter::Node::A_Star' );
62 5 50       12 return join( '.', $arg_as_text, $ind->as_ident ) if $ind->isa( 'Pg::SQL::PrettyPrinter::Node::String' );
63             }
64             return sprintf(
65             '%s%s',
66             $arg_as_text,
67 6         8 join( '', map { $_->as_text } @{ $self->{ 'indirection' } } )
  9         16  
  6         8  
68             );
69             }
70              
71             sub pretty_print {
72 6     6 1 9 my $self = shift;
73 6         17 my $arg_as_text = $self->{ 'arg' }->pretty_print;
74 6 100       25 $arg_as_text = "( $arg_as_text )" unless $self->{ 'arg' }->isa( 'Pg::SQL::PrettyPrinter::Node::ColumnRef' );
75 6 100       8 if ( 1 == scalar @{ $self->{ 'indirection' } } ) {
  6         14  
76 5         7 my $ind = $self->{ 'indirection' }->[ 0 ];
77 5 50       19 return "${arg_as_text}.*" if $ind->isa( 'Pg::SQL::PrettyPrinter::Node::A_Star' );
78 5 50       17 return join( '.', $arg_as_text, $ind->as_ident ) if $ind->isa( 'Pg::SQL::PrettyPrinter::Node::String' );
79             }
80              
81             return sprintf(
82             '%s%s',
83             $arg_as_text,
84 6         8 join( '', map { $_->pretty_print } @{ $self->{ 'indirection' } } )
  9         21  
  6         10  
85             );
86             }
87              
88             1;