blib/lib/Pod/Weaver/Section/SQL.pm

filter: Statementstmt 98.2% 113 / 115 Branchbran 72.7% 18 / 22 (2 uncoverable) Conditioncond 53.3% 8 / 15 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 100% 14 / 14 Podpod 100% 3 / 3 totaltotal 88.2% 159 / 178 (2 uncoverable) CC 20 SCAR 17.1
CC
20
Cov
90%
transform_document
9.6
format_sql
7.0
weave_section
6.1
CRAP 5.5
↑ Summary
1 package Pod::Weaver::Section::SQL;
2 {
3 3 $Pod::Weaver::Section::SQL::VERSION = '0.03';
4 }
5  
6 # ABSTRACT: Document SQL more easily by referencing only the SQL command in POD
7  
8  
9 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use List::Util 1.33 qw/any/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 with 'Pod::Weaver::Role::Section';
17 3 with 'Pod::Weaver::Role::Transformer';
18  
19 3 ▶use Pod::Elemental::Selectors -all;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use Pod::Elemental::Transformer::Nester;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use Pod::Elemental::Transformer::Gatherer;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 3 ▶use SQL::Statement;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 3 has __used_container => ( is => 'rw' );
26  
27  
28 20 ▶sub transform_document {
Subroutine transform_document: called CC=9 SCAR=22.6
Pod documented
29 20 my ( $self, $document ) = @_;
30  
31 20 my $selector = s_command('sql');
32 20 my $children = $document->children;
33  
34 # I don't know why this branch cannot be coverable, but manually my
35 # tests cover both branches.
36 #
37 # uncoverable branch true
38 # uncoverable branch false
39 20 ▶ return unless grep { $selector->($_) } @$children;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {&$selector($_);} @$children 2 18 20
40  
41 18 my $nester = Pod::Elemental::Transformer::Nester->new(
42 {
43 top_selector => $selector,
44 content_selectors =>
45 [ s_command( [qw/head2 head3 head4 over item back/] ), s_flat, ],
46 }
47 );
48  
49 my ($container_id) = grep {
50 18 my $c = $children->[$_];
51 28 ▶ $c->isa("Pod::Elemental::Element::Nested")
Condition33%2/6
$c->isa('Pod::Elemental::Element::Nested') && $c->command eq 'sql' && $c->content
!ll&&!rl&&r
2800
$c->isa("Pod::Elemental::Element::Nested") and $c->command eq "sql"
!ll&&!rl&&r
2800
MC/DC0%0/3
$c->isa('Pod::Elemental::Element::Nested') && $c->command eq 'sql' && $c->content
$c->isa("Pod::Elemental::Element::Nested") $c->command eq "sql" $c->content
Truth table25%1/4
A and B && C
A= $c->isa("Pod::Elemental::Element::Nested")B= $c->command eq "sql"C= $c->content
ABCresult
0XX0
10X0
1100
1111
52 and $c->command eq 'sql'
53 and $c->content;
54 } 0 .. $#$children;
55  
56 my $container =
57 $container_id
58 0 ▶ ? splice @{$children}, $container_id, 1
Branch: 1 branch
BranchTrueFalseTotal
$container_id ? : 0 18 18
59 : Pod::Elemental::Element::Nested->new(
60 {
61 command => 'head1',
62 content => 'SQL',
63 }
64 );
65  
66 18 $self->__used_container($container);
67  
68 18 my $gatherer = Pod::Elemental::Transformer::Gatherer->new(
69 {
70 gather_selector => $selector,
71 container => $container,
72 }
73 );
74  
75 18 $nester->transform_node($document);
76 18 $gatherer->transform_node($document);
77  
78 18 my @queue;
79 18 push @queue, @{ $container->children };
80 18 $container->children( [] );
81  
82 18 ▶ while ( my $node = shift @queue ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $node = shift @queue) 28 18 46
83 28 ▶ if ( $node->can('command')
Branch: 1 branch
BranchTrueFalseTotal
if ($node->can('command') and $node->command eq 'sql') { } 20 8 28
Condition100%3/3
$node->can("command") and $node->command eq "sql"
!ll&&!rl&&r
3520
MC/DC100%2/2
$node->can("command") and $node->command eq "sql"
$node->can("command") $node->command eq "sql"
Truth table100%3/3
A and B
A= $node->can("command")B= $node->command eq "sql"
ABresult
0X0
100
111
84 and $node->command eq 'sql' )
85 {
86 20 ▶ if ( $node->can('children') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->can("children")) 20 0 20
87  
88 # Move up every child of sql node
89 20 unshift @queue, @{ $node->children };
90 }
91 20 push @{ $container->children },
92 Pod::Elemental::Element::Generic::Text->new(
93 content => $self->format_sql( $node->content ) . "\n\n" );
94 }
95 else {
96 8 push @{ $container->children }, $node;
97 }
98 }
99 }
100  
101  
102 20 ▶sub weave_section {
Subroutine weave_section: called CC=6 SCAR=18.1
Pod documented
103 20 my ( $self, $document, $input ) = @_;
104  
105 20 ▶ return unless $self->__used_container;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->__used_container 2 18 20
106  
107 18 my $in_node = $input->{pod_document}->children;
108 18 my @found;
109 18 for my $i ( 0 .. $#$in_node ) {
110 18 my $para = $in_node->[$i];
111 push @found, $i
112 if ( $para == $self->__used_container
113 18 ▶ && @{ $self->__used_container->children } );
Branch: 1 branch
BranchTrueFalseTotal
if $para == $self->__used_container and @{$self->__used_container->children;} 18 0 18
Condition33%1/3
$para == $self->__used_container and @{$self->__used_container->children;}
!ll&&!rl&&r
0018
MC/DC0%0/2
$para == $self->__used_container and @{$self->__used_container->children;}
$para == $self->__used_container @{$self->__used_container->children;}
Truth table33%1/3
A and B
A= $para == $self->__used_containerB= @{$self->__used_container->children;}
ABresult
0X0
100
111
114 }
115  
116 18 my @to_add;
117 18 for my $i ( reverse @found ) {
118 18 push @to_add, splice @{$in_node}, $i, 1;
119 }
120  
121 18 push @{ $document->children }, @to_add;
122 }
123  
124 has keywords => (
125 is => 'ro',
126 lazy => 1,
127 16 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
128 16 my $parser = SQL::Parser->new( ANSI =>
129 {
130 RaiseError => 1,
131 PrintError => 1
132 }
133 );
134 16 my $stmt = SQL::Statement->new(
135 'SELECT * FROM some_unknown_table',
136 $parser
137 );
138  
139 my @keywords = (
140 16 keys %{$stmt->{opts}->{function_names}},
141 16 keys %{$stmt->{opts}->{reserved_words}},
142 16 keys %{$stmt->{opts}->{valid_commands}},
143 16 keys %{$stmt->{opts}->{valid_comparison_operators}},
144 16 keys %{$stmt->{opts}->{valid_data_types}}
145 );
146 16 return \@keywords;
147 },
148 3 );
149  
150  
151 20 ▶sub format_sql {
Subroutine format_sql: called CC=7 SCAR=19.5
Pod documented
152 20 my ( $self, $content ) = @_;
153  
154 20 ▶ if ( $content =~ m/SQL::Abstract/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /SQL::Abstract/) 4 16 20
155 4 $content =~ s/\n//g;
156 4 require SQL::Abstract;
157 4 $content =~ s/SQL::Abstract/\$sql_abstract/;
158 4 my $sql_abstract = SQL::Abstract->new;
159 4 my ($stmt, @bind) = eval $content;
160 4 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 4 4
161 0 croak "Trying to eval $content\n" . $@;
162 }
163 4 $content = $stmt;
164 }
165  
166 20 my %words;
167 20 map { $words{$_} = 1 } split ' ', $content;
168  
169 20 $content =~ s/C<([^>]+)>/C\[__\?:__$1__:\?__\]/g;
170  
171 20 my @keywords;
172 20 ▶ if ( ref( $self->keywords )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->keywords and ref $self->keywords eq 'ARRAY') { } 18 2 20
Condition66%2/3
ref $self->keywords and ref $self->keywords eq "ARRAY"
!ll&&!rl&&r
2018
MC/DC50%1/2
ref $self->keywords and ref $self->keywords eq "ARRAY"
ref $self->keywords ref $self->keywords eq "ARRAY"
Truth table66%2/3
A and B
A= ref $self->keywordsB= ref $self->keywords eq "ARRAY"
ABresult
0X0
100
111
173 and ref( $self->keywords ) eq 'ARRAY' )
174 {
175 18 @keywords = @{ $self->keywords };
176 }
177 else {
178 2 @keywords = split( ',', $self->keywords );
179 }
180  
181 20 for my $word ( keys %words ) {
182 147 ▶ if ( any { $word eq $_ } @keywords ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { $word eq $_; } , @keywords)) 63 84 147
183 63 my $new_word = $word;
184 63 $new_word =~ s/</E\[__?:__lt__:?__\]/;
185 63 $new_word =~ s/>/E\[__?:__gt__:?__\]/;
186 63 $content =~ s/(^| |\n)${word}( |$)/${1}B\[__?:__${new_word}__:?__\]${2}/g;
187 }
188 }
189  
190 20 $content =~ s/\[__\?:__/</g;
191 20 $content =~ s/__:\?__\]/>/g;
192 20 return $content;
193 }
194  
195 3 __PACKAGE__->meta->make_immutable;
196 3 1;
197  
198 __END__
↑ Summary