| 1 | package Pg::SQL::PrettyPrinter::Node::XmlSerialize; | ||||||||||
| 2 | |||||||||||
| 3 | # UTF8 boilerplace, per http://stackoverflow.com/questions/6162484/why-does-modern-perl-avoid-utf-8-by-default/ | ||||||||||
| 4 | 3 | ▶ | use v5.26; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 3 | ▶ | use warnings qw( FATAL utf8 ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 3 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 3 | ▶ | use open qw( :std :utf8 ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 3 | ▶ | use Unicode::Normalize qw( NFC ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 3 | ▶ | use Unicode::Collate; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 3 | ▶ | use Encode qw( decode ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 3 | ▶ | 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 | 3 | ▶ | use autodie; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | 3 | ▶ | use Carp qw( carp croak confess cluck ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | 3 | ▶ | use English qw( -no_match_vars ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | 3 | ▶ | 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 | 3 | }; | |||||||||
| 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 | 3 | }; | |||||||||
| 40 | |||||||||||
| 41 | # Useful common code | ||||||||||
| 42 | |||||||||||
| 43 | 3 | ▶ | use parent qw( Pg::SQL::PrettyPrinter::Node ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 44 | |||||||||||
| 45 | 2 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 46 | 2 | my $class = shift; | |||||||||
| 47 | 2 | my $self = $class->SUPER::new( @_ ); | |||||||||
| 48 | 2 | bless $self, $class; | |||||||||
| 49 | |||||||||||
| 50 | 2 | ▶ | croak( 'Unknown XML option: ' . $self->{ 'xmloption' } ) unless $self->{ 'xmloption' } =~ m{\AXMLOPTION_(DOCUMENT|CONTENT)\z}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | |||||||||||
| 52 | 2 | $self->objectify( 'expr', [ qw( typeName names ) ], [ qw( typeName typmods ) ], [ qw( typeName arrayBounds ) ] ); | |||||||||
| 53 | |||||||||||
| 54 | 2 | return $self; | |||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | 2 | ▶ | sub as_text { | ||||||||
|
Subroutine
as_text: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 58 | 2 | my $self = shift; | |||||||||
| 59 | 2 | my @elements = (); | |||||||||
| 60 | 2 | push @elements, 'XMLSERIALIZE('; | |||||||||
| 61 | 2 | push @elements, $self->{ 'xmloption' }; | |||||||||
| 62 | 2 | $elements[ -1 ] =~ s/^XMLOPTION_//; | |||||||||
| 63 | 2 | push @elements, $self->{ 'expr' }->as_text; | |||||||||
| 64 | 2 | push @elements, 'AS'; | |||||||||
| 65 | 2 | push @elements, $self->expr_type; | |||||||||
| 66 | 2 | push @elements, ')'; | |||||||||
| 67 | 2 | return join( ' ', @elements ); | |||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | 2 | ▶ | sub pretty_print { | ||||||||
|
Subroutine
pretty_print: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 71 | 2 | my $self = shift; | |||||||||
| 72 | 2 | my @lines = (); | |||||||||
| 73 | 2 | push @lines, 'XMLSERIALIZE('; | |||||||||
| 74 | 2 | push @lines, $self->increase_indent( $self->{ 'xmloption' } ); | |||||||||
| 75 | 2 | $lines[ -1 ] =~ s/^(\s+)XMLOPTION_/$1/; | |||||||||
| 76 | 2 | push @lines, $self->increase_indent( $self->{ 'expr' }->pretty_print ); | |||||||||
| 77 | 2 | $lines[ -1 ] .= ' AS ' . $self->expr_type; | |||||||||
| 78 | 2 | push @lines, ')'; | |||||||||
| 79 | 2 | return join( "\n", @lines ); | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | 4 | ▶ | sub expr_type { | ||||||||
|
Subroutine
expr_type: called CC=5 SCAR=18.0
Pod
documented
| |||||||||||
| 83 | 4 | my $self = shift; | |||||||||
| 84 | |||||||||||
| 85 | 4 | my $typname = join( '.', map { $_->as_ident } @{ $self->{ 'typeName' }->{ 'names' } } ); | |||||||||
| 86 | 4 | ▶ | $typname = 'char' if $typname eq 'pg_catalog.bpchar'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | |||||||||||
| 88 | 4 | my $typmods = ''; | |||||||||
| 89 | 4 | ▶ | if ( exists $self->{ 'typeName' }->{ 'typmods' } ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | 2 | $typmods = '( ' . join( ', ', map { $_->as_text } @{ $self->{ 'typeName' }->{ 'typmods' } } ) . ' )'; | |||||||||
| 91 | } | ||||||||||
| 92 | 4 | ▶ | if ( exists $self->{ 'typeName' }->{ 'arrayBounds' } ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 93 | 0 | my @bounds_as_text = map { $_->as_text } @{ $self->{ 'typeName' }->{ 'arrayBounds' } }; | |||||||||
| 94 | 0 | my $array_def = sprintf '[%s]', join( ', ', @bounds_as_text ); | |||||||||
| 95 | 0 | ▶ | $array_def = '[]' if $array_def eq '[-1]'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | 0 | $typmods .= $array_def; | |||||||||
| 97 | } | ||||||||||
| 98 | 4 | return $typname . $typmods; | |||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | 3 | 1; | |||||||||
| 102 | |||||||||||
| 103 | # vim: set ft=perl: | ||||||||||