| 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 | ▶ | use v5.26; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 7 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 7 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 7 | ▶ | use warnings qw( FATAL utf8 ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 7 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 7 | ▶ | use open qw( :std :utf8 ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 7 | ▶ | use Unicode::Normalize qw( NFC ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 7 | ▶ | use Unicode::Collate; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 7 | ▶ | use Encode qw( decode ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 7 | ▶ | if ( grep /\P{ASCII}/ => @ARGV ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 15 | 0 | @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 | ▶ | use autodie; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | 7 | ▶ | use Carp qw( carp croak confess cluck ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | 7 | ▶ | use English qw( -no_match_vars ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | 7 | ▶ | use Data::Dumper qw( Dumper ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 27 | |||||||||||
| 28 | # give a full stack dump on any untrapped exceptions | ||||||||||
| 29 | 0 | ▶ | local $SIG{ __DIE__ } = sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 30 | 0 | ▶ | confess "Uncaught exception: @_" unless $^S; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | 7 | }; | |||||||||
| 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 | 0 | ▶ | local $SIG{ __WARN__ } = sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 37 | 0 | ▶ | if ( $^S ) { cluck "Trapped warning: @_" } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | 0 | else { confess "Deadly warning: @_" } | |||||||||
| 39 | 7 | }; | |||||||||
| 40 | |||||||||||
| 41 | # Useful common code | ||||||||||
| 42 | |||||||||||
| 43 | 7 | ▶ | use parent qw( Pg::SQL::PrettyPrinter::Node ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 44 | |||||||||||
| 45 | 16 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 46 | 16 | my $class = shift; | |||||||||
| 47 | 16 | my $self = $class->SUPER::new( @_ ); | |||||||||
| 48 | 16 | bless $self, $class; | |||||||||
| 49 | |||||||||||
| 50 | 16 | $self->objectify( 'args' ); | |||||||||
| 51 | |||||||||||
| 52 | 16 | return $self; | |||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | 18 | ▶ | sub as_text { | ||||||||
|
Subroutine
as_text: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 56 | 18 | my $self = shift; | |||||||||
| 57 | 18 | my $this_op = $self->{ 'boolop' }; | |||||||||
| 58 | 18 | ▶ | $this_op =~ s/_EXPR\z// or croak( "Unknown boolean operation: ${this_op}!" ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 18 | my @nice_args; | |||||||||
| 60 | 18 | for my $arg ( @{ $self->{ 'args' } } ) { | |||||||||
| 61 | 46 | my $x = $arg->as_text; | |||||||||
| 62 | 46 | ▶ | if ( 'Pg::SQL::PrettyPrinter::Node::BoolExpr' eq ref $arg ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 1 | push @nice_args, "( ${x} )"; | |||||||||
| 64 | } | ||||||||||
| 65 | else { | ||||||||||
| 66 | 45 | push @nice_args, $x; | |||||||||
| 67 | } | ||||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | 18 | ▶ | if ( 1 == scalar @nice_args ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 2 | return sprintf "%s %s", $this_op, $nice_args[ 0 ]; | |||||||||
| 72 | } | ||||||||||
| 73 | 16 | return join( " ${this_op} ", @nice_args ); | |||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | 16 | ▶ | sub pretty_print { | ||||||||
|
Subroutine
pretty_print: called CC=8 SCAR=20.9
Pod
documented
| |||||||||||
| 77 | 16 | my $self = shift; | |||||||||
| 78 | 16 | my $this_op = $self->{ 'boolop' }; | |||||||||
| 79 | 16 | ▶ | $this_op =~ s/_EXPR\z// or croak( "Unknown boolean operation: ${this_op}!" ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | 16 | my @nice_args; | |||||||||
| 81 | |||||||||||
| 82 | 16 | for my $arg ( @{ $self->{ 'args' } } ) { | |||||||||
| 83 | 41 | my $x = $arg->pretty_print; | |||||||||
| 84 | 41 | ▶ | if ( 'Pg::SQL::PrettyPrinter::Node::BoolExpr' eq ref $arg ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | 1 | ▶ | if ( $x =~ m{\n} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | 1 | push @nice_args, join( "\n", "(", $self->increase_indent( $x ), ")" ); | |||||||||
| 87 | } | ||||||||||
| 88 | else { | ||||||||||
| 89 | 0 | push @nice_args, "( ${x} )"; | |||||||||
| 90 | } | ||||||||||
| 91 | } | ||||||||||
| 92 | else { | ||||||||||
| 93 | 40 | push @nice_args, $x; | |||||||||
| 94 | } | ||||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | 16 | ▶ | if ( 1 == scalar @nice_args ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | 2 | return sprintf "%s %s", $this_op, $nice_args[ 0 ]; | |||||||||
| 99 | } | ||||||||||
| 100 | 14 | my $out = ''; | |||||||||
| 101 | 14 | for my $i ( 0 .. $#nice_args ) { | |||||||||
| 102 | 39 | $out .= $nice_args[ $i ]; | |||||||||
| 103 | 39 | ▶ | if ( $i < $#nice_args ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 104 | 25 | $out .= " ${this_op}\n"; | |||||||||
| 105 | } | ||||||||||
| 106 | } | ||||||||||
| 107 | 14 | return $out; | |||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | 7 | 1; | |||||||||