blib/lib/SQL/NamedPlaceholder.pm

filter: Statementstmt 94.8% 37 / 39 Branchbran 91.6% 11 / 12 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 95.0% 58 / 61 CC 8 SCAR 16.7
CC
8
Cov
94%
bind_named
8.0
BEGIN
1.0
BEGIN
1.0
CRAP 5.3
↑ Summary
1 package SQL::NamedPlaceholder;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Exporter::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 our $VERSION = '0.10';
11 2 our @EXPORT_OK = qw(bind_named);
12  
13 33 ▶sub bind_named {
Subroutine bind_named: called CC=8 SCAR=20.8
Pod documented
14 33 my ($sql, $hash) = @_;
15 33 ▶ $sql or croak 'my ($sql, $bind) = bind_named($sql, $hash) requires $sql';
Branch: 1 branch
BranchTrueFalseTotal
$sql or croak("my (\$sql, \$bind) = bind_named(\$sql, \$hash) requires \$sql") 32 1 33
16 32 ▶ (reftype($hash) || '') eq 'HASH' or croak 'must specify HASH as bind values';
Branch: 1 branch
BranchTrueFalseTotal
(builtin::reftype($hash) || "") eq "HASH" or croak("must specify HASH as bind values") 30 2 32
Condition100%2/2
builtin::reftype($hash) || ''
l!l
311
MC/DC100%1/1
builtin::reftype($hash) || ''
builtin::reftype($hash)
Truth table100%2/2
A
A= builtin::reftype($hash)
Aresult
11
00
17  
18 # replace question marks as placeholder. e.g. [`hoge` = ?] to [`hoge` = :hoge]
19 30 $sql =~ s{(([`"]?)(\S+?)\2\s*(=|<=?|>=?|<>|!=|<=>)\s*)\?}{$1:$3}g;
20  
21 30 my $bind = [];
22  
23 30 $sql =~ s{:([A-Za-z_][A-Za-z0-9_]*)}{
24 53 ▶ croak("'$1' does not exist in bind hash") if !exists $hash->{$1};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $hash->{$1} 1 52 53
25 52 my $type = ref($hash->{$1});
26 52 ▶ if ($type eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'ARRAY') { } 2 50 52
27 2 ▶ if (@{ $hash->{$1} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$hash->{$1};}) { } 2 0 2
28 2 push @$bind, @{ $hash->{$1} };
29 2 join ', ', map { '?' } @{ $hash->{$1} };
30 } else {
31 0 push @$bind, undef;
32 0 '?';
33 }
34 } else {
35 50 push @$bind, $hash->{$1};
36 50 '?';
37 }
38 }eg;
39  
40 29 ▶ wantarray ? ($sql, $bind) : [$sql, $bind];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 28 1 29
41 }
42  
43 2 1;
44 __END__
↑ Summary