File Coverage

blib/lib/Pg/SQL/PrettyPrinter/Node/ExplainStmt.pm
Criterion Covered Total %
statement 86 86 100.0
branch 13 14 92.8
condition n/a
subroutine 18 18 100.0
pod 4 4 100.0
total 121 122 99.1


line stmt bran cond sub pod time code
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 2     2   1781 use v5.26;
  2         11  
5 2     2   12 use strict;
  2         5  
  2         49  
6 2     2   9 use warnings;
  2         4  
  2         62  
7 2     2   10 use warnings qw( FATAL utf8 );
  2         6  
  2         82  
8 2     2   12 use utf8;
  2         4  
  2         16  
9 2     2   90 use open qw( :std :utf8 );
  2         9  
  2         11  
10 2     2   312 use Unicode::Normalize qw( NFC );
  2         4  
  2         146  
11 2     2   44 use Unicode::Collate;
  2         5  
  2         101  
12 2     2   15 use Encode qw( decode );
  2         7  
  2         129  
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 2     2   520 use autodie;
  2         4  
  2         19  
24 2     2   11645 use Carp qw( carp croak confess cluck );
  2         4  
  2         187  
25 2     2   14 use English qw( -no_match_vars );
  2         5  
  2         28  
26 2     2   816 use Data::Dumper qw( Dumper );
  2         4  
  2         519  
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 2     2   17 use parent qw( Pg::SQL::PrettyPrinter::Node );
  2         4  
  2         13  
44              
45             sub new {
46 5     5 1 165 my $class = shift;
47 5         24 my $self = $class->SUPER::new( @_ );
48 5         10 bless $self, $class;
49              
50 5         21 $self->objectify( qw( query options ) );
51              
52 5 100       13 if ( exists $self->{ 'options' } ) {
53 4         7 $self->{ '_options' } = [ map { $_->as_text } @{ $self->{ 'options' } } ];
  13         27  
  4         9  
54             }
55 5         13 return $self;
56             }
57              
58             sub as_text {
59 5     5 1 22 my $self = shift;
60 5 100       15 return 'EXPLAIN ' . $self->{ 'query' }->as_text unless exists $self->{ 'options' };
61 4         8 my @elements = ();
62 4         8 push @elements, 'EXPLAIN';
63 4 100       9 if ( $self->has_complex_opts ) {
64 1         7 push @elements, '(';
65 1         3 push @elements, join( ', ', @{ $self->{ '_options' } } );
  1         4  
66 1         2 push @elements, ')';
67             }
68             else {
69 3         4 push @elements, map { uc $_ } @{ $self->{ '_options' } };
  4         27  
  3         6  
70             }
71 4         16 push @elements, $self->{ 'query' }->as_text;
72 4         18 return join( ' ', @elements );
73             }
74              
75             sub has_complex_opts {
76 8     8 1 26 my $self = shift;
77 8 50       20 return unless exists $self->{ '_options' };
78 8         10 for my $key ( @{ $self->{ '_options' } } ) {
  8         21  
79 14 100       61 return 1 unless $key =~ m{\A(?:analyze|verbose)\z};
80             }
81 6         19 return;
82             }
83              
84             sub pretty_print {
85 5     5 1 3296 my $self = shift;
86 5         11 my @lines = ();
87 5         11 push @lines, 'EXPLAIN';
88 5 100       17 if ( exists $self->{ 'options' } ) {
89 4 100       21 if ( $self->has_complex_opts ) {
90 1         3 $lines[ -1 ] .= ' (';
91 1         2 push @lines, map { $self->increase_indent( $_ ) . ',' } @{ $self->{ '_options' } };
  9         24  
  1         3  
92              
93             # Remove unnecessary trailing , in last element
94 1         10 $lines[ -1 ] =~ s/,\z//;
95 1         3 push @lines, ')';
96             }
97             else {
98 3         4 $lines[ -1 ] .= ' ' . join( ' ', map { uc $_ } @{ $self->{ '_options' } } );
  4         17  
  3         9  
99             }
100             }
101 5         20 push @lines, $self->{ 'query' }->pretty_print;
102 5         27 return join( "\n", @lines );
103             }
104              
105             1;
106              
107             # vim: set ft=perl: