ObjectDriver/SQL.pm

filter: Statementstmt 87.4% 237 / 271 Branchbran 77.4% 96 / 124 Conditioncond 56.2% 45 / 80 MC/DCmcdc 31.2% 15 / 48 Subroutinesub 91.3% 21 / 23 Podpod 100% 15 / 15 totaltotal 76.4% 429 / 561 CC 96 SCAR 28.0
CC
96
Cov
80%
_mk_term
33.3
as_sql
23.8
_parse_array_terms
14.2
CRAP 16.5
« ObjectDriver/BaseObject.pm ↑ Summary ObjectDriver.pm »
1 # $Id$
2  
3 package Data::ObjectDriver::SQL;
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use base qw( Class::Accessor::Fast );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 __PACKAGE__->mk_accessors(qw(
11 select distinct select_map select_map_reverse
12 from joins where bind limit offset group order
13 having where_values column_mutator index_hint
14 comment as
15 ));
16  
17 53 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
18 53 my $class = shift;
19 53 my $stmt = $class->SUPER::new(@_);
20 53 $stmt->select([]);
21 53 $stmt->distinct(0);
22 53 $stmt->select_map({});
23 53 $stmt->select_map_reverse({});
24 53 $stmt->bind([]);
25 53 $stmt->from([]);
26 53 $stmt->where([]);
27 53 $stmt->where_values({});
28 53 $stmt->having([]);
29 53 $stmt->joins([]);
30 53 $stmt->index_hint({});
31 53 $stmt;
32 }
33  
34 13 ▶sub add_select {
Subroutine add_select: called CC=6 SCAR=24.0
Pod documented
35 13 my $stmt = shift;
36 13 my($term, $col) = @_;
37 13 push @{ $stmt->select }, $term;
38 13 ▶ if (blessed($term) && $term->isa('Data::ObjectDriver::SQL')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($term) and $term->isa('Data::ObjectDriver::SQL')) { } 0 13 13
Condition33%1/3
builtin::blessed($term) and $term->isa("Data::ObjectDriver::SQL")
!ll&&!rl&&r
1300
MC/DC0%0/2
builtin::blessed($term) and $term->isa("Data::ObjectDriver::SQL")
builtin::blessed($term) $term->isa("Data::ObjectDriver::SQL")
Truth table33%1/3
A and B
A= builtin::blessed($term)B= $term->isa("Data::ObjectDriver::SQL")
ABresult
0X0
100
111
39 0 ▶ my $alias = $col || $term->as || $term->as_sql;
Condition0%0/6
$col || $term->as || $term->as_sql
l!l&&r!l&&!r
000
$col or $term->as
l!l&&r!l&&!r
000
MC/DC0%0/3
$col || $term->as || $term->as_sql
$col $term->as $term->as_sql
Truth table0%0/4
A or B || C
A= $colB= $term->asC= $term->as_sql
ABCresult
1XX1
01X1
0011
0000
40 0 $stmt->select_map->{$term} = $alias;
41 0 $stmt->select_map_reverse->{$alias} = $term;
42 } else {
43 13 ▶ $col ||= $term;
Condition66%2/3
$col ||= $term
l!l&&r!l&&!r
1030
MC/DC0%0/2
$col ||= $term
$col $term
Truth table66%2/3
A ||= B
A= $colB= $term
ABresult
1X1
011
000
44 13 $stmt->select_map->{$term} = $col;
45 13 $stmt->select_map_reverse->{$col} = $term;
46 }
47 }
48  
49 8 ▶sub add_join {
Subroutine add_join: called CC=2 SCAR=6.9
Pod documented
50 8 my $stmt = shift;
51 8 my($table, $joins) = @_;
52 8 ▶ push @{ $stmt->joins }, {
Branch: 1 branch
BranchTrueFalseTotal
ref $joins eq 'ARRAY' ? : 5 3 8
53 table => $table,
54 joins => ref($joins) eq 'ARRAY' ? $joins : [ $joins ],
55 };
56 }
57  
58 1 ▶sub add_index_hint {
Subroutine add_index_hint: called CC=3 SCAR=11.7
Pod documented
59 1 my $stmt = shift;
60 1 my($table, $hint) = @_;
61 $stmt->index_hint->{$table} = {
62 type => $hint->{type} || 'USE',
63 1 ▶ list => ref($hint->{list}) eq 'ARRAY' ? $hint->{list} : [ $hint->{list} ],
Branch: 1 branch
BranchTrueFalseTotal
ref $hint->{'list'} eq 'ARRAY' ? : 1 0 1
Condition50%1/2
$hint->{'type'} || 'USE'
l!l
10
MC/DC0%0/1
$hint->{'type'} || 'USE'
$hint->{'type'}
Truth table50%1/2
A
A= $hint->{'type'}
Aresult
11
00
64 };
65 }
66  
67 32 ▶sub as_sql {
Subroutine as_sql: called CC=20 SCAR=31.7
Pod documented
68 32 my $stmt = shift;
69 32 my $sql = '';
70 32 my @bind_for_select;
71  
72 32 ▶ if (@{ $stmt->select }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$stmt->select;}) 14 18 32
73 14 $sql .= 'SELECT ';
74 14 ▶ $sql .= 'DISTINCT ' if $stmt->distinct;
Branch: 1 branch
BranchTrueFalseTotal
if $stmt->distinct 1 13 14
75 14 my $select_map = $stmt->select_map;
76 $sql .= join(', ', map {
77 19 my $col = $_;
78 19 my $alias = $select_map->{$col};
79 19 ▶ if (blessed($col) && $col->isa('Data::ObjectDriver::SQL')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($col) and $col->isa('Data::ObjectDriver::SQL')) { } 0 19 19
Condition33%1/3
builtin::blessed($col) and $col->isa("Data::ObjectDriver::SQL")
!ll&&!rl&&r
1900
MC/DC0%0/2
builtin::blessed($col) and $col->isa("Data::ObjectDriver::SQL")
builtin::blessed($col) $col->isa("Data::ObjectDriver::SQL")
Truth table33%1/3
A and B
A= builtin::blessed($col)B= $col->isa("Data::ObjectDriver::SQL")
ABresult
0X0
100
111
80 0 push @bind_for_select, @{ $col->{bind} };
81 0 @{ $col->{bind} } = ();
82 0 $col->as_subquery($alias);
83 } else {
84 19 ▶ if ($alias) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alias) { } 19 0 19
85 19 ▶ /(?:^|\.)\Q$alias\E$/ ? $col : "$col $alias";
Branch: 1 branch
BranchTrueFalseTotal
/(?:^|\.)\Q$alias\E$/ ? : 15 4 19
86 } else {
87 0 $col;
88 }
89 }
90 14 } @{ $stmt->select }) . "\n";
91 }
92 32 $sql .= 'FROM ';
93  
94 ## Add any explicit JOIN statements before the non-joined tables.
95 32 my %joined;
96 32 ▶ my @from = @{ $stmt->from || [] };
Condition50%1/2
$stmt->from || []
l!l
320
MC/DC0%0/1
$stmt->from || []
$stmt->from
Truth table50%1/2
A
A= $stmt->from
Aresult
11
00
97 32 ▶ if ($stmt->joins && @{ $stmt->joins }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stmt->joins and @{$stmt->joins;}) 8 24 32
Condition66%2/3
$stmt->joins and @{$stmt->joins;}
!ll&&!rl&&r
0248
MC/DC50%1/2
$stmt->joins and @{$stmt->joins;}
$stmt->joins @{$stmt->joins;}
Truth table66%2/3
A and B
A= $stmt->joinsB= @{$stmt->joins;}
ABresult
0X0
100
111
98 8 my $initial_table_written = 0;
99 8 for my $j (@{ $stmt->joins }) {
100 11 my($table, $joins) = map { $j->{$_} } qw( table joins );
101 11 $table = $stmt->_add_index_hint($table); ## index hint handling
102 11 ▶ $sql .= $table unless $initial_table_written++;
Branch: 1 branch
BranchTrueFalseTotal
unless $initial_table_written++ 8 3 11
103 11 $joined{$table}++;
104 11 for my $join (@{ $j->{joins} }) {
105 13 $joined{$join->{table}}++;
106 $sql .= ' ' .
107 uc($join->{type}) . ' JOIN ' . $join->{table} . ' ON ' .
108 13 $join->{condition};
109 }
110 }
111 8 @from = grep { ! $joined{ $_ } } @from;
112 8 ▶ $sql .= ', ' if @from;
Branch: 1 branch
BranchTrueFalseTotal
if @from 1 7 8
113 }
114  
115 32 my @bind_for_from;
116  
117 32 ▶ if (@from) {
Branch: 1 branch
BranchTrueFalseTotal
if (@from) 25 7 32
118 $sql .= join ', ', map {
119 25 my $from = $_;
120 26 ▶ if (blessed($from) && $from->isa('Data::ObjectDriver::SQL')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($from) and $from->isa('Data::ObjectDriver::SQL')) { } 0 26 26
Condition33%1/3
builtin::blessed($from) and $from->isa("Data::ObjectDriver::SQL")
!ll&&!rl&&r
2600
MC/DC0%0/2
builtin::blessed($from) and $from->isa("Data::ObjectDriver::SQL")
builtin::blessed($from) $from->isa("Data::ObjectDriver::SQL")
Truth table33%1/3
A and B
A= builtin::blessed($from)B= $from->isa("Data::ObjectDriver::SQL")
ABresult
0X0
100
111
121 0 push @bind_for_from, @{$from->{bind}};
122 0 @{$from->{bind}} = ();
123 0 $from->as_subquery;
124 } else {
125 26 $stmt->_add_index_hint($from);
126 }
127 } @from;
128 }
129  
130 32 $sql .= "\n";
131 32 $sql .= $stmt->as_sql_where;
132  
133 32 $sql .= $stmt->as_aggregate('group');
134 32 $sql .= $stmt->as_sql_having;
135 32 $sql .= $stmt->as_aggregate('order');
136  
137 32 $sql .= $stmt->as_limit;
138 31 my $comment = $stmt->comment;
139 31 ▶ if ($comment && $comment =~ /([ 0-9a-zA-Z.:;()_#&,]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comment and $comment =~ /([ 0-9a-zA-Z.:;()_#&,]+)/) 3 28 31
Condition66%2/3
$comment and $comment =~ /([ 0-9a-zA-Z.:;()_#&,]+)/
!ll&&!rl&&r
2803
MC/DC50%1/2
$comment and $comment =~ /([ 0-9a-zA-Z.:;()_#&,]+)/
$comment $comment =~ /([ 0-9a-zA-Z.:;()_#&,]+)/
Truth table66%2/3
A and B
A= $commentB= $comment =~ /([ 0-9a-zA-Z.:;()_#&,]+)/
ABresult
0X0
100
111
140 3 ▶ $sql .= "-- $1" if $1;
Branch: 1 branch
BranchTrueFalseTotal
if $1 3 0 3
141 }
142  
143 31 @{ $stmt->{bind} } = (@bind_for_select, @bind_for_from, @{ $stmt->{bind} });
144  
145 31 return $sql;
146 }
147  
148 0 ▶sub as_subquery {
Subroutine as_subquery: not called CC=3 SCAR=24.8
Pod documented
149 0 my ($stmt, $alias) = @_;
150 0 my $subquery = '(' . $stmt->as_sql . ')';
151 0 ▶ $alias ||= $stmt->as;
Condition0%0/3
$alias ||= $stmt->as
l!l&&r!l&&!r
000
MC/DC0%0/2
$alias ||= $stmt->as
$alias $stmt->as
Truth table0%0/3
A ||= B
A= $aliasB= $stmt->as
ABresult
1X1
011
000
152 0 ▶ if ($alias) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alias) 0 0 0
153 0 $subquery .= ' '. $alias;
154 }
155 0 $subquery;
156 }
157  
158 32 ▶sub as_limit {
Subroutine as_limit: called CC=4 SCAR=13.9
Pod documented
159 32 my $stmt = shift;
160 32 ▶ my $n = $stmt->limit or
Branch: 1 branch
BranchTrueFalseTotal
my $n = $stmt->limit or return "" 4 28 32
161 return '';
162 4 ▶ die "Non-numerics in limit clause ($n)" if $n =~ /\D/;
Branch: 1 branch
BranchTrueFalseTotal
if $n =~ /\D/ 1 3 4
163 3 ▶ return sprintf "LIMIT %d%s\n", $n,
Branch: 1 branch
BranchTrueFalseTotal
$stmt->offset ? : 1 2 3
164 ($stmt->offset ? " OFFSET " . int($stmt->offset) : "");
165 }
166  
167 64 ▶sub as_aggregate {
Subroutine as_aggregate: called CC=4 SCAR=13.9
Pod documented
168 64 my $stmt = shift;
169 64 my($set) = @_;
170  
171 64 ▶ if (my $attribute = $stmt->$set()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $attribute = $stmt->$set) 10 54 64
172 10 ▶ my $elements = (ref($attribute) eq 'ARRAY') ? $attribute : [ $attribute ];
Branch: 1 branch
BranchTrueFalseTotal
ref $attribute eq 'ARRAY' ? : 3 7 10
173 return uc($set) . ' BY '
174 10 ▶ . join(', ', map { $_->{column} . ($_->{desc} ? (' ' . $_->{desc}) : '') } @$elements)
Branch: 1 branch
BranchTrueFalseTotal
$_->{'desc'} ? : 8 5 13
175 . "\n";
176 }
177  
178 54 return '';
179 }
180  
181 68 ▶sub as_sql_where {
Subroutine as_sql_where: called CC=3 SCAR=11.0
Pod documented
182 68 my $stmt = shift;
183 68 $stmt->where && @{ $stmt->where } ?
184 68 ▶ 'WHERE ' . join(' AND ', @{ $stmt->where }) . "\n" :
Branch: 1 branch
BranchTrueFalseTotal
$stmt->where && @{$stmt->where;} ? : 34 34 68
Condition66%2/3
$stmt->where and @{$stmt->where;}
!ll&&!rl&&r
03434
MC/DC50%1/2
$stmt->where and @{$stmt->where;}
$stmt->where @{$stmt->where;}
Truth table66%2/3
A and B
A= $stmt->whereB= @{$stmt->where;}
ABresult
0X0
100
111
185 '';
186 }
187  
188 32 ▶sub as_sql_having {
Subroutine as_sql_having: called CC=3 SCAR=11.0
Pod documented
189 32 my $stmt = shift;
190 32 $stmt->having && @{ $stmt->having } ?
191 32 ▶ 'HAVING ' . join(' AND ', @{ $stmt->having }) . "\n" :
Branch: 1 branch
BranchTrueFalseTotal
$stmt->having && @{$stmt->having;} ? : 1 31 32
Condition66%2/3
$stmt->having and @{$stmt->having;}
!ll&&!rl&&r
0311
MC/DC50%1/2
$stmt->having and @{$stmt->having;}
$stmt->having @{$stmt->having;}
Truth table66%2/3
A and B
A= $stmt->havingB= @{$stmt->having;}
ABresult
0X0
100
111
192 '';
193 }
194  
195 6 ▶sub as_escape {
Subroutine as_escape: called CC=3 SCAR=11.0
Pod documented
196 6 my ($stmt, $escape_char) = @_;
197  
198 # escape_char can be ''(two quotes), or \\ for mysql and \ for others, but it doesn't accept any injections.
199 6 ▶ die 'escape_char length must be up to two characters' if defined($escape_char) && length($escape_char) > 2;
Branch: 1 branch
BranchTrueFalseTotal
if defined $escape_char and length $escape_char > 2 1 5 6
Condition66%2/3
defined $escape_char and length $escape_char > 2
!ll&&!rl&&r
051
MC/DC50%1/2
defined $escape_char and length $escape_char > 2
defined $escape_char length $escape_char > 2
Truth table66%2/3
A and B
A= defined $escape_charB= length $escape_char > 2
ABresult
0X0
100
111
200  
201 5 return " ESCAPE '$escape_char'";
202 }
203  
204 30 ▶sub add_where {
Subroutine add_where: called CC=2 SCAR=6.9
Pod documented
205 30 my $stmt = shift;
206 ## xxx Need to support old range and transform behaviors.
207 30 my($col, $val) = @_;
208 30 ▶ Carp::croak("Invalid/unsafe column name $col") unless $col =~ /^[\w\.]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $col =~ /^[\w\.]+$/ 0 30 30
209 30 my($term, $bind, $tcol) = $stmt->_mk_term($col, $val);
210 29 push @{ $stmt->{where} }, "($term)";
211 29 push @{ $stmt->{bind} }, @$bind;
212 29 $stmt->where_values->{$tcol} = $val;
213 }
214  
215 9 ▶sub add_complex_where {
Subroutine add_complex_where: called CC=2 SCAR=6.9
Pod documented
216 9 my $stmt = shift;
217 9 my ($terms) = @_;
218 9 my ($where, $bind) = $stmt->_parse_array_terms($terms);
219 9 ▶ if ($where) {
Branch: 1 branch
BranchTrueFalseTotal
if ($where) 6 3 9
220 6 push @{ $stmt->{where} }, $where;
221 6 push @{ $stmt->{bind} }, @$bind;
222 }
223 }
224  
225 11 ▶sub _parse_array_terms {
Subroutine _parse_array_terms: called CC=12 SCAR=26.6
226 11 my $stmt = shift;
227 11 my ($term_list) = @_;
228  
229 11 my @out;
230 11 my $logic = 'AND';
231 11 my @bind;
232 11 foreach my $t ( @$term_list ) {
233 13 ▶ if (! ref $t ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $t) 0 13 13
234 0 ▶ $logic = $1 if uc($t) =~ m/^-?(OR|AND|OR_NOT|AND_NOT)$/;
Branch: 1 branch
BranchTrueFalseTotal
if uc($t) =~ /^-?(OR|AND|OR_NOT|AND_NOT)$/ 0 0 0
235 0 $logic =~ s/_/ /;
236 0 next;
237 }
238 13 my $out;
239 13 ▶ if (ref $t eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $t eq 'HASH') { } 11 2 13
elsif (ref $t eq 'ARRAY') { } 2 0 2
240 # bag of terms to apply $logic with
241 11 my @out;
242 11 foreach my $t2 ( keys %$t ) {
243 10 my ($term, $bind, $col) = $stmt->_mk_term($t2, $t->{$t2});
244 10 $stmt->where_values->{$col} = $t->{$t2};
245 10 ▶ if ($term) {
Branch: 1 branch
BranchTrueFalseTotal
if ($term) 10 0 10
246 10 push @out, "($term)";
247 10 push @bind, @$bind;
248 }
249 }
250 11 ▶ $out .= '(' . join(" AND ", @out) . ")" if @out;
Branch: 1 branch
BranchTrueFalseTotal
if @out 9 2 11
251 }
252 elsif (ref $t eq 'ARRAY') {
253 # another array of terms to process!
254 2 my ($where, $bind) = $stmt->_parse_array_terms( $t );
255 2 ▶ if ($where) {
Branch: 1 branch
BranchTrueFalseTotal
if ($where) 0 2 2
256 0 push @bind, @$bind;
257 0 $out = '(' . $where . ')';
258 }
259 }
260 13 ▶ push @out, (@out ? ' ' . $logic . ' ' : '') . $out if $out;
Branch: 2 branches
BranchTrueFalseTotal
if $out 9 4 13
@out ? : 3 6 9
261 }
262 11 return (join("", @out), \@bind);
263 }
264  
265 0 ▶sub has_where {
Subroutine has_where: not called CC=1 SCAR=6.9
Pod documented
266 0 my $stmt = shift;
267 0 my($col, $val) = @_;
268  
269 # TODO: should check if the value is same with $val?
270 0 exists $stmt->where_values->{$col};
271 }
272  
273 1 ▶sub add_having {
Subroutine add_having: called CC=4 SCAR=14.4
Pod documented
274 1 my $stmt = shift;
275 1 my($col, $val) = @_;
276 # Carp::croak("Invalid/unsafe column name $col") unless $col =~ /^[\w\.]+$/;
277  
278 1 ▶ if (my $orig = $stmt->select_map_reverse->{$col}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $orig = $stmt->select_map_reverse->{$col}) 1 0 1
279 1 ▶ if (blessed($orig) && $orig->isa('Data::ObjectDriver::SQL')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($orig) and $orig->isa('Data::ObjectDriver::SQL')) { } 0 1 1
Condition33%1/3
builtin::blessed($orig) and $orig->isa("Data::ObjectDriver::SQL")
!ll&&!rl&&r
100
MC/DC0%0/2
builtin::blessed($orig) and $orig->isa("Data::ObjectDriver::SQL")
builtin::blessed($orig) $orig->isa("Data::ObjectDriver::SQL")
Truth table33%1/3
A and B
A= builtin::blessed($orig)B= $orig->isa("Data::ObjectDriver::SQL")
ABresult
0X0
100
111
280 # do nothins
281 } else {
282 1 $col = $orig;
283 }
284 }
285  
286 1 my($term, $bind) = $stmt->_mk_term($col, $val);
287 1 push @{ $stmt->{having} }, "($term)";
288 1 push @{ $stmt->{bind} }, @$bind;
289 }
290  
291 62 ▶sub _mk_term {
Subroutine _mk_term: called CC=31 SCAR=35.1
292 62 my $stmt = shift;
293 62 my($col, $val) = @_;
294 62 my $term = '';
295 62 my (@bind, $m);
296 62 ▶ if (ref($val) eq 'ARRAY') {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $val eq 'ARRAY') { } 11 51 62
elsif (ref $val eq 'HASH') { } 20 31 51
elsif (ref $val eq 'SCALAR') { } 9 22 31
elsif (ref $val eq 'REF') { } 1 21 22
297 11 ▶ if (ref $val->[0] or (($val->[0] || '') eq '-and')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val->[0] or ($val->[0] || '') eq '-and') { } 9 2 11
Condition100%5/5
ref $val->[0] or ($val->[0] || "") eq "-and"
l!l&&r!l&&!r
452
$val->[0] || ''
l!l
61
MC/DC100%2/2
ref $val->[0] or ($val->[0] || "") eq "-and"
ref $val->[0] $val->[0]
Truth table100%3/3
A or B
A= ref $val->[0]B= $val->[0]
ABresult
1X1
011
000
298 9 my $logic = 'OR';
299 9 my @values = @$val;
300 9 ▶ if ($val->[0] eq '-and') {
Branch: 1 branch
BranchTrueFalseTotal
if ($val->[0] eq "-and") 5 4 9
301 5 $logic = 'AND';
302 5 shift @values;
303 }
304  
305 9 my @terms;
306 9 for my $v (@values) {
307 21 my($term, $bind) = $stmt->_mk_term($col, $v);
308 21 push @terms, "($term)";
309 21 push @bind, @$bind;
310 }
311 9 $term = join " $logic ", @terms;
312 } else {
313 2 ▶ $col = $m->($col) if $m = $stmt->column_mutator;
Branch: 1 branch
BranchTrueFalseTotal
if $m = $stmt->column_mutator 0 2 2
314 2 $term = $stmt->_mk_term_arrayref($col, 'IN', $val);
315 2 @bind = @$val;
316 }
317 } elsif (ref($val) eq 'HASH') {
318 20 ▶ my $c = $val->{column} || $col;
Condition66%2/3
$val->{'column'} || $col
l!l&&r!l&&!r
1190
MC/DC0%0/2
$val->{'column'} || $col
$val->{'column'} $col
Truth table66%2/3
A || B
A= $val->{'column'}B= $col
ABresult
1X1
011
000
319 20 ▶ $c = $m->($c) if $m = $stmt->column_mutator;
Branch: 1 branch
BranchTrueFalseTotal
if $m = $stmt->column_mutator 0 20 20
320 20 my $op = uc $val->{op};
321 20 ▶ if (($op eq 'IN' or $op eq 'NOT IN') and ref $val->{value} eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if ($op eq 'IN' || $op eq 'NOT IN' and ref $val->{'value'} eq 'ARRAY') { } 2 18 20
elsif ($op eq 'IN' || $op eq 'NOT IN' and ref $val->{'value'} eq 'REF') { } 1 17 18
elsif ($op eq 'BETWEEN' and ref $val->{'value'} eq 'ARRAY') { } 1 16 17
Condition100%6/6
$op eq "IN" || $op eq "NOT IN" and ref $val->{"value"} eq "ARRAY"
!ll&&!rl&&r
1622
$op eq "IN" or $op eq "NOT IN"
l!l&&r!l&&!r
3116
MC/DC100%3/3
$op eq "IN" || $op eq "NOT IN" and ref $val->{"value"} eq "ARRAY"
$op eq "IN" $op eq "NOT IN" ref $val->{"value"} eq "ARRAY"
Truth table80%4/5
A or B and C
A= $op eq "IN"B= $op eq "NOT IN"C= ref $val->{"value"} eq "ARRAY"
ABCresult
00X0
1X00
0100
1X11
0111
322 2 $term = $stmt->_mk_term_arrayref($c, $op, $val->{value});
323 2 push @bind, @{$val->{value}};
324 ▶ } elsif (($op eq 'IN' or $op eq 'NOT IN') and ref $val->{value} eq 'REF') {
Condition83%5/6
$op eq "IN" || $op eq "NOT IN" and ref $val->{"value"} eq "REF"
!ll&&!rl&&r
1611
$op eq "IN" or $op eq "NOT IN"
l!l&&r!l&&!r
2016
MC/DC66%2/3
$op eq "IN" || $op eq "NOT IN" and ref $val->{"value"} eq "REF"
$op eq "IN" $op eq "NOT IN" ref $val->{"value"} eq "REF"
Truth table60%3/5
A or B and C
A= $op eq "IN"B= $op eq "NOT IN"C= ref $val->{"value"} eq "REF"
ABCresult
00X0
1X00
0100
1X11
0111
325 1 my @values = @{${$val->{value}}};
326 1 $term = "$c $op (" . (shift @values) . ")";
327 1 push @bind, @values;
328 ▶ } elsif ($op eq 'BETWEEN' and ref $val->{value} eq 'ARRAY') {
Condition66%2/3
$op eq "BETWEEN" and ref $val->{"value"} eq "ARRAY"
!ll&&!rl&&r
1601
MC/DC50%1/2
$op eq "BETWEEN" and ref $val->{"value"} eq "ARRAY"
$op eq "BETWEEN" ref $val->{"value"} eq "ARRAY"
Truth table66%2/3
A and B
A= $op eq "BETWEEN"B= ref $val->{"value"} eq "ARRAY"
ABresult
0X0
100
111
329 1 ▶ Carp::croak "USAGE: foo => {op => 'BETWEEN', value => [\$a, \$b]}" if @{$val->{value}} != 2;
Branch: 1 branch
BranchTrueFalseTotal
if @{$val->{"value"};} != 2 0 1 1
330 1 $term = "$c $op ? AND ?";
331 1 push @bind, @{$val->{value}};
332 } else {
333 16 my $value = $val->{value};
334 16 ▶ if (ref $value eq 'SCALAR') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq 'SCALAR') { } 1 15 16
elsif (builtin::blessed($value) and $value->isa('Data::ObjectDriver::SQL')) { } 0 15 15
335 1 $term = "$c $val->{op} " . $$value;
336 ▶ } elsif (blessed($value) && $value->isa('Data::ObjectDriver::SQL')) {
Condition33%1/3
builtin::blessed($value) and $value->isa("Data::ObjectDriver::SQL")
!ll&&!rl&&r
1500
MC/DC0%0/2
builtin::blessed($value) and $value->isa("Data::ObjectDriver::SQL")
builtin::blessed($value) $value->isa("Data::ObjectDriver::SQL")
Truth table33%1/3
A and B
A= builtin::blessed($value)B= $value->isa("Data::ObjectDriver::SQL")
ABresult
0X0
100
111
337 0 local $value->{as} = undef;
338 0 $term = "$c $val->{op} ". $value->as_subquery;
339 0 push @bind, @{$value->{bind}};
340 } else {
341 15 $term = "$c $val->{op} ?";
342 15 ▶ $term .= $stmt->as_escape($val->{escape}) if $val->{escape} && $op =~ /^(?:NOT\s+)?I?LIKE$/;
Branch: 1 branch
BranchTrueFalseTotal
if $val->{"escape"} and $op =~ /^(?:NOT\s+)?I?LIKE$/ 6 9 15
Condition66%2/3
$val->{"escape"} and $op =~ /^(?:NOT\s+)?I?LIKE$/
!ll&&!rl&&r
906
MC/DC50%1/2
$val->{"escape"} and $op =~ /^(?:NOT\s+)?I?LIKE$/
$val->{"escape"} $op =~ /^(?:NOT\s+)?I?LIKE$/
Truth table66%2/3
A and B
A= $val->{"escape"}B= $op =~ /^(?:NOT\s+)?I?LIKE$/
ABresult
0X0
100
111
343 14 push @bind, $value;
344 }
345 }
346 } elsif (ref($val) eq 'SCALAR') {
347 9 ▶ $col = $m->($col) if $m = $stmt->column_mutator;
Branch: 1 branch
BranchTrueFalseTotal
if $m = $stmt->column_mutator 0 9 9
348 9 $term = "$col $$val";
349 } elsif (ref($val) eq 'REF') {
350 1 ▶ $col = $m->($col) if $m = $stmt->column_mutator;
Branch: 1 branch
BranchTrueFalseTotal
if $m = $stmt->column_mutator 0 1 1
351 1 my @values = @{$$val};
352 1 $term = "$col " . (shift @values);
353 1 push @bind, @values;
354 } else {
355 21 ▶ $col = $m->($col) if $m = $stmt->column_mutator;
Branch: 1 branch
BranchTrueFalseTotal
if $m = $stmt->column_mutator 0 21 21
356 21 ▶ if (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) { } 20 1 21
357 20 $term = "$col = ?";
358 20 push @bind, $val;
359 } else {
360 1 $term = "$col IS NULL";
361 }
362 }
363 61 ($term, \@bind, $col);
364 }
365  
366 4 ▶sub _mk_term_arrayref {
Subroutine _mk_term_arrayref: called CC=4 SCAR=15.2
367 4 my ($stmt, $col, $op, $val) = @_;
368 4 ▶ if (@$val) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$val) { } 3 1 4
369 3 return "$col $op (".join(',', ('?') x scalar @$val).')';
370 } else {
371 1 ▶ if ($op eq 'IN') {
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq 'IN') { } 1 0 1
elsif ($op eq 'NOT IN') { } 0 0 0
372 1 return '0 = 1';
373 } elsif ($op eq 'NOT IN') {
374 0 return '1 = 1';
375 }
376 }
377 }
378  
379 37 ▶sub _add_index_hint {
Subroutine _add_index_hint: called CC=6 SCAR=19.2
380 37 my $stmt = shift;
381 37 my ($tbl_name) = @_;
382 37 my $hint = $stmt->index_hint->{$tbl_name};
383 37 ▶ return $tbl_name unless $hint && ref($hint) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless $hint and ref $hint eq "HASH" 34 3 37
Condition66%2/3
$hint and ref $hint eq "HASH"
!ll&&!rl&&r
3403
MC/DC50%1/2
$hint and ref $hint eq "HASH"
$hint ref $hint eq "HASH"
Truth table66%2/3
A and B
A= $hintB= ref $hint eq "HASH"
ABresult
0X0
100
111
384 3 ▶ if ($hint->{list} && @{ $hint->{list} }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hint->{"list"} and @{$hint->{"list"};}) 3 0 3
Condition33%1/3
$hint->{"list"} and @{$hint->{"list"};}
!ll&&!rl&&r
003
MC/DC0%0/2
$hint->{"list"} and @{$hint->{"list"};}
$hint->{"list"} @{$hint->{"list"};}
Truth table33%1/3
A and B
A= $hint->{"list"}B= @{$hint->{"list"};}
ABresult
0X0
100
111
385 return $tbl_name . ' ' . uc($hint->{type} || 'USE') . ' INDEX (' .
386 3 ▶ join (',', @{ $hint->{list} }) .
Condition50%1/2
$hint->{'type'} || 'USE'
l!l
30
MC/DC0%0/1
$hint->{'type'} || 'USE'
$hint->{'type'}
Truth table50%1/2
A
A= $hint->{'type'}
Aresult
11
00
387 ')';
388 }
389 0 return $tbl_name;
390 }
391  
392 2 1;
393  
394 __END__
« ObjectDriver/BaseObject.pm ↑ Summary ObjectDriver.pm »