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