PrettyPrinter.pm

filter: Statementstmt 78.2% 79 / 101 Branchbran 61.7% 21 / 34 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 86.9% 20 / 23 Podpod 100% 5 / 5 totaltotal 76.6% 125 / 163 CC 17 SCAR 17.2
CC
17
Cov
74%
fetch_struct
20.7
new
6.0
__ANON__
6.0
CRAP 5.6
« PrettyPrinter/Node/InsertStmt.pm ↑ Summary PrettyPrinter/Node/UpdateStmt.pm »
1 package Pg::SQL::PrettyPrinter;
2  
3 # UTF8 boilerplace, per http://stackoverflow.com/questions/6162484/why-does-modern-perl-avoid-utf-8-by-default/
4 16 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use warnings qw( FATAL utf8 );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶use open qw( :std :utf8 );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 16 ▶use Unicode::Normalize qw( NFC );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 16 ▶use Unicode::Collate;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 16 ▶use Encode qw( decode );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 16 ▶if ( grep /\P{ASCII}/ => @ARGV ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep /\P{ASCII}/u, @ARGV) 0 16 16
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 16 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 16 ▶use Carp qw( carp croak confess cluck );
Subroutine BEGIN: called CC=1 SCAR=0.0
25 16 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
26 16 ▶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
BranchTrueFalseTotal
unless $^S 0 0 0
31 16 };
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
BranchTrueFalseTotal
if ($^S) { } 0 0 0
38 0 else { confess "Deadly warning: @_" }
39 16 };
40  
41 # Useful common code
42  
43 16 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 16 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 16 ▶use Pg::SQL::PrettyPrinter::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 16 our $VERSION = '0.12';
48  
49 143 ▶sub new {
Subroutine new: called CC=6 SCAR=18.0
Pod documented
50 143 my ( $class, %args ) = @_;
51 143 my $self = bless {}, $class;
52  
53 143 ▶ croak( 'SQL query was not provided!' ) unless $args{ 'sql' };
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"sql"} 1 142 143
54 142 $self->{ 'sql' } = $args{ 'sql' };
55  
56 142 ▶ if ( exists $args{ 'service' } ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $args{'service'}) { } 3 139 142
elsif (exists $args{'struct'}) { } 138 1 139
57 3 ▶ croak( 'You should provide only one of service/struct!' ) if $args{ 'struct' };
Branch: 1 branch
BranchTrueFalseTotal
if $args{"struct"} 1 2 3
58 2 ▶ croak( 'Invalid syntax for service!' ) unless $args{ 'service' } =~ m{
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"service"} =~ m[ \A http:// \d{1,3} (?: \. \d{1,3} ){3} # IP address for parse microservice : [1-9]\d+ # Port number for parse microservice / \z ]ux 2 0 2
59 \A
60 http://
61 \d{1,3} (?: \. \d{1,3} ){3} # IP address for parse microservice
62 :
63 [1-9]\d+ # Port number for parse microservice
64 /
65 \z
66 }x;
67 0 $self->{ 'service' } = $args{ 'service' };
68 }
69 elsif ( exists $args{ 'struct' } ) {
70 138 $self->validate_struct( $args{ 'struct' } );
71 131 $self->{ 'struct' } = $args{ 'struct' };
72 }
73 else {
74 1 croak( 'You have to provide either service or struct!' );
75 }
76 131 return $self;
77 }
78  
79 138 ▶sub validate_struct {
Subroutine validate_struct: called CC=6 SCAR=17.9
Pod documented
80 138 my ( $self, $struct ) = @_;
81 138 ▶ croak( 'Invalid parse struct!' ) unless 'HASH' eq ref $struct;
Branch: 1 branch
BranchTrueFalseTotal
unless "HASH" eq ref $struct 2 136 138
82 136 ▶ croak( 'Invalid parse struct (#2)!' ) unless $struct->{ 'version' };
Branch: 1 branch
BranchTrueFalseTotal
unless $struct->{"version"} 2 134 136
83 134 ▶ croak( 'Invalid parse struct (#3)!' ) unless $struct->{ 'stmts' };
Branch: 1 branch
BranchTrueFalseTotal
unless $struct->{"stmts"} 1 133 134
84 133 ▶ croak( 'Invalid parse struct (#4)!' ) unless 'ARRAY' eq ref $struct->{ 'stmts' };
Branch: 1 branch
BranchTrueFalseTotal
unless "ARRAY" eq ref $struct->{"stmts"} 1 132 133
85 132 ▶ croak( 'Invalid parse struct (#5)!' ) unless 0 < scalar @{ $struct->{ 'stmts' } };
Branch: 1 branch
BranchTrueFalseTotal
unless 0 < scalar @{$struct->{"stmts"};} 1 131 132
86 131 return;
87 }
88  
89 131 ▶sub parse {
Subroutine parse: called CC=1 SCAR=0.0
Pod documented
90 131 my $self = shift;
91 131 $self->fetch_struct();
92 131 $self->{ 'statements' } = [ map { Pg::SQL::PrettyPrinter::Node->make_from( $_->{ 'stmt' } ) } @{ $self->{ 'struct' }->{ 'stmts' } } ];
93 131 return;
94 }
95  
96 0 ▶sub remove_irrelevant {
Subroutine remove_irrelevant: not called CC=1 SCAR=6.9
Pod documented
97 0 my $self = shift;
98 0 my $q = $self->{ 'sql' };
99 0 $q =~ s{
100 \A # Beginning of sql
101 \s* # Eventual spacing, including new lines
102 [a-z0-9_]* # optional dbname
103 [=-]? # optional prompt type
104 [>#\$] # prompt final character, depending on user level, or common(ish) '$'
105 \s* # optional spaces
106 }{}x;
107 0 $self->{ 'sql' } = $q;
108 }
109  
110 131 ▶sub fetch_struct {
Subroutine fetch_struct: called CC=5 SCAR=30.3
Pod documented
111 131 my $self = shift;
112 131 ▶ return if $self->{ 'struct' };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"struct"} 131 0 131
113 0 $self->remove_irrelevant();
114 0 my $http = HTTP::Tiny->new( 'timeout' => 0.5 ); # There really isn't a reason why it should take longer than 0.3s
115 0 my $res = $http->post_form( $self->{ 'service' }, { 'q' => $self->{ 'sql' } } );
116 0 ▶ unless ( $res->{ 'success' } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->{"success"}) 0 0 0
117 0 ▶ croak( 'Timeout while parsing' ) if $res->{ 'content' } =~ m{\ATimed out while waiting for socket};
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"content"} =~ /\ATimed out while waiting for socket/u 0 0 0
118 0 croak( "Couldn't parse the queries! : " . Dumper( $res ) );
119 }
120 0 my $struct = decode_json( $res->{ 'content' } );
121 0 ▶ croak( "Parse error: " . $struct->{ 'error' } ) if exists $struct->{ 'error' };
Branch: 1 branch
BranchTrueFalseTotal
if exists $struct->{"error"} 0 0 0
122 0 $self->validate_struct( $struct );
123 0 $self->{ 'struct' } = $struct;
124 0 return;
125 }
126  
127 16 1;
« PrettyPrinter/Node/InsertStmt.pm ↑ Summary PrettyPrinter/Node/UpdateStmt.pm »