| 1 | package Pg::SQL::PrettyPrinter::Node::ExplainStmt; | ||||||||||
| 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 | 5 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 46 | 5 | my $class = shift; | |||||||||
| 47 | 5 | my $self = $class->SUPER::new( @_ ); | |||||||||
| 48 | 5 | bless $self, $class; | |||||||||
| 49 | |||||||||||
| 50 | 5 | $self->objectify( qw( query options ) ); | |||||||||
| 51 | |||||||||||
| 52 | 5 | ▶ | if ( exists $self->{ 'options' } ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | 4 | $self->{ '_options' } = [ map { $_->as_text } @{ $self->{ 'options' } } ]; | |||||||||
| 54 | } | ||||||||||
| 55 | 5 | return $self; | |||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | 5 | ▶ | sub as_text { | ||||||||
|
Subroutine
as_text: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 59 | 5 | my $self = shift; | |||||||||
| 60 | 5 | ▶ | return 'EXPLAIN ' . $self->{ 'query' }->as_text unless exists $self->{ 'options' }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 4 | my @elements = (); | |||||||||
| 62 | 4 | push @elements, 'EXPLAIN'; | |||||||||
| 63 | 4 | ▶ | if ( $self->has_complex_opts ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 1 | push @elements, '('; | |||||||||
| 65 | 1 | push @elements, join( ', ', @{ $self->{ '_options' } } ); | |||||||||
| 66 | 1 | push @elements, ')'; | |||||||||
| 67 | } | ||||||||||
| 68 | else { | ||||||||||
| 69 | 3 | push @elements, map { uc $_ } @{ $self->{ '_options' } }; | |||||||||
| 70 | } | ||||||||||
| 71 | 4 | push @elements, $self->{ 'query' }->as_text; | |||||||||
| 72 | 4 | return join( ' ', @elements ); | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | 8 | ▶ | sub has_complex_opts { | ||||||||
|
Subroutine
has_complex_opts: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 76 | 8 | my $self = shift; | |||||||||
| 77 | 8 | ▶ | return unless exists $self->{ '_options' }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | 8 | for my $key ( @{ $self->{ '_options' } } ) { | |||||||||
| 79 | 14 | ▶ | return 1 unless $key =~ m{\A(?:analyze|verbose)\z}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | } | ||||||||||
| 81 | 6 | return; | |||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | 5 | ▶ | sub pretty_print { | ||||||||
|
Subroutine
pretty_print: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 85 | 5 | my $self = shift; | |||||||||
| 86 | 5 | my @lines = (); | |||||||||
| 87 | 5 | push @lines, 'EXPLAIN'; | |||||||||
| 88 | 5 | ▶ | if ( exists $self->{ 'options' } ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 4 | ▶ | if ( $self->has_complex_opts ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | 1 | $lines[ -1 ] .= ' ('; | |||||||||
| 91 | 1 | push @lines, map { $self->increase_indent( $_ ) . ',' } @{ $self->{ '_options' } }; | |||||||||
| 92 | |||||||||||
| 93 | # Remove unnecessary trailing , in last element | ||||||||||
| 94 | 1 | $lines[ -1 ] =~ s/,\z//; | |||||||||
| 95 | 1 | push @lines, ')'; | |||||||||
| 96 | } | ||||||||||
| 97 | else { | ||||||||||
| 98 | 3 | $lines[ -1 ] .= ' ' . join( ' ', map { uc $_ } @{ $self->{ '_options' } } ); | |||||||||
| 99 | } | ||||||||||
| 100 | } | ||||||||||
| 101 | 5 | push @lines, $self->{ 'query' }->pretty_print; | |||||||||
| 102 | 5 | return join( "\n", @lines ); | |||||||||
| 103 | } | ||||||||||
| 104 | |||||||||||
| 105 | 3 | 1; | |||||||||
| 106 | |||||||||||
| 107 | # vim: set ft=perl: | ||||||||||