blib/lib/DBIx/DBO/DBD.pm

filter: Statementstmt 92.3% 435 / 471 Branchbran 77.6% 222 / 286 Conditioncond 71.1% 74 / 104 MC/DCmcdc 50.9% 26 / 51 Subroutinesub 95.1% 59 / 62 Podpod n/a totaltotal 83.7% 816 / 974 SCAR 57.6
CC
191
Cov
85%
_get_table_schema
56.0
_parse_val
23.4
_fast_bulk_insert
20.0
CRAP 316.6
« blib/lib/DBIx/DBO/Query.pm ↑ Summary blib/lib/DBIx/DBO/Row.pm »
1 package # hide from PAUSE
2 DBIx::DBO::DBD;
3  
4 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use constant PLACEHOLDER => "\x{b1}\x{a4}\x{221e}";
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 12 our @CARP_NOT = qw(DBIx::DBO DBIx::DBO::DBD DBIx::DBO::Table DBIx::DBO::Query DBIx::DBO::Row);
11 12 *DBIx::DBO::CARP_NOT = \@CARP_NOT;
12 12 *DBIx::DBO::Table::CARP_NOT = \@CARP_NOT;
13 12 *DBIx::DBO::Query::CARP_NOT = \@CARP_NOT;
14 12 *DBIx::DBO::Row::CARP_NOT = \@CARP_NOT;
15  
16 12 our $placeholder = PLACEHOLDER;
17 12 $placeholder = qr/\Q$placeholder/;
18  
19 sub _isa {
20 2071 ▶ my($me, @class) = @_;
Subroutine _isa: called CC=4 SCAR=13.9
21 2071 ▶ if (blessed $me) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($me)) 823 1248 2071
22 823 ▶ $me->isa($_) and return 1 for @class;
Branch: 1 branch
BranchTrueFalseTotal
$me->isa($_) and return 1 402 422 824
23 }
24 }
25  
26 sub _init_dbo {
27 13 ▶ my($class, $me) = @_;
Subroutine _init_dbo: called CC=1 SCAR=0.0
28 13 return $me;
29 }
30  
31 sub _get_table_schema {
32 0 ▶ my($class, $me, $schema, $table) = @_;
Subroutine _get_table_schema: not called CC=7 SCAR=40.3
33  
34 0 my $q_schema = $schema;
35 0 my $q_table = $table;
36 0 ▶ $q_schema =~ s/([\\_%])/\\$1/g if defined $q_schema;
Branch: 1 branch
BranchTrueFalseTotal
if defined $q_schema 0 0 0
37 0 $q_table =~ s/([\\_%])/\\$1/g;
38  
39 # First try just these types
40 0 my $info = $me->rdbh->table_info(undef, $q_schema, $q_table,
41 'TABLE,VIEW,GLOBAL TEMPORARY,LOCAL TEMPORARY,SYSTEM TABLE')->fetchall_arrayref;
42 # Then if we found nothing, try any type
43 0 ▶ $info = $me->rdbh->table_info(undef, $q_schema, $q_table)->fetchall_arrayref if $info and @$info == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $info and @$info == 0 0 0 0
Condition0%0/3
$info and @$info == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$info and @$info == 0
$info @$info == 0
Truth table0%0/3
A and B
A= $infoB= @$info == 0
ABresult
0X0
100
111
44 0 ▶ croak 'Invalid table: '.$class->_qi($me, $schema, $table) unless $info and @$info == 1 and $info->[0][2] eq $table;
Branch: 1 branch
BranchTrueFalseTotal
unless $info and @$info == 1 and $info->[0][2] eq $table 0 0 0
Condition0%0/6
$info and @$info == 1 and $info->[0][2] eq $table
!ll&&!rl&&r
000
$info and @$info == 1
!ll&&!rl&&r
000
MC/DC0%0/3
$info and @$info == 1 and $info->[0][2] eq $table
$info @$info == 1 $info->[0][2] eq $table
Truth table0%0/4
A and B and C
A= $infoB= @$info == 1C= $info->[0][2] eq $table
ABCresult
0XX0
10X0
1100
1111
45 0 return $info->[0][1];
46 }
47  
48 sub _get_column_info {
49 3 ▶ my($class, $me, $schema, $table) = @_;
Subroutine _get_column_info: called CC=4 SCAR=13.9
50  
51 3 my $cols = $me->rdbh->column_info(undef, $schema, $table, '%');
52 3 ▶ $cols = $cols && $cols->fetchall_arrayref({}) || [];
Condition80%4/5
$cols && $cols->fetchall_arrayref({}) || []
l!l
21
$cols and $cols->fetchall_arrayref({})
!ll&&!rl&&r
102
MC/DC50%1/2
$cols && $cols->fetchall_arrayref({}) || []
$cols $cols->fetchall_arrayref({})
Truth table66%2/3
A and B
A= $colsB= $cols->fetchall_arrayref({})
ABresult
111
0X0
100
53 3 ▶ croak 'Invalid table: '.$class->_qi($me, $schema, $table) unless @$cols;
Branch: 1 branch
BranchTrueFalseTotal
unless @$cols 1 2 3
54  
55 2 return map { $_->{COLUMN_NAME} => $_->{ORDINAL_POSITION} } @$cols;
56 }
57  
58 sub _get_table_info {
59 3 ▶ my($class, $me, $schema, $table) = @_;
Subroutine _get_table_info: called CC=2 SCAR=6.9
60  
61 3 my %h;
62 3 $h{Column_Idx} = { $class->_get_column_info($me, $schema, $table) };
63 2 $h{Columns} = [ sort { $h{Column_Idx}{$a} <=> $h{Column_Idx}{$b} } keys %{$h{Column_Idx}} ];
64  
65 2 $h{PrimaryKeys} = [];
66 2 $class->_set_table_key_info($me, $schema, $table, \%h);
67  
68 2 ▶ return $me->{TableInfo}{defined $schema ? $schema : ''}{$table} = \%h;
Branch: 1 branch
BranchTrueFalseTotal
defined $schema ? : 2 0 2
69 }
70  
71 sub _set_table_key_info {
72 2 ▶ my($class, $me, $schema, $table, $h) = @_;
Subroutine _set_table_key_info: called CC=3 SCAR=11.1
73  
74 2 ▶ if (my $sth = $me->rdbh->primary_key_info(undef, $schema, $table)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sth = $me->rdbh->primary_key_info(undef, $schema, $table)) 2 0 2
75 2 $h->{PrimaryKeys}[$_->{KEY_SEQ} - 1] = $_->{COLUMN_NAME} for @{$sth->fetchall_arrayref({})};
76 }
77 }
78  
79 sub _unquote_table {
80 18 ▶ my($class, $me, $table) = @_;
Subroutine _unquote_table: called CC=2 SCAR=7.1
81 # TODO: Better splitting of: schema.table or `schema`.`table` or "schema"."table"@"catalog" or ...
82 18 ▶ $table =~ /^(?:("|)(.+)\1\.|)("|)(.+)\3$/ or croak "Invalid table: \"$table\"";
Branch: 1 branch
BranchTrueFalseTotal
$table =~ /^(?:("|)(.+)\1\.|)("|)(.+)\3$/ or croak(qq[Invalid table: "$table"]) 0 18 18
83 18 return ($2, $4);
84 }
85  
86 sub _selectrow_array {
87 3 ▶ my($class, $me, $sql, $attr, @bind) = @_;
Subroutine _selectrow_array: called CC=1 SCAR=0.0
88 3 $class->_sql($me, $sql, @bind);
89 3 $me->rdbh->selectrow_array($sql, $attr, @bind);
90 }
91  
92 sub _selectrow_arrayref {
93 4 ▶ my($class, $me, $sql, $attr, @bind) = @_;
Subroutine _selectrow_arrayref: called CC=1 SCAR=0.0
94 4 $class->_sql($me, $sql, @bind);
95 4 $me->rdbh->selectrow_arrayref($sql, $attr, @bind);
96 }
97  
98 sub _selectrow_hashref {
99 1 ▶ my($class, $me, $sql, $attr, @bind) = @_;
Subroutine _selectrow_hashref: called CC=1 SCAR=0.0
100 1 $class->_sql($me, $sql, @bind);
101 1 $me->rdbh->selectrow_hashref($sql, $attr, @bind);
102 }
103  
104 sub _selectall_arrayref {
105 4 ▶ my($class, $me, $sql, $attr, @bind) = @_;
Subroutine _selectall_arrayref: called CC=1 SCAR=0.0
106 4 $class->_sql($me, $sql, @bind);
107 4 $me->rdbh->selectall_arrayref($sql, $attr, @bind);
108 }
109  
110 sub _selectall_hashref {
111 1 ▶ my($class, $me, $sql, $key, $attr, @bind) = @_;
Subroutine _selectall_hashref: called CC=1 SCAR=0.0
112 1 $class->_sql($me, $sql, @bind);
113 1 $me->rdbh->selectall_hashref($sql, $key, $attr, @bind);
114 }
115  
116 sub _qi {
117 206 ▶ my($class, $me, @id) = @_;
Subroutine _qi: called CC=5 SCAR=16.1
118 206 ▶ return $me->rdbh->quote_identifier(@id) if $me->config('QuoteIdentifier');
Branch: 1 branch
BranchTrueFalseTotal
if $me->config("QuoteIdentifier") 204 2 206
119 # Strip off any null/undef elements (ie schema)
120 2 ▶ shift(@id) while @id and not (defined $id[0] and length $id[0]);
Branch: 1 branch
BranchTrueFalseTotal
@id and not defined $id[0] && length $id[0] and do { shift @id } 3 2 5
Condition100%6/6
@id and not defined $id[0] && length $id[0]
!ll&&!rl&&r
113
defined $id[0] && length $id[0]
!ll&&!rl&&r
211
MC/DC100%3/3
@id and not defined $id[0] && length $id[0]
@id defined $id[0] length $id[0]
Truth table100%4/4
A and not(B && C)
A= @idB= defined $id[0]C= length $id[0]
ABCresult
0XX0
1110
10X1
1101
121 2 return join '.', @id;
122 }
123  
124 sub _sql {
125 87 ▶ my $class = shift;
Subroutine _sql: called CC=6 SCAR=29.7
126 87 my $me = shift;
127 87 ▶ if (my $hook = $me->config('HookSQL')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $hook = $me->config("HookSQL")) 87 0 87
128 87 $hook->($me, @_);
129 }
130 87 ▶ my $dbg = $me->config('DebugSQL') or return;
Branch: 1 branch
BranchTrueFalseTotal
my $dbg = $me->config("DebugSQL") or return 87 0 87
131 0 my($sql, @bind) = @_;
132  
133 0 ▶ require Carp::Heavy if eval "$Carp::VERSION < 1.12";
Branch: 1 branch
BranchTrueFalseTotal
if eval "$Carp::VERSION < 1.12" 0 0 0
134 0 my $loc = Carp::short_error_loc();
135 0 my %i = Carp::caller_info($loc);
136 0 my $trace;
137 0 ▶ if ($dbg > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbg > 1) { } 0 0 0
138 0 $trace = "\t$i{sub_name} called at $i{file} line $i{line}\n";
139 0 ▶ $trace .= "\t$i{sub_name} called at $i{file} line $i{line}\n" while %i = Carp::caller_info(++$loc);
Branch: 1 branch
BranchTrueFalseTotal
%i = Carp::caller_info(++$loc) and do { $trace .= qq[\t$i{"sub_name"} called at $i{"file"} line $i{"line"}\n] } 0 0 0
140 } else {
141 0 $trace = "\t$i{sub} called at $i{file} line $i{line}\n";
142 }
143 0 warn $sql."\n(".join(', ', map $me->rdbh->quote($_), @bind).")\n".$trace;
144 }
145  
146 sub _do {
147 20 ▶ my($class, $me, $sql, $attr, @bind) = @_;
Subroutine _do: called CC=1 SCAR=0.0
148 20 $class->_sql($me, $sql, @bind);
149 20 $me->dbh->do($sql, $attr, @bind);
150 }
151  
152 sub _build_sql_select {
153 44 ▶ my($class, $me) = @_;
Subroutine _build_sql_select: called CC=6 SCAR=17.9
154 44 my $sql = 'SELECT '.$class->_build_show($me);
155 44 $sql .= ' FROM '.$class->_build_from($me);
156 44 my $clause;
157 44 ▶ $sql .= ' WHERE '.$clause if $clause = $class->_build_where($me);
Branch: 1 branch
BranchTrueFalseTotal
if $clause = $class->_build_where($me) 23 21 44
158 44 ▶ $sql .= ' GROUP BY '.$clause if $clause = $class->_build_group($me);
Branch: 1 branch
BranchTrueFalseTotal
if $clause = $class->_build_group($me) 4 40 44
159 44 ▶ $sql .= ' HAVING '.$clause if $clause = $class->_build_having($me);
Branch: 1 branch
BranchTrueFalseTotal
if $clause = $class->_build_having($me) 2 42 44
160 44 ▶ $sql .= ' ORDER BY '.$clause if $clause = $class->_build_order($me);
Branch: 1 branch
BranchTrueFalseTotal
if $clause = $class->_build_order($me) 24 20 44
161 44 ▶ $sql .= ' '.$clause if $clause = $class->_build_limit($me);
Branch: 1 branch
BranchTrueFalseTotal
if $clause = $class->_build_limit($me) 8 36 44
162 44 return $sql;
163 }
164  
165 sub _bind_params_select {
166 52 ▶ my($class, $me) = @_;
Subroutine _bind_params_select: called CC=2 SCAR=6.9
167 52 my $h = $me->_build_data;
168 map {
169 52 ▶ exists $h->{$_} ? @{$h->{$_}} : ()
Branch: 1 branch
BranchTrueFalseTotal
exists $h->{$_} ? : 281 31 312
170 } qw(Show_Bind From_Bind Where_Bind Group_Bind Having_Bind Order_Bind);
171 }
172  
173 sub _build_sql_update {
174 6 ▶ my($class, $me, @arg) = @_;
Subroutine _build_sql_update: called CC=6 SCAR=18.4
175 6 ▶ croak 'Update is not valid with a GROUP BY clause' if $class->_build_group($me);
Branch: 1 branch
BranchTrueFalseTotal
if $class->_build_group($me) 0 6 6
176 6 ▶ croak 'Update is not valid with a HAVING clause' if $class->_build_having($me);
Branch: 1 branch
BranchTrueFalseTotal
if $class->_build_having($me) 0 6 6
177 6 my $sql = 'UPDATE '.$class->_build_from($me);
178 6 $sql .= ' SET '.$class->_build_set($me, @arg);
179 6 my $clause;
180 6 ▶ $sql .= ' WHERE '.$clause if $clause = $class->_build_where($me);
Branch: 1 branch
BranchTrueFalseTotal
if $clause = $class->_build_where($me) 5 1 6
181 6 ▶ $sql .= ' ORDER BY '.$clause if $clause = $class->_build_order($me);
Branch: 1 branch
BranchTrueFalseTotal
if $clause = $class->_build_order($me) 0 6 6
182 6 ▶ $sql .= ' '.$clause if $clause = $class->_build_limit($me);
Branch: 1 branch
BranchTrueFalseTotal
if $clause = $class->_build_limit($me) 0 6 6
183 6 $sql;
184 }
185  
186 sub _bind_params_update {
187 6 ▶ my($class, $me) = @_;
Subroutine _bind_params_update: called CC=2 SCAR=6.9
188 6 my $h = $me->_build_data;
189 map {
190 6 ▶ exists $h->{$_} ? @{$h->{$_}} : ()
Branch: 1 branch
BranchTrueFalseTotal
exists $h->{$_} ? : 21 3 24
191 } qw(From_Bind Set_Bind Where_Bind Order_Bind);
192 }
193  
194 sub _build_sql_delete {
195 1 ▶ my($class, $me) = @_;
Subroutine _build_sql_delete: called CC=5 SCAR=16.8
196 1 ▶ croak 'Delete is not valid with a GROUP BY clause' if $class->_build_group($me);
Branch: 1 branch
BranchTrueFalseTotal
if $class->_build_group($me) 0 1 1
197 1 my $sql = 'DELETE FROM '.$class->_build_from($me);
198 1 my $clause;
199 1 ▶ $sql .= ' WHERE '.$clause if $clause = $class->_build_where($me);
Branch: 1 branch
BranchTrueFalseTotal
if $clause = $class->_build_where($me) 1 0 1
200 1 ▶ $sql .= ' ORDER BY '.$clause if $clause = $class->_build_order($me);
Branch: 1 branch
BranchTrueFalseTotal
if $clause = $class->_build_order($me) 0 1 1
201 1 ▶ $sql .= ' '.$clause if $clause = $class->_build_limit($me);
Branch: 1 branch
BranchTrueFalseTotal
if $clause = $class->_build_limit($me) 0 1 1
202 1 $sql;
203 }
204  
205 sub _bind_params_delete {
206 1 ▶ my($class, $me) = @_;
Subroutine _bind_params_delete: called CC=2 SCAR=6.9
207 1 my $h = $me->_build_data;
208 map {
209 1 ▶ exists $h->{$_} ? @{$h->{$_}} : ()
Branch: 1 branch
BranchTrueFalseTotal
exists $h->{$_} ? : 2 1 3
210 } qw(From_Bind Where_Bind Order_Bind);
211 }
212  
213 sub _build_table {
214 28 ▶ my($class, $me, $t) = @_;
Subroutine _build_table: called CC=2 SCAR=6.9
215 28 my $from = $t->_from($me->{build_data});
216 28 my $alias = $me->_table_alias($t);
217 28 ▶ $alias = defined $alias ? ' '.$class->_qi($me, $alias) : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $alias ? : 12 16 28
218 28 return $from.$alias;
219 }
220  
221 sub _build_show {
222 44 ▶ my($class, $me) = @_;
Subroutine _build_show: called CC=8 SCAR=20.8
223 44 my $h = $me->_build_data;
224 44 ▶ return $h->{show} if defined $h->{show};
Branch: 1 branch
BranchTrueFalseTotal
if defined $h->{"show"} 21 23 44
225 23 ▶ my $distinct = $h->{Show_Distinct} ? 'DISTINCT ' : '';
Branch: 1 branch
BranchTrueFalseTotal
$h->{'Show_Distinct'} ? : 1 22 23
226 23 undef @{$h->{Show_Bind}};
227 23 ▶ return $h->{show} = $distinct.'*' unless @{$h->{Showing}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$h->{"Showing"};} 9 14 23
228 14 my @flds;
229 14 for my $fld (@{$h->{Showing}}) {
230 26 ▶ if (_isa($fld, 'DBIx::DBO::Table', 'DBIx::DBO::Query')) {
Branch: 1 branch
BranchTrueFalseTotal
if (_isa($fld, 'DBIx::DBO::Table', 'DBIx::DBO::Query')) { } 8 18 26
231 8 ▶ push @flds, $class->_qi($me, $me->_table_alias($fld) || $fld->{Name}).'.*';
Condition66%2/3
$me->_table_alias($fld) || $fld->{'Name'}
l!l&&r!l&&!r
440
MC/DC0%0/2
$me->_table_alias($fld) || $fld->{'Name'}
$me->_table_alias($fld) $fld->{'Name'}
Truth table66%2/3
A || B
A= $me->_table_alias($fld)B= $fld->{'Name'}
ABresult
1X1
011
000
232 } else {
233 18 ▶ $h->{_subqueries}{$fld->[0][0]} = $fld->[0][0]->sql if _isa($fld->[0][0], 'DBIx::DBO::Query');
Branch: 1 branch
BranchTrueFalseTotal
if _isa($fld->[0][0], "DBIx::DBO::Query") 0 18 18
234 18 push @flds, $class->_build_val($me, $h->{Show_Bind}, @$fld);
235 }
236 }
237 14 return $h->{show} = $distinct.join(', ', @flds);
238 }
239  
240 sub _build_from {
241 84 ▶ my($class, $me) = @_;
Subroutine _build_from: called CC=4 SCAR=13.9
242 84 my $h = $me->_build_data;
243 84 ▶ return $h->{from} if defined $h->{from};
Branch: 1 branch
BranchTrueFalseTotal
if defined $h->{"from"} 62 22 84
244 22 undef @{$h->{From_Bind}};
245 22 my @tables = $me->tables;
246 22 $h->{from} = $class->_build_table($me, $tables[0]);
247 22 ▶ for (my $i = 1; $i < @tables; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @tables) 6 22 28
248 6 $h->{from} .= $h->{Join}[$i].$class->_build_table($me, $tables[$i]);
249 $h->{from} .= ' ON '.join(' AND ', $class->_build_where_chunk($me, $h->{From_Bind}, 'OR', $h->{Join_On}[$i]))
250 6 ▶ if $h->{Join_On}[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $h->{"Join_On"}[$i] 4 2 6
251 }
252 22 return $h->{from};
253 }
254  
255 sub _parse_col_val {
256 69 ▶ my($class, $me, $col, %c) = @_;
Subroutine _parse_col_val: called CC=3 SCAR=11.0
257 69 ▶ unless (defined $c{Aliases}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $c{"Aliases"}) 54 15 69
258 54 (my $method = (caller(1))[3]) =~ s/.*:://;
259 54 $c{Aliases} = $class->_alias_preference($me, $method);
260 }
261 69 ▶ return $class->_parse_val($me, $col, Check => 'Column', %c) if ref $col;
Branch: 1 branch
BranchTrueFalseTotal
if ref $col 30 39 69
262 39 return [ $class->_parse_col($me, $col, $c{Aliases}) ];
263 }
264  
265 # In some cases column aliases can be used, but this differs by DB and where in the statement it's used.
266 # The $method is the method we were called from: (join_on|column|where|having|_del_where|order_by|group_by)
267 # This method provides a way for DBs to override the default which is always 1 except for join_on.
268 # Return values: 0 = Don't use aliases, 1 = Check aliases then columns, 2 = Check columns then aliases
269 sub _alias_preference {
270 # my($class, $me, $method) = @_;
271 55 ▶ return $_[2] eq 'join_on' ? 0 : 1;
Subroutine _alias_preference: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[2] eq 'join_on' ? : 12 43 55
272 }
273  
274 sub _valid_col {
275 61 ▶ my($class, $me, $col) = @_;
Subroutine _valid_col: called CC=4 SCAR=13.9
276 # Check if the object is an alias
277 61 ▶ return $col if $col->[0] == $me;
Branch: 1 branch
BranchTrueFalseTotal
if $col->[0] == $me 10 51 61
278 # TODO: Sub-queries
279 # Check if the column is from one of our tables
280 51 for my $tbl ($me->tables) {
281 62 ▶ return $col if $col->[0] == $tbl;
Branch: 1 branch
BranchTrueFalseTotal
if $col->[0] == $tbl 50 12 62
282 }
283 1 croak 'Invalid column, the column is from a table not included in this query';
284 }
285  
286 sub _parse_col {
287 101 ▶ my($class, $me, $col, $_check_aliases) = @_;
Subroutine _parse_col: called CC=4 SCAR=14.1
288 101 ▶ if (ref $col) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $col) 14 87 101
289 14 ▶ return $class->_valid_col($me, $col) if _isa($col, 'DBIx::DBO::Column');
Branch: 1 branch
BranchTrueFalseTotal
if _isa($col, "DBIx::DBO::Column") 14 0 14
290 0 croak 'Invalid column: '.$col;
291 }
292 # If $_check_aliases is not defined dont accept an alias
293 87 ▶ $me->_inner_col($col, $_check_aliases || 0);
Condition100%2/2
$_check_aliases || 0
l!l
3552
MC/DC100%1/1
$_check_aliases || 0
$_check_aliases
Truth table100%2/2
A
A= $_check_aliases
Aresult
11
00
294 }
295  
296 sub _build_col {
297 147 ▶ my($class, $me, $col) = @_;
Subroutine _build_col: called CC=1 SCAR=0.0
298 147 $class->_qi($me, $me->_table_alias($col->[0]), $col->[1]);
299 }
300  
301 sub _parse_val {
302 133 ▶ my($class, $me, $fld, %c) = @_;
Subroutine _parse_val: called CC=22 SCAR=31.5
303 133 ▶ $c{Check} = '' unless defined $c{Check};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c{"Check"} 79 54 133
304  
305 133 my $func;
306 my $opt;
307 133 ▶ if (ref $fld eq 'SCALAR') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $fld eq 'SCALAR') { } 15 118 133
elsif (ref $fld eq 'HASH') { } 18 100 118
elsif (_isa($fld, 'DBIx::DBO::Column')) { } 15 85 100
308 15 ▶ croak 'Invalid '.($c{Check} eq 'Column' ? 'column' : 'field').' reference (scalar ref to undef)'
Branch: 2 branches
BranchTrueFalseTotal
unless defined $$fld 0 15 15
$c{'Check'} eq 'Column' ? : 0 0 0
309 unless defined $$fld;
310 15 $func = $$fld;
311 15 $fld = [];
312 } elsif (ref $fld eq 'HASH') {
313 18 ▶ $func = $fld->{FUNC} if exists $fld->{FUNC};
Branch: 1 branch
BranchTrueFalseTotal
if exists $fld->{"FUNC"} 12 6 18
314 18 ▶ $opt->{AS} = $fld->{AS} if exists $fld->{AS};
Branch: 1 branch
BranchTrueFalseTotal
if exists $fld->{"AS"} 6 12 18
315 18 ▶ if (exists $fld->{ORDER}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $fld->{"ORDER"}) 2 16 18
316 2 ▶ croak 'Invalid ORDER, must be ASC or DESC' if $fld->{ORDER} !~ /^(A|DE)SC$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $fld->{"ORDER"} =~ /^(A|DE)SC$/i 0 2 2
317 2 $opt->{ORDER} = uc $fld->{ORDER};
318 }
319 18 ▶ $opt->{COLLATE} = $fld->{COLLATE} if exists $fld->{COLLATE};
Branch: 1 branch
BranchTrueFalseTotal
if exists $fld->{"COLLATE"} 1 17 18
320 18 ▶ if (exists $fld->{COL}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $fld->{'COL'}) { } 11 7 18
321 11 ▶ croak 'Invalid HASH containing both COL and VAL' if exists $fld->{VAL};
Branch: 1 branch
BranchTrueFalseTotal
if exists $fld->{"VAL"} 0 11 11
322 11 ▶ my @cols = ref $fld->{COL} eq 'ARRAY' ? @{$fld->{COL}} : $fld->{COL};
Branch: 1 branch
BranchTrueFalseTotal
ref $fld->{'COL'} eq 'ARRAY' ? : 1 10 11
323 11 $fld = [ map $class->_parse_col($me, $_, $c{Aliases}), @cols ];
324 } else {
325 7 ▶ $fld = exists $fld->{VAL} ? $fld->{VAL} : [];
Branch: 1 branch
BranchTrueFalseTotal
exists $fld->{'VAL'} ? : 5 2 7
326 }
327 } elsif (_isa($fld, 'DBIx::DBO::Column')) {
328 15 return [ $class->_valid_col($me, $fld) ];
329 }
330 118 ▶ $fld = [$fld] unless ref $fld eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $fld eq "ARRAY" 81 37 118
331  
332 # Swap placeholders
333 118 my $with = @$fld;
334 118 ▶ if (defined $func) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $func) { } 27 91 118
elsif ($with != 1 and $c{'Check'} ne 'Auto') { } 0 176 176
335 27 my $need = $class->_substitute_placeholders($me, $func);
336 27 ▶ croak "The number of params ($with) does not match the number of placeholders ($need)" if $need != $with;
Branch: 1 branch
BranchTrueFalseTotal
if $need != $with 1 26 27
337 ▶ } elsif ($with != 1 and $c{Check} ne 'Auto') {
Condition66%2/3
$with != 1 and $c{"Check"} ne "Auto"
!ll&&!rl&&r
8560
MC/DC0%0/2
$with != 1 and $c{"Check"} ne "Auto"
$with != 1 $c{"Check"} ne "Auto"
Truth table66%2/3
A and B
A= $with != 1B= $c{"Check"} ne "Auto"
ABresult
0X0
100
111
338 0 ▶ croak 'Invalid '.($c{Check} eq 'Column' ? 'column' : 'field')." reference (passed $with params instead of 1)";
Branch: 1 branch
BranchTrueFalseTotal
$c{'Check'} eq 'Column' ? : 0 0 0
339 }
340 117 return ($fld, $func, $opt);
341 }
342  
343 sub _substitute_placeholders {
344 27 ▶ my($class, $me) = @_;
Subroutine _substitute_placeholders: called CC=2 SCAR=6.9
345 27 my $num_placeholders = 0;
346 27 ▶ $_[2] =~ s/((?<!\\)(['"`]).*?[^\\]\2|\?)/$1 eq '?' ? (++$num_placeholders, PLACEHOLDER) : $1/eg;
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '?' ? : 14 6 20
347 27 return $num_placeholders;
348 }
349  
350 sub _build_val {
351 226 ▶ my($class, $me, $bind, $fld, $func, $opt) = @_;
Subroutine _build_val: called CC=11 SCAR=24.5
352 226 my $extra = '';
353 226 ▶ $extra .= ' COLLATE '.$me->rdbh->quote($opt->{COLLATE}) if exists $opt->{COLLATE};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opt->{"COLLATE"} 1 225 226
354 226 ▶ $extra .= ' AS '.$class->_qi($me, $opt->{AS}) if exists $opt->{AS};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opt->{"AS"} 9 217 226
355 226 ▶ $extra .= " $opt->{ORDER}" if exists $opt->{ORDER};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opt->{"ORDER"} 2 224 226
356  
357 my @ary = map {
358 226 ▶ if (!ref $_) {
Branch: 4 branches
BranchTrueFalseTotal
if (not ref $_) { } 115 99 214
elsif (_isa($_, 'DBIx::DBO::Column')) { } 97 2 99
elsif (ref $_ eq 'SCALAR') { } 2 0 2
elsif (_isa($_, 'DBIx::DBO::Query')) { } 0 0 0
359 115 push @$bind, $_;
360 115 '?';
361 } elsif (_isa($_, 'DBIx::DBO::Column')) {
362 97 $class->_build_col($me, $_);
363 } elsif (ref $_ eq 'SCALAR') {
364 2 $$_;
365 } elsif (_isa($_, 'DBIx::DBO::Query')) {
366 0 $_->_from($me->{build_data});
367 } else {
368 0 croak 'Invalid field: '.$_;
369 }
370 } @$fld;
371 226 ▶ unless (defined $func) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $func) 173 53 226
372 173 ▶ die "Number of placeholders and values don't match!" if @ary != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @ary != 1 0 173 173
373 173 return $ary[0].$extra;
374 }
375 # Add one value to @ary to make sure the number of placeholders & values match
376 53 push @ary, 'Error';
377 53 $func =~ s/$placeholder/shift @ary/ego;
378 # At this point all the values should have been used and @ary must only have 1 item!
379 53 ▶ die "Number of placeholders and values don't match!" if @ary != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @ary != 1 0 53 53
380 53 return $func.$extra;
381 }
382  
383 # Construct the WHERE clause
384 sub _build_where {
385 51 ▶ my($class, $me) = @_;
Subroutine _build_where: called CC=4 SCAR=13.9
386 51 my $h = $me->_build_data;
387 51 ▶ return $h->{where} if defined $h->{where};
Branch: 1 branch
BranchTrueFalseTotal
if defined $h->{"where"} 15 36 51
388 36 undef @{$h->{Where_Bind}};
389 36 my @where;
390 36 ▶ push @where, $class->_build_quick_where($me, $h->{Where_Bind}, @{$h->{Quick_Where}}) if exists $h->{Quick_Where};
Branch: 1 branch
BranchTrueFalseTotal
if exists $h->{"Quick_Where"} 16 20 36
391 36 ▶ push @where, $class->_build_where_chunk($me, $h->{Where_Bind}, 'OR', $h->{Where_Data}) if exists $h->{Where_Data};
Branch: 1 branch
BranchTrueFalseTotal
if exists $h->{"Where_Data"} 23 13 36
392 36 return $h->{where} = join ' AND ', @where;
393 }
394  
395 # Construct the WHERE contents of one set of parentheses
396 sub _build_where_chunk {
397 39 ▶ my($class, $me, $bind, $ag, $whs) = @_;
Subroutine _build_where_chunk: called CC=15 SCAR=27.1
398 39 my @str;
399 # Make a copy so we can hack at it
400 39 my @whs = @$whs;
401 39 ▶ while (my $wh = shift @whs) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $wh = shift @whs) 61 39 100
402 61 my @ary;
403 61 ▶ if (ref $wh->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $wh->[0]) { } 9 52 61
404 9 ▶ @ary = $class->_build_where_chunk($me, $bind, $ag eq 'OR' ? 'AND' : 'OR', $wh);
Branch: 1 branch
BranchTrueFalseTotal
$ag eq 'OR' ? : 5 4 9
405 } else {
406 52 @ary = $class->_build_where_piece($me, $bind, @$wh);
407 52 my($op, $fld, $fld_func, $fld_opt, $val, $val_func, $val_opt, $force) = @$wh;
408 # Group AND/OR'ed for same fld if $force or $op requires it
409 52 ▶ if ($ag eq ($force || _op_ag($op))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ag eq ($force || _op_ag($op))) 18 34 52
Condition100%3/3
$force || _op_ag($op)
l!l&&r!l&&!r
23812
MC/DC100%2/2
$force || _op_ag($op)
$force _op_ag($op)
Truth table100%3/3
A || B
A= $forceB= _op_ag($op)
ABresult
1X1
011
000
410 18 ▶ for (my $i = $#whs; $i >= 0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 19 18 37
411 # Right now this starts with the last @whs and works backwards
412 # It splices when the ag is the correct AND/OR and the funcs match and all flds match
413 19 ▶ next if ref $whs[$i][0] or $ag ne ($whs[$i][7] || _op_ag($whs[$i][0]));
Branch: 1 branch
BranchTrueFalseTotal
if ref $whs[$i][0] or $ag ne ($whs[$i][7] || _op_ag($whs[$i][0])) 11 8 19
Condition100%6/6
ref $whs[$i][0] or $ag ne ($whs[$i][7] || _op_ag($whs[$i][0]))
l!l&&r!l&&!r
568
$whs[$i][7] || _op_ag($whs[$i][0])
l!l&&r!l&&!r
2102
MC/DC100%2/2
ref $whs[$i][0] or $ag ne ($whs[$i][7] || _op_ag($whs[$i][0]))
ref $whs[$i][0] $ag ne ($whs[$i][7] || _op_ag($whs[$i][0]))
MC/DC100%2/2
$whs[$i][7] || _op_ag($whs[$i][0])
$whs[$i][7] _op_ag($whs[$i][0])
Truth table100%3/3
A or B
A= ref $whs[$i][0]B= $ag ne ($whs[$i][7] || _op_ag($whs[$i][0]))
ABresult
1X1
011
000
Truth table100%3/3
A || B
A= $whs[$i][7]B= _op_ag($whs[$i][0])
ABresult
1X1
011
000
414 12 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
415 8 ▶ next if $whs[$i][2] ne $fld_func;
Branch: 1 branch
BranchTrueFalseTotal
if $whs[$i][2] ne $fld_func 0 8 8
416 12 ▶ use warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
417 # next unless $fld_func ~~ $whs[$i][2];
418 8 my $l = $whs[$i][1];
419 8 ▶ next if ((ref $l eq 'ARRAY' ? "@$l" : $l) ne (ref $fld eq 'ARRAY' ? "@$fld" : $fld));
Branch: 3 branches
BranchTrueFalseTotal
if (ref $l eq "ARRAY" ? join($", @$l) : $l) ne (ref $fld eq "ARRAY" ? join($", @$fld) : $fld) 2 6 8
ref $l eq 'ARRAY' ? : 8 0 8
ref $fld eq 'ARRAY' ? : 8 0 8
420 # next unless $fld ~~ $whs[$i][1];
421 6 push @ary, $class->_build_where_piece($me, $bind, @{splice @whs, $i, 1});
422 }
423 }
424 }
425 61 ▶ push @str, @ary == 1 ? $ary[0] : '('.join(' '.$ag.' ', @ary).')';
Branch: 1 branch
BranchTrueFalseTotal
@ary == 1 ? : 47 14 61
426 }
427 39 return @str;
428 }
429  
430 sub _op_ag {
431 65 ▶ return 'OR' if $_[0] eq '=' or $_[0] eq 'IS' or $_[0] eq '<=>' or $_[0] eq 'IN' or $_[0] eq 'BETWEEN';
Subroutine _op_ag: called CC=10 SCAR=23.0
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq "=" or $_[0] eq "IS" or $_[0] eq "<=>" or $_[0] eq "IN" or $_[0] eq "BETWEEN" 4 37 41
Condition91%11/12
$_[0] eq "=" or $_[0] eq "IS" or $_[0] eq "<=>" or $_[0] eq "IN" or $_[0] eq "BETWEEN"
l!l&&r!l&&!r
26237
$_[0] eq "=" or $_[0] eq "IS" or $_[0] eq "<=>" or $_[0] eq "IN"
l!l&&r!l&&!r
24239
$_[0] eq "=" or $_[0] eq "IS" or $_[0] eq "<=>"
l!l&&r!l&&!r
24041
$_[0] eq "=" or $_[0] eq "IS"
l!l&&r!l&&!r
16841
MC/DC80%4/5
$_[0] eq "=" or $_[0] eq "IS" or $_[0] eq "<=>" or $_[0] eq "IN" or $_[0] eq "BETWEEN"
$_[0] eq "=" $_[0] eq "IS" $_[0] eq "<=>" $_[0] eq "IN" $_[0] eq "BETWEEN"
Truth table83%5/6
A or B or C or D or E
A= $_[0] eq "="B= $_[0] eq "IS"C= $_[0] eq "<=>"D= $_[0] eq "IN"E= $_[0] eq "BETWEEN"
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
432 37 ▶ return 'AND' if $_[0] eq '<>' or $_[0] eq 'IS NOT' or $_[0] eq 'NOT IN' or $_[0] eq 'NOT BETWEEN';
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq "<>" or $_[0] eq "IS NOT" or $_[0] eq "NOT IN" or $_[0] eq "NOT BETWEEN" 20 17 37
Condition100%9/9
$_[0] eq "<>" or $_[0] eq "IS NOT" or $_[0] eq "NOT IN" or $_[0] eq "NOT BETWEEN"
l!l&&r!l&&!r
16417
$_[0] eq "<>" or $_[0] eq "IS NOT" or $_[0] eq "NOT IN"
l!l&&r!l&&!r
12421
$_[0] eq "<>" or $_[0] eq "IS NOT"
l!l&&r!l&&!r
8425
MC/DC100%4/4
$_[0] eq "<>" or $_[0] eq "IS NOT" or $_[0] eq "NOT IN" or $_[0] eq "NOT BETWEEN"
$_[0] eq "<>" $_[0] eq "IS NOT" $_[0] eq "NOT IN" $_[0] eq "NOT BETWEEN"
Truth table100%5/5
A or B or C or D
A= $_[0] eq "<>"B= $_[0] eq "IS NOT"C= $_[0] eq "NOT IN"D= $_[0] eq "NOT BETWEEN"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
433 }
434  
435 # Construct one WHERE expression
436 sub _build_where_piece {
437 58 ▶ my($class, $me, $bind, $op, $fld, $fld_func, $fld_opt, $val, $val_func, $val_opt) = @_;
Subroutine _build_where_piece: called CC=1 SCAR=0.0
438 58 $class->_build_val($me, $bind, $fld, $fld_func, $fld_opt)." $op ".$class->_build_val($me, $bind, $val, $val_func, $val_opt);
439 }
440  
441 # Construct one WHERE expression (simple)
442 sub _build_quick_where {
443 28 ▶ croak 'Wrong number of arguments' unless @_ & 1;
Subroutine _build_quick_where: called CC=8 SCAR=20.8
Branch: 1 branch
BranchTrueFalseTotal
unless @_ & 1 0 28 28
444 28 my($class, $me, $bind) = splice @_, 0, 3;
445 28 my @where;
446 28 ▶ while (my($col, $val) = splice @_, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 26 27 53
447 # FIXME: What about aliases in quick_where?
448 26 push @where, $class->_build_col($me, $class->_parse_col($me, $col)) . do {
449 25 ▶ if (ref $val eq 'SCALAR' and $$val =~ /^\s*(?:NOT\s+)NULL\s*$/is) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $val eq 'SCALAR' and $$val =~ /^\s*(?:NOT\s+)NULL\s*$/is) { } 2 23 25
elsif (ref $val eq 'ARRAY') { } 2 21 23
elsif (defined $val) { } 20 1 21
Condition100%3/3
ref $val eq "SCALAR" and $$val =~ /^\s*(?:NOT\s+)NULL\s*$/is
!ll&&!rl&&r
2212
MC/DC100%2/2
ref $val eq "SCALAR" and $$val =~ /^\s*(?:NOT\s+)NULL\s*$/is
ref $val eq "SCALAR" $$val =~ /^\s*(?:NOT\s+)NULL\s*$/is
Truth table100%3/3
A and B
A= ref $val eq "SCALAR"B= $$val =~ /^\s*(?:NOT\s+)NULL\s*$/is
ABresult
0X0
100
111
450 2 ' IS ';
451 } elsif (ref $val eq 'ARRAY') {
452 2 ▶ croak 'Invalid value argument, IN requires at least 1 value' unless @$val;
Branch: 1 branch
BranchTrueFalseTotal
unless @$val 0 2 2
453 2 $val = { FUNC => '('.join(',', ('?') x @$val).')', VAL => $val };
454 2 ' IN ';
455 } elsif (defined $val) {
456 20 ' = ';
457 } else {
458 1 $val = \'NULL';
459 1 ' IS ';
460 }
461 } . $class->_build_val($me, $bind, $class->_parse_val($me, $val));
462 }
463 27 return join ' AND ', @where;
464 }
465  
466 sub _parse_set {
467 6 ▶ croak 'Wrong number of arguments' if @_ & 1;
Subroutine _parse_set: called CC=4 SCAR=13.9
Branch: 1 branch
BranchTrueFalseTotal
if @_ & 1 0 6 6
468 6 my($class, $me, @arg) = @_;
469 6 my @update;
470 my %remove_duplicates;
471 6 ▶ while (@arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (@arg) 8 6 14
472 8 my @val = $class->_parse_val($me, pop @arg);
473 8 my $col = $class->_parse_col($me, pop @arg);
474 8 ▶ unshift @update, $col, \@val unless $remove_duplicates{$col}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $remove_duplicates{$col}++ 7 1 8
475 }
476 6 return @update;
477 }
478  
479 sub _build_set {
480 6 ▶ my($class, $me, @arg) = @_;
Subroutine _build_set: called CC=2 SCAR=6.9
481 6 my $h = $me->_build_data;
482 6 undef @{$h->{Set_Bind}};
483 6 my @set;
484 6 ▶ while (@arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (@arg) 7 6 13
485 7 push @set, $class->_build_col($me, shift @arg).' = '.$class->_build_val($me, $h->{Set_Bind}, @{shift @arg});
486 }
487 6 return join ', ', @set;
488 }
489  
490 sub _build_group {
491 51 ▶ my($class, $me) = @_;
Subroutine _build_group: called CC=2 SCAR=6.9
492 51 my $h = $me->_build_data;
493 51 ▶ return $h->{group} if defined $h->{group};
Branch: 1 branch
BranchTrueFalseTotal
if defined $h->{"group"} 31 20 51
494 20 undef @{$h->{Group_Bind}};
495 20 return $h->{group} = join ', ', map $class->_build_val($me, $h->{Group_Bind}, @$_), @{$h->{GroupBy}};
496 }
497  
498 # Construct the HAVING clause
499 sub _build_having {
500 50 ▶ my($class, $me) = @_;
Subroutine _build_having: called CC=3 SCAR=11.0
501 50 my $h = $me->_build_data;
502 50 ▶ return $h->{having} if defined $h->{having};
Branch: 1 branch
BranchTrueFalseTotal
if defined $h->{"having"} 24 26 50
503 26 undef @{$h->{Having_Bind}};
504 26 my @having;
505 26 ▶ push @having, $class->_build_where_chunk($me, $h->{Having_Bind}, 'OR', $h->{Having_Data}) if exists $h->{Having_Data};
Branch: 1 branch
BranchTrueFalseTotal
if exists $h->{"Having_Data"} 3 23 26
506 26 return $h->{having} = join ' AND ', @having;
507 }
508  
509 sub _build_order {
510 51 ▶ my($class, $me) = @_;
Subroutine _build_order: called CC=2 SCAR=6.9
511 51 my $h = $me->_build_data;
512 51 ▶ return $h->{order} if defined $h->{order};
Branch: 1 branch
BranchTrueFalseTotal
if defined $h->{"order"} 27 24 51
513 24 undef @{$h->{Order_Bind}};
514 24 return $h->{order} = join ', ', map $class->_build_val($me, $h->{Order_Bind}, @$_), @{$h->{OrderBy}};
515 }
516  
517 sub _build_limit {
518 51 ▶ my($class, $me) = @_;
Subroutine _build_limit: called CC=4 SCAR=13.9
519 51 my $h = $me->_build_data;
520 51 ▶ return $h->{limit} if defined $h->{limit};
Branch: 1 branch
BranchTrueFalseTotal
if defined $h->{"limit"} 24 27 51
521 27 ▶ return $h->{limit} = '' unless defined $h->{LimitOffset};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $h->{"LimitOffset"} 20 7 27
522 7 $h->{limit} = 'LIMIT '.$h->{LimitOffset}[0];
523 7 ▶ $h->{limit} .= ' OFFSET '.$h->{LimitOffset}[1] if $h->{LimitOffset}[1];
Branch: 1 branch
BranchTrueFalseTotal
if $h->{"LimitOffset"}[1] 2 5 7
524 7 return $h->{limit};
525 }
526  
527 sub _get_config {
528 754 ▶ my($class, $opt, @confs) = @_;
Subroutine _get_config: called CC=3 SCAR=11.0
529 754 ▶ defined $_->{$opt} and return $_->{$opt} for @confs;
Branch: 1 branch
BranchTrueFalseTotal
defined $_->{$opt} and return $_->{$opt} 447 1431 1878
530 307 return;
531 }
532  
533 sub _set_config {
534 124 ▶ my($class, $ref, $opt, $val) = @_;
Subroutine _set_config: called CC=10 SCAR=23.4
535 124 ▶ croak "Invalid value for the 'OnRowUpdate' setting"
Branch: 1 branch
BranchTrueFalseTotal
if $opt eq "OnRowUpdate" and $val and $val ne "empty" and $val ne "simple" and $val ne "reload" 0 124 124
Condition66%8/12
$opt eq "OnRowUpdate" and $val and $val ne "empty" and $val ne "simple" and $val ne "reload"
!ll&&!rl&&r
12310
$opt eq "OnRowUpdate" and $val and $val ne "empty" and $val ne "simple"
!ll&&!rl&&r
12301
$opt eq "OnRowUpdate" and $val and $val ne "empty"
!ll&&!rl&&r
12301
$opt eq "OnRowUpdate" and $val
!ll&&!rl&&r
12301
MC/DC0%0/5
$opt eq "OnRowUpdate" and $val and $val ne "empty" and $val ne "simple" and $val ne "reload"
$opt eq "OnRowUpdate" $val $val ne "empty" $val ne "simple" $val ne "reload"
Truth table33%2/6
A and B and C and D and E
A= $opt eq "OnRowUpdate"B= $valC= $val ne "empty"D= $val ne "simple"E= $val ne "reload"
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
536 if $opt eq 'OnRowUpdate' and $val and $val ne 'empty' and $val ne 'simple' and $val ne 'reload';
537 124 ▶ croak "Invalid value for the 'UseHandle' setting"
Branch: 1 branch
BranchTrueFalseTotal
if $opt eq "UseHandle" and $val and $val ne "read-only" and $val ne "read-write" 1 123 124
Condition100%9/9
$opt eq "UseHandle" and $val and $val ne "read-only" and $val ne "read-write"
!ll&&!rl&&r
12211
$opt eq "UseHandle" and $val and $val ne "read-only"
!ll&&!rl&&r
12112
$opt eq "UseHandle" and $val
!ll&&!rl&&r
12013
MC/DC100%4/4
$opt eq "UseHandle" and $val and $val ne "read-only" and $val ne "read-write"
$opt eq "UseHandle" $val $val ne "read-only" $val ne "read-write"
Truth table100%5/5
A and B and C and D
A= $opt eq "UseHandle"B= $valC= $val ne "read-only"D= $val ne "read-write"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
538 if $opt eq 'UseHandle' and $val and $val ne 'read-only' and $val ne 'read-write';
539 123 my $old = $ref->{$opt};
540 123 $ref->{$opt} = $val;
541 123 return $old;
542 }
543  
544  
545 # Query methods
546 sub _rows {
547 1 ▶ my($class, $me) = @_;
Subroutine _rows: called CC=5 SCAR=19.4
548 1 ▶ $me->_sth and ($me->{sth}{Executed} or $me->run)
Branch: 1 branch
BranchTrueFalseTotal
$me->_sth and $me->{"sth"}{"Executed"} || $me->run or croak($me->rdbh->errstr) 0 1 1
Condition33%2/6
$me->_sth and $me->{"sth"}{"Executed"} || $me->run
!ll&&!rl&&r
010
$me->{"sth"}{"Executed"} or $me->run
l!l&&r!l&&!r
100
MC/DC0%0/3
$me->_sth and $me->{"sth"}{"Executed"} || $me->run
$me->_sth $me->{"sth"}{"Executed"} $me->run
Truth table0%0/4
A and B or C
A= $me->_sthB= $me->{"sth"}{"Executed"}C= $me->run
ABCresult
0XX0
1000
11X1
1011
549 or croak $me->rdbh->errstr;
550 1 my $rows = $me->_sth->rows;
551 1 ▶ $me->{Row_Count} = $rows == -1 ? undef : $rows;
Branch: 1 branch
BranchTrueFalseTotal
$rows == -1 ? : 0 1 1
552 }
553  
554 sub _calc_found_rows {
555 1 ▶ my($class, $me) = @_;
Subroutine _calc_found_rows: called CC=1 SCAR=0.0
556 1 local $me->{build_data}{limit} = '';
557 1 $me->{Found_Rows} = $me->count_rows;
558 }
559  
560  
561 # Table methods
562 ▶sub _save_last_insert_id {
Subroutine _save_last_insert_id: not called CC=1 SCAR=0.0
563 #my($class, $me, $sth) = @_;
564 # Should be provided in a DBD specific method
565 # It is called after insert and must return the autogenerated ID
566 #return $sth->{Database}->last_insert_id(undef, @$me{qw(Schema Name)}, undef);
567 }
568  
569 sub _fast_bulk_insert {
570 0 ▶ my($class, $me, $sql, $cols, %opt) = @_;
Subroutine _fast_bulk_insert: not called CC=4 SCAR=30.0
571  
572 0 my @vals;
573 my @bind;
574 0 ▶ if (ref $opt{rows}[0] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opt{'rows'}[0] eq 'ARRAY') { } 0 0 0
575 0 for my $row (@{$opt{rows}}) {
576 0 push @vals, '('.join(', ', map $class->_build_val($me, \@bind, $class->_parse_val($me, $_)), @$row).')';
577 }
578 } else {
579 0 for my $row (@{$opt{rows}}) {
580 0 push @vals, '('.join(', ', map $class->_build_val($me, \@bind, $class->_parse_val($me, $_)), @$row{@$cols}).')';
581 }
582 }
583  
584 0 $sql .= join(",\n", @vals);
585 0 $class->_do($me, $sql, undef, @bind);
586 }
587  
588 sub _safe_bulk_insert {
589 4 ▶ my($class, $me, $sql, $cols, %opt) = @_;
Subroutine _safe_bulk_insert: called CC=11 SCAR=24.2
590  
591 # TODO: Wrap in a transaction
592 4 my $rv;
593 my $sth;
594 4 my $prev_vals = '';
595 4 ▶ if (ref $opt{rows}[0] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opt{'rows'}[0] eq 'ARRAY') { } 2 2 4
596 2 for my $row (@{$opt{rows}}) {
597 8 my @bind;
598 8 my $vals = '('.join(', ', map $class->_build_val($me, \@bind, $class->_parse_val($me, $_)), @$row).')';
599 8 $class->_sql($me, $sql.$vals, @bind);
600 8 ▶ if ($prev_vals ne $vals) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev_vals ne $vals) 2 6 8
601 2 ▶ $sth = $me->dbh->prepare($sql.$vals) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$sth = $me->dbh->prepare($sql . $vals) or return undef 0 2 2
602 2 $prev_vals = $vals;
603 }
604 8 ▶ $rv += $sth->execute(@bind) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$rv += $sth->execute(@bind) or return undef 0 8 8
605 }
606 } else {
607 2 for my $row (@{$opt{rows}}) {
608 8 my @bind;
609 8 my $vals = '('.join(', ', map $class->_build_val($me, \@bind, $class->_parse_val($me, $_)), @$row{@$cols}).')';
610 8 $class->_sql($me, $sql.$vals, @bind);
611 8 ▶ if ($prev_vals ne $vals) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev_vals ne $vals) 2 6 8
612 2 ▶ $sth = $me->dbh->prepare($sql.$vals) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$sth = $me->dbh->prepare($sql . $vals) or return undef 0 2 2
613 2 $prev_vals = $vals;
614 }
615 8 ▶ $rv += $sth->execute(@bind) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$rv += $sth->execute(@bind) or return undef 0 8 8
616 }
617 }
618  
619 4 ▶ return $rv || '0E0';
Condition50%1/2
$rv || '0E0'
l!l
40
MC/DC0%0/1
$rv || '0E0'
$rv
Truth table50%1/2
A
A= $rv
Aresult
11
00
620 }
621 12 *_bulk_insert = \&_safe_bulk_insert;
622  
623  
624 # Row methods
625 sub _reset_row_on_update {
626 4 ▶ my($class, $me, @update) = @_;
Subroutine _reset_row_on_update: called CC=13 SCAR=27.3
627 4 ▶ my $on_row_update = $me->config('OnRowUpdate') || 'simple';
Condition50%1/2
$me->config('OnRowUpdate') || 'simple'
l!l
40
MC/DC0%0/1
$me->config('OnRowUpdate') || 'simple'
$me->config('OnRowUpdate')
Truth table50%1/2
A
A= $me->config('OnRowUpdate')
Aresult
11
00
628  
629 4 ▶ if ($on_row_update ne 'empty') {
Branch: 1 branch
BranchTrueFalseTotal
if ($on_row_update ne "empty") 4 0 4
630 # Set the row values if they are simple expressions
631 4 my @cant_update;
632 4 ▶ for (my $i = 0; $i < @update; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @update) 5 4 9
633 # Keep a list of columns we can't update, and skip them
634 next if $cant_update[ $me->_column_idx($update[0]) ] = (
635 5 ▶ defined $update[1][1] or @{$update[1][0]} != 1 or (
Branch: 1 branch
BranchTrueFalseTotal
if $cant_update[$me->_column_idx($update[0])] = defined $update[1][1] || @{$update[1][0];} != 1 || ref $update[1][0][0] && (!_isa($update[1][0][0], "DBIx::DBO::Column") || $cant_update[$me->_column_idx($update[1][0][0])]) 1 4 5
Condition41%5/12
defined $update[1][1] || @{$update[1][0];} != 1 || ref $update[1][0][0] && (!_isa($update[1][0][0], 'DBIx::DBO::Column') || $cant_update[$me->_column_idx($update[1][0][0])])
l!l&&r!l&&!r
104
defined $update[1][1] or @{$update[1][0];} != 1
l!l&&r!l&&!r
104
ref $update[1][0][0] and !_isa($update[1][0][0], "DBIx::DBO::Column") || $cant_update[$me->_column_idx($update[1][0][0])]
!ll&&!rl&&r
400
not _isa($update[1][0][0], "DBIx::DBO::Column") or $cant_update[$me->_column_idx($update[1][0][0])]
l!l&&r!l&&!r
000
MC/DC20%1/5
defined $update[1][1] || @{$update[1][0];} != 1 || ref $update[1][0][0] && (!_isa($update[1][0][0], 'DBIx::DBO::Column') || $cant_update[$me->_column_idx($update[1][0][0])])
defined $update[1][1] @{$update[1][0];} != 1 ref $update[1][0][0] not _isa($update[1][0][0], "DBIx::DBO::Column") $cant_update[$me->_column_idx($update[1][0][0])]
Truth table33%2/6
A or B || C and not(D) or E
A= defined $update[1][1]B= @{$update[1][0];} != 1C= ref $update[1][0][0]D= not _isa($update[1][0][0], "DBIx::DBO::Column")E= $cant_update[$me->_column_idx($update[1][0][0])]
ABCDEresult
1XXXX1
01XXX1
0011X1
001011
000XX0
001000
636 ref $update[1][0][0] and (
637 not _isa($update[1][0][0], 'DBIx::DBO::Column')
638 or $cant_update[ $me->_column_idx($update[1][0][0]) ]
639 )
640 )
641 );
642 4 my($col, $val) = splice @update, $i, 2;
643 4 $val = $val->[0][0];
644 4 ▶ $val = $$me->{array}[ $me->_column_idx($val) ] if ref $val;
Branch: 1 branch
BranchTrueFalseTotal
if ref $val 0 4 4
645 4 $$me->{array}[ $me->_column_idx($col) ] = $val;
646 4 $i -= 2;
647 }
648 # If we were able to update all the columns then return
649 4 ▶ grep $_, @cant_update or return;
Branch: 1 branch
BranchTrueFalseTotal
grep $_, @cant_update or return 3 1 4
650  
651 1 ▶ if ($on_row_update eq 'reload') {
Branch: 1 branch
BranchTrueFalseTotal
if ($on_row_update eq "reload") 1 0 1
652 # Attempt reload
653 1 my @cols = map $$me->{build_data}{Quick_Where}[$_ << 1], 0 .. $#{$$me->{build_data}{Quick_Where}} >> 1;
654 1 my @cidx = map $me->_column_idx($_), @cols;
655 1 ▶ unless (grep $cant_update[$_], @cidx) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep $cant_update[$_], @cidx) 1 0 1
656 1 my %bd = %{$$me->{build_data}};
657 1 delete $bd{Where_Data};
658 1 delete $bd{where};
659 1 $bd{Quick_Where} = [map { $cols[$_] => $$me->{array}[ $cidx[$_] ] } 0 .. $#cols];
660 1 my($sql, @bind) = do {
661 1 local $$me->{build_data} = \%bd;
662 1 ($class->_build_sql_select($me), $class->_bind_params_select($me));
663 };
664 1 return $me->_load($sql, @bind);
665 }
666 }
667 }
668 # If we can't update or reload then empty the Row
669 0 undef $$me->{array};
670 0 $$me->{hash} = {};
671 }
672  
673 sub _build_data_matching_this_row {
674 5 ▶ my($class, $me) = @_;
Subroutine _build_data_matching_this_row: called CC=6 SCAR=18.0
675 # Identify the row by the PrimaryKeys if any, otherwise by all Columns
676 5 my @quick_where;
677 5 for my $tbl (@{$$me->{Tables}}) {
678 5 ▶ for my $col (map $tbl ** $_, @{$tbl->{ @{$tbl->{PrimaryKeys}} ? 'PrimaryKeys' : 'Columns' }}) {
Branch: 1 branch
BranchTrueFalseTotal
@{$tbl->{'PrimaryKeys'};} ? : 5 0 5
679 5 my $i = $me->_column_idx($col);
680 5 ▶ defined $i or croak 'The '.$class->_qi($me, $tbl->{Name}, $col->[1]).' field needed to identify this row, was not included in this query';
Branch: 1 branch
BranchTrueFalseTotal
defined $i or croak('The ' . $class->_qi($me, $tbl->{"Name"}, $col->[1]) . ' field needed to identify this row, was not included in this query') 0 5 5
681 5 push @quick_where, $col => $$me->{array}[$i];
682 }
683 }
684 my %h = (
685 Showing => $$me->{build_data}{Showing},
686 from => $$me->{build_data}{from},
687 5 Quick_Where => \@quick_where
688 );
689 5 ▶ $h{From_Bind} = $$me->{build_data}{From_Bind} if exists $$me->{build_data}{From_Bind};
Branch: 1 branch
BranchTrueFalseTotal
if exists $$me->{"build_data"}{"From_Bind"} 1 4 5
690 5 return \%h;
691 }
692  
693  
694 # require the DBD module if it exists
695 12 my %inheritance;
696 sub _require_dbd_class {
697 22 ▶ my($class, $dbd) = @_;
Subroutine _require_dbd_class: called CC=8 SCAR=20.8
698 22 my $dbd_class = $class.'::'.$dbd;
699  
700 22 my $rv;
701 my @warn;
702 {
703 22 ▶ local $SIG{__WARN__} = sub { push @warn, join '', @_ };
Subroutine __ANON__: called CC=1 SCAR=0.0
704 22 $rv = eval "require $dbd_class";
705 }
706 22 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) { } 12 10 22
707 12 ▶ warn @warn if @warn;
Branch: 1 branch
BranchTrueFalseTotal
if @warn 0 12 12
708 } else {
709 10 (my $file = $dbd_class.'.pm') =~ s'::'/'g;
710 10 ▶ if ($@ !~ / \Q$file\E in \@INC /) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@ =~ / \Q$file\E in \@INC /) 1 9 10
711 1 (my $err = $@) =~ s/\n.*$//; # Remove the last line
712 1 chomp @warn;
713 1 chomp $err;
714 1 croak join "\n", @warn, $err, "Can't load $dbd driver";
715 }
716  
717 9 $@ = '';
718 9 delete $INC{$file};
719 9 $INC{$file} = 1;
720 }
721  
722 # Set the derived DBD class' inheritance
723 21 ▶ unless (exists $inheritance{$class}{$dbd}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $inheritance{$class}{$dbd}) 10 11 21
724 12 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
725 10 ▶ unless (@{$dbd_class.'::ISA'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$dbd_class . '::ISA';}) 10 0 10
726 10 my @isa = map $_->_require_dbd_class($dbd), grep $_->isa(__PACKAGE__), @{$class.'::ISA'};
727 10 @{$dbd_class.'::ISA'} = ($class, @isa);
728 10 ▶ if (@isa) {
Branch: 1 branch
BranchTrueFalseTotal
if (@isa) 1 9 10
729 1 mro::set_mro($dbd_class, 'c3');
730 1 ▶ Class::C3::initialize() if $] < 5.009_005;
Branch: 1 branch
BranchTrueFalseTotal
if $] < "5.009005" 0 1 1
731 }
732 }
733 10 push @CARP_NOT, $dbd_class;
734 10 $inheritance{$class}{$dbd} = $dbd_class;
735 }
736  
737 21 return $inheritance{$class}{$dbd};
738 }
739  
740 12 1;
« blib/lib/DBIx/DBO/Query.pm ↑ Summary blib/lib/DBIx/DBO/Row.pm »