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   9054 use v5.26;
  3         18  
5 3     3   23 use strict;
  3         6  
  3         97  
6 3     3   17 use warnings;
  3         5  
  3         264  
7 3     3   26 use warnings qw( FATAL utf8 );
  3         8  
  3         273  
8 3     3   23 use utf8;
  3         7  
  3         27  
9 3     3   202 use open qw( :std :utf8 );
  3         7  
  3         100  
10 3     3   1753 use Unicode::Normalize qw( NFC );
  3         34  
  3         368  
11 3     3   23 use Unicode::Collate;
  3         22  
  3         133  
12 3     3   19 use Encode qw( decode );
  3         6  
  3         2189  
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   33 use autodie;
  3         9  
  3         33  
24 3     3   25681 use Carp qw( carp croak confess cluck );
  3         51  
  3         487  
25 3     3   26 use English qw( -no_match_vars );
  3         6  
  3         46  
26 3     3   2122 use Data::Dumper qw( Dumper );
  3         8  
  3         1350  
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   30 use parent qw( Pg::SQL::PrettyPrinter::Node );
  3         7  
  3         39  
44              
45             sub new {
46 5     5 1 251 my $class = shift;
47 5         82 my $self = $class->SUPER::new( @_ );
48 5         16 bless $self, $class;
49              
50 5         34 $self->objectify( qw( query options ) );
51              
52 5 100       23 if ( exists $self->{ 'options' } ) {
53 4         10 $self->{ '_options' } = [ map { $_->as_text } @{ $self->{ 'options' } } ];
  13         38  
  4         14  
54             }
55 5         21 return $self;
56             }
57              
58             sub as_text {
59 5     5 1 41 my $self = shift;
60 5 100       22 return 'EXPLAIN ' . $self->{ 'query' }->as_text unless exists $self->{ 'options' };
61 4         11 my @elements = ();
62 4         10 push @elements, 'EXPLAIN';
63 4 100       13 if ( $self->has_complex_opts ) {
64 1         3 push @elements, '(';
65 1         3 push @elements, join( ', ', @{ $self->{ '_options' } } );
  1         7  
66 1         3 push @elements, ')';
67             }
68             else {
69 3         7 push @elements, map { uc $_ } @{ $self->{ '_options' } };
  4         17  
  3         10  
70             }
71 4         30 push @elements, $self->{ 'query' }->as_text;
72 4         37 return join( ' ', @elements );
73             }
74              
75             sub has_complex_opts {
76 8     8 1 17 my $self = shift;
77 8 50       25 return unless exists $self->{ '_options' };
78 8         16 for my $key ( @{ $self->{ '_options' } } ) {
  8         26  
79 14 100       82 return 1 unless $key =~ m{\A(?:analyze|verbose)\z};
80             }
81 6         22 return;
82             }
83              
84             sub pretty_print {
85 5     5 1 7446 my $self = shift;
86 5         55 my @lines = ();
87 5         15 push @lines, 'EXPLAIN';
88 5 100       23 if ( exists $self->{ 'options' } ) {
89 4 100       16 if ( $self->has_complex_opts ) {
90 1         5 $lines[ -1 ] .= ' (';
91 1         3 push @lines, map { $self->increase_indent( $_ ) . ',' } @{ $self->{ '_options' } };
  9         33  
  1         6  
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         9 $lines[ -1 ] .= ' ' . join( ' ', map { uc $_ } @{ $self->{ '_options' } } );
  4         25  
  3         11  
99             }
100             }
101 5         38 push @lines, $self->{ 'query' }->pretty_print;
102 5         38 return join( "\n", @lines );
103             }
104              
105             1;
106              
107             # vim: set ft=perl: