Lite/ResultSet.pm

filter: Statementstmt 69.9% 277 / 396 Branchbran 54.6% 94 / 172 Conditioncond 39.4% 45 / 114 MC/DCmcdc 15.2% 9 / 59 Subroutinesub 67.6% 44 / 65 Podpod 100% 33 / 33 totaltotal 59.8% 502 / 839 CC 138 SCAR 26.2
CC
138
Cov
61%
find_or_insert
42.0
_join
35.8
select_sql
35.3
CRAP 13.8
↑ Summary Lite/Row.pm »
1 package DBIx::Lite::ResultSet;
2 7 $DBIx::Lite::ResultSet::VERSION = '0.40';
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Clone qw(clone);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Data::Page;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use List::MoreUtils qw(uniq firstval);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use version 0.77;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use vars qw($AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 $Carp::Internal{$_}++ for __PACKAGE__;
13  
14 96 ▶sub _new {
Subroutine _new: called CC=6 SCAR=18.2
15 96 my $class = shift;
16 96 my (%params) = @_;
17
18 96 my $self = {
19 table_alias => 'me',
20 joins => [],
21 where => [],
22 select => undef,
23 rows_per_page => 10,
24 };
25
26 # required arguments
27 96 for (qw(dbix_lite table)) {
28 192 ▶ $self->{$_} = delete $params{$_} or croak "$_ argument needed";
Branch: 1 branch
BranchTrueFalseTotal
$self->{$_} = delete $params{$_} or croak("$_ argument needed") 192 0 192
29 }
30
31 # optional arguments
32 96 for (grep exists($params{$_}), qw(joins where select group_by having order_by
33 limit offset for rows_per_page page cur_table with from distinct table_alias)) {
34 342 $self->{$_} = delete $params{$_};
35 }
36 96 ▶ $self->{cur_table} //= $self->{table};
Condition66%2/3
$self->{"cur_table"} //= $self->{"table"}
l!l&&r!l&&!r
56400
MC/DC0%0/2
$self->{"cur_table"} //= $self->{"table"}
$self->{"cur_table"} $self->{"table"}
Truth table66%2/3
A //= B
A= $self->{"cur_table"}B= $self->{"table"}
ABresult
1X1
011
000
37
38 96 ▶ !%params
Branch: 1 branch
BranchTrueFalseTotal
%params and croak('Unknown options: ' . join(", ", keys %params)) 0 96 96
39 or croak "Unknown options: " . join(', ', keys %params);
40
41 96 bless $self, $class;
42 96 $self;
43 }
44  
45 # create setters
46 7 for my $methname (qw(group_by having order_by limit offset rows_per_page page from distinct table_alias)) {
47 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
48 15 ▶ *$methname = sub {
Subroutine __ANON__: called CC=4 SCAR=14.5
49 15 my $self = shift;
50
51 # we always return a new object for easy chaining
52 15 my $new_self = $self->_clone;
53
54 # set new values
55 15 ▶ $new_self->{$methname} = $methname =~ /^(group_by|order_by|from|distinct)$/ ? [@_] : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
$methname =~ /^(group_by|order_by|from|distinct)$/ ? : 14 1 15
56 15 ▶ $new_self->{pager}->current_page($_[0]) if $methname eq 'page' && $new_self->{pager};
Branch: 1 branch
BranchTrueFalseTotal
if $methname eq "page" and $new_self->{"pager"} 0 15 15
Condition33%1/3
$methname eq "page" and $new_self->{"pager"}
!ll&&!rl&&r
1500
MC/DC0%0/2
$methname eq "page" and $new_self->{"pager"}
$methname eq "page" $new_self->{"pager"}
Truth table33%1/3
A and B
A= $methname eq "page"B= $new_self->{"pager"}
ABresult
0X0
100
111
57
58 # return object
59 15 $new_self;
60 70 };
61 }
62  
63 0 ▶sub for_update {
Subroutine for_update: not called CC=1 SCAR=6.9
Pod documented
64 0 my ($self) = @_;
65
66 0 return $self->for('UPDATE');
67 }
68  
69 0 ▶sub for {
Subroutine for: not called CC=1 SCAR=6.9
Pod documented
70 0 my ($self, $for) = @_;
71
72 0 my $new_self = $self->_clone;
73 0 $new_self->{for} = $for;
74 0 $new_self;
75 }
76  
77 # return a clone of this object
78 56 ▶sub _clone {
Subroutine _clone: called CC=2 SCAR=6.9
79 56 my $self = shift;
80 (ref $self)->_new(
81 # clone all members except for some which we copy by reference
82 56 ▶ map { $_ => /^(?:dbix_lite|table|cur_table)$/ ? $self->{$_} : clone($self->{$_}) }
Branch: 1 branch
BranchTrueFalseTotal
/^(?:dbix_lite|table|cur_table)$/ ? : 168 286 454
83 grep !/^(?:sth|pager)$/, keys %$self,
84 );
85 }
86  
87 16 ▶sub select {
Subroutine select: called CC=2 SCAR=7.0
Pod documented
88 16 my $self = shift;
89  
90 16 my $new_self = $self->_clone;
91 16 ▶ $new_self->{select} = @_ ? [@_] : undef;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 16 0 16
92
93 16 $new_self;
94 }
95  
96 1 ▶sub select_also {
Subroutine select_also: called CC=2 SCAR=7.1
Pod documented
97 1 my $self = shift;
98 1 ▶ return $self->select(@{$self->{select} // [$self->{table_alias} . '.*']}, @_);
Condition50%1/2
$self->{"select"} // [$self->{"table_alias"} . '.*']
l!l
10
MC/DC0%0/1
$self->{"select"} // [$self->{"table_alias"} . '.*']
$self->{"select"}
Truth table50%1/2
A
A= $self->{"select"}
Aresult
11
00
99 }
100  
101 5 ▶sub with {
Subroutine with: called CC=5 SCAR=17.3
Pod documented
102 5 my $self = shift;
103 5 my %with = @_;
104
105 croak "with() requires a hash of scalarrefs or refs to arrayrefs"
106 5 ▶ if grep { ref($_) ne 'SCALAR'
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $_ ne "SCALAR" and not ref $_ eq "REF" && ref $$_ eq "ARRAY";} values %with 0 5 5
Condition50%3/6
ref $_ ne 'SCALAR' && !(ref $_ eq 'REF' && ref $$_ eq 'ARRAY')
!ll&&!rl&&r
410
ref $_ eq 'REF' && ref $$_ eq 'ARRAY'
!ll&&!rl&&r
001
MC/DC0%0/3
ref $_ ne 'SCALAR' && !(ref $_ eq 'REF' && ref $$_ eq 'ARRAY')
ref $_ ne 'SCALAR' ref $_ eq 'REF' ref $$_ eq 'ARRAY'
Truth table50%2/4
A && not(B && C)
A= ref $_ ne 'SCALAR'B= ref $_ eq 'REF'C= ref $$_ eq 'ARRAY'
ABCresult
0XX0
1110
10X1
1101
107 and ! ( ref($_) eq 'REF' && ref($$_) eq 'ARRAY' )
108 } values %with;
109
110 5 my $new_self = $self->_clone;
111 5 ▶ $new_self->{with} = %with ? {%with} : undef;
Branch: 1 branch
BranchTrueFalseTotal
%with ? : 5 0 5
112
113 5 $new_self;
114 }
115  
116 0 ▶sub with_also {
Subroutine with_also: not called CC=1 SCAR=6.9
Pod documented
117 0 my $self = shift;
118 0 return $self->with(%{$self->{with}}, @_);
119 }
120  
121 0 ▶sub pager {
Subroutine pager: not called CC=4 SCAR=30.0
Pod documented
122 0 my $self = shift;
123 0 ▶ if (!$self->{pager}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"pager"}) 0 0 0
124 0 ▶ $self->{pager} ||= Data::Page->new;
Condition0%0/3
$self->{"pager"} ||= "Data::Page"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"pager"} ||= "Data::Page"->new
$self->{"pager"} "Data::Page"->new
Truth table0%0/3
A ||= B
A= $self->{"pager"}B= "Data::Page"->new
ABresult
1X1
011
000
125 0 $self->{pager}->total_entries($self->page(undef)->count);
126 0 ▶ $self->{pager}->entries_per_page($self->{rows_per_page} // $self->{pager}->total_entries);
Condition0%0/3
$self->{"rows_per_page"} // $self->{"pager"}->total_entries
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"rows_per_page"} // $self->{"pager"}->total_entries
$self->{"rows_per_page"} $self->{"pager"}->total_entries
Truth table0%0/3
A // B
A= $self->{"rows_per_page"}B= $self->{"pager"}->total_entries
ABresult
1X1
011
000
127 0 $self->{pager}->current_page($self->{page});
128 }
129 0 return $self->{pager};
130 }
131  
132 15 ▶sub search {
Subroutine search: called CC=2 SCAR=7.0
Pod documented
133 15 my $self = shift;
134 15 my ($where) = @_;
135
136 15 my $new_self = $self->_clone;
137 15 ▶ push @{$new_self->{where}}, $where if defined $where;
Branch: 1 branch
BranchTrueFalseTotal
if defined $where 15 0 15
138 15 $new_self;
139 }
140  
141 0 ▶sub clear_search {
Subroutine clear_search: not called CC=1 SCAR=6.9
Pod documented
142 0 my $self = shift;
143
144 0 my $new_self = $self->_clone;
145 0 @{$new_self->{where}} = ();
146 0 $new_self;
147 }
148  
149 4 ▶sub find {
Subroutine find: called CC=4 SCAR=17.9
Pod documented
150 4 my $self = shift;
151 4 my ($where) = @_;
152
153 # if user did not supply a search hashref, we assume the supplied
154 # value(s) are the key(s) of the primary key column(s) defined for
155 # this table
156 4 ▶ if (!ref $where && (my @pk = $self->{table}->pk)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $where and my(@pk) = $self->{"table"}->pk) 0 4 4
Condition33%1/3
not ref $where and my(@pk) = $self->{"table"}->pk
!ll&&!rl&&r
400
MC/DC0%0/2
not ref $where and my(@pk) = $self->{"table"}->pk
not ref $where my(@pk) = $self->{"table"}->pk
Truth table33%1/3
not(A) and B
A= not ref $whereB= my(@pk) = $self->{"table"}->pk
ABresult
0X0
100
111
157 # prepend table alias to all pk columns
158 0 my $table_alias = $self->{table_alias};
159 0 $_ =~ s/^[^.]+$/$table_alias\.$&/ for @pk;
160
161 0 $where = { map +(shift(@pk) => $_), @_ };
162 }
163 4 return $self->search($where)->single;
164 }
165  
166 0 ▶sub where_sql {
Subroutine where_sql: not called CC=1 SCAR=6.9
Pod documented
167 0 my $self = shift;
168
169 0 my ($sql, @bind) = $self->{dbix_lite}->{abstract}->where({ -and => $self->{where} });
170 0 return ($sql, @bind);
171 }
172  
173 25 ▶sub select_sql {
Subroutine select_sql: called CC=30 SCAR=35.6
Pod documented
174 25 my $self = shift;
175
176 # prepare names of columns to be selected
177 25 my @cols = ();
178 25 my $cur_table_prefix = $self->_table_alias($self->{cur_table}{name}, 'select');
179 25 ▶ my $select = $self->{select} // [$self->{table_alias} . '.*'];
Condition100%2/2
$self->{"select"} // [$self->{"table_alias"} . '.*']
l!l
169
MC/DC100%1/1
$self->{"select"} // [$self->{"table_alias"} . '.*']
$self->{"select"}
Truth table100%2/2
A
A= $self->{"select"}
Aresult
11
00
180 25 foreach my $col (grep defined $_, @$select) {
181 # check whether user specified an alias
182 26 ▶ my ($expr, $as) = ref $col eq 'ARRAY' ? @$col : ($col, undef);
Branch: 1 branch
BranchTrueFalseTotal
ref $col eq 'ARRAY' ? : 7 19 26
183
184 # prepend table alias if column name doesn't contain one already
185 26 ▶ $expr =~ s/^[^.]+$/$cur_table_prefix\.$&/ if !ref($expr) && !$self->{from};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $expr or $self->{"from"} 19 7 26
Condition66%2/3
ref $expr or $self->{"from"}
l!l&&r!l&&!r
7019
MC/DC50%1/2
ref $expr or $self->{"from"}
ref $expr $self->{"from"}
Truth table66%2/3
A or B
A= ref $exprB= $self->{"from"}
ABresult
1X1
011
000
186
187 # explode the expression if it's a scalar ref
188 26 ▶ if (ref $expr eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $expr eq "SCALAR") 7 19 26
189 7 $expr = $$expr;
190 }
191
192 # build the column definition according to the SQL::Abstract::More syntax
193 26 ▶ push @cols, $expr . ($as ? "|$as" : "");
Branch: 1 branch
BranchTrueFalseTotal
$as ? : 7 19 26
194 }
195
196 # joins
197 25 my @joins = ();
198 25 foreach my $join (@{$self->{joins}}) {
199 # get table name and alias if any
200 4 my ($table_name, $table_alias) = @{$join->{table}};
201 4 my $left_table_prefix = $self->_table_alias($join->{cur_table}{name}, 'select');
202
203 # prepare join conditions
204 4 my %cond = ();
205 4 ▶ while (my ($col1, $col2) = each %{$join->{condition}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$join{"condition"};}) 5 4 9
206 # in case they have no explicit table alias,
207 # $col1 is supposed to belong to the current table, and
208 # $col2 is supposed to belong to the joined table
209
210 # prepend table alias to the column of the first table
211 5 $col1 =~ s/^[^.]+$/$left_table_prefix.$&/;
212
213 # in case user supplied the table name as table alias, replace it
214 # with the proper one (such as "me.")
215 5 $col1 =~ s/^$join->{cur_table}{name}\./$left_table_prefix./;
216
217 # prepend table alias to the column of the second table
218 5 ▶ $col2 = ($table_alias || $self->{dbix_lite}->_quote($table_name)) . ".$col2"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $col2 or $col2 =~ /\./ 4 1 5
Condition50%3/6
ref $col2 or $col2 =~ /\./
l!l&&r!l&&!r
104
$table_alias || $self->{'dbix_lite'}->_quote($table_name)
l!l&&r!l&&!r
040
MC/DC50%1/2
ref $col2 or $col2 =~ /\./
ref $col2 $col2 =~ /\./
MC/DC0%0/2
$table_alias || $self->{'dbix_lite'}->_quote($table_name)
$table_alias $self->{'dbix_lite'}->_quote($table_name)
Truth table66%2/3
A or B
A= ref $col2B= $col2 =~ /\./
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $table_aliasB= $self->{'dbix_lite'}->_quote($table_name)
ABresult
1X1
011
000
219 unless ref $col2 || $col2 =~ /\./;
220
221 # in case the second item is a scalar reference (literal SQL)
222 # or a hashref (search condition), pass it unchanged
223 5 ▶ $cond{$col1} = ref($col2) ? $col2 : \ "= $col2";
Branch: 1 branch
BranchTrueFalseTotal
ref $col2 ? : 1 4 5
224 }
225
226 # store the join definition according to the SQL::Abstract::More syntax
227 push @joins, {
228 4 ▶ operator => $join->{join_type} eq 'inner' ? '<=>' : '=>',
Branch: 1 branch
BranchTrueFalseTotal
$join->{'join_type'} eq 'inner' ? : 1 3 4
229 condition => \%cond,
230 };
231 4 ▶ push @joins, $table_name . ($table_alias ? "|$table_alias" : "");
Branch: 1 branch
BranchTrueFalseTotal
$table_alias ? : 0 4 4
232 }
233
234 # from
235 25 my @from = ();
236 25 ▶ if ($self->{from}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'from'}) { } 0 25 25
237 0 @from = @{$self->{from}};
238 } else {
239 25 @from = (-join => $self->{table}{name} . "|" . $self->{table_alias}, @joins);
240 }
241
242 # paging overrides limit and offset if any
243 25 ▶ if ($self->{page} && defined $self->{rows_per_page}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"page"} and defined $self->{"rows_per_page"}) 0 25 25
Condition33%1/3
$self->{"page"} and defined $self->{"rows_per_page"}
!ll&&!rl&&r
2500
MC/DC0%0/2
$self->{"page"} and defined $self->{"rows_per_page"}
$self->{"page"} defined $self->{"rows_per_page"}
Truth table33%1/3
A and B
A= $self->{"page"}B= defined $self->{"rows_per_page"}
ABresult
0X0
100
111
244 0 $self->{limit} = $self->{rows_per_page};
245 0 $self->{offset} = $self->pager->skipped;
246 }
247
248 # ordering
249 25 ▶ if ($self->{order_by}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"order_by"}) 8 17 25
250 $self->{order_by} = [$self->{order_by}]
251 8 ▶ unless ref $self->{order_by} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"order_by"} eq "ARRAY" 0 8 8
252 }
253
254 my ($sql, @bind) = $self->{dbix_lite}->{abstract}->select(
255 -columns => [ uniq @cols ],
256 -from => [ @from ],
257 -where => { -and => $self->{where} },
258 $self->{group_by} ? (-group_by => $self->{group_by}) : (),
259 $self->{having} ? (-having => $self->{having}) : (),
260 $self->{order_by} ? (-order_by => $self->{order_by}) : (),
261 $self->{limit} ? (-limit => $self->{limit}) : (),
262 $self->{offset} ? (-offset => $self->{offset}) : (),
263 25 ▶ $self->{for} ? (-for => $self->{for}) : (),
Branch: 6 branches
BranchTrueFalseTotal
$self->{'group_by'} ? : 0 25 25
$self->{'having'} ? : 0 25 25
$self->{'order_by'} ? : 8 17 25
$self->{'limit'} ? : 0 25 25
$self->{'offset'} ? : 0 25 25
$self->{'for'} ? : 0 25 25
264 );
265  
266 25 ▶ if ($self->{distinct}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"distinct"}) 3 22 25
267 3 my $distinct_sql;
268 3 ▶ if (@{$self->{distinct}} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{'distinct'};} > 0) { } 2 1 3
269 2 ▶ my @cols = map { ref($_) ? $$_ : $self->{dbix_lite}->_quote($_) } @{$self->{distinct}};
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 1 1 2
270 2 $distinct_sql = sprintf "DISTINCT ON (%s)", join ', ', @cols;
271 } else {
272 1 $distinct_sql = "DISTINCT";
273 }
274 3 $sql =~ s/^SELECT /SELECT $distinct_sql /i;
275 }
276
277 25 return $self->_apply_with($sql, @bind);
278 }
279  
280 39 ▶sub _apply_with {
Subroutine _apply_with: called CC=4 SCAR=13.9
281 39 my ($self, $sql, @bind) = @_;
282
283 39 ▶ return ($sql, @bind) if !$self->{with};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"with"} 34 5 39
284
285 5 my @with_sql = ();
286 5 foreach my $alias (keys %{$self->{with}}) {
287 5 my $def = $self->{with}{$alias};
288
289 5 ▶ if (ref $$def eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $$def eq 'ARRAY') { } 1 4 5
290 1 my ($wsql, @wbind) = @$$def;
291 1 push @with_sql, sprintf "%s AS (%s)", $alias, $wsql;
292 1 unshift @bind, @wbind;
293 } else {
294 4 push @with_sql, sprintf "%s AS (%s)", $alias, $$def;
295 }
296 }
297 5 $sql = sprintf 'WITH %s %s', join(', ', @with_sql), $sql;
298
299 5 return ($sql, @bind);
300 }
301  
302 16 ▶sub select_sth {
Subroutine select_sth: called CC=2 SCAR=7.1
Pod documented
303 16 my $self = shift;
304
305 16 my ($sql, @bind) = $self->select_sql;
306 16 ▶ return $self->{dbix_lite}->dbh->prepare($sql) || undef, @bind;
Condition50%1/2
$self->{'dbix_lite'}->dbh->prepare($sql) || undef
l!l
160
MC/DC0%0/1
$self->{'dbix_lite'}->dbh->prepare($sql) || undef
$self->{'dbix_lite'}->dbh->prepare($sql)
Truth table50%1/2
A
A= $self->{'dbix_lite'}->dbh->prepare($sql)
Aresult
11
00
307 }
308  
309 7 ▶sub _select_sth_for_object {
Subroutine _select_sth_for_object: called CC=10 SCAR=23.5
310 7 my $self = shift;
311
312 # check whether any of the selected columns is a scalar ref
313 7 my $cur_table_prefix = $self->_table_alias($self->{cur_table}{name}, 'select');
314 7 my $have_scalar_ref = 0;
315 7 my $have_star = 0;
316 7 ▶ my $select = $self->{select} // [$self->{table_alias} . '.*'];
Condition100%2/2
$self->{"select"} // [$self->{"table_alias"} . '.*']
l!l
16
MC/DC100%1/1
$self->{"select"} // [$self->{"table_alias"} . '.*']
$self->{"select"}
Truth table100%2/2
A
A= $self->{"select"}
Aresult
11
00
317 7 foreach my $col (grep defined $_, @$select) {
318 7 ▶ my $expr = ref($col) eq 'ARRAY' ? $col->[0] : $col;
Branch: 1 branch
BranchTrueFalseTotal
ref $col eq 'ARRAY' ? : 0 7 7
319 7 ▶ if (ref($expr) eq 'SCALAR') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $expr eq 'SCALAR') { } 0 7 7
elsif ($expr eq "$cur_table_prefix.*") { } 6 1 7
320 0 $have_scalar_ref = 1;
321 } elsif ($expr eq "$cur_table_prefix.*") {
322 6 $have_star = 1;
323 }
324 }
325
326 # always retrieve our primary key if provided and no col name is a scalar ref
327 # also skip this if we are retrieving all columns (me.*)
328 7 ▶ if (!$have_scalar_ref && !$have_star && (my @pk = $self->{cur_table}->pk)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $have_scalar_ref || $have_star and my(@pk) = $self->{"cur_table"}->pk) 1 6 7
Condition66%4/6
not $have_scalar_ref || $have_star and my(@pk) = $self->{"cur_table"}->pk
!ll&&!rl&&r
601
$have_scalar_ref || $have_star
l!l&&r!l&&!r
061
MC/DC33%1/3
not $have_scalar_ref || $have_star and my(@pk) = $self->{"cur_table"}->pk
$have_scalar_ref $have_star my(@pk) = $self->{"cur_table"}->pk
Truth table50%2/4
not(A || B) and C
A= $have_scalar_refB= $have_starC= my(@pk) = $self->{"cur_table"}->pk
ABCresult
1XX0
01X0
0000
0011
329 # prepend table alias to all pk columns
330 1 $_ =~ s/^[^.]+$/$cur_table_prefix\.$&/ for @pk;
331
332 # append instead of prepend, otherwise get_column() on a non-PK column
333 # would return the wrong values
334 1 $self = $self->select_also(@pk);
335 }
336
337 7 return $self->select_sth;
338 }
339  
340 12 ▶sub insert_sql {
Subroutine insert_sql: called CC=10 SCAR=24.3
Pod documented
341 12 my $self = shift;
342 12 my $insert_cols = shift;
343 12 ▶ ref $insert_cols eq 'HASH' or croak "insert_sql() requires a hashref";
Branch: 1 branch
BranchTrueFalseTotal
ref $insert_cols eq "HASH" or croak("insert_sql() requires a hashref") 12 0 12
344
345 12 ▶ if (@{$self->{joins}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"joins"};}) 0 12 12
346 0 warn "Attempt to call ->insert() after joining other tables\n";
347 }
348  
349 12 ▶ if ($self->{from} && !%$insert_cols) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"from"} and not %$insert_cols) 1 11 12
Condition100%3/3
$self->{"from"} and not %$insert_cols
!ll&&!rl&&r
921
MC/DC100%2/2
$self->{"from"} and not %$insert_cols
$self->{"from"} not %$insert_cols
Truth table100%3/3
A and not(B)
A= $self->{"from"}B= not %$insert_cols
ABresult
0X0
100
111
350 1 croak "insert() with from() requires a non-empty hashref";
351 }
352
353 11 my ($sql, @bind);
354 11 ▶ if (!%$insert_cols && $self->{dbix_lite}->driver_name eq 'Pg') {
Branch: 1 branch
BranchTrueFalseTotal
if (not %$insert_cols and $self->{'dbix_lite'}->driver_name eq 'Pg') { } 0 11 11
Condition33%1/3
not %$insert_cols and $self->{"dbix_lite"}->driver_name eq "Pg"
!ll&&!rl&&r
1100
MC/DC0%0/2
not %$insert_cols and $self->{"dbix_lite"}->driver_name eq "Pg"
not %$insert_cols $self->{"dbix_lite"}->driver_name eq "Pg"
Truth table33%1/3
not(A) and B
A= not %$insert_colsB= $self->{"dbix_lite"}->driver_name eq "Pg"
ABresult
0X0
100
111
355 # Postgres doesn't support the VALUES () syntax
356 $sql = sprintf "INSERT INTO %s DEFAULT VALUES",
357 0 $self->{dbix_lite}->_quote($self->{table}{name});
358 } else {
359 ($sql, @bind) = $self->{dbix_lite}->{abstract}->insert(
360 11 $self->{table}{name}, $insert_cols,
361 );
362 11 ▶ if ($self->{from}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"from"}) 2 9 11
363 my $from_sql = join ', ', map {
364 2 ▶ ref($_) ? $$_ : $_
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 2 2
365 2 } @{$self->{from}};
366 2 ▶ $sql =~ s/\s+VALUES\s+\(\s*(.*?)\s*\)\s*\z/ SELECT $1 FROM $from_sql/s
Branch: 1 branch
BranchTrueFalseTotal
$sql =~ s/\s+VALUES\s+\(\s*(.*?)\s*\)\s*\z/ SELECT $1 FROM $from_sql/s or croak("Failed to rewrite INSERT for from()") 2 0 2
367 or croak "Failed to rewrite INSERT for from()";
368 }
369 }
370
371 11 return $self->_apply_with($sql, @bind);
372 }
373  
374 8 ▶sub insert_sth {
Subroutine insert_sth: called CC=3 SCAR=11.3
Pod documented
375 8 my $self = shift;
376 8 my $insert_cols = shift;
377 8 ▶ ref $insert_cols eq 'HASH' or croak "insert_sth() requires a hashref";
Branch: 1 branch
BranchTrueFalseTotal
ref $insert_cols eq "HASH" or croak("insert_sth() requires a hashref") 8 0 8
378
379 8 my ($sql, @bind) = $self->insert_sql($insert_cols);
380 8 ▶ return $self->{dbix_lite}->dbh->prepare($sql) || undef, @bind;
Condition50%1/2
$self->{'dbix_lite'}->dbh->prepare($sql) || undef
l!l
80
MC/DC0%0/1
$self->{'dbix_lite'}->dbh->prepare($sql) || undef
$self->{'dbix_lite'}->dbh->prepare($sql)
Truth table50%1/2
A
A= $self->{'dbix_lite'}->dbh->prepare($sql)
Aresult
11
00
381 }
382  
383 8 ▶sub insert {
Subroutine insert: called CC=5 SCAR=18.0
Pod documented
384 8 my $self = shift;
385 8 my $insert_cols = shift;
386 8 ▶ ref $insert_cols eq 'HASH' or croak "insert() requires a hashref";
Branch: 1 branch
BranchTrueFalseTotal
ref $insert_cols eq "HASH" or croak("insert() requires a hashref") 8 0 8
387
388 # perform operation
389 8 my $res;
390 8 ▶ $self->{dbix_lite}->dbh_do(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
391 8 my ($sth, @bind) = $self->insert_sth($insert_cols);
392 8 $res = $sth->execute(@bind);
393 8 });
394 8 ▶ return undef if !$res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 8 8
395
396 # populate the autopk field if any
397 8 ▶ if (my $pk = $self->{table}->autopk) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pk = $self->{"table"}->autopk) 0 8 8
398 0 $insert_cols = clone $insert_cols;
399 $insert_cols->{$pk} = $self->{dbix_lite}->_autopk($self->{table}{name})
400 0 ▶ if !exists $insert_cols->{$pk};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $insert_cols->{$pk} 0 0 0
401 }
402
403 # return a DBIx::Lite::Row object with the inserted values
404 8 return $self->_inflate_row($insert_cols);
405 }
406  
407 2 ▶sub update_sql {
Subroutine update_sql: called CC=5 SCAR=23.4
Pod documented
408 2 my $self = shift;
409 2 my $update_cols = shift;
410 2 ▶ ref $update_cols eq 'HASH' or croak "update_sql() requires a hashref";
Branch: 1 branch
BranchTrueFalseTotal
ref $update_cols eq "HASH" or croak("update_sql() requires a hashref") 2 0 2
411
412 2 my $update_where = { -and => $self->{where} };
413
414 2 ▶ if ($self->{cur_table}{name} ne $self->{table}{name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cur_table"}{"name"} ne $self->{"table"}{"name"}) 0 2 2
415 my @pk = $self->{cur_table}->pk
416 0 ▶ or croak "No primary key defined for " . $self->{cur_table}{name} . "; cannot update using relationships";
Branch: 1 branch
BranchTrueFalseTotal
my(@pk) = $self->{"cur_table"}->pk or croak('No primary key defined for ' . $self->{"cur_table"}{"name"} . '; cannot update using relationships') 0 0 0
417 0 ▶ @pk == 1
Branch: 1 branch
BranchTrueFalseTotal
@pk == 1 or croak("Update across relationships is not allowed with multi-column primary keys") 0 0 0
418 or croak "Update across relationships is not allowed with multi-column primary keys";
419
420 0 my $fq_pk = $self->_table_alias($self->{cur_table}{name}, 'update') . "." . $pk[0];
421 # CTEs are applied on the outer UPDATE; strip them from the subquery
422 0 my $subquery = $self->select($pk[0]);
423 0 $subquery->{with} = undef;
424 0 $update_where = {
425 $fq_pk => {
426 -in => \[ $subquery->select_sql ],
427 },
428 };
429 }
430
431 return $self->_apply_with(
432 $self->{dbix_lite}->{abstract}->update(
433 2 -table => $self->_table_alias_expr($self->{cur_table}{name}, 'update'),
434 -set => $update_cols,
435 -where => $update_where,
436 )
437 );
438 }
439  
440 1 ▶sub update_sth {
Subroutine update_sth: called CC=3 SCAR=11.3
Pod documented
441 1 my $self = shift;
442 1 my $update_cols = shift;
443 1 ▶ ref $update_cols eq 'HASH' or croak "update_sth() requires a hashref";
Branch: 1 branch
BranchTrueFalseTotal
ref $update_cols eq "HASH" or croak("update_sth() requires a hashref") 1 0 1
444
445 1 my ($sql, @bind) = $self->update_sql($update_cols);
446 1 ▶ return $self->{dbix_lite}->dbh->prepare($sql) || undef, @bind;
Condition50%1/2
$self->{'dbix_lite'}->dbh->prepare($sql) || undef
l!l
10
MC/DC0%0/1
$self->{'dbix_lite'}->dbh->prepare($sql) || undef
$self->{'dbix_lite'}->dbh->prepare($sql)
Truth table50%1/2
A
A= $self->{'dbix_lite'}->dbh->prepare($sql)
Aresult
11
00
447 }
448  
449 1 ▶sub update {
Subroutine update: called CC=2 SCAR=7.0
Pod documented
450 1 my $self = shift;
451 1 my $update_cols = shift;
452 1 ▶ ref $update_cols eq 'HASH' or croak "update() requires a hashref";
Branch: 1 branch
BranchTrueFalseTotal
ref $update_cols eq "HASH" or croak("update() requires a hashref") 1 0 1
453
454 1 my $affected_rows;
455 1 ▶ $self->{dbix_lite}->dbh_do(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
456 1 my ($sth, @bind) = $self->update_sth($update_cols);
457 1 $affected_rows = $sth->execute(@bind);
458 1 });
459 1 return $affected_rows;
460 }
461  
462 0 ▶sub find_or_insert {
Subroutine find_or_insert: not called CC=6 SCAR=37.4
Pod documented
463 0 my $self = shift;
464 0 my $cols = shift;
465 0 ▶ ref $cols eq 'HASH' or croak "find_or_insert() requires a hashref";
Branch: 1 branch
BranchTrueFalseTotal
ref $cols eq "HASH" or croak("find_or_insert() requires a hashref") 0 0 0
466
467 0 my $sub;
468 my $object;
469  
470 0 my $driver_name = $self->{dbix_lite}->driver_name;
471  
472 # does the database support the PosttgreSQL "ON CONFLICT ... DO
473 # ..." UPSERT clause as well as the RETURNING clause
474 my $pg_upsert =
475 0 ▶ ( $driver_name eq 'Pg' && $self->{dbix_lite}->dbh->{pg_server_version} > 10_00_00 )
Condition0%0/9
$driver_name eq 'Pg' && $self->{'dbix_lite'}->dbh->{'pg_server_version'} > 100000 || $driver_name eq 'SQLite' && DBD::SQLite::Constants::SQLITE_VERSION_NUMBER() >= 3035000
l!l&&r!l&&!r
000
$driver_name eq "Pg" and $self->{"dbix_lite"}->dbh->{"pg_server_version"} > 100000
!ll&&!rl&&r
000
$driver_name eq "SQLite" and DBD::SQLite::Constants::SQLITE_VERSION_NUMBER() >= 3035000
!ll&&!rl&&r
000
MC/DC0%0/4
$driver_name eq 'Pg' && $self->{'dbix_lite'}->dbh->{'pg_server_version'} > 100000 || $driver_name eq 'SQLite' && DBD::SQLite::Constants::SQLITE_VERSION_NUMBER() >= 3035000
$driver_name eq "Pg" $self->{"dbix_lite"}->dbh->{"pg_server_version"} > 100000 $driver_name eq "SQLite" DBD::SQLite::Constants::SQLITE_VERSION_NUMBER() >= 3035000
Truth table0%0/7
A and B || C and D
A= $driver_name eq "Pg"B= $self->{"dbix_lite"}->dbh->{"pg_server_version"} > 100000C= $driver_name eq "SQLite"D= DBD::SQLite::Constants::SQLITE_VERSION_NUMBER() >= 3035000
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
476 ||
477 ($driver_name eq 'SQLite' && DBD::SQLite::Constants::SQLITE_VERSION_NUMBER() >= 3_035_000 );
478  
479 0 ▶ if ( $pg_upsert ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pg_upsert) { } 0 0 0
480 0 ▶ $sub = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
481 0 my ( $sql, @bind ) = $self->insert_sql( $cols );
482 0 $sql .= ' on conflict do nothing returning *';
483 0 my $sth = $self->{dbix_lite}->dbh->prepare( $sql );
484 0 ▶ return if !$sth->execute( @bind );
Branch: 1 branch
BranchTrueFalseTotal
unless $sth->execute(@bind) 0 0 0
485 0 my $hash = $sth->fetchrow_hashref;
486 0 ▶ $object = $hash ? $self->_inflate_row( $hash ) : $self->find( $cols );
Branch: 1 branch
BranchTrueFalseTotal
$hash ? : 0 0 0
487 0 };
488 }
489 else {
490 0 ▶ $sub = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
491 0 ▶ if (!($object = $self->find($cols))) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $object = $self->find($cols)) 0 0 0
492 0 $object = $self->insert($cols);
493 }
494 0 };
495 }
496 0 $self->{dbix_lite}->txn( $sub );
497 0 return $object;
498 }
499  
500 1 ▶sub delete_sql {
Subroutine delete_sql: called CC=4 SCAR=22.2
Pod documented
501 1 my $self = shift;
502
503 1 my $delete_where = { -and => $self->{where} };
504
505 1 ▶ if ($self->{cur_table}{name} ne $self->{table}{name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cur_table"}{"name"} ne $self->{"table"}{"name"}) 0 1 1
506 my @pk = $self->{cur_table}->pk
507 0 ▶ or croak "No primary key defined for " . $self->{cur_table}{name} . "; cannot delete using relationships";
Branch: 1 branch
BranchTrueFalseTotal
my(@pk) = $self->{"cur_table"}->pk or croak('No primary key defined for ' . $self->{"cur_table"}{"name"} . '; cannot delete using relationships') 0 0 0
508 0 ▶ @pk == 1
Branch: 1 branch
BranchTrueFalseTotal
@pk == 1 or croak("Delete across relationships is not allowed with multi-column primary keys") 0 0 0
509 or croak "Delete across relationships is not allowed with multi-column primary keys";
510
511 0 my $fq_pk = $self->_table_alias($self->{cur_table}{name}, 'delete') . "." . $pk[0];
512 # CTEs are applied on the outer DELETE; strip them from the subquery
513 0 my $subquery = $self->select($pk[0]);
514 0 $subquery->{with} = undef;
515 0 $delete_where = {
516 $fq_pk => {
517 -in => \[ $subquery->select_sql ],
518 },
519 };
520 }
521
522 return $self->_apply_with(
523 $self->{dbix_lite}->{abstract}->delete(
524 1 -from => $self->_table_alias_expr($self->{cur_table}{name}, 'delete'),
525 -where => $delete_where,
526 )
527 );
528 }
529  
530 0 ▶sub delete_sth {
Subroutine delete_sth: not called CC=2 SCAR=17.9
Pod documented
531 0 my $self = shift;
532
533 0 my ($sql, @bind) = $self->delete_sql;
534 0 ▶ return $self->{dbix_lite}->dbh->prepare($sql) || undef, @bind;
Condition0%0/2
$self->{'dbix_lite'}->dbh->prepare($sql) || undef
l!l
00
MC/DC0%0/1
$self->{'dbix_lite'}->dbh->prepare($sql) || undef
$self->{'dbix_lite'}->dbh->prepare($sql)
Truth table0%0/2
A
A= $self->{'dbix_lite'}->dbh->prepare($sql)
Aresult
11
00
535 }
536  
537 0 ▶sub delete {
Subroutine delete: not called CC=1 SCAR=6.9
Pod documented
538 0 my $self = shift;
539
540 0 my $affected_rows;
541 0 ▶ $self->{dbix_lite}->dbh_do(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
542 0 my ($sth, @bind) = $self->delete_sth;
543 0 $affected_rows = $sth->execute(@bind);
544 0 });
545 0 return $affected_rows;
546 }
547  
548 7 ▶sub single {
Subroutine single: called CC=2 SCAR=7.0
Pod documented
549 7 my $self = shift;
550
551 7 my $row;
552 7 ▶ $self->{dbix_lite}->dbh_do(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
553 7 my ($sth, @bind) = $self->_select_sth_for_object;
554 7 $sth->execute(@bind);
555 7 $row = $sth->fetchrow_hashref;
556 7 });
557 7 ▶ return $row ? $self->_inflate_row($row) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$row ? : 7 0 7
558 }
559  
560 0 ▶sub single_value {
Subroutine single_value: not called CC=1 SCAR=6.9
Pod documented
561 0 my $self = shift;
562
563 0 my $value;
564 0 ▶ $self->{dbix_lite}->dbh_do(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
565 0 my ($sth, @bind) = $self->select_sth;
566 0 $sth->execute(@bind);
567 0 ($value) = $sth->fetchrow_array;
568 0 });
569 0 return $value;
570 }
571  
572 0 ▶sub all {
Subroutine all: not called CC=1 SCAR=6.9
Pod documented
573 0 my $self = shift;
574
575 0 my $rows;
576 0 ▶ $self->{dbix_lite}->dbh_do(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
577 0 my ($sth, @bind) = $self->_select_sth_for_object;
578 0 $sth->execute(@bind);
579 0 $rows = $sth->fetchall_arrayref({});
580 0 });
581 0 return map $self->_inflate_row($_), @$rows;
582 }
583  
584 0 ▶sub next {
Subroutine next: not called CC=3 SCAR=24.8
Pod documented
585 0 my $self = shift;
586
587 0 ▶ $self->{dbix_lite}->dbh_do(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
588 0 ($self->{sth}, my @bind) = $self->_select_sth_for_object;
589 0 $self->{sth}->execute(@bind);
590 0 ▶ }) if !$self->{sth};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"sth"} 0 0 0
591
592 0 ▶ my $row = $self->{sth}->fetchrow_hashref or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $row = $self->{"sth"}->fetchrow_hashref or return undef 0 0 0
593 0 return $self->_inflate_row($row);
594 }
595  
596 7 ▶sub count {
Subroutine count: called CC=1 SCAR=0.0
Pod documented
597 7 my $self = shift;
598
599 7 my $count;
600 7 ▶ $self->{dbix_lite}->dbh_do(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
601 # Postgres throws an error when using ORDER BY clauses with COUNT(*)
602 # create a column alias so that if SQL::Abstract::More quotes columns
603 # it'll quote the column "count" rather than the expression "COUNT(*)"
604 7 my $count_rs = $self->select( [\'COUNT(*)', 'dbix_lite_rs_count' ])->order_by(undef);
605 7 my ($sth, @bind) = $count_rs->select_sth;
606 7 $sth->execute(@bind);
607 7 $count = +($sth->fetchrow_array)[0];
608 7 });
609 7 return $count;
610 }
611  
612 2 ▶sub column_names {
Subroutine column_names: called CC=3 SCAR=11.0
Pod documented
613 2 my $self = shift;
614  
615 2 ▶ $self->{dbix_lite}->dbh_do(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
616 2 ($self->{sth}, my @bind) = $self->select_sth;
617 2 $self->{sth}->execute(@bind);
618 2 ▶ }) if !$self->{sth};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"sth"} 2 0 2
619  
620 2 my $c = $self->{sth}->{NAME};
621 2 ▶ return wantarray ? @$c : $c;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
622 }
623  
624 1 ▶sub get_column {
Subroutine get_column: called CC=2 SCAR=6.9
Pod documented
625 1 my $self = shift;
626 1 ▶ my $column_name = shift or croak "get_column() requires a column name";
Branch: 1 branch
BranchTrueFalseTotal
my $column_name = shift() or croak("get_column() requires a column name") 1 0 1
627
628 1 my @values = ();
629 1 ▶ $self->{dbix_lite}->dbh_do(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
630 1 my $rs = ($self->_clone)->select($column_name);
631 1 my ($sql, @bind) = $rs->select_sql;
632
633 1 @values = @{$self->{dbix_lite}->dbh->selectcol_arrayref($sql, {}, @bind)};
634 1 });
635 1 return @values;
636 }
637  
638 1 ▶sub inner_join {
Subroutine inner_join: called CC=1 SCAR=0.0
Pod documented
639 1 my $self = shift;
640 1 return $self->_join('inner', @_);
641 }
642  
643 3 ▶sub left_join {
Subroutine left_join: called CC=1 SCAR=0.0
Pod documented
644 3 my $self = shift;
645 3 return $self->_join('left', @_);
646 }
647  
648 4 ▶sub _join {
Subroutine _join: called CC=10 SCAR=35.8
649 4 my $self = shift;
650 4 my ($type, $table_name, $condition, $options) = @_;
651 4 ▶ $options ||= {};
Condition50%1/2
$options ||= {}
l!l
04
MC/DC0%0/1
$options ||= {}
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
652 4 ▶ $table_name = [ $table_name, undef ] if ref $table_name ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $table_name ne "ARRAY" 4 0 4
653
654 4 my $new_self = $self->_clone;
655
656 # if user asked for duplicate join removal, check whether no joins
657 # with the same table alias exist
658 4 ▶ if ($options->{prevent_duplicates}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"prevent_duplicates"}) 0 4 4
659 0 foreach my $join (@{$self->{joins}}) {
660 0 ▶ if ((defined($table_name->[1]) && defined $join->{table}[1] && $table_name->[1] eq $join->{table}[1])
Branch: 1 branch
BranchTrueFalseTotal
if (defined $table_name->[1] and defined $join->{"table"}[1] and $table_name->[1] eq $join->{"table"}[1] or not defined $table_name->[1] and $table_name->[0] eq $join->{"table"}[0]) 0 0 0
Condition0%0/12
defined $table_name->[1] and defined $join->{"table"}[1] and $table_name->[1] eq $join->{"table"}[1] or not defined $table_name->[1] and $table_name->[0] eq $join->{"table"}[0]
l!l&&r!l&&!r
000
defined $table_name->[1] and defined $join->{"table"}[1] and $table_name->[1] eq $join->{"table"}[1]
!ll&&!rl&&r
000
defined $table_name->[1] and defined $join->{"table"}[1]
!ll&&!rl&&r
000
not defined $table_name->[1] and $table_name->[0] eq $join->{"table"}[0]
!ll&&!rl&&r
000
MC/DC0%0/5
defined $table_name->[1] and defined $join->{"table"}[1] and $table_name->[1] eq $join->{"table"}[1] or not defined $table_name->[1] and $table_name->[0] eq $join->{"table"}[0]
defined $table_name->[1] defined $join->{"table"}[1] $table_name->[1] eq $join->{"table"}[1] not defined $table_name->[1] $table_name->[0] eq $join->{"table"}[0]
Truth table0%0/10
A and B and C or not(D) and E
A= defined $table_name->[1]B= defined $join->{"table"}[1]C= $table_name->[1] eq $join->{"table"}[1]D= not defined $table_name->[1]E= $table_name->[0] eq $join->{"table"}[0]
ABCDEresult
111XX1
0XX111
10X111
110111
0XX0X0
0XX100
10X0X0
10X100
1100X0
110100
661 || (!defined($table_name->[1]) && $table_name->[0] eq $join->{table}[0])) {
662 0 return $new_self;
663 }
664 }
665 }
666
667 4 push @{$new_self->{joins}}, {
668 join_type => $type,
669 cur_table => $self->{cur_table},
670 4 table => $table_name,
671 condition => $condition,
672 };
673
674 4 $new_self;
675 }
676  
677 0 ▶sub clear_joins {
Subroutine clear_joins: not called CC=1 SCAR=6.9
Pod documented
678 0 my $self = shift;
679
680 0 my $new_self = $self->_clone;
681 0 @{$new_self->{joins}} = ();
682 0 $new_self;
683 }
684  
685 39 ▶sub _table_alias {
Subroutine _table_alias: called CC=9 SCAR=22.9
686 39 my $self = shift;
687 39 my ($table_name, $op) = @_;
688
689 39 my $driver_name = $self->{dbix_lite}->driver_name;
690
691 39 ▶ if ($table_name eq $self->{table}{name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($table_name eq $self->{"table"}{"name"}) 39 0 39
692 39 ▶ if ($op eq 'select'
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "select" or $op eq "update" and ($driver_name // "") =~ /^(?:MySQL|Pg)$/i or $op eq "delete" and ($driver_name // "") =~ /^Pg$/i) 36 3 39
Condition68%11/16
$op eq "select" or $op eq "update" and ($driver_name // "") =~ /^(?:MySQL|Pg)$/i or $op eq "delete" and ($driver_name // "") =~ /^Pg$/i
l!l&&r!l&&!r
3603
$op eq "select" or $op eq "update" and ($driver_name // "") =~ /^(?:MySQL|Pg)$/i
l!l&&r!l&&!r
3603
$op eq "update" and ($driver_name // "") =~ /^(?:MySQL|Pg)$/i
!ll&&!rl&&r
120
$driver_name // ""
l!l
11
$op eq "delete" and ($driver_name // "") =~ /^Pg$/i
!ll&&!rl&&r
210
$driver_name // ""
l!l
01
MC/DC20%1/5
$op eq "select" or $op eq "update" and ($driver_name // "") =~ /^(?:MySQL|Pg)$/i or $op eq "delete" and ($driver_name // "") =~ /^Pg$/i
$op eq "select" $op eq "update" $driver_name $op eq "delete" $driver_name
Truth table37%3/8
A or B and C or D and E
A= $op eq "select"B= $op eq "update"C= $driver_nameD= $op eq "delete"E= $driver_name
ABCDEresult
1XXXX1
011XX1
00X111
010111
00X0X0
00X100
0100X0
010100
693 || ($op eq 'update' && ($driver_name // '') =~ /^(?:MySQL|Pg)$/i)
694 || ($op eq 'delete' && ($driver_name // '') =~ /^Pg$/i)) {
695 36 return $self->{table_alias};
696 }
697 }
698
699 3 return $table_name;
700 }
701  
702 3 ▶sub _table_alias_expr {
Subroutine _table_alias_expr: called CC=6 SCAR=29.9
703 3 my $self = shift;
704 3 my ($table_name, $op) = @_;
705
706 3 my $table_alias = $self->_table_alias($table_name, $op);
707  
708 3 ▶ if ($table_name eq $table_alias) {
Branch: 1 branch
BranchTrueFalseTotal
if ($table_name eq $table_alias) { } 3 0 3
709 # foo
710 3 return $table_name;
711 } else {
712  
713 # As of SQL::Abstract::More v1.44, table alias syntax works for select, update, delete
714 0 ▶ my $supported_op = $op eq 'select' || $op eq 'update' || $op eq 'delete';
Condition0%0/6
$op eq 'select' || $op eq 'update' || $op eq 'delete'
l!l&&r!l&&!r
000
$op eq "select" or $op eq "update"
l!l&&r!l&&!r
000
MC/DC0%0/3
$op eq 'select' || $op eq 'update' || $op eq 'delete'
$op eq "select" $op eq "update" $op eq 'delete'
Truth table0%0/4
A or B || C
A= $op eq "select"B= $op eq "update"C= $op eq 'delete'
ABCresult
1XX1
01X1
0011
0000
715 0 ▶ if ( $supported_op && version->parse(SQL::Abstract::More->VERSION) >= version->parse('1.44') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($supported_op and 'version'->parse('SQL::Abstract::More'->VERSION) >= 'version'->parse('1.44')) { } 0 0 0
Condition0%0/3
$supported_op and "version"->parse("SQL::Abstract::More"->VERSION) >= "version"->parse("1.44")
!ll&&!rl&&r
000
MC/DC0%0/2
$supported_op and "version"->parse("SQL::Abstract::More"->VERSION) >= "version"->parse("1.44")
$supported_op "version"->parse("SQL::Abstract::More"->VERSION) >= "version"->parse("1.44")
Truth table0%0/3
A and B
A= $supported_opB= "version"->parse("SQL::Abstract::More"->VERSION) >= "version"->parse("1.44")
ABresult
0X0
100
111
716 0 return "$table_name|$table_alias";
717 }
718 else {
719 # foo AS my_foo
720 0 return $self->{dbix_lite}->{abstract}->table_alias($table_name, $table_alias);
721 }
722  
723 }
724  
725 }
726  
727 15 ▶sub _inflate_row {
Subroutine _inflate_row: called CC=9 SCAR=22.4
728 15 my $self = shift;
729 15 my ($hashref) = @_;
730
731 # get the row package
732 15 ▶ my $package = $self->{cur_table}{class} || 'DBIx::Lite::Row';
Condition100%2/2
$self->{'cur_table'}{'class'} || 'DBIx::Lite::Row'
l!l
114
MC/DC100%1/1
$self->{'cur_table'}{'class'} || 'DBIx::Lite::Row'
$self->{'cur_table'}{'class'}
Truth table100%2/2
A
A= $self->{'cur_table'}{'class'}
Aresult
11
00
733
734 # get the constructor, if any
735 15 my $constructor = $self->{cur_table}{class_constructor};
736 15 ▶ if (!defined $constructor && $package->can('new')) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $constructor and $package->can("new")) 0 15 15
Condition66%2/3
not defined $constructor and $package->can("new")
!ll&&!rl&&r
1140
MC/DC0%0/2
not defined $constructor and $package->can("new")
not defined $constructor $package->can("new")
Truth table66%2/3
not(A) and B
A= not defined $constructorB= $package->can("new")
ABresult
0X0
100
111
737 0 $constructor = 'new';
738 }
739
740 # create the object
741 15 my $object;
742 15 ▶ if (defined $constructor) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $constructor) { } 1 14 15
743 1 ▶ if (ref($constructor) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $constructor eq 'CODE') { } 0 1 1
744 0 $object = $constructor->($hashref);
745 } else {
746 1 $object = $package->$constructor;
747 }
748 } else {
749 14 $object = {};
750 14 bless $object, $package;
751 }
752
753 # get the hashref where we are going to store our data
754 15 my $storage;
755 15 ▶ if (my $method = $self->{cur_table}{class_storage}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $method = $self->{'cur_table'}{'class_storage'}) { } 1 14 15
756 1 ▶ croak "No ${package}::${method} method exists"
Branch: 1 branch
BranchTrueFalseTotal
unless $package->can($method) 0 1 1
757 if !$package->can($method);
758 1 $storage = $object->$method;
759 1 ▶ croak "${package}::${method}() did not return a hashref"
Branch: 1 branch
BranchTrueFalseTotal
if ref $storage ne "HASH" 0 1 1
760 if ref($storage) ne 'HASH';
761 } else {
762 14 $storage = $object;
763 }
764
765 # store our data
766 15 $storage->{dbix_lite} = $self->{dbix_lite};
767 15 $storage->{table} = $self->{cur_table};
768 15 $storage->{data} = $hashref;
769
770 15 return $object;
771 }
772  
773 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=4 SCAR=30.0
774 0 ▶ my $self = shift or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return undef 0 0 0
775
776 # Get the called method name and trim off the namespace
777 0 (my $method = $AUTOLOAD) =~ s/.*:://;
778
779 0 ▶ if (my $rel = $self->{cur_table}{has_many}{$method}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rel = $self->{"cur_table"}{"has_many"}{$method}) 0 0 0
780 0 my $new_self = $self->inner_join($rel->[0], $rel->[1])->select("$method.*");
781 0 $new_self->{cur_table} = $self->{dbix_lite}->schema->table($rel->[0]);
782 0 ▶ bless $new_self, $new_self->{cur_table}->resultset_class || __PACKAGE__;
Condition0%0/2
$new_self->{'cur_table'}->resultset_class || __PACKAGE__
l!l
00
MC/DC0%0/1
$new_self->{'cur_table'}->resultset_class || __PACKAGE__
$new_self->{'cur_table'}->resultset_class
Truth table0%0/2
A
A= $new_self->{'cur_table'}->resultset_class
Aresult
11
00
783 0 return $new_self;
784 }
785
786 0 croak "No $method method is provided by this " . ref($self) . " object";
787 }
788  
789 0 ▶sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
790  
791 7 1;
792  
793 __END__
↑ Summary Lite/Row.pm »