File Coverage

blib/lib/Pg/SQL/PrettyPrinter/Node/TypeCast.pm
Criterion Covered Total %
statement 68 69 98.5
branch 12 14 85.7
condition n/a
subroutine 16 16 100.0
pod 2 2 100.0
total 98 101 97.0


line stmt bran cond sub pod time code
1             package Pg::SQL::PrettyPrinter::Node::TypeCast;
2              
3             # UTF8 boilerplace, per http://stackoverflow.com/questions/6162484/why-does-modern-perl-avoid-utf-8-by-default/
4 3     3   2539 use v5.26;
  3         12  
5 3     3   17 use strict;
  3         9  
  3         65  
6 3     3   16 use warnings;
  3         6  
  3         89  
7 3     3   15 use warnings qw( FATAL utf8 );
  3         7  
  3         104  
8 3     3   17 use utf8;
  3         7  
  3         23  
9 3     3   89 use open qw( :std :utf8 );
  3         6  
  3         16  
10 3     3   415 use Unicode::Normalize qw( NFC );
  3         6  
  3         188  
11 3     3   22 use Unicode::Collate;
  3         16  
  3         110  
12 3     3   17 use Encode qw( decode );
  3         20  
  3         190  
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   683 use autodie;
  3         8  
  3         20  
24 3     3   16998 use Carp qw( carp croak confess cluck );
  3         7  
  3         255  
25 3     3   21 use English qw( -no_match_vars );
  3         9  
  3         18  
26 3     3   1132 use Data::Dumper qw( Dumper );
  3         8  
  3         671  
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   27 use parent qw( Pg::SQL::PrettyPrinter::Node );
  3         13  
  3         21  
44              
45             sub new {
46 33     33 1 1073 my $class = shift;
47 33         123 my $self = $class->SUPER::new( @_ );
48 33         79 bless $self, $class;
49              
50 33         224 $self->objectify(
51             'arg',
52             [ 'typeName', 'names' ],
53             [ 'typeName', 'typmods' ],
54             [ 'typeName', 'arrayBounds' ]
55             );
56              
57 33         123 return $self;
58             }
59              
60             sub as_text {
61 62     62 1 121 my $self = shift;
62 62         197 my $arg = $self->{ 'arg' }->as_text;
63 62 50       333 if ( ref( $self->{ 'arg' } ) =~ m{\APg::SQL::PrettyPrinter::Node::(?:A_|Bool)Expr\z} ) {
64 0         0 $arg = "( $arg )";
65             }
66 62         129 my $typname = join( '.', map { $_->as_ident } @{ $self->{ 'typeName' }->{ 'names' } } );
  90         261  
  62         162  
67              
68 62 100       195 if ( $typname eq 'pg_catalog.bool' ) {
69 24 100       71 return 'true' if $arg eq "'t'";
70 14 100       35 return 'false' if $arg eq "'f'";
71             }
72 50         103 my $typmods = '';
73 50 100       128 if ( exists $self->{ 'typeName' }->{ 'typmods' } ) {
74 2         4 $typmods = '( ' . join( ', ', map { $_->as_text } @{ $self->{ 'typeName' }->{ 'typmods' } } ) . ' )';
  2         6  
  2         5  
75             }
76 50 100       124 if ( exists $self->{ 'typeName' }->{ 'arrayBounds' } ) {
77 12         55 my @bounds_as_text = map { $_->as_text } @{ $self->{ 'typeName' }->{ 'arrayBounds' } };
  12         42  
  12         28  
78 12         66 my $array_def = sprintf '[%s]', join( ', ', @bounds_as_text );
79 12 50       48 $array_def = '[]' if $array_def eq '[-1]';
80 12         27 $typmods .= $array_def;
81             }
82 50         247 return sprintf '%s::%s%s', $arg, $typname, $typmods;
83             }
84              
85             1;