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   6150 use v5.26;
  7         23  
5 7     7   33 use strict;
  7         13  
  7         151  
6 7     7   25 use warnings;
  7         11  
  7         350  
7 7     7   28 use warnings qw( FATAL utf8 );
  7         10  
  7         291  
8 7     7   32 use utf8;
  7         11  
  7         44  
9 7     7   232 use open qw( :std :utf8 );
  7         12  
  7         44  
10 7     7   828 use Unicode::Normalize qw( NFC );
  7         20  
  7         400  
11 7     7   32 use Unicode::Collate;
  7         10  
  7         190  
12 7     7   33 use Encode qw( decode );
  7         10  
  7         932  
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   39 use autodie;
  7         12  
  7         54  
24 7     7   27349 use Carp qw( carp croak confess cluck );
  7         13  
  7         536  
25 7     7   34 use English qw( -no_match_vars );
  7         12  
  7         47  
26 7     7   2245 use Data::Dumper qw( Dumper );
  7         11  
  7         1274  
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   37 use parent qw( Pg::SQL::PrettyPrinter::Node );
  7         12  
  7         44  
44              
45             sub new {
46 16     16 1 339 my $class = shift;
47 16         76 my $self = $class->SUPER::new( @_ );
48 16         31 bless $self, $class;
49              
50 16         77 $self->objectify( 'args' );
51              
52 16         35 return $self;
53             }
54              
55             sub as_text {
56 18     18 1 28 my $self = shift;
57 18         34 my $this_op = $self->{ 'boolop' };
58 18 50       110 $this_op =~ s/_EXPR\z// or croak( "Unknown boolean operation: ${this_op}!" );
59 18         27 my @nice_args;
60 18         26 for my $arg ( @{ $self->{ 'args' } } ) {
  18         35  
61 46         93 my $x = $arg->as_text;
62 46 100       89 if ( 'Pg::SQL::PrettyPrinter::Node::BoolExpr' eq ref $arg ) {
63 1         3 push @nice_args, "( ${x} )";
64             }
65             else {
66 45         84 push @nice_args, $x;
67             }
68             }
69              
70 18 100       42 if ( 1 == scalar @nice_args ) {
71 2         7 return sprintf "%s %s", $this_op, $nice_args[ 0 ];
72             }
73 16         64 return join( " ${this_op} ", @nice_args );
74             }
75              
76             sub pretty_print {
77 16     16 1 27 my $self = shift;
78 16         32 my $this_op = $self->{ 'boolop' };
79 16 50       66 $this_op =~ s/_EXPR\z// or croak( "Unknown boolean operation: ${this_op}!" );
80 16         24 my @nice_args;
81              
82 16         24 for my $arg ( @{ $self->{ 'args' } } ) {
  16         34  
83 41         95 my $x = $arg->pretty_print;
84 41 100       84 if ( 'Pg::SQL::PrettyPrinter::Node::BoolExpr' eq ref $arg ) {
85 1 50       5 if ( $x =~ m{\n} ) {
86 1         7 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         73 push @nice_args, $x;
94             }
95             }
96              
97 16 100       42 if ( 1 == scalar @nice_args ) {
98 2         6 return sprintf "%s %s", $this_op, $nice_args[ 0 ];
99             }
100 14         26 my $out = '';
101 14         37 for my $i ( 0 .. $#nice_args ) {
102 39         65 $out .= $nice_args[ $i ];
103 39 100       71 if ( $i < $#nice_args ) {
104 25         83 $out .= " ${this_op}\n";
105             }
106             }
107 14         76 return $out;
108             }
109              
110             1;