Lite/ResultSet.pm

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