blib/lib/DBIO/Storage/DBI/NoBindVars.pm

filter: Statementstmt 100% 72 / 72 Branchbran 95.4% 21 / 22 Conditioncond 66.6% 10 / 15 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 100% 18 / 18 Podpod 100% 4 / 4 totaltotal 92.8% 129 / 139 CC 19 SCAR 12.7
CC
19
Cov
94%
_interpolate_bind_values
7.0
interpolate_unquoted
5.7
_scan_sql
4.0
CRAP 3.5
« blib/lib/DBIO/Extension.pm ↑ Summary blib/lib/DBIO/UUIDColumns.pm »
1 package DBIO::Storage::DBI::NoBindVars;
2 # ABSTRACT: Some DBDs have poor to no support for bind variables
3  
4 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 8 our $VERSION = '0.900007';
8  
9 8 ▶use base 'DBIO::Storage::DBI';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 8 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 5 ▶sub connect_info {
Subroutine connect_info: called CC=1 SCAR=0.0
Pod documented
16 5 my $self = shift;
17 5 my $retval = $self->next::method(@_);
18 5 $self->disable_sth_caching(1);
19 5 $retval;
20 }
21  
22  
23 1 ▶sub _prep_for_execute {
Subroutine _prep_for_execute: called CC=1 SCAR=0.0
24 1 my $self = shift;
25  
26 1 my ($sql, $bind) = $self->next::method(@_);
27  
28 1 return $self->_interpolate_bind_values($sql, $bind);
29 }
30  
31  
32 3 ▶sub _prep_rendered_for_execute {
Subroutine _prep_rendered_for_execute: called CC=1 SCAR=0.0
33 3 my ($self, $sql, $bind) = @_;
34  
35 3 return $self->_interpolate_bind_values($sql, $bind);
36 }
37  
38  
39 22 ▶sub _interpolate_bind_values {
Subroutine _interpolate_bind_values: called CC=7 SCAR=19.5
40 22 my ($self, $sql, $bind) = @_;
41  
42 22 my @sql_part = split_at_placeholders($sql);
43 22 my $new_sql = '';
44  
45 22 ▶ $self->throw_exception(sprintf
Branch: 1 branch
BranchTrueFalseTotal
unless $#sql_part == @$bind 3 19 22
46 'placeholder count %d does not match bind count %d',
47 $#sql_part, scalar @$bind,
48 ) unless $#sql_part == @$bind;
49  
50 19 for (@$bind) {
51 26 ▶ my $data = (ref $_->[1]) ? "$_->[1]" : $_->[1]; # always stringify, array types are currently not supported
Branch: 1 branch
BranchTrueFalseTotal
ref $_->[1] ? : 0 26 26
52  
53 26 my $datatype = $_->[0]{sqlt_datatype};
54  
55 26 ▶ $data = $self->_prep_interpolated_value($datatype, $data)
Branch: 1 branch
BranchTrueFalseTotal
if $datatype 3 23 26
56 if $datatype;
57  
58 26 ▶ $data = $self->_get_dbh->quote($data)
Branch: 1 branch
BranchTrueFalseTotal
unless $datatype and $self->interpolate_unquoted($datatype, $data) 24 2 26
Condition100%3/3
$datatype and $self->interpolate_unquoted($datatype, $data)
!ll&&!rl&&r
2312
MC/DC100%2/2
$datatype and $self->interpolate_unquoted($datatype, $data)
$datatype $self->interpolate_unquoted($datatype, $data)
Truth table100%3/3
A and B
A= $datatypeB= $self->interpolate_unquoted($datatype, $data)
ABresult
0X0
100
111
59 unless ($datatype and $self->interpolate_unquoted($datatype, $data) );
60  
61 26 $new_sql .= shift(@sql_part) . $data;
62 }
63  
64 19 $new_sql .= join '', @sql_part;
65  
66 # the bind list is empty, and remembers the statement in placeholder form
67 # and the values for the text of an error (_error_form_of_bind)
68 return ($new_sql, DBIO::Storage::DBI::NoBindVars::Interpolated->new(
69 19 $sql, [ map { $_->[1] } @$bind ],
70 ));
71 }
72  
73  
74 4 ▶sub _error_form_of_bind {
Subroutine _error_form_of_bind: called CC=2 SCAR=6.9
75 4 my ($self, $sql, $bind) = @_;
76  
77 4 my $form = DBIO::Storage::DBI::NoBindVars::Interpolated->form_of($bind);
78 4 ▶ return $form ? @$form : $self->next::method($sql, $bind);
Branch: 1 branch
BranchTrueFalseTotal
$form ? : 3 1 4
79 }
80  
81  
82 # The one scanner rule. Returns [ $is_placeholder, $text, $offset ] for every
83 # token of the statement; only a free ? is a placeholder, quoted names,
84 # literals and comments come through as text.
85 32 ▶sub _scan_sql {
Subroutine _scan_sql: called CC=4 SCAR=13.9
86 32 my ($sql) = @_;
87  
88 32 my @tok;
89 32 ▶ while ($sql =~ /\G(?:
Branch: 1 branch
BranchTrueFalseTotal
if ($sql =~ m(\G(?: ( "(?:[^"]|"")*(?:"|\z) | `(?:[^`]|``)*(?:`|\z) | '(?:[^']|'')*(?:'|\z) | \[(?:[^\]]|\]\])*\] # quoted: kept as text | --[^\n]* | /\*.*?(?:\*/|\z) ) # comment: kept as text | ( \? ) # free placeholder | ( [^"`'\[?\-/]+ | . ) # anything else ))gsx) 167 32 199
90 ( "(?:[^"]|"")*(?:"|\z) | `(?:[^`]|``)*(?:`|\z) | '(?:[^']|'')*(?:'|\z)
91 | \[(?:[^\]]|\]\])*\] # quoted: kept as text
92 | --[^\n]* | \/\*.*?(?:\*\/|\z) ) # comment: kept as text
93 | ( \? ) # free placeholder
94 | ( [^"`'\[?\-\/]+ | . ) # anything else
95 )/gxs) {
96 167 my $off = $-[0];
97 167 ▶ if (defined $1) { push @tok, [ 0, $1, $off ] }
Branch: 2 branches
BranchTrueFalseTotal
if (defined $1) { } 40 127 167
elsif (defined $2) { } 41 86 127
98 41 elsif (defined $2) { push @tok, [ 1, $2, $off ] }
99 86 else { push @tok, [ 0, $3, $off ] }
100 }
101 32 return @tok;
102 }
103  
104 28 ▶sub split_at_placeholders {
Subroutine split_at_placeholders: called CC=3 SCAR=11.0
Pod documented
105 28 my @part = ('');
106 28 for my $tok (_scan_sql($_[0])) {
107 148 ▶ if ($tok->[0]) { push @part, '' }
Branch: 1 branch
BranchTrueFalseTotal
if ($tok->[0]) { } 37 111 148
108 111 else { $part[-1] .= $tok->[1] }
109 }
110 28 return @part;
111 }
112  
113  
114 4 ▶sub placeholder_offsets {
Subroutine placeholder_offsets: called CC=1 SCAR=0.0
Pod documented
115 4 return map { $_->[2] } grep { $_->[0] } _scan_sql($_[0]);
116 }
117  
118  
119 3 ▶sub interpolate_unquoted {
Subroutine interpolate_unquoted: called CC=5 SCAR=17.5
Pod documented
120 #my ($self, $datatype, $value) = @_;
121  
122 3 ▶ return 1 if (
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[2] and $_[1] and not $_[2] =~ /\D/ and $_[1] =~ /int(?:eger)? | (?:tiny|small|medium|big)int/xi 2 1 3
Condition55%5/9
defined $_[2] and $_[1] and not $_[2] =~ /\D/ and $_[1] =~ /int(?:eger)? | (?:tiny|small|medium|big)int/xi
!ll&&!rl&&r
102
defined $_[2] and $_[1] and not $_[2] =~ /\D/
!ll&&!rl&&r
012
defined $_[2] and $_[1]
!ll&&!rl&&r
003
MC/DC25%1/4
defined $_[2] and $_[1] and not $_[2] =~ /\D/ and $_[1] =~ /int(?:eger)? | (?:tiny|small|medium|big)int/xi
defined $_[2] $_[1] not $_[2] =~ /\D/ $_[1] =~ /int(?:eger)? | (?:tiny|small|medium|big)int/xi
Truth table40%2/5
A and B and not(C) and D
A= defined $_[2]B= $_[1]C= not $_[2] =~ /\D/D= $_[1] =~ /int(?:eger)? | (?:tiny|small|medium|big)int/xi
ABCDresult
0XXX0
10XX0
110X0
11100
11111
123 defined $_[2]
124 and
125 $_[1]
126 and
127 $_[2] !~ /\D/
128 and
129 $_[1] =~ /int(?:eger)? | (?:tiny|small|medium|big)int/ix
130 );
131  
132 1 return 0;
133 }
134  
135  
136 3 ▶sub _prep_interpolated_value {
Subroutine _prep_interpolated_value: called CC=1 SCAR=0.0
137 #my ($self, $datatype, $value) = @_;
138 3 return $_[2];
139 }
140  
141 # The empty bind list NoBindVars hands on. It behaves as the empty array and
142 # carries, apart from its own slots, the statement in placeholder form and the
143 # values that were written into the statement (inside-out, keyed by address).
144 package DBIO::Storage::DBI::NoBindVars::Interpolated;
145  
146 8 my %form;
147  
148 19 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
149 19 my ($class, $sql, $values) = @_;
150 19 my $self = bless [], $class;
151 19 $form{ Scalar::Util::refaddr($self) } = [ $sql, $values ];
152 19 return $self;
153 }
154  
155 4 ▶sub form_of {
Subroutine form_of: called CC=3 SCAR=11.0
156 4 my ($class, $bind) = @_;
157 4 ▶ return undef unless Scalar::Util::blessed($bind) and $bind->isa($class);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($bind) and $bind->isa($class) 1 3 4
Condition66%2/3
builtin::blessed($bind) and $bind->isa($class)
!ll&&!rl&&r
103
MC/DC50%1/2
builtin::blessed($bind) and $bind->isa($class)
builtin::blessed($bind) $bind->isa($class)
Truth table66%2/3
A and B
A= builtin::blessed($bind)B= $bind->isa($class)
ABresult
0X0
100
111
158 3 return $form{ Scalar::Util::refaddr($bind) };
159 }
160  
161 19 ▶sub DESTROY { delete $form{ Scalar::Util::refaddr($_[0]) } }
Subroutine DESTROY: called CC=1 SCAR=0.0
162  
163 8 1;
164  
165 __END__
« blib/lib/DBIO/Extension.pm ↑ Summary blib/lib/DBIO/UUIDColumns.pm »