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   10235 use v5.26;
  11         43  
5 11     11   52 use strict;
  11         21  
  11         233  
6 11     11   59 use warnings;
  11         17  
  11         622  
7 11     11   47 use warnings qw( FATAL utf8 );
  11         18  
  11         514  
8 11     11   50 use utf8;
  11         17  
  11         134  
9 11     11   461 use open qw( :std :utf8 );
  11         30  
  11         101  
10 11     11   1571 use Unicode::Normalize qw( NFC );
  11         18  
  11         723  
11 11     11   57 use Unicode::Collate;
  11         19  
  11         327  
12 11     11   48 use Encode qw( decode );
  11         50  
  11         1530  
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   60 use autodie;
  11         20  
  11         76  
24 11     11   51863 use Carp qw( carp croak confess cluck );
  11         25  
  11         963  
25 11     11   68 use English qw( -no_match_vars );
  11         19  
  11         130  
26 11     11   4355 use Data::Dumper qw( Dumper );
  11         22  
  11         2316  
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   72 use parent qw( Pg::SQL::PrettyPrinter::Node );
  11         19  
  11         82  
44              
45             sub new {
46 326     326 1 7639 my $class = shift;
47 326         849 my $self = $class->SUPER::new( @_ );
48 326         439 bless $self, $class;
49              
50 326         926 $self->objectify( 'val' );
51 326         538 return $self;
52             }
53              
54             sub as_text {
55 345     345 1 423 my $self = shift;
56 345   50     901 my $mode = shift || 'select';
57              
58 345         457 my $val;
59 345 50       552 if ( $mode eq 'assignment' ) {
60 0         0 $val = $self->quote_ident( $self->{ 'name' } ) . ' = ' . $self->{ 'val' }->as_text;
61             }
62             else {
63 345 100       595 if ( exists $self->{ 'val' } ) {
64 265         688 $val = $self->{ 'val' }->as_text();
65 265 100       541 if ( exists( $self->{ 'name' } ) ) {
66 18         83 $val .= ' AS ' . $self->quote_ident( $self->{ 'name' } );
67             }
68             }
69             else {
70 80         173 $val = $self->quote_ident( $self->{ 'name' } );
71             }
72             }
73 345         952 return $val;
74             }
75              
76             sub pretty_print {
77 248     248 1 298 my $self = shift;
78 248   50     857 my $mode = shift || 'select';
79              
80 248         302 my $val;
81 248 50       403 if ( $mode eq 'assignment' ) {
82 0         0 $val = $self->quote_ident( $self->{ 'name' } ) . ' = ' . $self->{ 'val' }->pretty_print;
83             }
84             else {
85 248 50       394 if ( exists $self->{ 'val' } ) {
86 248         708 $val = $self->{ 'val' }->pretty_print();
87 248 100       497 if ( exists( $self->{ 'name' } ) ) {
88 18         45 $val .= ' AS ' . $self->quote_ident( $self->{ 'name' } );
89             }
90             }
91             else {
92 0         0 $val = $self->quote_ident( $self->{ 'name' } );
93             }
94             }
95 248         808 return $val;
96             }
97              
98             1;