blib/lib/Parse/SQLOutput.pm

filter: Statementstmt 20.7% 16 / 77 Branchbran 0.0% 0 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 23.0% 3 / 13 Podpod 0.0% 0 / 4 totaltotal 15.5% 19 / 122 CC 19 SCAR 32.4
CC
19
Cov
15%
parse
56.0
format_hoh
30.0
format_lol
30.0
CRAP 25.6
↑ Summary
1 1 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package Parse::SQLOutput;
3 1 our $VERSION = '0.08';
4  
5 1 ▶use Mo qw'default';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 0 ▶has as => (default => sub { 'hoh' });
Subroutine __ANON__: not called CC=1 SCAR=1.2
8 0 ▶has header => (default => sub { 0 });
Subroutine __ANON__: not called CC=1 SCAR=1.2
9 0 ▶has key => (default => sub { '' });
Subroutine __ANON__: not called CC=1 SCAR=1.2
10  
11 0 ▶sub parse {
Subroutine parse: not called CC=7 SCAR=40.3
Pod undocumented
12 0 my ($self, $text) = @_;
13 0 my @lines = split /\r?\n/, $text;
14  
15 0 my @tables = ();
16  
17 0 ▶ while (@lines) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines) 0 0 0
18 0 $_ = shift(@lines);
19 0 ▶ next unless /^\+/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^\+/ 0 0 0
20 0 my $offset = 2;
21 0 my $fields = [];
22 0 for (/\+(\-+)/g) {
23 0 my $length = length() - 2;
24 0 push @$fields, [$offset, $length];
25 0 $offset += $length + 3;
26 }
27 0 my $header = shift(@lines);
28 0 for (@$fields) {
29 0 push(@$_, $self->_get_field($header, $_));
30 }
31 0 my $table = [[map $_->[-1], @$fields]];
32 0 shift(@lines);
33 0 ▶ while (my $line = shift(@lines)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = shift @lines) 0 0 0
34 0 ▶ last unless $line =~ /^\|/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /^\|/ 0 0 0
35 push @$table, [
36 map {
37 0 $self->_get_field($line, $_);
38 } @$fields
39 ];
40 }
41 0 push @tables, $self->format($table);
42 }
43 0 return @tables;
44 }
45  
46 0 ▶sub format {
Subroutine format: not called CC=2 SCAR=17.9
Pod undocumented
47 0 my ($self, $input) = @_;
48 0 my $method = 'format_' . $self->as;
49 0 ▶ die "'as' must be 'hoh' or 'hol' or 'loh' or 'lol'"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($method) 0 0 0
50 unless $self->can($method);
51 0 return $self->$method($input);
52 }
53  
54 0 ▶sub format_hoh {
Subroutine format_hoh: not called CC=5 SCAR=34.0
Pod undocumented
55 0 my ($self, $input) = @_;
56 0 my $output = {};
57 0 my $as = $self->as;
58 0 my $header = shift @$input;
59 0 my $pos = $self->_key_pos($header);
60 0 ▶ $output->{''} = $header if $self->header;
Branch: 1 branch
BranchTrueFalseTotal
if $self->header 0 0 0
61 0 for my $row (@$input) {
62 0 my $key = $row->[$pos];
63 0 ▶ $output->{$key} =
Branch: 2 branches
BranchTrueFalseTotal
$as eq 'hoh' ? : 0 0 0
$as eq 'hol' ? : 0 0 0
64 $as eq 'hoh' ? { $self->_zip($header, $row) } :
65 $as eq 'hol' ? $row : die;
66 }
67 0 return $output;
68 }
69 1 *format_hol = \&format_hoh;
70  
71 0 ▶sub format_lol {
Subroutine format_lol: not called CC=5 SCAR=34.0
Pod undocumented
72 0 my ($self, $input) = @_;
73 0 my $output = [];
74 0 my $as = $self->as;
75 0 my $header = shift @$input;
76 0 ▶ push @$output, $header if $self->header;
Branch: 1 branch
BranchTrueFalseTotal
if $self->header 0 0 0
77 0 for my $row (@$input) {
78 0 ▶ push @$output,
Branch: 2 branches
BranchTrueFalseTotal
$as eq 'loh' ? : 0 0 0
$as eq 'lol' ? : 0 0 0
79 $as eq 'loh' ? { $self->_zip($header, $row) } :
80 $as eq 'lol' ? $row : die;
81 }
82 0 return $output;
83 }
84 1 *format_loh = \&format_lol;
85  
86 0 ▶sub _get_field {
Subroutine _get_field: not called CC=1 SCAR=6.9
87 0 my ($self, $line, $offsets) = @_;
88 0 my $value = substr($line, $offsets->[0], $offsets->[1]);
89 0 $value =~ s!^\s*(.*?)\s*$!$1!;
90 0 return $value;
91 }
92  
93 0 ▶sub _key_pos {
Subroutine _key_pos: not called CC=4 SCAR=30.0
94 0 my ($self, $header) = @_;
95 0 my $key = $self->key;
96 0 ▶ return 0 unless length $key;
Branch: 1 branch
BranchTrueFalseTotal
unless length $key 0 0 0
97 0 ▶ for (my $i = 0; $i < @$header; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$header) 0 0 0
98 0 ▶ return $i if $key eq $header->[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $key eq $header->[$i] 0 0 0
99 }
100 0 die "'$key' is an invalid key name";
101 }
102  
103 0 ▶sub _zip {
Subroutine _zip: not called CC=1 SCAR=6.9
104 0 my ($self, $header, $row) = @_;
105 map {
106 0 ($_, shift(@$row));
107 } @$header;
108 }
109  
110 1 1;
↑ Summary