File Coverage

blib/lib/Pg/SQL/PrettyPrinter/Node/BoolExpr.pm
Criterion Covered Total %
statement 78 79 98.7
branch 13 16 81.2
condition n/a
subroutine 17 17 100.0
pod 3 3 100.0
total 111 115 96.5


line stmt bran cond sub pod time code
1             package Pg::SQL::PrettyPrinter::Node::BoolExpr;
2              
3             # UTF8 boilerplace, per http://stackoverflow.com/questions/6162484/why-does-modern-perl-avoid-utf-8-by-default/
4 7     7   14153 use v5.26;
  7         38  
5 7     7   46 use strict;
  7         34  
  7         222  
6 7     7   36 use warnings;
  7         15  
  7         612  
7 7     7   44 use warnings qw( FATAL utf8 );
  7         13  
  7         555  
8 7     7   47 use utf8;
  7         14  
  7         57  
9 7     7   334 use open qw( :std :utf8 );
  7         12  
  7         87  
10 7     7   1381 use Unicode::Normalize qw( NFC );
  7         27  
  7         908  
11 7     7   56 use Unicode::Collate;
  7         15  
  7         324  
12 7     7   52 use Encode qw( decode );
  7         15  
  7         1474  
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 7     7   56 use autodie;
  7         15  
  7         78  
24 7     7   62600 use Carp qw( carp croak confess cluck );
  7         24  
  7         919  
25 7     7   57 use English qw( -no_match_vars );
  7         21  
  7         120  
26 7     7   4073 use Data::Dumper qw( Dumper );
  7         24  
  7         2197  
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 7     7   65 use parent qw( Pg::SQL::PrettyPrinter::Node );
  7         23  
  7         72  
44              
45             sub new {
46 16     16 1 499 my $class = shift;
47 16         106 my $self = $class->SUPER::new( @_ );
48 16         38 bless $self, $class;
49              
50 16         109 $self->objectify( 'args' );
51              
52 16         52 return $self;
53             }
54              
55             sub as_text {
56 18     18 1 42 my $self = shift;
57 18         58 my $this_op = $self->{ 'boolop' };
58 18 50       3831 $this_op =~ s/_EXPR\z// or croak( "Unknown boolean operation: ${this_op}!" );
59 18         41 my @nice_args;
60 18         35 for my $arg ( @{ $self->{ 'args' } } ) {
  18         54  
61 46         189 my $x = $arg->as_text;
62 46 100       159 if ( 'Pg::SQL::PrettyPrinter::Node::BoolExpr' eq ref $arg ) {
63 1         4 push @nice_args, "( ${x} )";
64             }
65             else {
66 45         122 push @nice_args, $x;
67             }
68             }
69              
70 18 100       64 if ( 1 == scalar @nice_args ) {
71 2         10 return sprintf "%s %s", $this_op, $nice_args[ 0 ];
72             }
73 16         118 return join( " ${this_op} ", @nice_args );
74             }
75              
76             sub pretty_print {
77 16     16 1 36 my $self = shift;
78 16         47 my $this_op = $self->{ 'boolop' };
79 16 50       100 $this_op =~ s/_EXPR\z// or croak( "Unknown boolean operation: ${this_op}!" );
80 16         32 my @nice_args;
81              
82 16         32 for my $arg ( @{ $self->{ 'args' } } ) {
  16         53  
83 41         141 my $x = $arg->pretty_print;
84 41 100       140 if ( 'Pg::SQL::PrettyPrinter::Node::BoolExpr' eq ref $arg ) {
85 1 50       4 if ( $x =~ m{\n} ) {
86 1         9 push @nice_args, join( "\n", "(", $self->increase_indent( $x ), ")" );
87             }
88             else {
89 0         0 push @nice_args, "( ${x} )";
90             }
91             }
92             else {
93 40         117 push @nice_args, $x;
94             }
95             }
96              
97 16 100       67 if ( 1 == scalar @nice_args ) {
98 2         10 return sprintf "%s %s", $this_op, $nice_args[ 0 ];
99             }
100 14         35 my $out = '';
101 14         55 for my $i ( 0 .. $#nice_args ) {
102 39         105 $out .= $nice_args[ $i ];
103 39 100       116 if ( $i < $#nice_args ) {
104 25         88 $out .= " ${this_op}\n";
105             }
106             }
107 14         74 return $out;
108             }
109              
110             1;