blib/lib/DBIx/DBO/Row.pm

filter: Statementstmt 90.8% 188 / 207 Branchbran 69.6% 71 / 102 Conditioncond 50.6% 41 / 81 MC/DCmcdc 23.9% 11 / 46 Subroutinesub 97.2% 35 / 36 Podpod 100% 14 / 14 totaltotal 74.0% 360 / 486 CC 88 SCAR 23.5
CC
88
Cov
77%
_check_alias
42.0
_column_idx
27.0
_inner_col
25.1
CRAP 10.5
« Test/DBO.pm ↑ Summary blib/lib/DBIx/DBO/Query.pm »
1 package DBIx::DBO::Row;
2  
3 14 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 14 ▶use DBIx::DBO;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 14 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 14 ▶use Scalar::Util qw(blessed weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 14 ▶use Storable ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 14 ▶use overload '@{}' => sub {${$_[0]}->{array} || []}, '%{}' => sub {${$_[0]}->{hash}}, fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=2 SCAR=7.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
${$_[0];}->{"array"} or [] 4 0 4
12  
13 9 ▶sub query_class { ${$_[0]}->{DBO}->query_class }
Subroutine query_class: called CC=1 SCAR=0.0
Pod documented
14  
15 14 *_isa = \&DBIx::DBO::DBD::_isa;
16  
17 =head1 NAME
18  
19 DBIx::DBO::Row - An OO interface to SQL queries and results. Encapsulates a fetched row of data in an object.
20  
21 =head1 SYNOPSIS
22  
23 # Create a Row object for the `users` table
24 my $row = $dbo->row('users');
25
26 # Load my record
27 $row->load(login => 'vlyon') or die "Where am I?";
28
29 # Double my salary :)
30 $row->update(salary => {FUNC => '? * 2', COL => 'salary'});
31
32 # Print my email address
33 print $row->{email};
34
35 # Delete my boss
36 $row->load(id => $row->{boss_id})->delete or die "Can't kill the boss";
37  
38 =head1 METHODS
39  
40 =head3 C<new>
41  
42 DBIx::DBO::Row->new($dbo, $table);
43 DBIx::DBO::Row->new($dbo, $query_object);
44  
45 Create and return a new C<Row> object.
46 The object returned represents rows in the given table/query.
47 Can take the same arguments as L<DBIx::DBO::Query/new> or a L<Query|DBIx::DBO::Query> object can be used.
48  
49 =cut
50  
51 22 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
52 22 my $proto = shift;
53 22 ▶ eval { $_[0]->isa('DBIx::DBO') } or croak 'Invalid DBO Object for new Row';
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $_[0]->isa("DBIx::DBO") } } or croak("Invalid DBO Object for new Row") 21 1 22
54 21 ▶ my $class = ref($proto) || $proto;
Condition66%2/3
ref $proto || $proto
l!l&&r!l&&!r
1200
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
55 21 $class->_init(@_);
56 }
57  
58 21 ▶sub _init {
Subroutine _init: called CC=6 SCAR=18.0
59 21 my($class, $dbo, @args) = @_;
60  
61 21 my $me = bless \{ DBO => $dbo, array => undef, hash => {} }, $class;
62 21 ▶ my $parent = (@args == 1 and _isa($args[0], 'DBIx::DBO::Query'))
Branch: 1 branch
BranchTrueFalseTotal
@args == 1 && _isa($args[0], 'DBIx::DBO::Query') ? : 11 10 21
Condition100%3/3
@args == 1 and _isa($args[0], "DBIx::DBO::Query")
!ll&&!rl&&r
2811
MC/DC100%2/2
@args == 1 and _isa($args[0], "DBIx::DBO::Query")
@args == 1 _isa($args[0], "DBIx::DBO::Query")
Truth table100%3/3
A and B
A= @args == 1B= _isa($args[0], "DBIx::DBO::Query")
ABresult
0X0
100
111
63 ? $args[0]
64 : $me->query_class->new($dbo, @args);
65  
66 18 ▶ if ($parent->isa('DBIx::DBO::Query')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->isa('DBIx::DBO::Query')) { } 18 0 18
67 18 ▶ croak 'This query is from a different DBO connection' if $parent->{DBO} != $dbo;
Branch: 1 branch
BranchTrueFalseTotal
if $parent->{"DBO"} != $dbo 1 17 18
68 # We must weaken this to avoid a circular reference
69 17 $$me->{Parent} = $parent;
70 17 weaken $$me->{Parent};
71 # Add a weak ref onto the list of attached_rows to release freed rows
72 17 push @{ $$me->{Parent}{attached_rows} }, $me;
73 17 weaken $$me->{Parent}{attached_rows}[-1];
74 } else {
75 0 croak 'Invalid parent for new Row';
76 }
77 17 ▶ return wantarray ? ($me, $me->tables) : $me;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 16 17
78 }
79  
80 110 ▶sub _build_data {
Subroutine _build_data: called CC=2 SCAR=7.0
81 110 ▶ ${$_[0]}->{build_data} // ${$_[0]}->{Parent}{build_data};
Condition66%2/3
${$_[0];}->{"build_data"} // ${$_[0];}->{"Parent"}{"build_data"}
l!l&&r!l&&!r
10910
MC/DC0%0/2
${$_[0];}->{"build_data"} // ${$_[0];}->{"Parent"}{"build_data"}
${$_[0];}->{"build_data"} ${$_[0];}->{"Parent"}{"build_data"}
Truth table66%2/3
A // B
A= ${$_[0];}->{"build_data"}B= ${$_[0];}->{"Parent"}{"build_data"}
ABresult
1X1
011
000
82 }
83  
84 =head3 C<tables>
85  
86 Return a list of L<Table|DBIx::DBO::Table> objects for this row.
87  
88 =cut
89  
90 56 ▶sub tables {
Subroutine tables: called CC=2 SCAR=6.9
Pod documented
91 56 my $me = $_[0];
92 56 ▶ return @{ ${exists $$me->{Parent} ? $$me->{Parent} : $$me}{Tables} };
Branch: 1 branch
BranchTrueFalseTotal
exists $$me->{'Parent'} ? : 1 55 56
93 }
94  
95 18 ▶sub _table_idx {
Subroutine _table_idx: called CC=4 SCAR=14.9
96 18 my($me, $tbl) = @_;
97 18 ▶ my $tables = ${exists $$me->{Parent} ? $$me->{Parent} : $$me}{Tables};
Branch: 1 branch
BranchTrueFalseTotal
exists $$me->{'Parent'} ? : 0 18 18
98 18 for my $i (0 .. $#$tables) {
99 18 ▶ return $i if $tbl == $tables->[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $tbl == $tables->[$i] 18 0 18
100 }
101 0 return undef;
102 }
103  
104 20 ▶sub _table_alias {
Subroutine _table_alias: called CC=4 SCAR=14.1
105 20 my($me, $tbl) = @_;
106 20 ▶ return undef if $tbl == $me;
Branch: 1 branch
BranchTrueFalseTotal
if $tbl == $me 2 18 20
107 18 my $i = $me->_table_idx($tbl);
108 18 ▶ croak 'The table is not in this query' unless defined $i;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $i 0 18 18
109 18 ▶ return $me->tables > 1 ? 't'.($i + 1) : ();
Branch: 1 branch
BranchTrueFalseTotal
$me->tables > 1 ? : 0 18 18
110 }
111  
112 =head3 C<columns>
113  
114 Return a list of column names.
115  
116 =cut
117  
118 9 ▶sub columns {
Subroutine columns: called CC=5 SCAR=16.1
Pod documented
119 9 my($me) = @_;
120  
121 9 ▶ return $$me->{Parent}->columns if exists $$me->{Parent};
Branch: 1 branch
BranchTrueFalseTotal
if exists $$me->{"Parent"} 1 8 9
122  
123 $$me->{Columns} //= [
124 3 @{$me->_build_data->{select}}
125 ? map {
126 2 ▶ _isa($_, 'DBIx::DBO::Table', 'DBIx::DBO::Query') ? ($_->columns) : $me->_build_col_val_name(@$_)
Branch: 1 branch
BranchTrueFalseTotal
_isa($_, 'DBIx::DBO::Table', 'DBIx::DBO::Query') ? : 0 2 2
127 1 } @{$me->_build_data->{select}}
128 8 ▶ : map { $_->columns } $me->tables
Branch: 1 branch
BranchTrueFalseTotal
@{$me->_build_data->{'select'};} ? : 1 2 3
Condition100%2/2
$$me->{"Columns"} //= [@{$me->_build_data->{"select"};} ? map({_isa($_, "DBIx::DBO::Table", "DBIx::DBO::Query") ? $_->columns : $me->_build_col_val_name(@$_);} @{$me->_build_data->{"select"};}) : map({$_->columns;} $me->tables)]
l!l
53
MC/DC100%1/1
$$me->{"Columns"} //= [@{$me->_build_data->{"select"};} ? map({_isa($_, "DBIx::DBO::Table", "DBIx::DBO::Query") ? $_->columns : $me->_build_col_val_name(@$_);} @{$me->_build_data->{"select"};}) : map({$_->columns;} $me->tables)]
$$me->{"Columns"}
Truth table100%2/2
A
A= $$me->{"Columns"}
Aresult
11
00
129 ];
130  
131 8 @{$$me->{Columns}};
132 }
133  
134 14 *_build_col_val_name = \&DBIx::DBO::Query::_build_col_val_name;
135  
136 13 ▶sub _column_idx {
Subroutine _column_idx: called CC=9 SCAR=33.0
137 13 my($me, $col) = @_;
138 13 my $idx = -1;
139 13 my @show;
140 13 ▶ @show = @{$me->_build_data->{select}} or @show = $me->tables;
Branch: 1 branch
BranchTrueFalseTotal
@show = @{$me->_build_data->{"select"};} or @show = $me->tables 0 13 13
141 13 for my $fld (@show) {
142 13 ▶ if (_isa($fld, 'DBIx::DBO::Table')) {
Branch: 1 branch
BranchTrueFalseTotal
if (_isa($fld, "DBIx::DBO::Table")) 13 0 13
143 13 ▶ if ($col->[0] == $fld and exists $fld->{Column_Idx}{$col->[1]}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col->[0] == $fld and exists $fld->{"Column_Idx"}{$col->[1]}) 13 0 13
Condition33%1/3
$col->[0] == $fld and exists $fld->{"Column_Idx"}{$col->[1]}
!ll&&!rl&&r
0013
MC/DC0%0/2
$col->[0] == $fld and exists $fld->{"Column_Idx"}{$col->[1]}
$col->[0] == $fld exists $fld->{"Column_Idx"}{$col->[1]}
Truth table33%1/3
A and B
A= $col->[0] == $fldB= exists $fld->{"Column_Idx"}{$col->[1]}
ABresult
0X0
100
111
144 13 return $idx + $fld->{Column_Idx}{$col->[1]};
145 }
146 0 $idx += keys %{$fld->{Column_Idx}};
147 0 next;
148 }
149 0 $idx++;
150 0 ▶ return $idx if not defined $fld->[1] and @{$fld->[0]} == 1 and $col == $fld->[0][0];
Branch: 1 branch
BranchTrueFalseTotal
if not defined $fld->[1] and @{$fld->[0];} == 1 and $col == $fld->[0][0] 0 0 0
Condition0%0/6
not defined $fld->[1] and @{$fld->[0];} == 1 and $col == $fld->[0][0]
!ll&&!rl&&r
000
not defined $fld->[1] and @{$fld->[0];} == 1
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $fld->[1] and @{$fld->[0];} == 1 and $col == $fld->[0][0]
not defined $fld->[1] @{$fld->[0];} == 1 $col == $fld->[0][0]
Truth table0%0/4
not(A) and B and C
A= not defined $fld->[1]B= @{$fld->[0];} == 1C= $col == $fld->[0][0]
ABCresult
0XX0
10X0
1100
1111
151 }
152 0 return undef;
153 }
154  
155 =head3 C<column>
156  
157 $row->column($column_name);
158  
159 Returns a column reference from the name or alias.
160  
161 =cut
162  
163 2 ▶sub column {
Subroutine column: called CC=11 SCAR=25.6
Pod documented
164 2 my($me, $col) = @_;
165 2 my @show;
166 2 ▶ @show = @{$me->_build_data->{select}} or @show = $me->tables;
Branch: 1 branch
BranchTrueFalseTotal
@show = @{$me->_build_data->{"select"};} or @show = $me->tables 1 1 2
167 2 for my $fld (@show) {
168 return $$me->{Column}{$col} //= bless [$me, $col], 'DBIx::DBO::Column'
169 if (_isa($fld, 'DBIx::DBO::Table') and exists $fld->{Column_Idx}{$col})
170 1 ▶ or (_isa($fld, 'DBIx::DBO::Query') and eval { $fld->column($col) })
Condition50%4/8
ref $fld eq "ARRAY" and exists $fld->[2]{"AS"} and $col eq $fld->[2]{"AS"}
!ll&&!rl&&r
200
ref $fld eq "ARRAY" and exists $fld->[2]{"AS"}
!ll&&!rl&&r
110
$$me->{"Column"}{$col} //= bless([$me, $col], "DBIx::DBO::Column")
l!l
01
MC/DC0%0/3
ref $fld eq "ARRAY" and exists $fld->[2]{"AS"} and $col eq $fld->[2]{"AS"}
ref $fld eq "ARRAY" exists $fld->[2]{"AS"} $col eq $fld->[2]{"AS"}
MC/DC0%0/1
$$me->{"Column"}{$col} //= bless([$me, $col], "DBIx::DBO::Column")
$$me->{"Column"}{$col}
Truth table0%0/4
A and B and C
A= ref $fld eq "ARRAY"B= exists $fld->[2]{"AS"}C= $col eq $fld->[2]{"AS"}
ABCresult
0XX0
10X0
1100
1111
Truth table50%1/2
A
A= $$me->{"Column"}{$col}
Aresult
11
00
171 3 ▶ or (ref($fld) eq 'ARRAY' and exists $fld->[2]{AS} and $col eq $fld->[2]{AS});
Branch: 1 branch
BranchTrueFalseTotal
if _isa($fld, "DBIx::DBO::Table") and exists $fld->{"Column_Idx"}{$col} or _isa($fld, "DBIx::DBO::Query") and eval { do { $fld->column($col) } } or ref $fld eq "ARRAY" and exists $fld->[2]{"AS"} and $col eq $fld->[2]{"AS"} 1 2 3
Condition66%8/12
_isa($fld, "DBIx::DBO::Table") and exists $fld->{"Column_Idx"}{$col} or _isa($fld, "DBIx::DBO::Query") and eval { do { $fld->column($col) } } or ref $fld eq "ARRAY" and exists $fld->[2]{"AS"} and $col eq $fld->[2]{"AS"}
l!l&&r!l&&!r
102
_isa($fld, "DBIx::DBO::Table") and exists $fld->{"Column_Idx"}{$col} or _isa($fld, "DBIx::DBO::Query") and eval { do { $fld->column($col) } }
l!l&&r!l&&!r
012
_isa($fld, "DBIx::DBO::Table") and exists $fld->{"Column_Idx"}{$col}
!ll&&!rl&&r
210
_isa($fld, "DBIx::DBO::Query") and eval { do { $fld->column($col) } }
!ll&&!rl&&r
201
MC/DC0%0/5
_isa($fld, "DBIx::DBO::Table") and exists $fld->{"Column_Idx"}{$col} or _isa($fld, "DBIx::DBO::Query") and eval { do { $fld->column($col) } } or ref $fld eq "ARRAY" and exists $fld->[2]{"AS"} and $col eq $fld->[2]{"AS"}
_isa($fld, "DBIx::DBO::Table") exists $fld->{"Column_Idx"}{$col} _isa($fld, "DBIx::DBO::Query") eval { do { $fld->column($col) } } ref $fld eq "ARRAY" and exists $fld->[2]{"AS"} and $col eq $fld->[2]{"AS"}
Truth table27%3/11
A and B or C and D or E
A= _isa($fld, "DBIx::DBO::Table")B= exists $fld->{"Column_Idx"}{$col}C= _isa($fld, "DBIx::DBO::Query")D= eval { do { $fld->column($col) } }E= ref $fld eq "ARRAY" and exists $fld->[2]{"AS"} and $col eq $fld->[2]{"AS"}
ABCDEresult
11XXX1
0X11X1
1011X1
0X0X11
0X1011
100X11
101011
0X0X00
0X1000
100X00
101000
172 }
173 1 croak 'No such column: '.$$me->{DBO}{dbd_class}->_qi($me, $col);
174 }
175  
176 11 ▶sub _inner_col {
Subroutine _inner_col: called CC=9 SCAR=32.2
177 11 my($me, $col, $_check_aliases) = @_;
178 11 ▶ $_check_aliases = $$me->{DBO}{dbd_class}->_alias_preference($me, 'column') unless defined $_check_aliases;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_check_aliases 0 11 11
179 11 my $column;
180 11 ▶ return $column if $_check_aliases == 1 and $column = $me->_check_alias($col);
Branch: 1 branch
BranchTrueFalseTotal
if $_check_aliases == 1 and $column = $me->_check_alias($col) 0 11 11
Condition33%1/3
$_check_aliases == 1 and $column = $me->_check_alias($col)
!ll&&!rl&&r
1100
MC/DC0%0/2
$_check_aliases == 1 and $column = $me->_check_alias($col)
$_check_aliases == 1 $column = $me->_check_alias($col)
Truth table33%1/3
A and B
A= $_check_aliases == 1B= $column = $me->_check_alias($col)
ABresult
0X0
100
111
181 11 for my $tbl ($me->tables) {
182 11 ▶ return $tbl->column($col) if exists $tbl->{Column_Idx}{$col};
Branch: 1 branch
BranchTrueFalseTotal
if exists $tbl->{"Column_Idx"}{$col} 11 0 11
183 }
184 0 ▶ return $column if $_check_aliases == 2 and $column = $me->_check_alias($col);
Branch: 1 branch
BranchTrueFalseTotal
if $_check_aliases == 2 and $column = $me->_check_alias($col) 0 0 0
Condition0%0/3
$_check_aliases == 2 and $column = $me->_check_alias($col)
!ll&&!rl&&r
000
MC/DC0%0/2
$_check_aliases == 2 and $column = $me->_check_alias($col)
$_check_aliases == 2 $column = $me->_check_alias($col)
Truth table0%0/3
A and B
A= $_check_aliases == 2B= $column = $me->_check_alias($col)
ABresult
0X0
100
111
185 0 ▶ croak 'No such column'.($_check_aliases ? '/alias' : '').': '.$$me->{DBO}{dbd_class}->_qi($me, $col);
Branch: 1 branch
BranchTrueFalseTotal
$_check_aliases ? : 0 0 0
186 }
187  
188 0 ▶sub _check_alias {
Subroutine _check_alias: not called CC=6 SCAR=37.4
189 0 my($me, $col) = @_;
190 0 for my $fld (@{$me->_build_data->{select}}) {
191 return $$me->{Column}{$col} //= bless [$me, $col], 'DBIx::DBO::Column'
192 0 ▶ if ref($fld) eq 'ARRAY' and exists $fld->[2]{AS} and $col eq $fld->[2]{AS};
Branch: 1 branch
BranchTrueFalseTotal
if ref $fld eq "ARRAY" and exists $fld->[2]{"AS"} and $col eq $fld->[2]{"AS"} 0 0 0
Condition0%0/8
ref $fld eq "ARRAY" and exists $fld->[2]{"AS"} and $col eq $fld->[2]{"AS"}
!ll&&!rl&&r
000
ref $fld eq "ARRAY" and exists $fld->[2]{"AS"}
!ll&&!rl&&r
000
$$me->{"Column"}{$col} //= bless([$me, $col], "DBIx::DBO::Column")
l!l
00
MC/DC0%0/3
ref $fld eq "ARRAY" and exists $fld->[2]{"AS"} and $col eq $fld->[2]{"AS"}
ref $fld eq "ARRAY" exists $fld->[2]{"AS"} $col eq $fld->[2]{"AS"}
MC/DC0%0/1
$$me->{"Column"}{$col} //= bless([$me, $col], "DBIx::DBO::Column")
$$me->{"Column"}{$col}
Truth table0%0/4
A and B and C
A= ref $fld eq "ARRAY"B= exists $fld->[2]{"AS"}C= $col eq $fld->[2]{"AS"}
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/2
A
A= $$me->{"Column"}{$col}
Aresult
11
00
193 }
194 }
195  
196 =head3 C<value>
197  
198 $value = $row->value($column);
199  
200 Return the value in the C<$column> field.
201 C<$column> can be a column name or a C<Column> object.
202  
203 Values in the C<Row> can also be obtained by using the object as an array/hash reference.
204  
205 $value = $row->[2];
206 $value = $row->{some_column};
207  
208 =cut
209  
210 3 ▶sub value {
Subroutine value: called CC=5 SCAR=16.8
Pod documented
211 3 my($me, $col) = @_;
212 3 ▶ croak 'The row is empty' unless $$me->{array};
Branch: 1 branch
BranchTrueFalseTotal
unless $$me->{"array"} 1 2 3
213 2 ▶ if (_isa($col, 'DBIx::DBO::Column')) {
Branch: 1 branch
BranchTrueFalseTotal
if (_isa($col, "DBIx::DBO::Column")) 1 1 2
214 1 my $i = $me->_column_idx($col);
215 1 ▶ return $$me->{array}[$i] if defined $i;
Branch: 1 branch
BranchTrueFalseTotal
if defined $i 1 0 1
216 0 croak 'The field '.$$me->{DBO}{dbd_class}->_qi($me, $col->[0]{Name}, $col->[1]).' was not included in this query';
217 }
218 1 ▶ return $$me->{hash}{$col} if exists $$me->{hash}{$col};
Branch: 1 branch
BranchTrueFalseTotal
if exists $$me->{"hash"}{$col} 1 0 1
219 0 croak 'No such column: '.$$me->{DBO}{dbd_class}->_qi($me, $col);
220 }
221  
222 =head3 C<load>
223  
224 $row->load(id => 123);
225 $row->load(name => 'Bob', status => 'Employed');
226  
227 Fetch a new row using the where definition specified.
228 Returns the C<Row> object if the row is found and loaded successfully.
229 Returns an empty list if there is no row or an error occurs.
230  
231 =cut
232  
233 6 ▶sub load {
Subroutine load: called CC=2 SCAR=7.0
Pod documented
234 6 my $me = shift;
235  
236 6 $me->_detach;
237  
238 # Use Quick_Where to load a row, but make sure to restore its value afterward
239 6 my $old_qw = $#{$$me->{build_data}{Quick_Where}};
240 6 push @{$$me->{build_data}{Quick_Where}}, @_;
241 6 my $sql = $$me->{DBO}{dbd_class}->_build_sql_select($me);
242 6 my @bind = $$me->{DBO}{dbd_class}->_bind_params_select($me);
243 0 ▶ $old_qw < 0 ? delete $$me->{build_data}{Quick_Where} : ($#{$$me->{build_data}{Quick_Where}} = $old_qw);
Branch: 1 branch
BranchTrueFalseTotal
$old_qw < 0 ? : 6 0 6
244 6 delete $$me->{build_data}{Where_Bind};
245  
246 6 return $me->_load($sql, @bind);
247 }
248  
249 7 ▶sub _load {
Subroutine _load: called CC=6 SCAR=18.3
250 7 my($me, $sql, @bind) = @_;
251 7 undef $$me->{array};
252 7 $$me->{hash} = \my %hash;
253 7 $$me->{DBO}{dbd_class}->_sql($me, $sql, @bind);
254 7 my $sth = $me->rdbh->prepare($sql);
255 7 ▶ return unless $sth and $sth->execute(@bind);
Branch: 1 branch
BranchTrueFalseTotal
unless $sth and $sth->execute(@bind) 0 7 7
Condition33%1/3
$sth and $sth->execute(@bind)
!ll&&!rl&&r
007
MC/DC0%0/2
$sth and $sth->execute(@bind)
$sth $sth->execute(@bind)
Truth table33%1/3
A and B
A= $sthB= $sth->execute(@bind)
ABresult
0X0
100
111
256  
257 7 my $i;
258 my @array;
259 7 for ($me->columns) {
260 15 $i++;
261 15 ▶ $sth->bind_col($i, \$hash{$_}) unless exists $hash{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $hash{$_} 15 0 15
262 }
263 7 ▶ $$me->{array} = $sth->fetch or return;
Branch: 1 branch
BranchTrueFalseTotal
$$me->{"array"} = $sth->fetch or return 6 1 7
264 6 $sth->finish;
265 6 $me;
266 }
267  
268 21 ▶sub _detach {
Subroutine _detach: called CC=11 SCAR=24.0
269 21 my $me = $_[0];
270 21 ▶ if (exists $$me->{Parent}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$me->{"Parent"}) 13 8 21
271 13 ▶ $$me->{array} &&= \@{ $$me->{array} };
Condition100%2/2
$$me->{"array"} &&= \@{$$$me{"array"};}
!ll
112
MC/DC100%1/1
$$me->{"array"} &&= \@{$$$me{"array"};}
$$me->{"array"}
Truth table100%2/2
A
A= $$me->{"array"}
Aresult
00
11
272 13 $$me->{hash} = \%{ $$me->{hash} };
273 13 for ($$me->{Parent}{Row}, @{ $$me->{Parent}{attached_rows} }) {
274 15 ▶ undef $_ if defined $_ and $_ == $me;
Branch: 1 branch
BranchTrueFalseTotal
if defined $_ and $_ == $me 8 7 15
Condition66%2/3
defined $_ and $_ == $me
!ll&&!rl&&r
708
MC/DC50%1/2
defined $_ and $_ == $me
defined $_ $_ == $me
Truth table66%2/3
A and B
A= defined $_B= $_ == $me
ABresult
0X0
100
111
275 }
276 # Store needed build_data
277 13 $$me->{Tables} = [ @{$$me->{Parent}{Tables}} ];
278 13 $$me->{build_data}{from_sql} = $$me->{DBO}{dbd_class}->_build_from($$me->{Parent});
279 13 for my $f (qw(select From_Bind where order group)) {
280 65 ▶ $$me->{build_data}{$f} = $me->_copy($$me->{Parent}{build_data}{$f}) if exists $$me->{Parent}{build_data}{$f};
Branch: 1 branch
BranchTrueFalseTotal
if exists $$me->{"Parent"}{"build_data"}{$f} 53 12 65
281 }
282 # Save config from Parent
283 13 ▶ if ($$me->{Parent}{Config} and %{$$me->{Parent}{Config}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$me->{"Parent"}{"Config"} and %{$$me->{"Parent"}{"Config"};}) 3 10 13
Condition100%3/3
$$me->{"Parent"}{"Config"} and %{$$me->{"Parent"}{"Config"};}
!ll&&!rl&&r
733
MC/DC100%2/2
$$me->{"Parent"}{"Config"} and %{$$me->{"Parent"}{"Config"};}
$$me->{"Parent"}{"Config"} %{$$me->{"Parent"}{"Config"};}
Truth table100%3/3
A and B
A= $$me->{"Parent"}{"Config"}B= %{$$me->{"Parent"}{"Config"};}
ABresult
0X0
100
111
284 0 ▶ $$me->{Config} = { %{$$me->{Parent}{Config}}, $$me->{Config} ? %{$$me->{Config}} : () };
Branch: 1 branch
BranchTrueFalseTotal
$$me->{'Config'} ? : 0 3 3
285 }
286 }
287 21 delete $$me->{Parent};
288 }
289  
290 100 ▶sub _copy {
Subroutine _copy: called CC=5 SCAR=16.1
291 100 my($me, $val) = @_;
292 return bless [$me, $val->[1]], 'DBIx::DBO::Column'
293 100 ▶ if _isa($val, 'DBIx::DBO::Column') and $val->[0] == $$me->{Parent};
Branch: 1 branch
BranchTrueFalseTotal
if _isa($val, "DBIx::DBO::Column") and $val->[0] == $$me->{"Parent"} 2 98 100
Condition100%3/3
_isa($val, "DBIx::DBO::Column") and $val->[0] == $$me->{"Parent"}
!ll&&!rl&&r
9352
MC/DC100%2/2
_isa($val, "DBIx::DBO::Column") and $val->[0] == $$me->{"Parent"}
_isa($val, "DBIx::DBO::Column") $val->[0] == $$me->{"Parent"}
Truth table100%3/3
A and B
A= _isa($val, "DBIx::DBO::Column")B= $val->[0] == $$me->{"Parent"}
ABresult
0X0
100
111
294 98 ▶ ref $val eq 'ARRAY' ? [map $me->_copy($_), @$val] : ref $val eq 'HASH' ? {map $me->_copy($_), %$val} : $val;
Branch: 2 branches
BranchTrueFalseTotal
ref $val eq 'ARRAY' ? : 69 29 98
ref $val eq 'HASH' ? : 2 27 29
295 }
296  
297 =head3 C<update>
298  
299 $row->update(id => 123);
300 $row->update(name => 'Bob', status => 'Employed');
301  
302 Updates the current row with the new values specified.
303 Returns the number of rows updated or C<'0E0'> for no rows to ensure the value is true,
304 and returns false if there was an error.
305  
306 Note: If C<LIMIT> is supported on C<UPDATE>s then only the first matching row will be updated
307 otherwise ALL rows matching the current row will be updated.
308  
309 =cut
310  
311 5 ▶sub update {
Subroutine update: called CC=6 SCAR=19.2
Pod documented
312 5 my $me = shift;
313 5 ▶ croak "Can't update an empty row" unless $$me->{array};
Branch: 1 branch
BranchTrueFalseTotal
unless $$me->{"array"} 2 3 5
314 3 my @update = $$me->{DBO}{dbd_class}->_parse_set($me, @_);
315 3 local $$me->{build_data} = $$me->{DBO}{dbd_class}->_build_data_matching_this_row($me);
316 3 ▶ $$me->{build_data}{limit} = ($me->config('LimitRowUpdate') and $me->tables == 1) ? [1] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$me->config('LimitRowUpdate') && $me->tables == 1 ? : 0 3 3
Condition33%1/3
$me->config("LimitRowUpdate") and $me->tables == 1
!ll&&!rl&&r
300
MC/DC0%0/2
$me->config("LimitRowUpdate") and $me->tables == 1
$me->config("LimitRowUpdate") $me->tables == 1
Truth table33%1/3
A and B
A= $me->config("LimitRowUpdate")B= $me->tables == 1
ABresult
0X0
100
111
317 3 my $sql = $$me->{DBO}{dbd_class}->_build_sql_update($me, @update);
318  
319 3 my $rv = $$me->{DBO}{dbd_class}->_do($me, $sql, undef, $$me->{DBO}{dbd_class}->_bind_params_update($me));
320 3 ▶ $$me->{DBO}{dbd_class}->_reset_row_on_update($me, @update) if $rv and $rv > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $rv and $rv > 0 3 0 3
Condition33%1/3
$rv and $rv > 0
!ll&&!rl&&r
003
MC/DC0%0/2
$rv and $rv > 0
$rv $rv > 0
Truth table33%1/3
A and B
A= $rvB= $rv > 0
ABresult
0X0
100
111
321 3 return $rv;
322 }
323  
324 =head3 C<delete>
325  
326 $row->delete;
327  
328 Deletes the current row.
329 Returns the number of rows deleted or C<'0E0'> for no rows to ensure the value is true,
330 and returns false if there was an error.
331 The C<Row> object will then be empty.
332  
333 Note: If C<LIMIT> is supported on C<DELETE>s then only the first matching row will be deleted
334 otherwise ALL rows matching the current row will be deleted.
335  
336 =cut
337  
338 2 ▶sub delete {
Subroutine delete: called CC=4 SCAR=14.2
Pod documented
339 2 my $me = shift;
340 2 ▶ croak "Can't delete an empty row" unless $$me->{array};
Branch: 1 branch
BranchTrueFalseTotal
unless $$me->{"array"} 1 1 2
341 1 local $$me->{build_data} = $$me->{DBO}{dbd_class}->_build_data_matching_this_row($me);
342 1 ▶ $$me->{build_data}{limit} = ($me->config('LimitRowDelete') and $me->tables == 1) ? [1] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$me->config('LimitRowDelete') && $me->tables == 1 ? : 0 1 1
Condition33%1/3
$me->config("LimitRowDelete") and $me->tables == 1
!ll&&!rl&&r
100
MC/DC0%0/2
$me->config("LimitRowDelete") and $me->tables == 1
$me->config("LimitRowDelete") $me->tables == 1
Truth table33%1/3
A and B
A= $me->config("LimitRowDelete")B= $me->tables == 1
ABresult
0X0
100
111
343 1 my $sql = $$me->{DBO}{dbd_class}->_build_sql_delete($me, @_);
344  
345 1 undef $$me->{array};
346 1 $$me->{hash} = {};
347 1 $$me->{DBO}{dbd_class}->_do($me, $sql, undef, $$me->{DBO}{dbd_class}->_bind_params_delete($me));
348 }
349  
350 =head3 C<is_empty>
351  
352 return $row->{id} unless $row->is_empty;
353  
354 Checks to see if it's an empty C<Row>, and returns true or false.
355  
356 =cut
357  
358 3 ▶sub is_empty {
Subroutine is_empty: called CC=1 SCAR=0.0
Pod documented
359 3 my $me = shift;
360 3 return not defined $$me->{array};
361 }
362  
363 =head2 Common Methods
364  
365 These methods are accessible from all DBIx::DBO* objects.
366  
367 =head3 C<dbo>
368  
369 The C<DBO> object.
370  
371 =head3 C<dbh>
372  
373 The I<read-write> C<DBI> handle.
374  
375 =head3 C<rdbh>
376  
377 The I<read-only> C<DBI> handle, or if there is no I<read-only> connection, the I<read-write> C<DBI> handle.
378  
379 =cut
380  
381 8 ▶sub dbo { ${$_[0]}->{DBO} }
Subroutine dbo: called CC=1 SCAR=0.0
Pod documented
382 4 ▶sub dbh { ${$_[0]}->{DBO}->dbh }
Subroutine dbh: called CC=1 SCAR=0.0
Pod documented
383 31 ▶sub rdbh { ${$_[0]}->{DBO}->rdbh }
Subroutine rdbh: called CC=1 SCAR=0.0
Pod documented
384  
385 =head3 C<config>
386  
387 $row_setting = $row->config($option);
388 $row->config($option => $row_setting);
389  
390 Get or set the C<Row> config settings. When setting an option, the previous value is returned. When getting an option's value, if the value is undefined, the C<Query> object (If the the C<Row> belongs to one) or L<DBIx::DBO|DBIx::DBO>'s value is returned.
391  
392 See L<DBIx::DBO/"Available config options">.
393  
394 =cut
395  
396 68 ▶sub config {
Subroutine config: called CC=5 SCAR=16.1
Pod documented
397 68 my $me = shift;
398 68 my $opt = shift;
399 68 ▶ return $$me->{DBO}{dbd_class}->_set_config($$me->{Config} //= {}, $opt, shift) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 14 54 68
Condition100%2/2
$$me->{"Config"} //= {}
l!l
131
MC/DC100%1/1
$$me->{"Config"} //= {}
$$me->{"Config"}
Truth table100%2/2
A
A= $$me->{"Config"}
Aresult
11
00
400 54 ▶ $$me->{DBO}{dbd_class}->_get_config($opt, $$me->{Config} //= {}, defined $$me->{Parent} ? ($$me->{Parent}{Config}) : (), $$me->{DBO}{Config}, \%DBIx::DBO::Config);
Branch: 1 branch
BranchTrueFalseTotal
defined $$me->{'Parent'} ? : 2 52 54
Condition100%2/2
$$me->{"Config"} //= {}
l!l
504
MC/DC100%1/1
$$me->{"Config"} //= {}
$$me->{"Config"}
Truth table100%2/2
A
A= $$me->{"Config"}
Aresult
11
00
401 }
402  
403 14 ▶if (eval { Storable->VERSION(2.38) }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { "Storable"->VERSION("2.38") } }) 14 0 14
404 3 ▶ *STORABLE_freeze = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
405 3 my($me, $cloning) = @_;
406 3 $me->_detach;
407 3 my $frozen = Storable::nfreeze($$me);
408 3 return $frozen;
409 14 };
410  
411 3 ▶ *STORABLE_thaw = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
412 3 my($me, $cloning, @frozen) = @_;
413 3 $$me = \%{ Storable::thaw(@frozen) }; # Copy the hash, or Storable will wipe it out
414 14 };
415 }
416  
417 20 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
418 20 undef %${$_[0]};
419 }
420  
421 14 1;
422  
423 __END__
« Test/DBO.pm ↑ Summary blib/lib/DBIx/DBO/Query.pm »