| 1 | package Pg::SQL::PrettyPrinter::Node; | ||||||||||
| 2 | |||||||||||
| 3 | # UTF8 boilerplace, per http://stackoverflow.com/questions/6162484/why-does-modern-perl-avoid-utf-8-by-default/ | ||||||||||
| 4 | 16 | ▶ | use v5.26; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 16 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 16 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 16 | ▶ | use warnings qw( FATAL utf8 ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 16 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 16 | ▶ | use open qw( :std :utf8 ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 16 | ▶ | use Unicode::Normalize qw( NFC ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 16 | ▶ | use Unicode::Collate; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 16 | ▶ | use Encode qw( decode ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 16 | ▶ | 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 | 16 | ▶ | use autodie; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | 16 | ▶ | use Carp qw( carp croak confess cluck ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | 16 | ▶ | use English qw( -no_match_vars ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | 16 | ▶ | 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 | 16 | }; | |||||||||
| 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 | 16 | }; | |||||||||
| 40 | |||||||||||
| 41 | # Useful common code | ||||||||||
| 42 | |||||||||||
| 43 | 16 | ▶ | use Module::Runtime qw( use_module ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 44 | 16 | ▶ | use Clone qw( clone ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 45 | |||||||||||
| 46 | 2605 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 47 | 2605 | my ( $class, $the_rest ) = @_; | |||||||||
| 48 | 2605 | my $self = clone( $the_rest ); | |||||||||
| 49 | 2605 | bless $self, $class; | |||||||||
| 50 | 2605 | return $self; | |||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | 3525 | ▶ | sub make_from { | ||||||||
|
Subroutine
make_from: called CC=8 SCAR=21.9
Pod
documented
| |||||||||||
| 54 | 3525 | my ( $self, $data ) = @_; | |||||||||
| 55 | |||||||||||
| 56 | 3525 | ▶ | return unless defined $data; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | |||||||||||
| 58 | 3525 | ▶ | if ( 'ARRAY' eq ref $data ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 916 | return [ map { $self->make_from( $_ ) } @{ $data } ]; | |||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | 2609 | ▶ | croak( 'Invalid data for making Pg::SQL::PrettyPrinter::Node: ' . Dumper( $data ) ) unless 'HASH' eq ref $data; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | |||||||||||
| 64 | 2609 | my @all_keys = keys %{ $data }; | |||||||||
| 65 | 2609 | ▶ | return if 0 == scalar @all_keys; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 2605 | ▶ | croak( 'Invalid data for making Pg::SQL::PrettyPrinter::Node (#2): ' . join( ', ', @all_keys ) ) unless 1 == scalar @all_keys; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | 2605 | my $class_suffix = $all_keys[ 0 ]; | |||||||||
| 68 | 2605 | ▶ | croak( "Invalid data for making Pg::SQL::PrettyPrinter::Node (#3): $class_suffix" ) unless $class_suffix =~ /^[A-Z][a-zA-Z0-9_-]+$/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | |||||||||||
| 70 | 2605 | my $class = 'Pg::SQL::PrettyPrinter::Node::' . $class_suffix; | |||||||||
| 71 | 2605 | my $object; | |||||||||
| 72 | 2605 | eval { $object = use_module( $class )->new( $data->{ $class_suffix } ); }; | |||||||||
| 73 | 2605 | ▶ | if ( $EVAL_ERROR ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | 0 | my $msg = $EVAL_ERROR; | |||||||||
| 75 | 0 | my $keys = join( '; ', sort keys %{ $data } ); | |||||||||
| 76 | 0 | croak( "Can't make object out of [${keys}]:\n" . Dumper( $data ) . "\n" . $msg ); | |||||||||
| 77 | } | ||||||||||
| 78 | 2605 | return $object; | |||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | 1787 | ▶ | sub objectify { | ||||||||
|
Subroutine
objectify: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 82 | 1787 | my $self = shift; | |||||||||
| 83 | 1787 | my @keys = @_; | |||||||||
| 84 | |||||||||||
| 85 | # Only arrays and hashes (well, references to them) can be objectified. | ||||||||||
| 86 | 1787 | my %types_ok = map { $_ => 1 } qw{ ARRAY HASH }; | |||||||||
| 87 | |||||||||||
| 88 | 1787 | for my $key ( @keys ) { | |||||||||
| 89 | 3733 | my ( $container, $real_key ) = $self->get_container_key( $key ); | |||||||||
| 90 | 3733 | ▶ | next unless defined $container; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 91 | 3380 | ▶ | next unless exists $container->{ $real_key }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | |||||||||||
| 93 | 1973 | my $val = $container->{ $real_key }; | |||||||||
| 94 | 1973 | my $type = ref $val; | |||||||||
| 95 | 1973 | ▶ | next unless $types_ok{ $type }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | |||||||||||
| 97 | 1973 | $container->{ $real_key } = $self->make_from( $val ); | |||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | 1787 | return; | |||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | 3733 | ▶ | sub get_container_key { | ||||||||
|
Subroutine
get_container_key: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||
| 104 | 3733 | my $self = shift; | |||||||||
| 105 | 3733 | my $path = shift; | |||||||||
| 106 | |||||||||||
| 107 | 3733 | my $type = ref $path; | |||||||||
| 108 | 3733 | ▶ | return $self, $path if '' eq $type; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 109 | 546 | ▶ | croak( "Can't get container/key for non-array: $type" ) unless 'ARRAY' eq $type; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 110 | 546 | ▶ | croak( "Can't get container/key for empty array" ) if 0 == scalar @{ $path }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | 546 | ▶ | return $self, $path->[ 0 ] if 1 == scalar @{ $path }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 112 | |||||||||||
| 113 | 546 | my $container = $self; | |||||||||
| 114 | 546 | ▶ | for ( my $i = 0 ; $i < $#{ $path } ; $i++ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 115 | 553 | my $key = $path->[ $i ]; | |||||||||
| 116 | 553 | ▶ | return unless exists $container->{ $key }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 117 | 200 | $container = $container->{ $key }; | |||||||||
| 118 | } | ||||||||||
| 119 | |||||||||||
| 120 | 193 | return $container, $path->[ -1 ]; | |||||||||
| 121 | } | ||||||||||
| 122 | |||||||||||
| 123 | 557 | ▶ | sub pretty_print { | ||||||||
|
Subroutine
pretty_print: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 124 | 557 | my $self = shift; | |||||||||
| 125 | 557 | return $self->as_text( @_ ); | |||||||||
| 126 | } | ||||||||||
| 127 | |||||||||||
| 128 | 227 | ▶ | sub quote_literal { | ||||||||
|
Subroutine
quote_literal: called CC=2 SCAR=7.9
Pod
documented
| |||||||||||
| 129 | 227 | my $self = shift; | |||||||||
| 130 | 227 | my $val = shift; | |||||||||
| 131 | |||||||||||
| 132 | # Set of characters that, if found, should be converted to \escaped, and they change how string is printed (E'' vs. '') | ||||||||||
| 133 | 227 | my $rep = {}; | |||||||||
| 134 | 227 | $rep->{ "\r" } = "\\r"; | |||||||||
| 135 | 227 | $rep->{ "\t" } = "\\t"; | |||||||||
| 136 | 227 | $rep->{ "\n" } = "\\n"; | |||||||||
| 137 | 227 | my $look_for = join( '|', keys %{ $rep } ); | |||||||||
| 138 | |||||||||||
| 139 | 227 | ▶ | if ( $val =~ /${look_for}/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 140 | |||||||||||
| 141 | # If we are representing string using E'' notation, ' character has to be escaped too | ||||||||||
| 142 | 0 | $rep->{ "'" } = "\\'"; | |||||||||
| 143 | 0 | $look_for = join( '|', keys %{ $rep } ); | |||||||||
| 144 | |||||||||||
| 145 | # Replace all characters that need it | ||||||||||
| 146 | 0 | $val =~ s/(${look_for})/$rep->{$1}/ge; | |||||||||
| 147 | 0 | return "E'${val}'"; | |||||||||
| 148 | } | ||||||||||
| 149 | |||||||||||
| 150 | # For '' strings, we just need to change each ' into ''. | ||||||||||
| 151 | 227 | $val =~ s/'/''/g; | |||||||||
| 152 | 227 | return "'${val}'"; | |||||||||
| 153 | } | ||||||||||
| 154 | |||||||||||
| 155 | 1302 | ▶ | sub quote_ident { | ||||||||
|
Subroutine
quote_ident: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 156 | 1302 | my $self = shift; | |||||||||
| 157 | 1302 | my $val = shift; | |||||||||
| 158 | 1302 | ▶ | return $val if $val =~ m{\A[a-z0-9_]+\z}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 159 | 17 | $val =~ s/"/""/g; | |||||||||
| 160 | 17 | return '"' . $val . '"'; | |||||||||
| 161 | } | ||||||||||
| 162 | |||||||||||
| 163 | 569 | ▶ | sub increase_indent { | ||||||||
|
Subroutine
increase_indent: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 164 | 569 | my $self = shift; | |||||||||
| 165 | 569 | my $input = shift; | |||||||||
| 166 | 569 | return $self->increase_indent_n( 1, $input ); | |||||||||
| 167 | } | ||||||||||
| 168 | |||||||||||
| 169 | 571 | ▶ | sub increase_indent_n { | ||||||||
|
Subroutine
increase_indent_n: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 170 | 571 | my $self = shift; | |||||||||
| 171 | 571 | my $levels = shift; | |||||||||
| 172 | 571 | my $input = shift; | |||||||||
| 173 | 571 | ▶ | croak( "Bad number of levels ($levels) to increase indent!" ) unless $levels =~ m{\A[1-9]\d*\z}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 174 | 571 | my $prefix = ' ' x $levels; | |||||||||
| 175 | 571 | my @lines = split /\n/, $input; | |||||||||
| 176 | 571 | return join( "\n", map { $prefix . $_ } @lines ); | |||||||||
| 177 | } | ||||||||||
| 178 | |||||||||||
| 179 | 16 | 1; | |||||||||