Alzabo/Runtime/Row.pm

filter: Statementstmt 36.0% 49 / 136 Branchbran 3.1% 1 / 32 Conditioncond 4.1% 1 / 24 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 51.6% 16 / 31 Podpod 44.4% 4 / 9 totaltotal 29.1% 71 / 244 CC 27 SCAR 29.2
CC
27
Cov
27%
make_live
56.0
_cached_data_is_same
56.0
new
20.0
CRAP 18.6
« Alzabo/Create/Column.pm ↑ Summary Alzabo/Table.pm »
1 package Alzabo::Runtime::Row;
2  
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 11 ▶use Alzabo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 11 ▶use Alzabo::Exceptions ( abbr => [ qw( logic_exception no_such_row_exception
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 params_exception storable_exception ) ] );
10  
11 11 ▶use Alzabo::Runtime;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use Alzabo::Runtime::RowState::Deleted;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use Alzabo::Runtime::RowState::Live;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 11 ▶use Alzabo::Runtime::RowState::Potential;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 11 ▶use Alzabo::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 11 ▶use Params::Validate qw( validate validate_with UNDEF SCALAR HASHREF BOOLEAN );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 Params::Validate::validation_options
19 0 ▶ ( on_fail => sub { params_exception join '', @_ } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
20  
21 11 ▶use Storable ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 11 $VERSION = 2.0;
24  
25 BEGIN
26 11 ▶{
Subroutine BEGIN: called CC=2 SCAR=7.2
27 11 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 11 foreach my $meth ( qw( select select_hash update refresh delete
29 id_as_string is_live is_potential is_deleted ) )
30 {
31 99 *{ __PACKAGE__ . "::$meth" } =
32 0 ▶ sub { my $s = shift;
Subroutine __ANON__: not called CC=1 SCAR=2.6
33 0 $s->{state}->$meth( $s, @_ ) };
34 }
35 }
36  
37 11 ▶use constant NEW_SPEC => { table => { isa => 'Alzabo::Runtime::Table' },
Subroutine BEGIN: called CC=1 SCAR=0.0
38 pk => { type => SCALAR | HASHREF,
39 optional => 1,
40 },
41 prefetch => { type => UNDEF | HASHREF,
42 optional => 1,
43 },
44 state => { type => SCALAR,
45 default => 'Alzabo::Runtime::RowState::Live',
46 },
47 potential_row => { isa => 'Alzabo::Runtime::Row',
48 optional => 1,
49 },
50 values => { type => HASHREF,
51 default => {},
52 },
53 no_cache => { type => BOOLEAN, default => 0 },
54 11 };
55  
56 sub new
57 0 ▶{
Subroutine new: not called CC=4 SCAR=30.0
Pod undocumented
58 0 my $proto = shift;
59 0 ▶ my $class = ref $proto || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
60  
61 0 my %p =
62 validate( @_, NEW_SPEC );
63  
64 0 ▶ my $self = $p{potential_row} ? $p{potential_row} : {};
Branch: 1 branch
BranchTrueFalseTotal
$p{'potential_row'} ? : 0 0 0
65  
66 0 bless $self, $class;
67  
68 0 $self->{table} = $p{table};
69 0 $self->{state} = $p{state};
70  
71 0 ▶ $self->{state}->_init($self, @_) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"state"}->_init($self, @_) or return 0 0 0
72  
73 0 return $self;
74 }
75  
76 sub table
77 0 ▶{
Subroutine table: not called CC=1 SCAR=6.9
Pod documented
78 0 my $self = shift;
79  
80 0 return $self->{table};
81 }
82  
83 sub schema
84 0 ▶{
Subroutine schema: not called CC=1 SCAR=6.9
Pod documented
85 0 my $self = shift;
86  
87 0 return $self->table->schema;
88 }
89  
90 0 ▶sub set_state { $_[0]->{state} = $_[1] };
Subroutine set_state: not called CC=1 SCAR=6.9
Pod undocumented
91  
92 11 ▶use constant ROWS_BY_FOREIGN_KEY_SPEC => { foreign_key => { isa => 'Alzabo::ForeignKey' } };
Subroutine BEGIN: called CC=1 SCAR=0.0
93  
94 sub rows_by_foreign_key
95 0 ▶{
Subroutine rows_by_foreign_key: not called CC=4 SCAR=30.0
Pod documented
96 0 my $self = shift;
97 0 my %p = validate_with( params => \@_,
98 spec => ROWS_BY_FOREIGN_KEY_SPEC,
99 allow_extra => 1,
100 );
101  
102 0 my $fk = delete $p{foreign_key};
103  
104 0 ▶ if ($p{where})
Branch: 1 branch
BranchTrueFalseTotal
if ($p{"where"}) 0 0 0
105 {
106 0 ▶ $p{where} = [ $p{where} ] unless Alzabo::Utils::is_arrayref( $p{where}[0] );
Branch: 1 branch
BranchTrueFalseTotal
unless Alzabo::Utils::is_arrayref($p{"where"}[0]) 0 0 0
107 }
108  
109 0 push @{ $p{where} },
110 0 map { [ $_->[1], '=', $self->select( $_->[0]->name ) ] } $fk->column_pairs;
111  
112 # if the relationship is not 1..n, then only one row can be
113 # returned (or referential integrity has been hosed in the
114 # database).
115 0 ▶ return $fk->is_one_to_many ? $fk->table_to->rows_where(%p) : $fk->table_to->one_row(%p);
Branch: 1 branch
BranchTrueFalseTotal
$fk->is_one_to_many ? : 0 0 0
116 }
117  
118 # class method
119 sub id_as_string_ext
120 0 ▶{
Subroutine id_as_string_ext: not called CC=1 SCAR=6.9
Pod undocumented
121 0 my $class = shift;
122 0 my %p = @_;
123 0 my $id_hash = $class->_make_id_hash(%p);
124  
125 0 local $^W; # weirdly, enough there are code paths that can
126 # lead here that'd lead to $id_hash having some
127 # values that are undef
128 return join ';:;_;:;', ( $p{table}->schema->name,
129 $p{table}->name,
130 0 map { $_, $id_hash->{$_} } sort keys %$id_hash );
131 }
132  
133 sub _make_id_hash
134 0 ▶{
Subroutine _make_id_hash: not called CC=2 SCAR=17.9
135 0 my $self = shift;
136 0 my %p = @_;
137  
138 0 ▶ return $p{pk} if ref $p{pk};
Branch: 1 branch
BranchTrueFalseTotal
if ref $p{"pk"} 0 0 0
139  
140 0 return { ($p{table}->primary_key)[0]->name => $p{pk} };
141 }
142  
143 sub _update_pk_hash
144 0 ▶{
Subroutine _update_pk_hash: not called CC=1 SCAR=6.9
145 0 my $self = shift;
146  
147 0 my @pk = keys %{ $self->{pk} };
148  
149 0 @{ $self->{pk} }{ @pk } = @{ $self->{data} }{ @pk };
150  
151 0 delete $self->{id_string};
152 }
153  
154 sub make_live
155 0 ▶{
Subroutine make_live: not called CC=7 SCAR=40.3
Pod documented
156 0 my $self = shift;
157  
158 logic_exception "Can only call make_live on potential rows"
159 0 ▶ unless $self->{state}->is_potential;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"state"}->is_potential 0 0 0
160  
161 0 my %p = @_;
162  
163 0 my %values;
164 0 foreach ( $self->table->columns )
165 {
166 0 ▶ next unless exists $p{values}->{ $_->name } || exists $self->{data}->{ $_->name };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $p{"values"}{$_->name} or exists $self->{"data"}{$_->name} 0 0 0
Condition0%0/3
exists $p{"values"}{$_->name} or exists $self->{"data"}{$_->name}
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $p{"values"}{$_->name} or exists $self->{"data"}{$_->name}
exists $p{"values"}{$_->name} exists $self->{"data"}{$_->name}
Truth table0%0/3
A or B
A= exists $p{"values"}{$_->name}B= exists $self->{"data"}{$_->name}
ABresult
1X1
011
000
167 $values{ $_->name } = ( exists $p{values}->{ $_->name } ?
168 $p{values}->{ $_->name } :
169 0 ▶ $self->{data}->{ $_->name } );
Branch: 1 branch
BranchTrueFalseTotal
exists $p{'values'}{$_->name} ? : 0 0 0
170 }
171  
172 0 my $table = $self->table;
173 0 delete @{ $self }{keys %$self}; # clear out everything
174  
175 0 ▶ $table->insert( @_,
Branch: 1 branch
BranchTrueFalseTotal
%values ? : 0 0 0
176 potential_row => $self,
177 %values ? ( values => \%values ) : (),
178 );
179 }
180  
181 sub _cached_data_is_same
182 0 ▶{
Subroutine _cached_data_is_same: not called CC=7 SCAR=40.3
183 0 my $self = shift;
184 0 my ( $key, $val ) = @_;
185  
186 # The convolutions here are necessary to avoid avoid treating
187 # undef as being equal to 0 or ''. Stupid NULLs.
188 return 1
189 if ( exists $self->{data}{$key} &&
190 ( ( ! defined $val && ! defined $self->{data}{$key} ) ||
191 ( defined $val &&
192 defined $self->{data}{$key} &&
193 0 ▶ ( $val eq $self->{data}{$key} )
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"data"}{$key} and !(defined $val || defined $self->{"data"}{$key}) || defined $val && defined $self->{"data"}{$key} && $val eq $self->{"data"}{$key} 0 0 0
Condition0%0/15
exists $self->{"data"}{$key} and !(defined $val || defined $self->{"data"}{$key}) || defined $val && defined $self->{"data"}{$key} && $val eq $self->{"data"}{$key}
!ll&&!rl&&r
000
not defined $val || defined $self->{"data"}{$key} or defined $val and defined $self->{"data"}{$key} and $val eq $self->{"data"}{$key}
l!l&&r!l&&!r
000
defined $val || defined $self->{'data'}{$key}
l!l&&r!l&&!r
000
defined $val and defined $self->{"data"}{$key} and $val eq $self->{"data"}{$key}
!ll&&!rl&&r
000
defined $val and defined $self->{"data"}{$key}
!ll&&!rl&&r
000
MC/DC0%0/6
exists $self->{"data"}{$key} and !(defined $val || defined $self->{"data"}{$key}) || defined $val && defined $self->{"data"}{$key} && $val eq $self->{"data"}{$key}
exists $self->{"data"}{$key} defined $val defined $self->{'data'}{$key} defined $val defined $self->{"data"}{$key} $val eq $self->{"data"}{$key}
Truth table0%0/10
A and not(B || C) or D and E and F
A= exists $self->{"data"}{$key}B= defined $valC= defined $self->{'data'}{$key}D= defined $valE= defined $self->{"data"}{$key}F= $val eq $self->{"data"}{$key}
ABCDEFresult
0XXXXX0
11X0XX0
11X10X0
11X1100
1010XX0
10110X0
1011100
100XXX1
11X1111
1011111
194 )
195 )
196 );
197  
198 0 return 0;
199 }
200  
201 sub _no_such_row_error
202 0 ▶{
Subroutine _no_such_row_error: not called CC=3 SCAR=24.8
203 0 my $self = shift;
204  
205 0 my $err = 'Unable to find a row in ' . $self->table->name . ' where ';
206 0 my @vals;
207 0 ▶ while ( my( $k, $v ) = each %{ $self->{pk} } )
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"pk"};}) 0 0 0
208 {
209 0 ▶ $v = '<NULL>' unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 0 0
210 0 my $val = "$k = $v";
211 0 push @vals, $val;
212 }
213 0 $err .= join ', ', @vals;
214  
215 0 no_such_row_exception $err;
216 }
217  
218 sub STORABLE_freeze
219 0 ▶{
Subroutine STORABLE_freeze: not called CC=2 SCAR=17.9
Pod undocumented
220 0 my $self = shift;
221 0 my $cloning = shift;
222  
223 0 my %data = %$self;
224  
225 0 my $table = delete $data{table};
226  
227 0 $data{schema} = $table->schema->name;
228 0 $data{table_name} = $table->name;
229  
230 0 my $ser = eval { Storable::nfreeze(\%data) };
231  
232 0 ▶ storable_exception $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
233  
234 0 return $ser;
235 }
236  
237 sub STORABLE_thaw
238 0 ▶{
Subroutine STORABLE_thaw: not called CC=2 SCAR=17.9
Pod undocumented
239 0 my ( $self, $cloning, $ser ) = @_;
240  
241 0 my $data = eval { Storable::thaw($ser) };
242  
243 0 ▶ storable_exception $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
244  
245 0 %$self = %$data;
246  
247 0 my $s = Alzabo::Runtime::Schema->load_from_file( name => delete $self->{schema} );
248 0 $self->{table} = $s->table( delete $self->{table_name} );
249  
250 0 return $self;
251 }
252  
253 BEGIN
254 11 ▶{
Subroutine BEGIN: called CC=3 SCAR=15.4
255 # dumb hack to fix bugs in Storable 2.00 - 2.03 w/ a non-threaded
256 # Perl
257 #
258 # Basically, Storable somehow screws up the hooks business the
259 # _first_ time an object from a class with hooks is stored. So
260 # we'll just _force_ it do it once right away.
261 11 ▶ if ( $Storable::VERSION >= 2 && $Storable::VERSION <= 2.03 )
Branch: 1 branch
BranchTrueFalseTotal
if ($Storable::VERSION >= 2 and $Storable::VERSION <= "2.03") 0 11 11
Condition33%1/3
$Storable::VERSION >= 2 and $Storable::VERSION <= "2.03"
!ll&&!rl&&r
0110
MC/DC0%0/2
$Storable::VERSION >= 2 and $Storable::VERSION <= "2.03"
$Storable::VERSION >= 2 $Storable::VERSION <= "2.03"
Truth table33%1/3
A and B
A= $Storable::VERSION >= 2B= $Storable::VERSION <= "2.03"
ABresult
0X0
100
111
262 {
263 0 eval <<'EOF';
264 { package ___name; sub name { 'foo' } }
265 { package ___table; @table::ISA = '___name'; sub schema { bless {}, '___name' } }
266 my $row = bless { table => bless {}, '___table' }, __PACKAGE__;
267 Storable::thaw(Storable::nfreeze($row));
268 EOF
269 }
270 }
271  
272  
273 11 1;
274  
275 __END__
« Alzabo/Create/Column.pm ↑ Summary Alzabo/Table.pm »