DBIx/Class/Storage/DBI/NoBindVars.pm

filter: Statementstmt 100% 33 / 33 Branchbran 75.0% 6 / 8 Conditioncond 58.3% 7 / 12 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 84.0% 58 / 69 CC 10 SCAR 14.3
CC
10
Cov
87%
_prep_for_execute
6.1
interpolate_unquoted
5.7
BEGIN
1.0
CRAP 4.2
« DBIx/Class/Storage/DBI/ADO/MS_Jet.pm ↑ Summary DBIx/Class/Storage/DBI/Informix.pm »
1 package DBIx::Class::Storage::DBI::NoBindVars;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use base 'DBIx::Class::Storage::DBI';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 DBIx::Class::Storage::DBI::NoBindVars - Sometime DBDs have poor to no support for bind variables
12  
13 =head1 DESCRIPTION
14  
15 This class allows queries to work when the DBD or underlying library does not
16 support the usual C<?> placeholders, or at least doesn't support them very
17 well, as is the case with L<DBD::Sybase>
18  
19 =head1 METHODS
20  
21 =head2 connect_info
22  
23 We can't cache very effectively without bind variables, so force the C<disable_sth_caching> setting to be turned on when the connect info is set.
24  
25 =cut
26  
27 1 ▶sub connect_info {
Subroutine connect_info: called CC=1 SCAR=0.0
Pod documented
28 1 my $self = shift;
29 1 my $retval = $self->next::method(@_);
30 1 $self->disable_sth_caching(1);
31 1 $retval;
32 }
33  
34 =head2 _prep_for_execute
35  
36 Manually subs in the values for the usual C<?> placeholders.
37  
38 =cut
39  
40 9 ▶sub _prep_for_execute {
Subroutine _prep_for_execute: called CC=6 SCAR=18.1
41 9 my $self = shift;
42  
43 9 my ($sql, $bind) = $self->next::method(@_);
44  
45 # stringify bind args, quote via $dbh, and manually insert
46 #my ($op, $ident, $args) = @_;
47 9 my $ident = $_[1];
48  
49 9 my @sql_part = split /\?/, $sql;
50 9 my $new_sql;
51  
52 9 for (@$bind) {
53 9 ▶ my $data = (ref $_->[1]) ? "$_->[1]" : $_->[1]; # always stringify, array types are currently not supported
Branch: 1 branch
BranchTrueFalseTotal
ref $_->[1] ? : 0 9 9
54  
55 9 my $datatype = $_->[0]{sqlt_datatype};
56  
57 9 ▶ $data = $self->_prep_interpolated_value($datatype, $data)
Branch: 1 branch
BranchTrueFalseTotal
if $datatype 9 0 9
58 if $datatype;
59  
60 9 ▶ $data = $self->_get_dbh->quote($data)
Branch: 1 branch
BranchTrueFalseTotal
unless $datatype and $self->interpolate_unquoted($datatype, $data) 7 2 9
Condition66%2/3
$datatype and $self->interpolate_unquoted($datatype, $data)
!ll&&!rl&&r
072
MC/DC50%1/2
$datatype and $self->interpolate_unquoted($datatype, $data)
$datatype $self->interpolate_unquoted($datatype, $data)
Truth table66%2/3
A and B
A= $datatypeB= $self->interpolate_unquoted($datatype, $data)
ABresult
0X0
100
111
61 unless ($datatype and $self->interpolate_unquoted($datatype, $data) );
62  
63 9 $new_sql .= shift(@sql_part) . $data;
64 }
65  
66 9 $new_sql .= join '', @sql_part;
67  
68 9 return ($new_sql, []);
69 }
70  
71 =head2 interpolate_unquoted
72  
73 This method is called by L</_prep_for_execute> for every column in
74 order to determine if its value should be quoted or not. The arguments
75 are the current column data type and the actual bind value. The return
76 value is interpreted as: true - do not quote, false - do quote. You should
77 override this in you Storage::DBI::<database> subclass, if your RDBMS
78 does not like quotes around certain datatypes (e.g. Sybase and integer
79 columns). The default method returns false, except for integer datatypes
80 paired with values containing nothing but digits.
81  
82 WARNING!!!
83  
84 Always validate that the bind-value is valid for the current datatype.
85 Otherwise you may very well open the door to SQL injection attacks.
86  
87 =cut
88  
89 9 ▶sub interpolate_unquoted {
Subroutine interpolate_unquoted: called CC=5 SCAR=17.5
Pod documented
90 #my ($self, $datatype, $value) = @_;
91  
92 9 ▶ 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 7 9
Condition55%5/9
defined $_[2] and $_[1] and not $_[2] =~ /\D/ and $_[1] =~ /int(?:eger)? | (?:tiny|small|medium|big)int/xi
!ll&&!rl&&r
702
defined $_[2] and $_[1] and not $_[2] =~ /\D/
!ll&&!rl&&r
072
defined $_[2] and $_[1]
!ll&&!rl&&r
009
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
93 defined $_[2]
94 and
95 $_[1]
96 and
97 $_[2] !~ /\D/
98 and
99 $_[1] =~ /int(?:eger)? | (?:tiny|small|medium|big)int/ix
100 );
101  
102 7 return 0;
103 }
104  
105 =head2 _prep_interpolated_value
106  
107 Given a datatype and the value to be inserted directly into a SQL query, returns
108 the necessary string to represent that value (by e.g. adding a '$' sign)
109  
110 =cut
111  
112 9 ▶sub _prep_interpolated_value {
Subroutine _prep_interpolated_value: called CC=1 SCAR=0.0
113 #my ($self, $datatype, $value) = @_;
114 9 return $_[2];
115 }
116  
117 =head1 FURTHER QUESTIONS?
118  
119 Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
120  
121 =head1 COPYRIGHT AND LICENSE
122  
123 This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
124 by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
125 redistribute it and/or modify it under the same terms as the
126 L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.
127  
128 =cut
129  
130 3 1;
« DBIx/Class/Storage/DBI/ADO/MS_Jet.pm ↑ Summary DBIx/Class/Storage/DBI/Informix.pm »