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