File Coverage

blib/lib/Pg/SQL/PrettyPrinter/Node/ResTarget.pm
Criterion Covered Total %
statement 65 68 95.5
branch 9 12 75.0
condition 2 4 50.0
subroutine 17 17 100.0
pod 3 3 100.0
total 96 104 92.3


line stmt bran cond sub pod time code
1             package Pg::SQL::PrettyPrinter::Node::ResTarget;
2              
3             # UTF8 boilerplace, per http://stackoverflow.com/questions/6162484/why-does-modern-perl-avoid-utf-8-by-default/
4 11     11   9713 use v5.26;
  11         42  
5 11     11   58 use strict;
  11         18  
  11         253  
6 11     11   75 use warnings;
  11         21  
  11         640  
7 11     11   82 use warnings qw( FATAL utf8 );
  11         33  
  11         490  
8 11     11   50 use utf8;
  11         19  
  11         118  
9 11     11   417 use open qw( :std :utf8 );
  11         21  
  11         78  
10 11     11   1457 use Unicode::Normalize qw( NFC );
  11         20  
  11         667  
11 11     11   75 use Unicode::Collate;
  11         17  
  11         438  
12 11     11   52 use Encode qw( decode );
  11         52  
  11         1403  
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 11     11   62 use autodie;
  11         20  
  11         73  
24 11     11   51150 use Carp qw( carp croak confess cluck );
  11         26  
  11         996  
25 11     11   73 use English qw( -no_match_vars );
  11         29  
  11         89  
26 11     11   4168 use Data::Dumper qw( Dumper );
  11         18  
  11         2530  
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 11     11   96 use parent qw( Pg::SQL::PrettyPrinter::Node );
  11         23  
  11         115  
44              
45             sub new {
46 326     326 1 9246 my $class = shift;
47 326         994 my $self = $class->SUPER::new( @_ );
48 326         526 bless $self, $class;
49              
50 326         972 $self->objectify( 'val' );
51 326         654 return $self;
52             }
53              
54             sub as_text {
55 345     345 1 513 my $self = shift;
56 345   50     1217 my $mode = shift || 'select';
57              
58 345         543 my $val;
59 345 50       652 if ( $mode eq 'assignment' ) {
60 0         0 $val = $self->quote_ident( $self->{ 'name' } ) . ' = ' . $self->{ 'val' }->as_text;
61             }
62             else {
63 345 100       700 if ( exists $self->{ 'val' } ) {
64 265         789 $val = $self->{ 'val' }->as_text();
65 265 100       782 if ( exists( $self->{ 'name' } ) ) {
66 18         68 $val .= ' AS ' . $self->quote_ident( $self->{ 'name' } );
67             }
68             }
69             else {
70 80         191 $val = $self->quote_ident( $self->{ 'name' } );
71             }
72             }
73 345         1067 return $val;
74             }
75              
76             sub pretty_print {
77 248     248 1 405 my $self = shift;
78 248   50     809 my $mode = shift || 'select';
79              
80 248         397 my $val;
81 248 50       498 if ( $mode eq 'assignment' ) {
82 0         0 $val = $self->quote_ident( $self->{ 'name' } ) . ' = ' . $self->{ 'val' }->pretty_print;
83             }
84             else {
85 248 50       516 if ( exists $self->{ 'val' } ) {
86 248         930 $val = $self->{ 'val' }->pretty_print();
87 248 100       682 if ( exists( $self->{ 'name' } ) ) {
88 18         48 $val .= ' AS ' . $self->quote_ident( $self->{ 'name' } );
89             }
90             }
91             else {
92 0         0 $val = $self->quote_ident( $self->{ 'name' } );
93             }
94             }
95 248         886 return $val;
96             }
97              
98             1;