blib/lib/DBIx/NamedParams.pm

filter: Statementstmt 47.2% 69 / 146 Branchbran 5.2% 2 / 38 Conditioncond 6.6% 3 / 45 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 68.0% 17 / 25 Podpod 83.3% 5 / 6 totaltotal 34.0% 96 / 282 CC 37 SCAR 37.8
CC
37
Cov
32%
bind_param_ex
107.3
prepare_ex
72.0
_parse_ex2
72.0
CRAP 43.9
↑ Summary
1 package DBIx::NamedParams;
2  
3 1 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use parent qw( DBI );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use DBI::Const::GetInfoType;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Log::Dispatch;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use POSIX qw( strftime );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Scalar::Util qw( reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Term::Encoding qw( term_encoding );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use version 0.77; our $VERSION = version->declare("v0.0.11");
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 our $KeepBindingIfNoKey = 0;
19  
20 1 my $_package = __PACKAGE__;
21 1 ▶my $_default_log_filename = $ENV{'HOME'} || $ENV{'USERPROFILE'} || $ENV{'TMP'} || '/tmp';
Condition37%3/8
$ENV{'HOME'} || $ENV{'USERPROFILE'} || $ENV{'TMP'} || '/tmp'
l!l
10
$ENV{"HOME"} or $ENV{"USERPROFILE"} or $ENV{"TMP"}
l!l&&r!l&&!r
100
$ENV{"HOME"} or $ENV{"USERPROFILE"}
l!l&&r!l&&!r
100
MC/DC0%0/3
$ENV{'HOME'} || $ENV{'USERPROFILE'} || $ENV{'TMP'} || '/tmp'
$ENV{"HOME"} $ENV{"USERPROFILE"} $ENV{"TMP"}
Truth table25%1/4
A or B or C
A= $ENV{"HOME"}B= $ENV{"USERPROFILE"}C= $ENV{"TMP"}
ABCresult
1XX1
01X1
0011
0000
22 1 $_default_log_filename =~ s#\\#/#g;
23 1 $_default_log_filename .= '/DBIx-NamedParams.log';
24  
25 1 my %_SQL_TypeRefs = ();
26 1 my %_SQL_TypeInvs = ();
27 1 my $_SQL_Types = '';
28 1 my $_log = undef;
29  
30 1 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
31 1 DBI->import();
32 1 *{DBI::db::driver_typename_map} = \&driver_typename_map;
33 1 *{DBI::db::prepare_ex} = \&prepare_ex;
34 1 *{DBI::st::mapped_params} = \&mapped_params;
35 1 *{DBI::st::bind_param_ex} = \&bind_param_ex;
36 1 _init();
37 }
38  
39 1 ▶sub _init {
Subroutine _init: called CC=3 SCAR=11.0
40 1 foreach ( @{ $DBI::EXPORT_TAGS{sql_types} } ) {
41 58 my $refFunc = \&{"DBI::$_"};
42 58 ▶ if (/^SQL_(.*)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^SQL_(.*)$/i) 58 0 58
43 58 $_SQL_TypeRefs{$1} = &{$refFunc};
44 58 $_SQL_TypeInvs{ &{$refFunc} } = $1;
45 }
46 }
47 1 $_SQL_Types = all_sql_types();
48 }
49  
50 0 ▶sub _thisFuncName {
Subroutine _thisFuncName: not called CC=1 SCAR=6.9
51 0 ( caller(1) )[3] =~ /([^:]+)$/;
52 0 return $1;
53 }
54  
55 0 ▶sub debug_log {
Subroutine debug_log: not called CC=2 SCAR=17.9
Pod documented
56 0 ▶ my $filename = shift || $_default_log_filename;
Condition0%0/3
shift() || $_default_log_filename
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $_default_log_filename
shift() $_default_log_filename
Truth table0%0/3
A || B
A= shift()B= $_default_log_filename
ABresult
1X1
011
000
57 0 $_log = Log::Dispatch->new(
58 outputs => [
59 [ 'File',
60 min_level => 'debug',
61 filename => encode( term_encoding, $filename ),
62 binmode => ":utf8",
63 permissions => 0666,
64 newline => 1,
65 ],
66 ],
67 );
68 0 $_log->info( _thisFuncName(), strftime( "%Y-%m-%d %H:%M:%S", localtime ) );
69 }
70  
71 1 ▶sub all_sql_types {
Subroutine all_sql_types: called CC=2 SCAR=7.6
Pod documented
72 return wantarray
73 1 ▶ ? sort( keys(%_SQL_TypeRefs) )
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
74 : join( "|", sort( keys(%_SQL_TypeRefs) ) );
75 }
76  
77 0 ▶sub driver_typename_map {
Subroutine driver_typename_map: not called CC=6 SCAR=37.4
Pod documented
78 0 my $self = shift;
79 my %map = map {
80 0 my $datatype = $_->{'SQL_DATA_TYPE'} # MS SQL Server
81 || $_->{'SQL_DATATYPE'} # MySQL
82 0 ▶ || $_->{'DATA_TYPE'}; # SQLite
Condition0%0/6
$_->{'SQL_DATA_TYPE'} || $_->{'SQL_DATATYPE'} || $_->{'DATA_TYPE'}
l!l&&r!l&&!r
000
$_->{"SQL_DATA_TYPE"} or $_->{"SQL_DATATYPE"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$_->{'SQL_DATA_TYPE'} || $_->{'SQL_DATATYPE'} || $_->{'DATA_TYPE'}
$_->{"SQL_DATA_TYPE"} $_->{"SQL_DATATYPE"} $_->{'DATA_TYPE'}
Truth table0%0/4
A or B || C
A= $_->{"SQL_DATA_TYPE"}B= $_->{"SQL_DATATYPE"}C= $_->{'DATA_TYPE'}
ABCresult
1XX1
01X1
0011
0000
83 0 ▶ ( $_->{'TYPE_NAME'} || '' ) => $_SQL_TypeInvs{$datatype} || 'WVARCHAR';
Condition0%0/4
$_->{'TYPE_NAME'} || ''
l!l
00
$_SQL_TypeInvs{$datatype} || 'WVARCHAR'
l!l
00
MC/DC0%0/1
$_->{'TYPE_NAME'} || ''
$_->{'TYPE_NAME'}
MC/DC0%0/1
$_SQL_TypeInvs{$datatype} || 'WVARCHAR'
$_SQL_TypeInvs{$datatype}
Truth table0%0/2
A
A= $_->{'TYPE_NAME'}
Aresult
11
00
Truth table0%0/2
A
A= $_SQL_TypeInvs{$datatype}
Aresult
11
00
84 } $self->type_info();
85 0 ▶ if ( $self->get_info( $GetInfoType{'SQL_DBMS_NAME'} ) eq 'Microsoft SQL Server' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_info($DBIx::NamedParams::GetInfoType{"SQL_DBMS_NAME"}) eq "Microsoft SQL Server") 0 0 0
86 0 $map{'datetime'} = 'WVARCHAR';
87 0 $map{'smalldatetime'} = 'WVARCHAR';
88 }
89 0 return %map;
90 }
91  
92 0 ▶sub prepare_ex {
Subroutine prepare_ex: not called CC=8 SCAR=42.8
Pod documented
93 0 my ( $self, $sqlex, $refHash ) = @_;
94 0 ▶ my $validHash = defined($refHash) && ( reftype($refHash) || '' ) eq 'HASH';
Condition0%0/5
defined $refHash && (builtin::reftype($refHash) || '') eq 'HASH'
!ll&&!rl&&r
000
builtin::reftype($refHash) || ''
l!l
00
MC/DC0%0/2
defined $refHash && (builtin::reftype($refHash) || '') eq 'HASH'
defined $refHash builtin::reftype($refHash)
Truth table0%0/3
A && B
A= defined $refHashB= builtin::reftype($refHash)
ABresult
0X0
100
111
95 0 ▶ if ( $sqlex =~ /\:([\w]+)\+-($_SQL_Types)\b/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sqlex =~ /\:([\w]+)\+-($_SQL_Types)\b/) 0 0 0
96 0 ▶ if ($validHash) {
Branch: 1 branch
BranchTrueFalseTotal
if ($validHash) { } 0 0 0
97 0 $sqlex =~ s/\:([\w]+)\+-($_SQL_Types)\b/_parse_ex1($refHash,$1,$2);/ge;
98 } else {
99 0 croak("prepare_ex need a hash reference when SQL is variable length.");
100 }
101 }
102 0 my @params = ();
103 0 $sqlex =~ s/\:([\w]+)(?:\{(\d+)\})?-($_SQL_Types)\b/_parse_ex2(\@params,$1,$2,$3);/ge;
104 0 ▶ if ($_log) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_log) 0 0 0
105 0 $_log->info( _thisFuncName(), 'sql_raw', "{{$sqlex}}" );
106 }
107 0 ▶ my $sth = $self->prepare($sqlex) or croak($DBI::errstr);
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $self->prepare($sqlex) or croak($DBI::errstr) 0 0 0
108 0 $sth->mapped_params(@params);
109 0 ▶ if ($validHash) {
Branch: 1 branch
BranchTrueFalseTotal
if ($validHash) 0 0 0
110 0 $sth->bind_param_ex($refHash);
111 }
112 0 return $sth;
113 }
114  
115 0 ▶sub _parse_ex1 {
Subroutine _parse_ex1: not called CC=2 SCAR=17.9
116 0 my ( $refHash, $name, $type ) = @_;
117 0 my $numOfArray = 0;
118 0 ▶ if ( ref( $refHash->{$name} ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $refHash->{$name} eq 'ARRAY') { } 0 0 0
119 0 $numOfArray = scalar( @{ $refHash->{$name} } );
120 } else {
121 0 croak("Must be array: ${name}");
122 }
123 0 return ":${name}{${numOfArray}}-${type}";
124 }
125  
126 0 ▶sub _parse_ex2 {
Subroutine _parse_ex2: not called CC=8 SCAR=42.8
127 0 my $params = shift;
128 0 ▶ my $name = shift || '';
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
129 0 ▶ my $repeat = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
130 0 ▶ my $type = shift || '';
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
131 0 my $index = scalar( @{$params} ) + 1;
132  
133 0 ▶ if ($_log) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_log) 0 0 0
134 0 ▶ $_log->info( _thisFuncName(), "[${index}]", "\"${name}\"",
Branch: 1 branch
BranchTrueFalseTotal
!$repeat ? : 0 0 0
135 ( !$repeat ) ? "scalar" : "array[$repeat]", $type );
136 }
137 0 ▶ if ( !$repeat ) { # scalar
Branch: 1 branch
BranchTrueFalseTotal
if (not $repeat) { } 0 0 0
138 push(
139 0 @{$params},
140 { Name => $name,
141 0 Type => $_SQL_TypeRefs{$type},
142 Array => -1,
143 }
144 );
145 0 return '?';
146 } else { # array
147 0 ▶ for ( my $i = 0; $i < $repeat; ++$i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $repeat) 0 0 0
148 push(
149 0 @{$params},
150 { Name => $name,
151 0 Type => $_SQL_TypeRefs{$type},
152 Array => $i,
153 }
154 );
155 }
156 0 return substr( '?,' x $repeat, 0, -1 );
157 }
158 }
159  
160 0 ▶sub mapped_params {
Subroutine mapped_params: not called CC=3 SCAR=24.8
Pod undocumented
161 0 my $self = shift;
162 0 my $inner = tied( %{$self} );
163 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
164 0 $inner->{$_package}{MappedParams} = [@_];
165 }
166 0 ▶ return @{ $inner->{$_package}{MappedParams} || [] };
Condition0%0/2
$inner->{$_package}{'MappedParams'} || []
l!l
00
MC/DC0%0/1
$inner->{$_package}{'MappedParams'} || []
$inner->{$_package}{'MappedParams'}
Truth table0%0/2
A
A= $inner->{$_package}{'MappedParams'}
Aresult
11
00
167 }
168  
169 0 ▶sub bind_param_ex {
Subroutine bind_param_ex: not called CC=11 SCAR=46.8
Pod documented
170 1 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
171 0 my ( $self, $refHash ) = @_;
172 0 ▶ if ( !defined($refHash) || ( reftype($refHash) || '' ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $refHash or (builtin::reftype($refHash) || "") ne "HASH") 0 0 0
Condition0%0/5
not defined $refHash or (builtin::reftype($refHash) || "") ne "HASH"
l!l&&r!l&&!r
000
builtin::reftype($refHash) || ''
l!l
00
MC/DC0%0/2
not defined $refHash or (builtin::reftype($refHash) || "") ne "HASH"
not defined $refHash builtin::reftype($refHash)
Truth table0%0/3
not(A) or B
A= not defined $refHashB= builtin::reftype($refHash)
ABresult
1X1
011
000
173 0 croak("bind_param_ex need a hash reference.");
174 }
175 0 my $thisFunc = _thisFuncName();
176 0 my $i = 0;
177 0 foreach my $param ( $self->mapped_params() ) {
178 0 ++$i;
179 0 ▶ if ( $KeepBindingIfNoKey && !exists( $refHash->{ $param->{'Name'} } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DBIx::NamedParams::KeepBindingIfNoKey and not exists $$refHash{$param->{"Name"}}) 0 0 0
Condition0%0/3
$DBIx::NamedParams::KeepBindingIfNoKey and not exists $$refHash{$param->{"Name"}}
!ll&&!rl&&r
000
MC/DC0%0/2
$DBIx::NamedParams::KeepBindingIfNoKey and not exists $$refHash{$param->{"Name"}}
$DBIx::NamedParams::KeepBindingIfNoKey not exists $$refHash{$param->{"Name"}}
Truth table0%0/3
A and not(B)
A= $DBIx::NamedParams::KeepBindingIfNoKeyB= not exists $$refHash{$param->{"Name"}}
ABresult
0X0
100
111
180 0 next;
181 }
182 0 my $idx = $param->{'Array'};
183 0 my $value1 = $refHash->{ $param->{'Name'} };
184 0 ▶ my $value2
Branch: 1 branch
BranchTrueFalseTotal
$idx < 0 || ref $value1 ne 'ARRAY' ? : 0 0 0
Condition0%0/3
$idx < 0 or ref $value1 ne "ARRAY"
l!l&&r!l&&!r
000
MC/DC0%0/2
$idx < 0 or ref $value1 ne "ARRAY"
$idx < 0 ref $value1 ne "ARRAY"
Truth table0%0/3
A or B
A= $idx < 0B= ref $value1 ne "ARRAY"
ABresult
1X1
011
000
185 = ( $idx < 0 || ref($value1) ne 'ARRAY' )
186 ? $value1
187 : $value1->[$idx];
188 0 my $datatype = $param->{'Type'};
189 0 ▶ if ($_log) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_log) 0 0 0
190 0 $_log->info( $thisFunc, "[$i]", "\"$value2\"", $_SQL_TypeInvs{$datatype} );
191 }
192 0 ▶ $self->bind_param( $i, $value2, { TYPE => $datatype } )
Branch: 1 branch
BranchTrueFalseTotal
$self->bind_param($i, $value2, {"TYPE", $datatype}) or croak($DBI::errstr) 0 0 0
193 or croak($DBI::errstr);
194 }
195 0 return $self;
196 }
197  
198 1 1;
199 __END__
↑ Summary