Database/PreparedStatementTemplate.pm

filter: Statementstmt 99.0% 104 / 105 Branchbran 83.9% 47 / 56 Conditioncond 82.3% 42 / 51 MC/DCmcdc 60.0% 15 / 25 Subroutinesub 100% 14 / 14 Podpod 100% 2 / 2 totaltotal 88.5% 224 / 253 CC 50 SCAR 24.1
CC
50
Cov
91%
_transform_binding
20.3
_remove_comments
14.0
_bind
8.2
CRAP 11.1
« Database/OracleProvider.pm ↑ Summary Database/Command/db/backup.pm »
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Footprintless::Plugin::Database::PreparedStatementTemplate;
5 1 $Footprintless::Plugin::Database::PreparedStatementTemplate::VERSION = '1.06';
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp 'verbose';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Log::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 my $logger = Log::Any->get_logger();
12  
13 11 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
14 11 my $self = bless( {}, shift );
15 11 $self->_init(@_);
16 }
17  
18 11 ▶sub _init {
Subroutine _init: called CC=3 SCAR=11.0
19 11 my ( $self, $sql_template, %bindings ) = @_;
20 my @binding_keys =
21 11 ▶ sort { ( length($b) <=> length($a) ) || ( $a cmp $b ) }
Condition66%2/3
length $b <=> length $a || $a cmp $b
l!l&&r!l&&!r
9160
MC/DC0%0/2
length $b <=> length $a || $a cmp $b
length $b <=> length $a $a cmp $b
Truth table66%2/3
A || B
A= length $b <=> length $aB= $a cmp $b
ABresult
1X1
011
000
22 keys(%bindings);
23 11 my @split_text;
24 my @index_to_key;
25 $self->{bindings} =
26 11 { map { $_ => _transform_binding( $_, $bindings{$_} ) } @binding_keys };
27 11 _dice( _remove_comments($sql_template), \@split_text, \@index_to_key, @binding_keys );
28 11 $self->{prepared_statement} = join( '?', @split_text );
29 $self->{parameter_bindings} =
30 11 [ map { $self->{bindings}->{$_} } @index_to_key ];
31  
32 11 my %used_keys = map { $_ => 1 } @index_to_key;
33 11 foreach my $unused_key ( grep { !$used_keys{$_} } @binding_keys ) {
34 5 $logger->warn("Template var [$unused_key] is never used!");
35 5 delete( $self->{bindings}->{$unused_key} );
36 }
37 11 return $self;
38 }
39  
40 41 ▶sub _bind {
Subroutine _bind: called CC=8 SCAR=21.0
41 41 my ( $binding, $context ) = @_;
42 41 ▶ if ( defined( my $key = $binding->{key} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined(my $key = $binding->{'key'})) { } 12 29 41
elsif (defined(my $reference = $binding->{'reference'})) { } 6 23 29
elsif (defined(my $code = $binding->{'code'})) { } 10 13 23
43 6 eval { $binding->{value} = $context->$key() }
44 12 ▶ if ( !defined( $binding->{value} = $context->{$key} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined($binding->{"value"} = $context->{$key}) 6 6 12
45 croak(
46 "Cannot bind template var [$binding->{template_key}] - property [$key] cannot be bound in context"
47 12 ▶ ) unless defined( $binding->{value} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $binding->{"value"} 0 12 12
48 }
49 elsif ( defined( my $reference = $binding->{reference} ) ) {
50 croak("Cannot bind template var [$binding->{template_key}] - reference to undefined")
51 6 ▶ unless defined( $binding->{value} = $$reference );
Branch: 1 branch
BranchTrueFalseTotal
unless defined($binding->{"value"} = $$reference) 0 6 6
52 }
53 elsif ( defined( my $code = $binding->{code} ) ) {
54 croak("Cannot bind template var [$binding->{template_key}] - code returns undefined")
55 10 ▶ unless defined( $binding->{value} = $code->() );
Branch: 1 branch
BranchTrueFalseTotal
unless defined($binding->{"value"} = &$code()) 0 10 10
56 }
57 }
58  
59 128 ▶sub _dice {
Subroutine _dice: called CC=5 SCAR=16.1
60 128 my ( $text, $split_text, $index_to_key, $key, @keys ) = @_;
61 128 ▶ if ( !$key ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $key) { } 50 78 128
62 50 push( @$split_text, $text );
63 }
64 else {
65 78 my $add_ix = 0;
66  
67 # We need at least one element with a blank string in split...
68 78 ▶ foreach ( $text ? split( /\Q$key\E/, $text, -1 ) : ('') ) {
Branch: 1 branch
BranchTrueFalseTotal
$text ? : 69 9 78
69 117 ▶ push( @$index_to_key, $key ) if ( $add_ix++ );
Branch: 1 branch
BranchTrueFalseTotal
if $add_ix++ 39 78 117
70 117 _dice( $_, $split_text, $index_to_key, @keys );
71 }
72 }
73 }
74  
75 16 ▶sub query {
Subroutine query: called CC=4 SCAR=13.9
Pod documented
76 16 my ( $self, $context ) = @_;
77 16 my $query = { sql => $self->{prepared_statement} };
78 16 ▶ if ( %{ $self->{bindings} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%{$self->{"bindings"};}) 16 0 16
79 16 foreach ( values( %{ $self->{bindings} } ) ) { _bind( $_, $context ) }
80 $query->{parameters} =
81 16 [ map { $_->{value} } @{ $self->{parameter_bindings} } ];
82 16 foreach ( values( %{ $self->{bindings} } ) ) { _unbind( $_, $context ) }
83 }
84 16 return $query;
85 }
86  
87 11 ▶sub _remove_comments {
Subroutine _remove_comments: called CC=14 SCAR=26.4
88 11 my ($sql) = @_;
89 11 my $sql_out;
90 11 ▶ open( my $fh, '>', \$sql_out ) || croak("Cannot write to string!");
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", \$sql_out or croak("Cannot write to string!") 11 0 11
91 11 my ( $in_block_comment, $in_line_comment, $in_quote ) = ( 0, 0, 0 );
92 11 ▶ for ( my $ix = 0; $ix < length($sql); ++$ix ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ix < length $sql) 1304 11 1315
93 1304 ▶ if ($in_block_comment) {
Branch: 2 branches
BranchTrueFalseTotal
if ($in_block_comment) { } 278 1026 1304
elsif ($in_line_comment) { } 253 773 1026
94 278 ▶ if ( substr( $sql, $ix, 2 ) eq '*/' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($sql, $ix, 2) eq "*/") 11 267 278
95 11 $in_block_comment = 0;
96 11 ++$ix;
97 }
98 }
99 elsif ($in_line_comment) {
100 253 ▶ if ( substr( $sql, $ix, 1 ) eq "\n" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($sql, $ix, 1) eq "\n") 10 243 253
101 10 $in_line_comment = 0;
102 10 print $fh ("\n");
103 }
104 }
105 else {
106 773 my $char = substr( $sql, $ix, 1 );
107 773 ▶ if ( !$in_quote && $char eq '/' && substr( $sql, $ix + 1, 1 ) eq '*' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $in_quote and $char eq '/' and substr($sql, $ix + 1, 1) eq '*') { } 11 762 773
elsif (not $in_quote and $char eq '-' and substr($sql, $ix + 1, 1) eq '-') { } 10 752 762
Condition83%5/6
not $in_quote and $char eq "/" and substr($sql, $ix + 1, 1) eq "*"
!ll&&!rl&&r
762011
not $in_quote and $char eq "/"
!ll&&!rl&&r
4571711
MC/DC66%2/3
not $in_quote and $char eq "/" and substr($sql, $ix + 1, 1) eq "*"
not $in_quote $char eq "/" substr($sql, $ix + 1, 1) eq "*"
Truth table75%3/4
not(A) and B and C
A= not $in_quoteB= $char eq "/"C= substr($sql, $ix + 1, 1) eq "*"
ABCresult
0XX0
10X0
1100
1111
108 11 $in_block_comment = 1;
109 11 ++$ix;
110 }
111 ▶ elsif ( !$in_quote && $char eq '-' && substr( $sql, $ix + 1, 1 ) eq '-' ) {
Condition83%5/6
not $in_quote and $char eq "-" and substr($sql, $ix + 1, 1) eq "-"
!ll&&!rl&&r
752010
not $in_quote and $char eq "-"
!ll&&!rl&&r
4570710
MC/DC66%2/3
not $in_quote and $char eq "-" and substr($sql, $ix + 1, 1) eq "-"
not $in_quote $char eq "-" substr($sql, $ix + 1, 1) eq "-"
Truth table75%3/4
not(A) and B and C
A= not $in_quoteB= $char eq "-"C= substr($sql, $ix + 1, 1) eq "-"
ABCresult
0XX0
10X0
1100
1111
112 10 $in_line_comment = 1;
113 10 ++$ix;
114 }
115 else {
116 752 ▶ $in_quote = !$in_quote if ( $char eq "'" );
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "'" 10 742 752
117 752 print $fh ($char);
118 }
119 }
120 }
121 11 close($fh);
122 11 return $sql_out;
123 }
124  
125 29 ▶sub _transform_binding {
Subroutine _transform_binding: called CC=18 SCAR=30.1
126 29 my ( $template_key, $binding ) = @_;
127 29 my $ref = ref($binding);
128 29 my $new_binding = { template_key => $template_key };
129 29 ▶ if ( my $key = ( ( !$ref && $binding ) || ( $ref eq 'HASH' && $binding->{key} ) ) ) {
Branch: 4 branches
BranchTrueFalseTotal
if (my $key = !$ref && $binding || $ref eq 'HASH' && $binding->{'key'}) { } 6 23 29
elsif (my $reference = $ref eq 'SCALAR' && $binding || $ref eq 'HASH' && $binding->{'reference'}) { } 3 20 23
elsif ($ref eq 'HASH' and defined $binding->{'value'}) { } 15 5 20
elsif (my $code = $ref eq 'CODE' && $binding || $ref eq 'HASH' && $binding->{'code'}) { } 5 0 5
Condition88%8/9
!$ref && $binding || $ref eq 'HASH' && $binding->{'key'}
l!l&&r!l&&!r
4223
not $ref and $binding
!ll&&!rl&&r
2504
$ref eq "HASH" and $binding->{"key"}
!ll&&!rl&&r
5182
MC/DC75%3/4
!$ref && $binding || $ref eq 'HASH' && $binding->{'key'}
not $ref $binding $ref eq "HASH" $binding->{"key"}
Truth table57%4/7
not(A) and B || C and D
A= not $refB= $bindingC= $ref eq "HASH"D= $binding->{"key"}
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
130 6 $new_binding->{key} = $key;
131 }
132 ▶ elsif ( my $reference =
Condition88%8/9
$ref eq 'SCALAR' && $binding || $ref eq 'HASH' && $binding->{'reference'}
l!l&&r!l&&!r
2120
$ref eq "SCALAR" and $binding
!ll&&!rl&&r
2102
$ref eq "HASH" and $binding->{"reference"}
!ll&&!rl&&r
3171
MC/DC75%3/4
$ref eq 'SCALAR' && $binding || $ref eq 'HASH' && $binding->{'reference'}
$ref eq "SCALAR" $binding $ref eq "HASH" $binding->{"reference"}
Truth table57%4/7
A and B || C and D
A= $ref eq "SCALAR"B= $bindingC= $ref eq "HASH"D= $binding->{"reference"}
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
133 ( ( $ref eq 'SCALAR' && $binding ) || ( $ref eq 'HASH' && $binding->{reference} ) ) )
134 {
135 3 ▶ croak(
Branch: 1 branch
BranchTrueFalseTotal
unless ref $reference eq "SCALAR" 0 3 3
136 "Template var [$template_key] - 'reference' property of binding is not a 'SCALAR' ref"
137 ) unless ref($reference) eq 'SCALAR';
138 3 $new_binding->{reference} = $reference;
139 }
140 ▶ elsif ( $ref eq 'HASH' && defined( $binding->{value} ) ) {
Condition100%3/3
$ref eq "HASH" and defined $binding->{"value"}
!ll&&!rl&&r
3215
MC/DC100%2/2
$ref eq "HASH" and defined $binding->{"value"}
$ref eq "HASH" defined $binding->{"value"}
Truth table100%3/3
A and B
A= $ref eq "HASH"B= defined $binding->{"value"}
ABresult
0X0
100
111
141 croak("'Template var [$template_key] - value' property of binding is a ref")
142 15 ▶ if ref( $binding->{value} );
Branch: 1 branch
BranchTrueFalseTotal
if ref $binding->{"value"} 0 15 15
143 15 $new_binding->{value} = $binding->{value};
144 }
145 ▶ elsif ( my $code =
Condition55%5/9
$ref eq 'CODE' && $binding || $ref eq 'HASH' && $binding->{'code'}
l!l&&r!l&&!r
320
$ref eq "CODE" and $binding
!ll&&!rl&&r
203
$ref eq "HASH" and $binding->{"code"}
!ll&&!rl&&r
002
MC/DC0%0/4
$ref eq 'CODE' && $binding || $ref eq 'HASH' && $binding->{'code'}
$ref eq "CODE" $binding $ref eq "HASH" $binding->{"code"}
Truth table28%2/7
A and B || C and D
A= $ref eq "CODE"B= $bindingC= $ref eq "HASH"D= $binding->{"code"}
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
146 ( ( $ref eq 'CODE' && $binding ) || ( $ref eq 'HASH' && $binding->{code} ) ) )
147 {
148 5 ▶ croak("Template var [$template_key] - 'code' property of binding is not a 'CODE' ref")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 0 5 5
149 unless ref($code) eq 'CODE';
150 5 $new_binding->{code} = $code;
151 }
152 else {
153 0 croak( "Template var [$template_key] - binding [%s] is invalid", Dumper($binding) );
154 }
155 29 return $new_binding;
156 }
157  
158 41 ▶sub _unbind {
Subroutine _unbind: called CC=4 SCAR=13.9
159 41 my ($binding) = @_;
160 delete( $binding->{value} )
161 if defined( $binding->{key} )
162 || defined( $binding->{reference} )
163 41 ▶ || defined( $binding->{code} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $binding->{"key"} or defined $binding->{"reference"} or defined $binding->{"code"} 28 13 41
Condition100%6/6
defined $binding->{"key"} or defined $binding->{"reference"} or defined $binding->{"code"}
l!l&&r!l&&!r
181013
defined $binding->{"key"} or defined $binding->{"reference"}
l!l&&r!l&&!r
12623
MC/DC100%3/3
defined $binding->{"key"} or defined $binding->{"reference"} or defined $binding->{"code"}
defined $binding->{"key"} defined $binding->{"reference"} defined $binding->{"code"}
Truth table100%4/4
A or B or C
A= defined $binding->{"key"}B= defined $binding->{"reference"}C= defined $binding->{"code"}
ABCresult
1XX1
01X1
0011
0000
164 }
165  
166 1 1;
167  
168 __END__
« Database/OracleProvider.pm ↑ Summary Database/Command/db/backup.pm »