blib/lib/DBIx/DBO/DBD.pm

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