File Coverage

blib/lib/Pg/Explain/FromYAML.pm
Criterion Covered Total %
statement 103 103 100.0
branch 23 28 82.1
condition 2 3 66.6
subroutine 23 23 100.0
pod 1 1 100.0
total 152 158 96.2


line stmt bran cond sub pod time code
1             package Pg::Explain::FromYAML;
2              
3             # UTF8 boilerplace, per http://stackoverflow.com/questions/6162484/why-does-modern-perl-avoid-utf-8-by-default/
4 75     75   1062 use v5.18;
  75         272  
5 75     75   609 use strict;
  75         158  
  75         1692  
6 75     75   559 use warnings;
  75         156  
  75         2065  
7 75     75   595 use warnings qw( FATAL utf8 );
  75         167  
  75         2379  
8 75     75   607 use utf8;
  75         156  
  75         434  
9 75     75   2187 use open qw( :std :utf8 );
  75         160  
  75         371  
10 75     75   9881 use Unicode::Normalize qw( NFC );
  75         233  
  75         4159  
11 75     75   741 use Unicode::Collate;
  75         179  
  75         2519  
12 75     75   656 use Encode qw( decode );
  75         152  
  75         4579  
13              
14             if ( grep /\P{ASCII}/ => @ARGV ) {
15             @ARGV = map { decode( 'UTF-8', $_ ) } @ARGV;
16             }
17              
18             # UTF8 boilerplace, per http://stackoverflow.com/questions/6162484/why-does-modern-perl-avoid-utf-8-by-default/
19              
20 75     75   16586 use base qw( Pg::Explain::From );
  75         182  
  75         35142  
21 75     75   32897 use YAML::XS;
  75         216062  
  75         4409  
22 75     75   744 use Carp;
  75         186  
  75         3747  
23 75     75   671 use Pg::Explain::JIT;
  75         193  
  75         1596  
24 75     75   589 use Pg::Explain::Buffers;
  75         183  
  75         56359  
25              
26             =head1 NAME
27              
28             Pg::Explain::FromYAML - Parser for explains in YAML format
29              
30             =head1 VERSION
31              
32             Version 2.4
33              
34             =cut
35              
36             our $VERSION = '2.4';
37              
38             =head1 SYNOPSIS
39              
40             It's internal class to wrap some work. It should be used by Pg::Explain, and not directly.
41              
42             =head1 FUNCTIONS
43              
44             =head2 parse_source
45              
46             Function which parses actual plan, and constructs Pg::Explain::Node objects
47             which represent it.
48              
49             Returns Top node of query plan.
50              
51             =cut
52              
53             sub parse_source {
54 71     71 1 466 my $self = shift;
55 71         161 my $source = shift;
56              
57             # If this is plan from auto-explain
58 71 100       920 if ( $source =~ s{ \A (\s*) Query \s+ Text : \s+ " ( [^\n]* ) " \s* \n }{}xms ) {
59 5         203 my ( $prefix, $query ) = ( $1, $2 );
60              
61             # Change prefix to two spaces in all lines
62 5         106 $source =~ s{^$prefix}{ }gm;
63              
64             # Add - to first line (should be Plan:)
65 5         30 $source =~ s{ \A \s \s }{- }xms;
66              
67 5         212 $query =~ s/\\n/\n/g;
68 5         18 $query =~ s/\\r/\r/g;
69 5         14 $query =~ s/\\t/\t/g;
70 5         206 $query =~ s/\\(.)/$1/g;
71 5         22 $self->explain->query( $query );
72              
73             }
74              
75 71 50       1398 unless ( $source =~ s{\A .*? ^ (\s*) ( - \s+ Plan: \s*\n )}{$1$2}xms ) {
76 1         160 carp( 'Source does not match first s///' );
77 1         7 return;
78             }
79              
80 71         2054 $source =~ s{ ^ ( \s* | [^\s-] [^\n] * ) \n .* \z }{}xms;
81              
82             # Make sure boolean variables will be properly marked if/for exporting to JSON
83 71         429 $YAML::XS::Boolean = "JSON::PP";
84 71     19   2307 my $struct = Load( $source )->[ 0 ];
  18     13   14447  
  18     6   257677  
  18     5   4302  
  12     4   124  
  12     3   26  
  12     3   2330  
  5     2   34  
  5         12  
  5         942  
  4         35  
  4         8  
  4         661  
  3         24  
  3         7  
  3         423  
  2         13  
  2         4  
  2         356  
  2         13  
  2         6  
  2         380  
  2         18  
  2         5  
  2         418  
85              
86 71         883 my $top_node = $self->make_node_from( $struct->{ 'Plan' } );
87              
88 71 100       566 if ( $struct->{ 'Planning' } ) {
    100          
89 5         28 $self->explain->planning_time( $struct->{ 'Planning' }->{ 'Planning Time' } );
90 5         22 my $buffers = Pg::Explain::Buffers->new( $struct->{ 'Planning' } );
91 5 100       203 $self->explain->planning_buffers( $buffers ) if $buffers;
92             }
93             elsif ( $struct->{ 'Planning Time' } ) {
94 50         163 $self->explain->planning_time( $struct->{ 'Planning Time' } );
95             }
96 71 100       302 $self->explain->execution_time( $struct->{ 'Execution Time' } ) if $struct->{ 'Execution Time' };
97 71 100       546 $self->explain->total_runtime( $struct->{ 'Total Runtime' } ) if $struct->{ 'Total Runtime' };
98 70 100       188 if ( $struct->{ 'Triggers' } ) {
99 1         2 for my $t ( @{ $struct->{ 'Triggers' } } ) {
  1         4  
100 2         5 my $ts = {};
101 2 50       7 $ts->{ 'calls' } = $t->{ 'Calls' } if defined $t->{ 'Calls' };
102 2 50       6 $ts->{ 'time' } = $t->{ 'Time' } if defined $t->{ 'Time' };
103 2 50       6 $ts->{ 'relation' } = $t->{ 'Relation' } if defined $t->{ 'Relation' };
104 2 50       6 $ts->{ 'name' } = $t->{ 'Trigger Name' } if defined $t->{ 'Trigger Name' };
105 2         4 $self->explain->add_trigger_time( $ts );
106             }
107             }
108 70 100       219 $self->explain->jit( Pg::Explain::JIT->new( 'struct' => $struct->{ 'JIT' } ) ) if $struct->{ 'JIT' };
109              
110 70 100 66     287 $self->explain->settings( $struct->{ 'Settings' } ) if ( $struct->{ 'Settings' } ) && ( 0 < scalar keys %{ $struct->{ 'Settings' } } );
  2         13  
111              
112 70         775 return $top_node;
113             }
114              
115             =head1 AUTHOR
116              
117             hubert depesz lubaczewski, C<< >>
118              
119             =head1 BUGS
120              
121             Please report any bugs or feature requests to C.
122              
123             =head1 SUPPORT
124              
125             You can find documentation for this module with the perldoc command.
126              
127             perldoc Pg::Explain
128              
129             =head1 COPYRIGHT & LICENSE
130              
131             Copyright 2008-2021 hubert depesz lubaczewski, all rights reserved.
132              
133             This program is free software; you can redistribute it and/or modify it
134             under the same terms as Perl itself.
135              
136             =cut
137              
138             1; # End of Pg::Explain::FromYAML