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 80     80   1200 use v5.18;
  80         319  
5 80     80   680 use strict;
  80         180  
  80         2936  
6 80     80   502 use warnings;
  80         179  
  80         7893  
7 80     80   583 use warnings qw( FATAL utf8 );
  80         165  
  80         4252  
8 80     80   646 use utf8;
  80         180  
  80         542  
9 80     80   3997 use open qw( :std :utf8 );
  80         161  
  80         562  
10 80     80   12781 use Unicode::Normalize qw( NFC );
  80         257  
  80         5607  
11 80     80   774 use Unicode::Collate;
  80         185  
  80         2866  
12 80     80   641 use Encode qw( encode decode );
  80         288  
  80         13040  
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 80     80   743 use base qw( Pg::Explain::From );
  80         226  
  80         47679  
21 80     80   41927 use YAML::XS;
  80         283605  
  80         5959  
22 80     80   900 use Carp;
  80         182  
  80         5007  
23 80     80   763 use Pg::Explain::JIT;
  80         171  
  80         2212  
24 80     80   747 use Pg::Explain::Buffers;
  80         182  
  80         85205  
25              
26             =head1 NAME
27              
28             Pg::Explain::FromYAML - Parser for explains in YAML format
29              
30             =head1 VERSION
31              
32             Version 2.9
33              
34             =cut
35              
36             our $VERSION = '2.9';
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 72     72 1 469 my $self = shift;
55 72         737 my $source = encode( 'UTF-8', shift );
56              
57             # If this is plan from auto-explain
58 72 100       4248 if ( $source =~ s{ \A (\s*) Query \s+ Text : \s+ " ( [^\n]* ) " \s* \n }{}xms ) {
59 5         270 my ( $prefix, $query ) = ( $1, $2 );
60              
61             # Change prefix to two spaces in all lines
62 5         94 $source =~ s{^$prefix}{ }gm;
63              
64             # Add - to first line (should be Plan:)
65 5         19 $source =~ s{ \A \s \s }{- }xms;
66              
67 5         290 $query =~ s/\\n/\n/g;
68 5         39 $query =~ s/\\r/\r/g;
69 5         10 $query =~ s/\\t/\t/g;
70 5         332 $query =~ s/\\(.)/$1/g;
71 5         34 $self->explain->query( $query );
72              
73             }
74              
75 72 50       1240 unless ( $source =~ s{\A .*? ^ (\s*) ( - \s+ Plan: \s*\n )}{$1$2}xms ) {
76 1         356 carp( 'Source does not match first s///' );
77 1         15 return;
78             }
79              
80 72         2221 $source =~ s{ ^ ( \s* | [^\s-] [^\n] * ) \n .* \z }{}xms;
81              
82             # Make sure boolean variables will be properly marked if/for exporting to JSON
83 72         574 $YAML::XS::Boolean = "JSON::PP";
84 72     20   3377 my $struct = Load( $source )->[ 0 ];
  19     13   208  
  19     6   39  
  19     5   5421  
  12     4   109  
  12     3   26  
  12     3   2770  
  5     2   57  
  5         14  
  5         1182  
  4         46  
  4         9  
  4         979  
  3         36  
  3         9  
  3         591  
  2         24  
  2         6  
  2         609  
  2         20  
  2         5  
  2         440  
  2         19  
  2         3  
  2         447  
85              
86 72         976 my $top_node = $self->make_node_from( $struct->{ 'Plan' } );
87              
88 72 100       730 if ( $struct->{ 'Planning' } ) {
    100          
89 6         34 $self->explain->planning_time( $struct->{ 'Planning' }->{ 'Planning Time' } );
90 6         30 my $buffers = Pg::Explain::Buffers->new( $struct->{ 'Planning' } );
91 6 100       286 $self->explain->planning_buffers( $buffers ) if $buffers;
92             }
93             elsif ( $struct->{ 'Planning Time' } ) {
94 50         215 $self->explain->planning_time( $struct->{ 'Planning Time' } );
95             }
96 72 100       331 $self->explain->execution_time( $struct->{ 'Execution Time' } ) if $struct->{ 'Execution Time' };
97 72 100       574 $self->explain->total_runtime( $struct->{ 'Total Runtime' } ) if $struct->{ 'Total Runtime' };
98 71 100       306 if ( $struct->{ 'Triggers' } ) {
99 1         3 for my $t ( @{ $struct->{ 'Triggers' } } ) {
  1         4  
100 2         4 my $ts = {};
101 2 50       8 $ts->{ 'calls' } = $t->{ 'Calls' } if defined $t->{ 'Calls' };
102 2 50       6 $ts->{ 'time' } = $t->{ 'Time' } if defined $t->{ 'Time' };
103 2 50       7 $ts->{ 'relation' } = $t->{ 'Relation' } if defined $t->{ 'Relation' };
104 2 50       7 $ts->{ 'name' } = $t->{ 'Trigger Name' } if defined $t->{ 'Trigger Name' };
105 2         7 $self->explain->add_trigger_time( $ts );
106             }
107             }
108 71 100       262 $self->explain->jit( Pg::Explain::JIT->new( 'struct' => $struct->{ 'JIT' } ) ) if $struct->{ 'JIT' };
109              
110 71 100 66     267 $self->explain->settings( $struct->{ 'Settings' } ) if ( $struct->{ 'Settings' } ) && ( 0 < scalar keys %{ $struct->{ 'Settings' } } );
  2         11  
111              
112 71         1201 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-2023 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