Elive/DAO.pm

filter: Statementstmt 91.7% 565 / 616 Branchbran 68.6% 221 / 322 Conditioncond 57.9% 106 / 183 MC/DCmcdc 28.0% 30 / 107 Subroutinesub 95.3% 61 / 64 Podpod 96.1% 25 / 26 totaltotal 76.4% 1008 / 1318 CC 254 SCAR 28.5
CC
254
Cov
80%
list
72.0
update
61.0
__set_db_data
29.9
CRAP 17.3
« Elive/Connection.pm ↑ Summary Elive/Entity/ServerParameters.pm »
1 package Elive::DAO;
2 37 ▶use warnings; use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 37 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 37 ▶use Mouse::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 37 our $VERSION = '1.36';
8  
9 37 ▶use parent 'Elive::DAO::_Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 37 ▶use YAML::Syck;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 37 ▶use Scalar::Util qw{weaken};
Subroutine BEGIN: called CC=1 SCAR=0.0
13 37 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 37 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 37 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 37 ▶use Elive::Util qw{1.36};
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 37 __PACKAGE__->mk_classdata('_entities' => {});
20 37 __PACKAGE__->mk_classdata('_aliases');
21 37 __PACKAGE__->mk_classdata('_derivable' => {});
22 37 __PACKAGE__->mk_classdata('_entity_name');
23 37 __PACKAGE__->mk_classdata('_primary_key' => []);
24 37 __PACKAGE__->mk_classdata('_params' => {});
25 37 __PACKAGE__->mk_classdata('collection_name');
26 37 __PACKAGE__->mk_classdata('_isa');
27  
28 37 foreach my $accessor (qw{_db_data _deleted _is_copy}) {
29 111 __PACKAGE__->has_metadata($accessor);
30 }
31  
32 =head1 NAME
33  
34 Elive::DAO - Abstract class for Elive Data Access Objects
35  
36 =head1 DESCRIPTION
37  
38 This is an abstract class for retrieving and managing objects mapped to a
39 datastore.
40  
41 =cut
42  
43 37 our %Stored_Objects;
44  
45 sub BUILDARGS {
46 116 ▶ my ($class, $raw, @args) = @_;
Subroutine BUILDARGS: called CC=9 SCAR=23.5
Pod documented
47  
48 116 ▶ warn "$class - ignoring arguments to new: @args\n"
Branch: 1 branch
BranchTrueFalseTotal
if @args 0 116 116
49 if @args;
50  
51 116 ▶ if (Elive::Util::_reftype($raw) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (Elive::Util::_reftype($raw) eq "HASH") 116 0 116
52  
53 116 my $types = $class->property_types;
54  
55 116 my %cooked;
56  
57 116 my $aliases = $class->_get_aliases;
58  
59 116 foreach (keys %$raw) {
60  
61 #
62 # apply any aliases
63 #
64  
65 my $prop = (exists $aliases->{$_}
66 394 ▶ ? ($aliases->{$_}{to} or die "$class has malformed alias: $_")
Branch: 1 branch
BranchTrueFalseTotal
exists $aliases->{$_} ? : 0 394 394
Condition0%0/2
$aliases->{$_}{"to"} or die "$class has malformed alias: $_"
l!l
00
MC/DC0%0/1
$aliases->{$_}{"to"} or die "$class has malformed alias: $_"
$aliases->{$_}{"to"}
Truth table0%0/2
A
A= $aliases->{$_}{"to"}
Aresult
11
00
67 : $_);
68  
69 394 my $value = $raw->{$_};
70 394 ▶ if (my $type = $types->{$prop}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $type = $types->{$prop}) { } 394 0 394
71 394 ▶ if (ref($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 30 364 394
72 #
73 # inspect the item to see if we need to stringify an
74 # object to obtain a simple string. The property is
75 # likely to be a foreign key.
76 #
77 30 ▶ $value = Elive::Util::string($value, $type)
Branch: 1 branch
BranchTrueFalseTotal
unless Elive::Util::inspect_type($type)->is_ref 3 27 30
78 unless Elive::Util::inspect_type($type)->is_ref;
79 }
80 }
81 else {
82 0 Carp::carp "$class: unknown property: $prop";
83 }
84  
85 394 $cooked{$prop} = $value;
86 }
87  
88 116 return \%cooked;
89 }
90  
91 0 return $raw;
92 }
93  
94 =head1 METHODS
95  
96 =cut
97  
98 sub stringify {
99 1463 ▶ my $class = shift;
Subroutine stringify: called CC=6 SCAR=17.9
Pod documented
100 1463 my $data = shift;
101  
102 1463 ▶ $data = $class
Branch: 1 branch
BranchTrueFalseTotal
if not defined $data and ref $class 284 1179 1463
Condition100%3/3
not defined $data and ref $class
!ll&&!rl&&r
11727284
MC/DC100%2/2
not defined $data and ref $class
not defined $data ref $class
Truth table100%3/3
not(A) and B
A= not defined $dataB= ref $class
ABresult
0X0
100
111
103 if !defined $data && ref $class;
104  
105 1463 ▶ return $data
Branch: 1 branch
BranchTrueFalseTotal
unless $data and Elive::Util::_reftype($data) 28 1435 1463
Condition100%3/3
$data and Elive::Util::_reftype($data)
!ll&&!rl&&r
9191435
MC/DC100%2/2
$data and Elive::Util::_reftype($data)
$data Elive::Util::_reftype($data)
Truth table100%3/3
A and B
A= $dataB= Elive::Util::_reftype($data)
ABresult
0X0
100
111
106 unless $data && Elive::Util::_reftype($data);
107  
108 1435 ▶ my @primary_key = $class->primary_key
Branch: 1 branch
BranchTrueFalseTotal
my(@primary_key) = $class->primary_key or return 0 1435 1435
109 or return; # weak entity - e.g. Elive::StandardV2::ServerVersions
110  
111 1435 my $types = $class->property_types;
112  
113 1435 my $string = join('/', map {Elive::Util::_freeze($data->{$_},
114 1435 $types->{$_})}
115 @primary_key);
116  
117 1435 return $string;
118 }
119  
120 =head2 entity_name
121  
122 my $entity_name = MyApp::Entity::User->entity_name
123 ok($entity_name eq 'user');
124  
125 =cut
126  
127 sub entity_name {
128 1014 ▶ my $entity_class = shift;
Subroutine entity_name: called CC=3 SCAR=11.0
Pod documented
129  
130 1014 ▶ if (my $entity_name = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $entity_name = shift()) 303 711 1014
131  
132 #
133 # Set our entity name. Register it in our parent
134 #
135 303 $entity_class->_entity_name(ucfirst($entity_name));
136  
137 303 my $entities = $entity_class->_entities;
138  
139 die "Entity $entity_name redeclared "
140 303 ▶ if exists $entities->{$entity_name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $entities->{$entity_name} 0 303 303
141  
142 303 $entities->{lcfirst($entity_name)} = $entity_class;
143 }
144  
145 1014 return $entity_class->_entity_name;
146 }
147  
148 =head2 collection_name
149  
150 my $collection_name = MyApp::Entity::User->collection_name
151 ok($collection_name eq 'users');
152  
153 =cut
154  
155 # Class::Data::Inheritable property
156  
157 # _alias, _get_aliases
158 #
159 # MyApp::Entity::Meeting->_alias(requiredSeats => 'seats');
160 #
161 # Return or set data mappings.
162 #
163 # These methods assist with the handling of data inconsistancies that
164 # sometimes exist between freeze/thaw property names; or between versions.
165 # These are always trapped at the data level (_freeze & _thaw).
166 #
167  
168 sub _alias {
169 707 ▶ my ($entity_class, $from, $to, %opt) = @_;
Subroutine _alias: called CC=9 SCAR=26.2
170  
171 707 $from = lcfirst($from);
172 707 $to = lcfirst($to);
173  
174 707 ▶ die 'usage: $entity_class->_alias(alias, prop, %opts)'
Branch: 1 branch
BranchTrueFalseTotal
unless $entity_class and $from and not ref $from and $to and not ref $to 0 707 707
Condition33%4/12
$entity_class and $from and not ref $from and $to and not ref $to
!ll&&!rl&&r
00707
$entity_class and $from and not ref $from and $to
!ll&&!rl&&r
00707
$entity_class and $from and not ref $from
!ll&&!rl&&r
00707
$entity_class and $from
!ll&&!rl&&r
00707
MC/DC0%0/5
$entity_class and $from and not ref $from and $to and not ref $to
$entity_class $from not ref $from $to not ref $to
Truth table16%1/6
A and B and not(C) and D and not(E)
A= $entity_classB= $fromC= not ref $fromD= $toE= not ref $to
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
175 unless ($entity_class
176 && $from && !ref($from)
177 && $to && !ref($to));
178  
179 707 my $aliases = $entity_class->_get_aliases;
180  
181 #
182 # Set our entity name. Register it in our parent
183 #
184 die "$entity_class: attempted redefinition of alias: $from"
185 707 ▶ if $aliases->{$from};
Branch: 1 branch
BranchTrueFalseTotal
if $aliases->{$from} 0 707 707
186  
187 707 ▶ die "$entity_class: can't alias $from it's already a property!"
Branch: 1 branch
BranchTrueFalseTotal
if $entity_class->meta->get_attribute($from) 0 707 707
188 if $entity_class->meta->get_attribute($from);
189  
190 707 ▶ die "$entity_class: attempt to alias $from to non-existant property $to - check spelling and declaration order"
Branch: 1 branch
BranchTrueFalseTotal
unless $entity_class->meta->get_attribute($to) 0 707 707
191 unless $entity_class->meta->get_attribute($to);
192  
193 707 $opt{to} = $to;
194 707 $aliases->{$from} = \%opt;
195  
196 707 return \%opt;
197 }
198  
199 sub _get_aliases {
200 1979 ▶ my $entity_class = shift;
Subroutine _get_aliases: called CC=2 SCAR=6.9
201  
202 1979 my $aliases = $entity_class->_aliases;
203  
204 1979 ▶ unless ($aliases) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($aliases) 268 1711 1979
205 268 $aliases = {};
206 268 $entity_class->_aliases( $aliases );
207 }
208  
209 1979 return $aliases
210 }
211  
212 =head2 id
213  
214 my @primary_vals = $entity_obj->id
215  
216 Return primary key values.
217  
218 =cut
219  
220 sub id {
221 10 ▶ my $self = shift;
Subroutine id: called CC=1 SCAR=0.0
Pod documented
222 10 return map {$self->$_} ($self->primary_key );
223 }
224  
225 =head2 primary_key
226  
227 Setter/getter for primary key field(s) for this entity class
228  
229 my @pkey = MyApp::Entity::User->primary_key
230  
231 =cut
232  
233 sub primary_key {
234 6412 ▶ my ($entity_class, @pkey) = @_;
Subroutine primary_key: called CC=2 SCAR=6.9
Pod documented
235  
236 6412 ▶ $entity_class->_primary_key(\@pkey)
Branch: 1 branch
BranchTrueFalseTotal
if @pkey 262 6150 6412
237 if (@pkey);
238  
239 6412 return @{$entity_class->_primary_key};
240 }
241  
242 =head2 params
243  
244 Setter/getter for parameter field(s) for this entity class
245  
246 Elive::Entity::User->params(loginName => 'Str');
247 my %params = MyApp::Entity::User->params;
248  
249 =cut
250  
251 sub params {
252 622 ▶ my ($entity_class, %params) = @_;
Subroutine params: called CC=2 SCAR=6.9
Pod documented
253  
254 622 ▶ $entity_class->_params(\%params)
Branch: 1 branch
BranchTrueFalseTotal
if keys %params 129 493 622
255 if (keys %params);
256  
257 622 return %{$entity_class->_params};
258 }
259  
260 =head2 derivable
261  
262 Setter/getter for derivable field(s) for this entity class
263  
264 =cut
265  
266 sub derivable {
267 87 ▶ my ($entity_class, %derivable) = @_;
Subroutine derivable: called CC=2 SCAR=6.9
Pod documented
268  
269 87 ▶ $entity_class->_derivable(\%derivable)
Branch: 1 branch
BranchTrueFalseTotal
if keys %derivable 43 44 87
270 if (keys %derivable);
271  
272 87 return %{$entity_class->_derivable};
273 }
274  
275 =head2 entities
276  
277 my $entities = Entity::Entity->entities
278  
279 print "user has entity class: $entities->{user}\n";
280 print "meetingParticipant entity class has not been loaded\n"
281 unless ($entities->{meetingParticipant});
282  
283 Return has hash ref of all loaded entity names and classes
284  
285 =cut
286  
287 sub entities {
288 102 ▶ my $entity_class = shift;
Subroutine entities: called CC=1 SCAR=0.0
Pod documented
289  
290 102 return $entity_class->_entities;
291 }
292  
293 sub _ordered_attribute_names {
294 3933 ▶ my $class = shift;
Subroutine _ordered_attribute_names: called CC=4 SCAR=13.9
295  
296 3933 my %order;
297 my $rank;
298 #
299 # Put primary key fields at the top
300 #
301 3933 foreach ($class->primary_key) {
302 3845 $order{$_} = ++$rank;
303 }
304  
305 #
306 # Sort remaining fields alphabetically
307 #
308 3933 my @atts = $class->meta->get_attribute_list;
309  
310 3933 foreach (sort @atts) {
311 28516 ▶ $order{$_} ||= ++$rank;
Condition66%2/3
$order{$_} ||= ++$rank
l!l&&r!l&&!r
3845246710
MC/DC0%0/2
$order{$_} ||= ++$rank
$order{$_} ++$rank
Truth table66%2/3
A ||= B
A= $order{$_}B= ++$rank
ABresult
1X1
011
000
312 }
313  
314 3933 my @atts_sorted = sort {$order{$a} <=> $order{$b}} (keys %order);
315 3933 return @atts_sorted;
316 }
317  
318 sub _ordered_attributes {
319 3933 ▶ my $class = shift;
Subroutine _ordered_attributes: called CC=2 SCAR=7.0
320  
321 3933 my $meta = $class->meta;
322  
323 3933 ▶ return map {$meta->get_attribute($_) or die "$class: unknown attribute $_"} ($class->_ordered_attribute_names);
Branch: 1 branch
BranchTrueFalseTotal
$meta->get_attribute($_) or die "${class}: unknown attribute $_" 0 28516 28516
324 }
325  
326 sub _cmp_col {
327 2881 ▶ my ($class, $data_type, $v1, $v2, %opt) = @_;
Subroutine _cmp_col: called CC=17 SCAR=29.3
328  
329 #
330 # Compare two values for a property
331 #
332  
333 return
334 2881 ▶ unless (defined $v1 && defined $v2);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v1 and defined $v2 1740 1141 2881
Condition100%3/3
defined $v1 and defined $v2
!ll&&!rl&&r
173911141
MC/DC100%2/2
defined $v1 and defined $v2
defined $v1 defined $v2
Truth table100%3/3
A and B
A= defined $v1B= defined $v2
ABresult
0X0
100
111
335  
336 1141 my $type_info = Elive::Util::inspect_type($data_type);
337 1141 my $array_type = $type_info->array_type;
338 1141 my $type = $type_info->elemental_type;
339 1141 my $cmp;
340  
341 1141 ▶ if ($array_type || $type_info->is_struct) {
Branch: 2 branches
BranchTrueFalseTotal
if ($array_type or $type_info->is_struct) { } 238 903 1141
elsif ($type =~ /^Ref|Any/xi) { } 0 903 903
Condition100%3/3
$array_type or $type_info->is_struct
l!l&&r!l&&!r
88150903
MC/DC100%2/2
$array_type or $type_info->is_struct
$array_type $type_info->is_struct
Truth table100%3/3
A or B
A= $array_typeB= $type_info->is_struct
ABresult
1X1
011
000
342 #
343 # Note shallow comparision of entities and arrays.
344 #
345 238 ▶ my $t = $array_type || $type;
Condition66%2/3
$array_type || $type
l!l&&r!l&&!r
881500
MC/DC0%0/2
$array_type || $type
$array_type $type
Truth table66%2/3
A || B
A= $array_typeB= $type
ABresult
1X1
011
000
346 238 $cmp = $t->stringify($v1) cmp $t->stringify($v2);
347 }
348 elsif ($type =~ m{^Ref|Any}ix) {
349 0 $cmp = YAML::Syck::Dump($v1) cmp YAML::Syck::Dump($v2);
350 }
351 else {
352 #
353 # Elemental comparision. Use normalised frozen values
354 #
355 903 $v1 = Elive::Util::_freeze($v1, $type);
356 903 $v2 = Elive::Util::_freeze($v2, $type);
357  
358 903 ▶ if ($type =~ m{^(Str|Enum|HiResDate)}ix) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type =~ /^(Str|Enum|HiResDate)/xi) { } 641 262 903
elsif ($type =~ /^Bool/xi) { } 51 211 262
elsif ($type =~ /^Int/xi) { } 211 0 211
359 #
360 # string comparision. works on simple strings and
361 # stringified entities. Also used for hires dates
362 # integer comparision may result in arithmetic overflow
363 #
364 $cmp = ($opt{case_insensitive}
365 641 ▶ ? uc($v1) cmp uc($v2)
Branch: 1 branch
BranchTrueFalseTotal
$opt{'case_insensitive'} ? : 1 640 641
366 : $v1 cmp $v2);
367 }
368 elsif ($type =~ m{^Bool}ix) {
369 # boolean comparison
370 51 ▶ $cmp = ($v1 eq 'true'? 1: 0) <=> ($v2 eq 'true'? 1: 0);
Branch: 2 branches
BranchTrueFalseTotal
$v1 eq 'true' ? : 32 19 51
$v2 eq 'true' ? : 33 18 51
371 }
372 elsif ($type =~ m{^Int}ix) {
373 # int comparision
374 211 ▶ $cmp = defined $v1 && defined $v2 && $v1 <=> $v2;
Condition50%3/6
defined $v1 && defined $v2 && $v1 <=> $v2
!ll&&!rl&&r
02074
defined $v1 and defined $v2
!ll&&!rl&&r
00211
MC/DC33%1/3
defined $v1 && defined $v2 && $v1 <=> $v2
defined $v1 defined $v2 $v1 <=> $v2
Truth table50%2/4
A and B && C
A= defined $v1B= defined $v2C= $v1 <=> $v2
ABCresult
0XX0
10X0
1100
1111
375 }
376 else {
377 0 Carp::croak "class $class: unknown type: $type\n";
378 }
379 }
380  
381 1141 ▶ warn YAML::Syck::Dump {cmp => {result =>$cmp,
Branch: 1 branch
BranchTrueFalseTotal
if ($class->debug || 0) >= 5 0 1141 1141
Condition50%1/2
$class->debug || 0
l!l
01141
MC/DC0%0/1
$class->debug || 0
$class->debug
Truth table50%1/2
A
A= $class->debug
Aresult
11
00
382 class => $class,
383 data_type => "$data_type",
384 v1 => $v1,
385 v2 => $v2
386 }}
387 if ($class->debug||0) >= 5;
388  
389 1141 return $cmp;
390 }
391  
392 =head2 properties
393  
394 my @properties = MyApp::Entity::User->properties;
395  
396 Return the property accessor names for an entity
397  
398 =cut
399  
400 sub properties {
401 991 ▶ my $class = shift;
Subroutine properties: called CC=1 SCAR=0.0
Pod documented
402 991 return map {$_->name} ($class->_ordered_attributes);
403 }
404  
405 =head2 property_types
406  
407 my $user_types = MyApp::Entity::User->property_types;
408 my $type_info = Elive::Util::inspect_type($user_types->{role})
409  
410 Return a hashref of attribute data types.
411  
412 =cut
413  
414 sub property_types {
415 2942 ▶ my $class = shift;
Subroutine property_types: called CC=1 SCAR=0.0
Pod documented
416  
417 2942 my @atts = $class->_ordered_attributes;
418  
419 return {
420 2942 map {$_->name => $_->type_constraint} @atts
421 };
422 }
423  
424 =head2 property_doco
425  
426 my $user_doc = MyApp::Entity::User->property_doc
427 my $user_password_doco = $user_doc->{loginPassword}
428  
429 Return a hashref of documentation for properties
430  
431 =cut
432  
433 sub property_doco {
434 0 ▶ my $class = shift;
Subroutine property_doco: not called CC=1 SCAR=6.9
Pod documented
435  
436 0 my @atts = $class->_ordered_attributes;
437  
438 return {
439 0 map {$_->name => $_->{documentation}} @atts
440 };
441 }
442  
443 =head2 stringify
444  
445 Return a human readable string representation of an object. For database
446 entities, this is the primary key:
447  
448 if ($user_obj->stringify eq "11223344") {
449 ....
450 }
451  
452 Arrays of sub-items evaluated, in a string context, to a semi-colon separated
453 string of the individual values sorted.
454  
455 my $group = Elive::Entity::Group->retrieve(98765);
456 if ($group->members->stringify eq "11223344;2222222") {
457 ....
458 }
459  
460 In particular meeting participants stringify to userId=role, e.g.
461  
462 my $participant_list = Elive::Entity::ParticipantList->retrieve(98765);
463 if ($participant_list->participants->stringify eq "11223344=3;2222222=2") {
464 ....
465 }
466  
467 =cut
468  
469 =head2 connection
470  
471 my $default_connection = Elive::Entity::User->connection;
472 my $connection = $entity_obj->connection;
473  
474 Return a connection. Either the actual connection associated with a entity
475 instance, or the default connection that will be used.
476  
477 =cut
478  
479 =head2 disconnect
480  
481 Disconnects and disassociates an Elluminate connection from this class. It is
482 recommended that you do this prior to exiting your program.
483  
484 =cut
485  
486 sub disconnect {
487 18 ▶ my ($class, %opt) = @_;
Subroutine disconnect: called CC=2 SCAR=6.9
Pod documented
488  
489 18 ▶ if (my $connection = $class->connection) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $connection = $class->connection) 8 10 18
490 8 $connection->disconnect;
491 8 $class->connection(undef);
492 }
493  
494 18 return;
495 }
496  
497 sub _restful_url {
498 297 ▶ my $class = shift;
Subroutine _restful_url: called CC=2 SCAR=7.1
499 297 ▶ my $connection = shift || $class->connection;
Condition33%1/3
shift() || $class->connection
l!l&&r!l&&!r
29700
MC/DC0%0/2
shift() || $class->connection
shift() $class->connection
Truth table33%1/3
A || B
A= shift()B= $class->connection
ABresult
1X1
011
000
500 297 my $path = shift;
501  
502 297 my $uri_obj = URI->new( $connection->url );
503 297 $uri_obj->scheme('http');
504  
505 297 return join('/', $uri_obj->as_string,
506 $class->entity_name,
507 $path);
508 }
509  
510 =head2 url
511  
512 my $url = $user->url
513  
514 Abstract method to compute a restful url for an object instance. This will
515 include both the url of the connection string and the entity class name. It
516 is used internally to uniquely identify and cache objects across repositories.
517  
518 =cut
519  
520 sub url {
521 326 ▶ my $self = shift;
Subroutine url: called CC=3 SCAR=11.0
Pod documented
522 326 ▶ my $connection = shift || $self->connection;
Condition66%2/3
shift() || $self->connection
l!l&&r!l&&!r
310160
MC/DC0%0/2
shift() || $self->connection
shift() $self->connection
Truth table66%2/3
A || B
A= shift()B= $self->connection
ABresult
1X1
011
000
523 326 ▶ my $path = $self->stringify
Branch: 1 branch
BranchTrueFalseTotal
my $path = $self->stringify or return 37 289 326
524 or return;
525 289 return $self->_restful_url($connection, $path);
526 }
527  
528 =head2 construct
529  
530 my $user = Entity::User->construct(
531 {userId = 123456,
532 loginName => 'demo_user',
533 role => {
534 roleId => 1
535 }
536 },
537 overwrite => 1, # overwrite any unsaved changes in cache
538 connection => $conn, # connection to use
539 copy => 1, # return a simple blessed uncached object.
540 );
541  
542 Abstract method to construct a data mapped entity. A copy is made of the
543 data for use by the C<is_changed> and C<revert> methods.
544  
545 =cut
546  
547 sub construct {
548 147 ▶ my ($class, $data, %opt) = @_;
Subroutine construct: called CC=12 SCAR=25.8
Pod documented
549  
550 147 ▶ $data = $class->BUILDARGS($data) if $class->can('BUILDARGS');
Branch: 1 branch
BranchTrueFalseTotal
if $class->can("BUILDARGS") 147 0 147
551  
552 147 ▶ croak "usage: ${class}->construct( \\%data )"
Branch: 1 branch
BranchTrueFalseTotal
unless Elive::Util::_reftype($data) eq "HASH" 0 147 147
553 unless (Elive::Util::_reftype($data) eq 'HASH');
554  
555 147 do {
556 147 my %unknown_properties;
557 147 @unknown_properties{keys %$data} = undef;
558 147 delete $unknown_properties{$_} for ($class->properties);
559 147 my @unknown = sort keys %unknown_properties;
560 147 ▶ carp "$class - unknown properties: @unknown" if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 1 146 147
561 };
562  
563 147 ▶ warn YAML::Syck::Dump({class => $class, construct => $data})
Branch: 1 branch
BranchTrueFalseTotal
if "Elive"->debug > 1 0 147 147
564 if (Elive->debug > 1);
565  
566 147 my $self;
567  
568 147 ▶ $self = Scalar::Util::blessed($data)
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($data) ? : 0 147 147
569 ? $data
570 : $class->new($data);
571  
572 138 ▶ my $connection = delete $opt{connection} || $class->connection
Branch: 1 branch
BranchTrueFalseTotal
my $connection = delete $opt{"connection"} || $class->connection or die "not connected" 0 138 138
Condition66%2/3
delete $opt{'connection'} || $class->connection
l!l&&r!l&&!r
84540
MC/DC0%0/2
delete $opt{'connection'} || $class->connection
delete $opt{'connection'} $class->connection
Truth table66%2/3
A || B
A= delete $opt{'connection'}B= $class->connection
ABresult
1X1
011
000
573 or die "not connected";
574  
575 138 my %primary_key_data = map {$_ => $data->{ $_ }} ($class->primary_key);
576  
577 138 foreach (keys %primary_key_data) {
578 134 ▶ unless (defined $primary_key_data{ $_ }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $primary_key_data{$_}) 0 134 134
579 0 die "can't construct $class without value for primary key field: $_";
580 }
581 }
582  
583 $self->_is_copy(1)
584 138 ▶ if $opt{copy};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"copy"} 1 137 138
585  
586 138 my $data_copy = Elive::Util::_clone($self);
587 return $self->__set_db_data($data_copy,
588 connection => $connection,
589 overwrite => $opt{overwrite},
590 138 );
591 }
592  
593 sub __set_db_data {
594 342 ▶ my $struct = shift;
Subroutine __set_db_data: called CC=26 SCAR=34.0
595 342 my $data_copy = shift;
596 342 my %opt = @_;
597  
598 342 my $connection = $opt{connection};
599  
600 342 my $type = Elive::Util::_reftype( $struct );
601  
602 342 ▶ if ($type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type) 342 0 342
603  
604 342 ▶ if (Scalar::Util::blessed $struct
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($struct) and $struct->can("_is_copy")) 342 0 342
Condition33%1/3
builtin::blessed($struct) and $struct->can("_is_copy")
!ll&&!rl&&r
00342
MC/DC0%0/2
builtin::blessed($struct) and $struct->can("_is_copy")
builtin::blessed($struct) $struct->can("_is_copy")
Truth table33%1/3
A and B
A= builtin::blessed($struct)B= $struct->can("_is_copy")
ABresult
0X0
100
111
605 && $struct->can('_is_copy')) {
606  
607 342 ▶ $opt{copy} ||= $struct->_is_copy;
Condition100%3/3
$opt{"copy"} ||= $struct->_is_copy
l!l&&r!l&&!r
11340
MC/DC100%2/2
$opt{"copy"} ||= $struct->_is_copy
$opt{"copy"} $struct->_is_copy
Truth table100%3/3
A ||= B
A= $opt{"copy"}B= $struct->_is_copy
ABresult
1X1
011
000
608  
609 $struct->_is_copy(1)
610 342 ▶ if $opt{copy};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"copy"} 2 340 342
611 }
612  
613 # recurse
614 342 ▶ if ($type eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'ARRAY') { } 30 312 342
elsif ($type eq 'HASH') { } 312 0 312
615 30 foreach (0 .. scalar(@$struct)) {
616 116 ▶ $struct->[$_] = __set_db_data($struct->[$_], $data_copy->[$_], %opt)
Branch: 1 branch
BranchTrueFalseTotal
if ref $struct->[$_] 37 79 116
617 if ref $struct->[$_];
618 }
619 }
620 elsif ($type eq 'HASH') {
621 312 foreach (sort keys %$struct) {
622 $struct->{$_} = __set_db_data($struct->{$_}, $data_copy->{$_}, %opt)
623 808 ▶ if ref $struct->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if ref $struct->{$_} 167 641 808
624 }
625 }
626 else {
627 0 warn "don't know how to set db data for sub-type $type";
628 }
629  
630 342 ▶ if (Scalar::Util::blessed $struct) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($struct)) 342 0 342
631 342 ▶ if ($connection && $struct->can('connection')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($connection and $struct->can("connection")) 342 0 342
Condition33%1/3
$connection and $struct->can("connection")
!ll&&!rl&&r
00342
MC/DC0%0/2
$connection and $struct->can("connection")
$connection $struct->can("connection")
Truth table33%1/3
A and B
A= $connectionB= $struct->can("connection")
ABresult
0X0
100
111
632  
633 342 ▶ if (!$opt{copy}
Branch: 1 branch
BranchTrueFalseTotal
if (not $opt{"copy"} and $struct->can("url") and my $obj_url = $struct->url($connection)) 273 69 342
Condition100%6/6
not $opt{"copy"} and $struct->can("url") and my $obj_url = $struct->url($connection)
!ll&&!rl&&r
3237273
not $opt{"copy"} and $struct->can("url")
!ll&&!rl&&r
230310
MC/DC100%3/3
not $opt{"copy"} and $struct->can("url") and my $obj_url = $struct->url($connection)
not $opt{"copy"} $struct->can("url") my $obj_url = $struct->url($connection)
Truth table100%4/4
not(A) and B and C
A= not $opt{"copy"}B= $struct->can("url")C= my $obj_url = $struct->url($connection)
ABCresult
0XX0
10X0
1100
1111
634 && $struct->can('url')
635 && (my $obj_url = $struct->url($connection))
636 ) {
637  
638 273 my $cache_access;
639  
640 273 ▶ if (my $cached = $Stored_Objects{ $obj_url }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cached = $Elive::DAO::Stored_Objects{$obj_url}) { } 76 197 273
641 76 $cache_access = 'reuse';
642 #
643 # Overwrite the cached object, then reuse it.
644 #
645 die "attempted overwrite of object with unsaved changes ($obj_url)"
646 76 ▶ if !$opt{overwrite} && $cached->is_changed;
Branch: 1 branch
BranchTrueFalseTotal
if not $opt{"overwrite"} and $cached->is_changed 2 74 76
Condition100%3/3
not $opt{"overwrite"} and $cached->is_changed
!ll&&!rl&&r
11632
MC/DC100%2/2
not $opt{"overwrite"} and $cached->is_changed
not $opt{"overwrite"} $cached->is_changed
Truth table100%3/3
not(A) and B
A= not $opt{"overwrite"}B= $cached->is_changed
ABresult
0X0
100
111
647  
648 74 ▶ die "cache type conflict. $obj_url contains an ".ref($cached)." object, but requested ".ref($struct)
Branch: 1 branch
BranchTrueFalseTotal
unless $cached->isa(ref $struct) 0 74 74
649 unless $cached->isa(ref($struct));
650  
651  
652 74 %{$cached} = %{$struct};
653 74 $struct = $cached;
654 }
655 else {
656 197 $cache_access = 'init';
657 }
658  
659 # rewrite, for benefit of 5.13.3
660 271 weaken ($Stored_Objects{$obj_url} = $struct);
661  
662 271 ▶ if ($struct->debug >= 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($struct->debug >= 5) 0 271 271
663 0 warn YAML::Syck::Dump({opt => \%opt, struct => $struct, class => ref($struct), url => $obj_url, cache => $cache_access, ref1 => "$struct", ref2 => "$Stored_Objects{$obj_url}"});
664 }
665 }
666  
667 340 $struct->connection( $connection );
668 }
669  
670 340 ▶ if ($struct->can('_db_data')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($struct->can("_db_data")) 310 30 340
671 #
672 # save before image from database
673 #
674 310 ▶ $data_copy->_db_data(undef)
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($data_copy) and $data_copy->can("_db_data") 310 0 310
Condition33%1/3
builtin::blessed($data_copy) and $data_copy->can("_db_data")
!ll&&!rl&&r
00310
MC/DC0%0/2
builtin::blessed($data_copy) and $data_copy->can("_db_data")
builtin::blessed($data_copy) $data_copy->can("_db_data")
Truth table33%1/3
A and B
A= builtin::blessed($data_copy)B= $data_copy->can("_db_data")
ABresult
0X0
100
111
675 if Scalar::Util::blessed($data_copy)
676 && $data_copy->can('_db_data');
677  
678 310 $struct->_db_data($data_copy);
679 }
680 }
681 }
682  
683 340 return $struct;
684 }
685  
686 #
687 # _freeze - construct name/value pairs for database inserts or updates
688 #
689  
690 sub _freeze {
691 392 ▶ my $class = shift;
Subroutine _freeze: called CC=11 SCAR=24.6
692 392 my $db_data = shift;
693 392 my %opt = @_;
694  
695 392 ▶ $db_data ||= $class if ref($class);
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 23 369 392
Condition66%2/3
$db_data ||= $class
l!l&&r!l&&!r
2210
MC/DC0%0/2
$db_data ||= $class
$db_data $class
Truth table66%2/3
A ||= B
A= $db_dataB= $class
ABresult
1X1
011
000
696 392 ▶ $db_data ||= {};
Condition100%2/2
$db_data ||= {}
l!l
37319
MC/DC100%1/1
$db_data ||= {}
$db_data
Truth table100%2/2
A
A= $db_data
Aresult
11
00
697 392 $db_data = Elive::Util::_clone( $db_data );
698  
699 392 ▶ my $property_types = $class->property_types || {};
Condition50%1/2
$class->property_types || {}
l!l
3920
MC/DC0%0/1
$class->property_types || {}
$class->property_types
Truth table50%1/2
A
A= $class->property_types
Aresult
11
00
700 392 my %param_types = $class->params;
701  
702 392 $class->_canonicalize_properties( $db_data );
703  
704 392 foreach (keys %$db_data) {
705  
706 942 ▶ my $property = $property_types->{$_} || $param_types{$_};
Condition66%2/3
$property_types->{$_} || $param_types{$_}
l!l&&r!l&&!r
916260
MC/DC0%0/2
$property_types->{$_} || $param_types{$_}
$property_types->{$_} $param_types{$_}
Truth table66%2/3
A || B
A= $property_types->{$_}B= $param_types{$_}
ABresult
1X1
011
000
707  
708 942 ▶ unless ($property) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($property) 0 942 942
709 0 my @properties = $class->properties;
710 0 my @param_names = sort keys %param_types;
711 0 Carp::croak "$class: unknown property/parameter: $_: expected: ",join(',', @properties, @param_names);
712 }
713  
714 942 my $type_info = Elive::Util::inspect_type($property);
715 942 my $type = $type_info->elemental_type;
716 942 my $is_array = $type_info->is_array;
717  
718 942 for ($db_data->{$_}) {
719  
720 942 ▶ $_ = Elive::Util::_freeze($_, $is_array ? $property : $type);
Branch: 1 branch
BranchTrueFalseTotal
$is_array ? : 24 918 942
721  
722 }
723 }
724  
725 #
726 # apply any freeze alias mappings
727 #
728 $class->__apply_freeze_aliases( $db_data )
729 392 ▶ unless $opt{canonical};
Branch: 1 branch
BranchTrueFalseTotal
unless $opt{"canonical"} 110 282 392
730  
731 392 return $db_data;
732 }
733  
734 sub _canonicalize_properties {
735 422 ▶ my $class = shift;
Subroutine _canonicalize_properties: called CC=2 SCAR=6.9
736 422 my $data = shift;
737  
738 422 my %aliases = $class->_to_aliases;
739  
740 422 for (grep {exists $data->{$_}} (keys %aliases)) {
741 4 my $att = $aliases{$_};
742 4 $data->{$att} = delete $data->{$_};
743 }
744  
745 422 return $data;
746 }
747  
748 sub __apply_freeze_aliases {
749 114 ▶ my $class = shift;
Subroutine __apply_freeze_aliases: called CC=5 SCAR=16.1
750 114 my $db_data = shift;
751  
752 114 my $aliases = $class->_get_aliases;
753  
754 114 foreach my $alias (keys %$aliases) {
755 410 ▶ if ($aliases->{$alias}{freeze}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($aliases->{$alias}{"freeze"}) 308 102 410
756 my $to = $aliases->{$alias}{to}
757 308 ▶ or die "malformed alias: $alias";
Branch: 1 branch
BranchTrueFalseTotal
my $to = $aliases->{$alias}{"to"} or die "malformed alias: $alias" 0 308 308
758 #
759 # Freeze with this alias
760 #
761 $db_data->{ $alias } = delete $db_data->{ $to }
762 308 ▶ if exists $db_data->{ $to };
Branch: 1 branch
BranchTrueFalseTotal
if exists $db_data->{$to} 28 280 308
763 }
764 }
765  
766 114 return $db_data;
767 }
768  
769 # _find_entities()
770 #
771 # my %entities = Elive::DAO::find_entities( $db_data );
772 #
773 # A utility function to locate entities in SOAP response data. This should be
774 # applied after unpacking and before thawing.
775  
776 sub _find_entities {
777 169 ▶ my $db_data = shift;
Subroutine _find_entities: called CC=2 SCAR=7.1
778  
779 169 ▶ return map {m{^(.*)(Adapter|Response)$}? ($1 => $_): ()} (keys %$db_data);
Branch: 1 branch
BranchTrueFalseTotal
/^(.*)(Adapter|Response)$/ ? : 169 0 169
780 }
781  
782 sub __dereference_adapter {
783 168 ▶ my $class = shift;
Subroutine __dereference_adapter: called CC=5 SCAR=16.2
784 168 my $db_data = shift;
785 168 my $path = shift;
786  
787 168 my $adapter_found;
788 my $entity_data;
789  
790 168 ▶ if (Elive::Util::_reftype($db_data) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (Elive::Util::_reftype($db_data) eq "HASH") 168 0 168
791  
792 168 my %entities = _find_entities( $db_data );
793  
794 168 my $adapter = delete $entities{ $class->entity_name };
795  
796 168 ▶ if ($adapter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($adapter) 167 1 168
797 167 $entity_data = $db_data->{$adapter};
798 167 $$path .= $adapter;
799 }
800  
801 168 my @unknown_entities = sort keys %entities;
802 168 ▶ die "unexpected entities in response:: @unknown_entities"
Branch: 1 branch
BranchTrueFalseTotal
if @unknown_entities 1 167 168
803 if @unknown_entities;
804 }
805  
806 167 ▶ return $entity_data || $db_data;
Condition33%1/3
$entity_data || $db_data
l!l&&r!l&&!r
16700
MC/DC0%0/2
$entity_data || $db_data
$entity_data $db_data
Truth table33%1/3
A || B
A= $entity_dataB= $db_data
ABresult
1X1
011
000
807 }
808  
809 #
810 # _thaw - perform database to perl type conversions
811 #
812  
813 sub _thaw {
814 168 ▶ my $class = shift;
Subroutine _thaw: called CC=25 SCAR=32.8
815 168 my $db_data = shift;
816 168 ▶ my $path = shift || '';
Condition100%2/2
shift() || ''
l!l
67101
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
817  
818 168 $path .= '/';
819  
820 168 my $entity_data = __dereference_adapter( $class, $db_data, \$path);
821 167 ▶ return unless defined $entity_data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $entity_data 0 167 167
822  
823 167 my $ref_type = Elive::Util::_reftype($entity_data);
824 167 ▶ die "thawing $class. expected $path to contain HASH data. found: $ref_type"
Branch: 1 branch
BranchTrueFalseTotal
unless $ref_type eq "HASH" 0 167 167
825 unless $ref_type eq 'HASH';
826  
827 167 my %data;
828 167 my @properties = $class->properties;
829 167 my $aliases = $class->_get_aliases;
830  
831 #
832 # Normalise:
833 # 1. Entity names returned capitalised: 'LoginName' => 'loginName
834 # 2. Primary key may be returned as Id, rather than <entity_name>Id
835 # 3. Apply aliases.
836 #
837 167 my %prop_key_map = map {ucfirst($_) => $_} @properties;
838  
839 167 my @primary_key = $class->primary_key;
840  
841 167 ▶ $prop_key_map{Id} = lcfirst($primary_key[0])
Branch: 1 branch
BranchTrueFalseTotal
if @primary_key 164 3 167
842 if @primary_key;
843  
844 167 foreach my $alias (keys %$aliases) {
845 my $to = $aliases->{$alias}{to}
846 112 ▶ or die "malformed alias: $alias";
Branch: 1 branch
BranchTrueFalseTotal
my $to = $aliases->{$alias}{"to"} or die "malformed alias: $alias" 0 112 112
847  
848 112 $prop_key_map{ ucfirst($alias) } = $to;
849 }
850  
851 167 my $property_types = $class->property_types;
852  
853 167 foreach my $key (keys %$entity_data) {
854  
855 531 my $val = $entity_data->{ $key };
856 531 ▶ my $prop_key = $prop_key_map{$key} || $key;
Condition66%2/3
$prop_key_map{$key} || $key
l!l&&r!l&&!r
475560
MC/DC0%0/2
$prop_key_map{$key} || $key
$prop_key_map{$key} $key
Truth table66%2/3
A || B
A= $prop_key_map{$key}B= $key
ABresult
1X1
011
000
857 531 $data{$prop_key} = $val;
858 }
859  
860 167 foreach my $col (grep {defined $data{$_}} @properties) {
861  
862 529 my $property_type = $property_types->{$col};
863 529 my $type_info = Elive::Util::inspect_type($property_type);
864 529 my $type = $type_info->elemental_type;
865 529 my $is_array = $type_info->is_array;
866 529 my $is_struct = $type_info->is_struct;
867  
868 529 ▶ next unless $col && defined $data{$col};
Branch: 1 branch
BranchTrueFalseTotal
unless $col and defined $data{$col} 0 529 529
Condition33%1/3
$col and defined $data{$col}
!ll&&!rl&&r
00529
MC/DC0%0/2
$col and defined $data{$col}
$col defined $data{$col}
Truth table33%1/3
A and B
A= $colB= defined $data{$col}
ABresult
0X0
100
111
869  
870 529 for my $val ($data{$col}) {
871  
872 529 my $i = 0;
873  
874 529 ▶ if ($is_array) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_array) 8 521 529
875  
876 8 ▶ my $val_type = Elive::Util::_reftype($val) || 'Scalar';
Condition100%2/2
Elive::Util::_reftype($val) || 'Scalar'
l!l
71
MC/DC100%1/1
Elive::Util::_reftype($val) || 'Scalar'
Elive::Util::_reftype($val)
Truth table100%2/2
A
A= Elive::Util::_reftype($val)
Aresult
11
00
877  
878 8 ▶ unless ($val_type eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($val_type eq "ARRAY") 1 7 8
879 #
880 # A single value deserialises to a simple
881 # struct. Coerce it to a one element array
882 #
883 1 $val = [$val];
884 1 ▶ warn "thawing $val_type coerced element into array for $col"
Branch: 1 branch
BranchTrueFalseTotal
if $class->debug 0 1 1
885 if ($class->debug);
886 }
887 }
888  
889 529 ▶ foreach ($is_array? @$val: $val) {
Branch: 1 branch
BranchTrueFalseTotal
$is_array ? : 8 521 529
890  
891 551 ▶ next unless defined;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 551 551
892  
893 551 ▶ my $idx = $is_array? '['.$i.']': '';
Branch: 1 branch
BranchTrueFalseTotal
$is_array ? : 30 521 551
894  
895 551 ▶ if ($is_struct) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_struct) { } 94 457 551
896  
897 94 $_ = $type->_thaw($_, $path . $idx);
898  
899 }
900 else {
901 457 $_ = Elive::Util::_thaw($_, $type);
902 }
903 }
904  
905 529 ▶ if ($is_array) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_array) 8 521 529
906 8 @$val = grep {defined $_} @$val;
907 }
908  
909 #
910 # don't store null values, just omit the property.
911 # saves a heap of work in Moose/Mouse constraints
912 #
913 529 ▶ if (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) { } 529 0 529
914 529 $data{$col} = $val;
915 }
916 else {
917 0 delete $data{$col};
918 }
919 }
920 }
921  
922 167 ▶ if ($class->debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->debug) 0 167 167
923 0 warn "thawed: $class: ".YAML::Syck::Dump(
924 {db => $entity_data,
925 thawed => \%data}
926 );
927 }
928
929 167 return \%data;
930 }
931  
932 sub _process_results {
933 98 ▶ my ($class, $soap_results) = @_;
Subroutine _process_results: called CC=2 SCAR=6.9
934  
935 #
936 # Thaw our returned SOAP responses to reconstruct the data
937 # image.
938 #
939  
940 98 my @rows;
941  
942 98 foreach (@$soap_results) {
943  
944 91 my $row = $class->_thaw($_);
945  
946 91 push(@rows, $row);
947 }
948  
949 98 return \@rows;
950 }
951  
952 sub _readback_check {
953 118 ▶ my ($class, $updates_raw, $rows, %opt) = @_;
Subroutine _readback_check: called CC=10 SCAR=23.2
954  
955 118 my $updates = $class->_freeze( $updates_raw, canonical => 1);
956  
957 118 ▶ warn YAML::Syck::Dump({class => $class, updates_raw => $updates_raw, updates => $updates})
Branch: 1 branch
BranchTrueFalseTotal
if $class->debug >= 5 0 118 118
958 if ($class->debug >= 5);
959  
960 118 foreach my $row_raw (@$rows) {
961  
962 118 my $row = $class->_freeze( $row_raw, canonical => 1);
963  
964 118 ▶ warn YAML::Syck::Dump({row_raw => $row_raw, row => $row})
Branch: 1 branch
BranchTrueFalseTotal
if $class->debug >= 5 0 118 118
965 if ($class->debug >= 5);
966  
967 118 foreach ($class->properties) {
968 1284 ▶ if (exists $updates->{$_} && exists $row->{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $updates->{$_} and exists $row->{$_}) 211 1073 1284
Condition100%3/3
exists $updates->{$_} and exists $row->{$_}
!ll&&!rl&&r
105320211
MC/DC100%2/2
exists $updates->{$_} and exists $row->{$_}
exists $updates->{$_} exists $row->{$_}
Truth table100%3/3
A and B
A= exists $updates->{$_}B= exists $row->{$_}
ABresult
0X0
100
111
969 211 my $write_val = $updates->{$_};
970 211 my $read_val = $row->{$_};
971  
972 211 ▶ if ($write_val ne $read_val) {
Branch: 1 branch
BranchTrueFalseTotal
if ($write_val ne $read_val) 7 204 211
973  
974 7 my $property_type = $class->property_types->{$_};
975  
976 7 my $sent = Elive::Util::string($write_val, $property_type);
977 7 my $read = Elive::Util::string($read_val, $property_type);
978  
979 7 ▶ unless ($sent eq $read) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sent eq $read) 7 0 7
980 7 ▶ warn YAML::Syck::Dump({read => $read, sent => $sent, type => "$property_type"})
Branch: 1 branch
BranchTrueFalseTotal
if $class->debug >= 2 0 7 7
981 if ($class->debug >= 2);
982  
983 7 croak "${class}: Update consistancy check failed on $_ (${property_type}), sent:$sent; read-back:$read";
984 }
985 }
986 }
987 }
988 }
989  
990 111 return @$rows;
991 }
992  
993 =head2 is_changed
994  
995 Abstract method. Returns a list of properties that have been changed since the
996 entity was last retrieved or saved.
997  
998 =cut
999  
1000 sub is_changed {
1001 397 ▶ my $self = shift;
Subroutine is_changed: called CC=8 SCAR=20.8
Pod documented
1002  
1003 397 my @updated_properties;
1004 397 my $db_data = $self->_db_data;
1005  
1006 397 ▶ unless ($db_data) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($db_data) 1 396 397
1007 #
1008 # not mapped to a stored data value. scratch object?, sub entity?
1009 #
1010 1 Carp::carp( ref($self)."->is_changed called on non-database object (".$self->stringify.")\n" );
1011 1 return;
1012 }
1013  
1014 396 my @props = $self->properties;
1015 396 my $property_types = $self->property_types;
1016  
1017 396 foreach my $prop (@props) {
1018  
1019 2830 my $new = $self->$prop;
1020 2830 my $old = $db_data->$prop;
1021 2830 my $type = $property_types->{$prop};
1022  
1023 2830 ▶ die (ref($self)." - attribute $prop contains tainted data")
Branch: 1 branch
BranchTrueFalseTotal
if Elive::Util::_tainted($new) or Elive::Util::_tainted($old) 0 2830 2830
Condition33%1/3
Elive::Util::_tainted($new) or Elive::Util::_tainted($old)
l!l&&r!l&&!r
002830
MC/DC0%0/2
Elive::Util::_tainted($new) or Elive::Util::_tainted($old)
Elive::Util::_tainted($new) Elive::Util::_tainted($old)
Truth table33%1/3
A or B
A= Elive::Util::_tainted($new)B= Elive::Util::_tainted($old)
ABresult
1X1
011
000
1024 if Elive::Util::_tainted($new) || Elive::Util::_tainted($old);
1025  
1026 2830 ▶ if (defined ($new) != defined ($old)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new != defined $old or $self->_cmp_col($type, $new, $old)) 62 2768 2830
Condition100%3/3
defined $new != defined $old or $self->_cmp_col($type, $new, $old)
l!l&&r!l&&!r
16462768
MC/DC100%2/2
defined $new != defined $old or $self->_cmp_col($type, $new, $old)
defined $new != defined $old $self->_cmp_col($type, $new, $old)
Truth table100%3/3
A or B
A= defined $new != defined $oldB= $self->_cmp_col($type, $new, $old)
ABresult
1X1
011
000
1027 || $self->_cmp_col($type, $new, $old)) {
1028  
1029 62 push (@updated_properties, $prop);
1030 }
1031 }
1032  
1033 #
1034 # warn if we catch a primary key modification, after the fact
1035 #
1036 396 my %primary_key = map {$_ => 1} ($self->primary_key);
1037 396 my @primary_key_updates = grep { exists $primary_key{$_} } @updated_properties;
1038 396 foreach my $prop (@primary_key_updates) {
1039  
1040 1 my $type = $property_types->{$prop};
1041 1 my $old_str = Elive::Util::string($db_data->$prop => $type);
1042 1 my $new_str = Elive::Util::string($self->$prop => $type);
1043  
1044 1 Carp::carp( ref($self).": primary key field has been modified $prop: $old_str => $new_str" );
1045 }
1046  
1047 396 return @updated_properties;
1048 }
1049  
1050 =head2 set
1051  
1052 $obj->set(prop1 => val1, prop2 => val2 [,...])
1053  
1054 Abstract method to assign values to entity properties.
1055  
1056 =cut
1057  
1058 sub set {
1059 22 ▶ my $self = shift;
Subroutine set: called CC=16 SCAR=28.1
Pod documented
1060 22 my %data = @_;
1061  
1062 22 ▶ croak "attempt to modify data in a deleted record"
Branch: 1 branch
BranchTrueFalseTotal
if $self->_deleted 1 21 22
1063 if ($self->_deleted);
1064  
1065 21 my %entity_column = map {$_ => 1} ($self->properties);
1066 21 my %primary_key = map {$_ => 1} ($self->primary_key);
1067  
1068 21 $self->_canonicalize_properties( \%data );
1069
1070 21 foreach (keys %data) {
1071  
1072 29 ▶ unless ($entity_column{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($entity_column{$_}) 1 28 29
1073 1 ▶ Carp::carp ((ref($self)||$self).": unknown property: $_");
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
100
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
1074 1 next;
1075 }
1076  
1077 28 ▶ my $type = $self->property_types->{$_}
Branch: 1 branch
BranchTrueFalseTotal
my $type = $self->property_types->{$_} or die +(ref $self || $self) . ": unable to determine property type for field: $_" 0 28 28
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
1078 or die ((ref($self)||$self).": unable to determine property type for field: $_");
1079  
1080 28 ▶ if (exists $primary_key{ $_ }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $primary_key{$_}) 3 25 28
1081  
1082 3 my $old_val = $self->{$_};
1083  
1084 3 ▶ if (defined $old_val && !defined $data{$_}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $old_val and not defined $data{$_}) { } 1 2 3
elsif ($self->_cmp_col($type, $old_val, $data{$_})) { } 1 1 2
Condition66%2/3
defined $old_val and not defined $data{$_}
!ll&&!rl&&r
021
MC/DC50%1/2
defined $old_val and not defined $data{$_}
defined $old_val not defined $data{$_}
Truth table66%2/3
A and not(B)
A= defined $old_valB= not defined $data{$_}
ABresult
0X0
100
111
1085 1 die "attempt to delete primary key";
1086 }
1087 elsif ($self->_cmp_col($type, $old_val, $data{$_})) {
1088 1 die "attempt to update primary key";
1089 }
1090 }
1091  
1092 26 my $meta = $self->meta;
1093 26 my $attribute = $meta->get_attribute($_);
1094 26 my $value = $data{$_};
1095  
1096 26 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 23 3 26
1097  
1098 23 ▶ if (ref($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 3 20 23
1099 #
1100 # inspect the item to see if we need to stringify back to
1101 # a simpler type. For example we may have been passed an
1102 # object, rather than just its primary key.
1103 #
1104 3 ▶ $value = Elive::Util::string($value, $type)
Branch: 1 branch
BranchTrueFalseTotal
unless Elive::Util::inspect_type($type)->is_ref 1 2 3
1105 unless Elive::Util::inspect_type($type)->is_ref;
1106 }
1107  
1108 23 ▶ die (ref($self)." - attempt to set attribute $_ to tainted data")
Branch: 1 branch
BranchTrueFalseTotal
if Elive::Util::_tainted($value) 0 23 23
1109 if Elive::Util::_tainted($value);
1110  
1111 23 $self->$_($value);
1112 }
1113 else {
1114  
1115 3 ▶ die ref($self).": attempt to delete required attribute: $_"
Branch: 1 branch
BranchTrueFalseTotal
if $attribute->is_required 1 2 3
1116 if $attribute->is_required;
1117  
1118 2 delete $self->{$_};
1119 }
1120 }
1121  
1122 18 return $self;
1123 }
1124  
1125 sub _readback {
1126 61 ▶ my ($class, $som, $sent_data, $connection, %opt) = @_;
Subroutine _readback: called CC=1 SCAR=0.0
1127 #
1128 # Inserts and updates normally return a copy of the entity after
1129 # an insert or update. Confirm that the output record contains
1130 # the updates and return it.
1131  
1132 61 my $results = $class->_get_results($som, $connection);
1133 #
1134 # Check that the return response has our inserts/updates
1135 #
1136 61 my $rows = $class->_process_results( $results );
1137 61 $class->_readback_check($sent_data, $rows, %opt);
1138  
1139 61 return @$rows;
1140 }
1141  
1142 sub _to_aliases {
1143 525 ▶ my $class = shift;
Subroutine _to_aliases: called CC=3 SCAR=11.0
1144  
1145 525 my $aliases = $class->_get_aliases;
1146  
1147 525 my %aliased_to;
1148  
1149 525 foreach my $alias (keys %$aliases) {
1150 my $to = $aliases->{$alias}{to}
1151 1440 ▶ or die "malformed alias: $alias";
Branch: 1 branch
BranchTrueFalseTotal
my $to = $aliases->{$alias}{"to"} or die "malformed alias: $alias" 0 1440 1440
1152  
1153 1440 $aliased_to{$alias} = $to;
1154 }
1155  
1156 525 return %aliased_to;
1157 }
1158  
1159 =head2 insert
1160  
1161 my $new_user = Elive::Entity::User->insert(
1162 {loginName => 'demo_user',
1163 email => 'demo.user@test.org'}
1164 },
1165 connection => $con, # connection to use,
1166 command => $cmd, # soap command to use
1167 );
1168  
1169 print "inserted user with id: ".$new_user->userId."\n";
1170  
1171 Abstract method to insert new entities. The primary key is generally not
1172 required. It is generated for you and returned with the newly created object.
1173  
1174 =cut
1175  
1176 sub insert {
1177 53 ▶ my ($class, $_insert_data, %opt) = @_;
Subroutine insert: called CC=6 SCAR=18.2
Pod documented
1178  
1179 53 ▶ my $connection = $opt{connection} || $class->connection
Branch: 1 branch
BranchTrueFalseTotal
my $connection = $opt{"connection"} || $class->connection or die "not connected" 0 53 53
Condition66%2/3
$opt{'connection'} || $class->connection
l!l&&r!l&&!r
40130
MC/DC0%0/2
$opt{'connection'} || $class->connection
$opt{'connection'} $class->connection
Truth table66%2/3
A || B
A= $opt{'connection'}B= $class->connection
ABresult
1X1
011
000
1180 or die "not connected";
1181  
1182 53 my %insert_data = %$_insert_data;
1183 53 ▶ my %params = %{delete $opt{param} || {}};
Branch: 1 branch
BranchTrueFalseTotal
delete $opt{"param"} or {} 53 0 53
1184  
1185 53 my $data_params = $class->_freeze({%insert_data, %params});
1186  
1187 53 ▶ my $command = $opt{command} || 'create'.$class->entity_name;
Condition50%1/2
$opt{'command'} || 'create' . $class->entity_name
l!l
053
MC/DC0%0/1
$opt{'command'} || 'create' . $class->entity_name
$opt{'command'}
Truth table50%1/2
A
A= $opt{'command'}
Aresult
11
00
1188  
1189 53 $connection->check_command($command => 'c');
1190  
1191 53 my $som = $connection->call($command, %$data_params);
1192  
1193 53 my @rows = $class->_readback($som, $_insert_data, $connection, %opt);
1194  
1195 53 my @objs = (map {$class->construct( $_, connection => $connection )}
1196 @rows);
1197 #
1198 # possibly return a list of recurring meetings.
1199 #
1200 53 ▶ return wantarray? @objs : $objs[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 52 53
1201 }
1202  
1203 =head2 live_entity
1204  
1205 my $user_ref
1206 = Elive::Entity->live_entity('http://test.org/User/1234567890');
1207  
1208 Returns a reference to an object in the Elive::Entity cache.
1209  
1210 =cut
1211  
1212 sub live_entity {
1213 10 ▶ my $class = shift;
Subroutine live_entity: called CC=1 SCAR=0.0
Pod documented
1214 10 my $url = shift;
1215  
1216 10 return $Stored_Objects{ $url };
1217 }
1218  
1219 =head2 live_entities
1220  
1221 my $live_entities = Elive::Entity->live_entities;
1222  
1223 my $user_ref = $live_entities->{'http://test.org/User/1234567890'};
1224  
1225 Returns a reference to the Elive::Entity cache.
1226  
1227 =cut
1228  
1229 sub live_entities {
1230 2 ▶ my $class = shift;
Subroutine live_entities: called CC=1 SCAR=0.0
Pod documented
1231 2 return \%Stored_Objects;
1232 }
1233  
1234 =head2 update
1235  
1236 Abstract method to update entities. The following commits outstanding changes
1237 to the object.
1238  
1239 $obj->{foo} = 'Foo'; # change foo attribute directly
1240 $foo->update; # save
1241  
1242 $obj->bar('Bar'); # change bar via its accessor
1243 $obj->update; # save
1244  
1245 Updates may also be passed as parameters:
1246  
1247 # change and save foo and bar. All in one go.
1248 $obj->update({foo => 'Foo', bar => 'Bar'});
1249  
1250 This method can be called from the class level. You will need to
1251 supply the primary key and all mandatory fields.
1252  
1253 =cut
1254  
1255 sub update {
1256 8 ▶ my ($self, $_update_data, %opt) = @_;
Subroutine update: called CC=23 SCAR=41.1
Pod documented
1257  
1258 8 ▶ die "attempted to update deleted record"
Branch: 1 branch
BranchTrueFalseTotal
if $self->_deleted 0 8 8
1259 if ($self->_deleted);
1260  
1261 8 ▶ my %params = %{ $opt{param} || {} };
Branch: 1 branch
BranchTrueFalseTotal
$opt{"param"} or {} 8 0 8
1262 8 my %primary_key = map {$_ => 1} ($self->primary_key);
1263 8 my %updates;
1264  
1265 8 ▶ if (! ref $self) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $self) { } 0 8 8
elsif ($_update_data) { } 5 3 8
1266 # class level update
1267 0 ▶ $opt{connection} ||= $self->connection
Branch: 1 branch
BranchTrueFalseTotal
if $self->connection 0 0 0
Condition0%0/3
$opt{"connection"} ||= $self->connection
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt{"connection"} ||= $self->connection
$opt{"connection"} $self->connection
Truth table0%0/3
A ||= B
A= $opt{"connection"}B= $self->connection
ABresult
1X1
011
000
1268 if $self->connection;
1269 0 $self = $self->construct( $_update_data, %opt);
1270 0 for (keys %$self) {
1271 0 $updates{$_} = $self->$_;
1272 }
1273 }
1274 elsif ($_update_data) {
1275  
1276 5 ▶ croak 'usage: $obj->update( \%data )'
Branch: 1 branch
BranchTrueFalseTotal
unless Elive::Util::_reftype($_update_data) eq "HASH" 0 5 5
1277 unless (Elive::Util::_reftype($_update_data) eq 'HASH');
1278  
1279 5 my %update_data = %{ $_update_data };
1280 #
1281 # sift out things which are included in the data payload, but should
1282 # be parameters.
1283 #
1284 5 my %param_names = $self->params;
1285 5 foreach (grep {exists $update_data{$_}} %param_names) {
1286 0 my $val = delete $update_data{$_};
1287 0 ▶ $params{$_} = $val unless exists $params{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params{$_} 0 0 0
1288 }
1289  
1290 5 ▶ $self->set( %update_data)
Branch: 1 branch
BranchTrueFalseTotal
if keys %update_data 5 0 5
1291 if (keys %update_data);
1292 }
1293  
1294 #
1295 # Write only changed properties.
1296 #
1297 my @updated_properties = ($opt{changed}
1298 8 ▶ ? @{$opt{changed}}
Branch: 1 branch
BranchTrueFalseTotal
$opt{'changed'} ? : 2 6 8
1299 : $self->is_changed);
1300  
1301 #
1302 # merge in pending updates to the current entity.
1303 #
1304  
1305 8 foreach (@updated_properties, keys %primary_key) {
1306  
1307 25 my $update_val = $self->$_;
1308  
1309 25 ▶ if (exists $primary_key{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $primary_key{$_}) 8 17 25
1310 8 my $type = $self->property_types->{$_};
1311 8 my $db_val = $self->_db_data->$_;
1312 8 ▶ croak 'primary key field $_ updated - refusing to save'
Branch: 1 branch
BranchTrueFalseTotal
if $self->_cmp_col($type, $db_val, $update_val) 0 8 8
1313 if $self->_cmp_col($type, $db_val, $update_val);
1314 }
1315  
1316 25 $updates{$_} = $update_val;
1317 }
1318  
1319 8 ▶ my $command = $opt{command} || 'update'.$self->entity_name;
Condition100%2/2
$opt{'command'} || 'update' . $self->entity_name
l!l
17
MC/DC100%1/1
$opt{'command'} || 'update' . $self->entity_name
$opt{'command'}
Truth table100%2/2
A
A= $opt{'command'}
Aresult
11
00
1320  
1321 8 $self->connection->check_command($command => 'u');
1322  
1323 8 my $data_frozen = $self->_freeze({%updates, %params});
1324  
1325 8 my $som = $self->connection->call($command, %$data_frozen);
1326  
1327 8 my $class = ref($self);
1328  
1329 8 my @rows = $class->_readback($som, \%updates, $self->connection, %opt);
1330 8 my $data = $rows[0];
1331  
1332 8 ▶ unless ($data && Elive::Util::_reftype($data) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data and Elive::Util::_reftype($data) eq "HASH") 0 8 8
Condition33%1/3
$data and Elive::Util::_reftype($data) eq "HASH"
!ll&&!rl&&r
008
MC/DC0%0/2
$data and Elive::Util::_reftype($data) eq "HASH"
$data Elive::Util::_reftype($data) eq "HASH"
Truth table33%1/3
A and B
A= $dataB= Elive::Util::_reftype($data) eq "HASH"
ABresult
0X0
100
111
1333  
1334 0 ▶ warn "no data in update response - having to re-fetch (grrrr!)"
Branch: 1 branch
BranchTrueFalseTotal
if $class->debug 0 0 0
1335 if $class->debug;
1336  
1337 0 ▶ $data = $class->retrieve( $self->stringify, raw => 1)
Branch: 1 branch
BranchTrueFalseTotal
$data = $class->retrieve($self->stringify, "raw", 1) or die "unable to get update results" 0 0 0
1338 or die "unable to get update results";
1339  
1340 0 $class->_readback_check(\%updates, [$data], %opt);
1341 }
1342  
1343 #
1344 # refresh the object from the database read-back
1345 #
1346 8 my $obj = $self->construct($data, connection => $self->connection, overwrite => 1, copy => $self->_is_copy);
1347  
1348 8 ▶ unless ($obj->_refaddr eq $self->_refaddr) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj->_refaddr eq $self->_refaddr) 0 8 8
1349 0 ▶ warn $obj->url." (obj=$obj, self=$self) - not in cache, nor is it a copy."
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_copy 0 0 0
1350 unless $self->_is_copy;
1351 # clone the result
1352 0 %{$self} = %{ Elive::Util::_clone($obj) };
1353 0 $self->__set_db_data( Elive::Util::_clone($obj->_db_data), connection => $self->connection, copy => 1);
1354 }
1355  
1356 8 return $self;
1357 }
1358  
1359 =head2 list
1360  
1361 my $users = Elive::Entity::User->list(
1362 filter => 'surname = smith', # filter results (server side)
1363 command => $cmd, # soap command to use
1364 connection => $connection, # connection to use
1365 raw => 1, # return unblessed data
1366 );
1367  
1368 Abstract method to list entity objects.
1369  
1370 =cut
1371  
1372 sub list {
1373 0 ▶ my ($class, %opt) = @_;
Subroutine list: not called CC=8 SCAR=42.8
Pod documented
1374  
1375 my @params = $opt{params}
1376 0 ▶ ? %{ $class->_freeze( delete $opt{params} ) }
Branch: 1 branch
BranchTrueFalseTotal
$opt{'params'} ? : 0 0 0
1377 : ();
1378  
1379 0 ▶ if (my $filter = delete $opt{filter} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $filter = delete $opt{"filter"}) 0 0 0
1380 0 push( @params, filter => Elive::Util::_freeze($filter => 'Str') );
1381 }
1382  
1383 0 ▶ my $connection = $opt{connection} || $class->connection
Branch: 1 branch
BranchTrueFalseTotal
my $connection = $opt{"connection"} || $class->connection or die "no connection active" 0 0 0
Condition0%0/3
$opt{'connection'} || $class->connection
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt{'connection'} || $class->connection
$opt{'connection'} $class->connection
Truth table0%0/3
A || B
A= $opt{'connection'}B= $class->connection
ABresult
1X1
011
000
1384 or die "no connection active";
1385  
1386 0 ▶ my $collection_name = $class->collection_name || $class->entity_name;
Condition0%0/3
$class->collection_name || $class->entity_name
l!l&&r!l&&!r
000
MC/DC0%0/2
$class->collection_name || $class->entity_name
$class->collection_name $class->entity_name
Truth table0%0/3
A || B
A= $class->collection_nameB= $class->entity_name
ABresult
1X1
011
000
1387  
1388 0 ▶ die "misconfigured class $class - has neither a collection_name or entity_name"
Branch: 1 branch
BranchTrueFalseTotal
unless $collection_name 0 0 0
1389 unless $collection_name;
1390  
1391 0 ▶ my $command = $opt{command} || 'list'.$collection_name;
Condition0%0/2
$opt{'command'} || 'list' . $collection_name
l!l
00
MC/DC0%0/1
$opt{'command'} || 'list' . $collection_name
$opt{'command'}
Truth table0%0/2
A
A= $opt{'command'}
Aresult
11
00
1392 0 $connection->check_command($command => 'r');
1393  
1394 0 my $som = $connection->call($command, @params);
1395  
1396 0 my $results = $class->_get_results($som,$connection);
1397  
1398 0 my $rows = $class->_process_results( $results );
1399  
1400 return [
1401 0 map { $class->construct( $_, connection => $connection) }
1402 @$rows
1403 ];
1404 }
1405  
1406 sub _fetch {
1407 27 ▶ my ($class, $db_query, %opt) = @_;
Subroutine _fetch: called CC=9 SCAR=22.5
1408  
1409 27 ▶ $db_query ||= {};
Condition50%1/2
$db_query ||= {}
l!l
270
MC/DC0%0/1
$db_query ||= {}
$db_query
Truth table50%1/2
A
A= $db_query
Aresult
11
00
1410  
1411 27 ▶ croak "usage: ${class}->_fetch( \\%query )"
Branch: 1 branch
BranchTrueFalseTotal
unless Elive::Util::_reftype($db_query) eq "HASH" 0 27 27
1412 unless (Elive::Util::_reftype($db_query) eq 'HASH');
1413  
1414 27 ▶ my $connection = $opt{connection} || $class->connection
Branch: 1 branch
BranchTrueFalseTotal
my $connection = $opt{"connection"} || $class->connection or die "no connection active" 0 27 27
Condition66%2/3
$opt{'connection'} || $class->connection
l!l&&r!l&&!r
13140
MC/DC0%0/2
$opt{'connection'} || $class->connection
$opt{'connection'} $class->connection
Truth table66%2/3
A || B
A= $opt{'connection'}B= $class->connection
ABresult
1X1
011
000
1415 or die "no connection active";
1416  
1417 27 ▶ my $command = $opt{command} || 'get'.$class->entity_name;
Condition50%1/2
$opt{'command'} || 'get' . $class->entity_name
l!l
027
MC/DC0%0/1
$opt{'command'} || 'get' . $class->entity_name
$opt{'command'}
Truth table50%1/2
A
A= $opt{'command'}
Aresult
11
00
1418  
1419 27 ▶ warn "get: entity name for $class: ".$class->entity_name.", command: ".$command
Branch: 1 branch
BranchTrueFalseTotal
if $class->debug 0 27 27
1420 if $class->debug;
1421  
1422 27 $connection->check_command($command => 'r');
1423  
1424 27 my $db_query_frozen = $class->_freeze( $db_query );
1425  
1426 27 my $som = $connection->call($command, %{$db_query_frozen});
1427  
1428 27 my $results = $class->_get_results($som, $connection);
1429  
1430 27 my $rows = $class->_process_results( $results );
1431 27 ▶ return $rows if $opt{raw};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"raw"} 1 26 27
1432 #
1433 # 0 results => not found. Would be treated by readback as an error,
1434 # but perfectly valid here. Just means we didn't find a matching entity.
1435 #
1436 26 ▶ return []
Branch: 1 branch
BranchTrueFalseTotal
unless @$rows 7 19 26
1437 unless @$rows;
1438  
1439 19 $class->_readback_check($db_query, $rows, %opt);
1440 19 return [map {$class->construct( $_, connection => $connection )} @$rows];
1441 }
1442  
1443 =head2 retrieve
1444  
1445 my $user = Elive::Entity::User->retrieve(
1446 $user_id,
1447 reuse => 1, # use cached data if present.
1448 );
1449
1450  
1451 Abstract method to retrieve a single entity object by primary key.
1452  
1453 =cut
1454  
1455 sub retrieve {
1456 16 ▶ my ($class, $vals, %opt) = @_;
Subroutine retrieve: called CC=12 SCAR=26.5
Pod documented
1457  
1458 16 ▶ $vals = [$vals]
Branch: 1 branch
BranchTrueFalseTotal
if $vals and Elive::Util::_reftype($vals) ne "ARRAY" 16 0 16
Condition33%1/3
$vals and Elive::Util::_reftype($vals) ne "ARRAY"
!ll&&!rl&&r
0016
MC/DC0%0/2
$vals and Elive::Util::_reftype($vals) ne "ARRAY"
$vals Elive::Util::_reftype($vals) ne "ARRAY"
Truth table33%1/3
A and B
A= $valsB= Elive::Util::_reftype($vals) ne "ARRAY"
ABresult
0X0
100
111
1459 if $vals && Elive::Util::_reftype($vals) ne 'ARRAY';
1460  
1461 16 my @key_cols = $class->primary_key;
1462  
1463 16 ▶ for (my $n = 0; $n < @key_cols; $n++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < @key_cols) 16 16 32
1464  
1465 16 ▶ die "incomplete primary key value for: $key_cols[$n]"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $vals->[$n] 0 16 16
1466 unless defined ($vals->[$n]);
1467 }
1468  
1469 16 ▶ my $connection = $opt{connection} || $class->connection
Branch: 1 branch
BranchTrueFalseTotal
my $connection = $opt{"connection"} || $class->connection or die "not connected" 0 16 16
Condition66%2/3
$opt{'connection'} || $class->connection
l!l&&r!l&&!r
6100
MC/DC0%0/2
$opt{'connection'} || $class->connection
$opt{'connection'} $class->connection
Truth table66%2/3
A || B
A= $opt{'connection'}B= $class->connection
ABresult
1X1
011
000
1470 or die "not connected";
1471  
1472 16 ▶ if ($opt{reuse}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"reuse"}) 8 8 16
1473 #
1474 # Have we already got the object cached? If so return it
1475 #
1476 8 my %pkey;
1477 8 @pkey{$class->primary_key} = @$vals;
1478  
1479 8 my $obj_url = $class->_restful_url(
1480 $connection,
1481 $class->stringify(\%pkey)
1482 );
1483  
1484 8 ▶ if ( my $cached = $class->live_entity($obj_url) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cached = $class->live_entity($obj_url)) 8 0 8
1485 8 ▶ die "cache type conflict. $obj_url contains an ".ref($cached)." object, but requested $class"
Branch: 1 branch
BranchTrueFalseTotal
unless $cached->isa($class) 0 8 8
1486 unless $cached->isa($class);
1487  
1488 8 ▶ warn "retrieve from cache $obj_url (".ref($cached).")"
Branch: 1 branch
BranchTrueFalseTotal
if $class->debug 0 8 8
1489 if $class->debug;
1490  
1491 8 return $cached
1492 }
1493 }
1494 #
1495 # need to fetch it
1496 #
1497 8 my $all = $class->_retrieve_all($vals, %opt);
1498  
1499 #
1500 # We've supplied a full primary key, so can expect 0 or 1 values
1501 # to be returned.
1502 #
1503 7 ▶ warn "${class}->retrieve([@$vals]) returned extraneous data - discarding\n"
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$all > 1 0 7 7
1504 if (scalar @$all > 1);
1505  
1506 7 return $all->[0];
1507 }
1508  
1509 # _retrieve_all() - Retrieve entity objects by partial primary key.
1510 #
1511 # my $participants
1512 # = Elive::Entity::ParticipantList->_retrieve_all($meeting_id)
1513 #
1514  
1515 sub _retrieve_all {
1516 8 ▶ my ($class, $vals, %opt) = @_;
Subroutine _retrieve_all: called CC=6 SCAR=18.3
1517  
1518 8 ▶ croak 'usage $class->_retrieve_all([$val,..],%opt)'
Branch: 1 branch
BranchTrueFalseTotal
unless Elive::Util::_reftype($vals) eq "ARRAY" 0 8 8
1519 unless Elive::Util::_reftype($vals) eq 'ARRAY';
1520  
1521 8 my @key_cols = $class->primary_key;
1522 8 my @vals = @$vals;
1523  
1524 8 my %fetch;
1525  
1526 8 ▶ while (@vals && @key_cols) {
Branch: 1 branch
BranchTrueFalseTotal
if (@vals and @key_cols) 8 8 16
Condition66%2/3
@vals and @key_cols
!ll&&!rl&&r
808
MC/DC50%1/2
@vals and @key_cols
@vals @key_cols
Truth table66%2/3
A and B
A= @valsB= @key_cols
ABresult
0X0
100
111
1527 8 my $key = shift(@key_cols);
1528 8 my $val = shift(@vals);
1529  
1530 8 ▶ $fetch{$key} = $val
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 8 0 8
1531 if (defined $val);
1532 }
1533  
1534 8 ▶ die "nothing to retrieve"
Branch: 1 branch
BranchTrueFalseTotal
unless keys %fetch 0 8 8
1535 unless (keys %fetch);
1536  
1537 8 return $class->_fetch(\%fetch, %opt);
1538 }
1539  
1540 =head2 delete
1541  
1542 $user_obj->delete;
1543  
1544 Elive::Entity::Session->delete(sessionId = 123456);
1545  
1546 Abstract method to delete an entity.
1547  
1548 =cut
1549  
1550 sub delete {
1551 10 ▶ my ($self, %opt) = @_;
Subroutine delete: called CC=7 SCAR=21.6
Pod documented
1552  
1553 10 my @primary_key = $self->primary_key;
1554 10 my @id;
1555  
1556 10 ▶ die "entity lacks a primary key - can't delete"
Branch: 1 branch
BranchTrueFalseTotal
unless @primary_key > 0 0 10 10
1557 unless (@primary_key > 0);
1558  
1559 10 ▶ if (ref($self)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $self) { } 10 0 10
elsif ($opt{$primary_key[0]}) { } 0 0 0
1560 10 @id = $self->id;
1561 }
1562 elsif ($opt{ $primary_key[0] }) {
1563 # class level delete - primary key supplied in options
1564 0 @id = map { $opt{$_} } @primary_key;
1565 }
1566 else {
1567 0 die "can't determine primary key without object or @primary_key"
1568 }
1569  
1570 my @params = map {
1571 10 $_ => shift( @id );
1572 } @primary_key;
1573  
1574 10 ▶ my $command = $opt{command} || 'delete'.$self->entity_name;
Condition50%1/2
$opt{'command'} || 'delete' . $self->entity_name
l!l
010
MC/DC0%0/1
$opt{'command'} || 'delete' . $self->entity_name
$opt{'command'}
Truth table50%1/2
A
A= $opt{'command'}
Aresult
11
00
1575 10 $self->connection->check_command($command => 'd');
1576  
1577 10 my $som = $self->connection->call($command, @params);
1578  
1579 10 my $results = $self->_get_results($som, $self->connection);
1580 10 my $rows = $self->_process_results($results);
1581  
1582 #
1583 # Umm, we did get a read-back of the record, but the contents
1584 # seem to be dubious. Perform cardinality checks, but don't do
1585 # write-back checks.
1586 #
1587  
1588 10 ▶ croak "Didn't receive a response for deletion: ".$self->entity_name
Branch: 1 branch
BranchTrueFalseTotal
unless @$rows 0 10 10
1589 unless @$rows;
1590  
1591 10 ▶ croak "Received multiple responses for deletion: ".$self->entity_name
Branch: 1 branch
BranchTrueFalseTotal
if @$rows > 1 0 10 10
1592 if (@$rows > 1);
1593  
1594 10 return $self->_deleted(1);
1595 }
1596  
1597 =head2 revert
1598  
1599 $user->revert # revert entire entity
1600 $user->revert(qw/loginName email/); # revert selected properties
1601  
1602 Abstract method to revert an entity to its last constructed value.
1603  
1604 =cut
1605  
1606 37 our $REVERTING;
1607  
1608 sub revert {
1609 30 ▶ my ($self, @props) = @_;
Subroutine revert: called CC=5 SCAR=16.1
Pod documented
1610  
1611 30 local( $REVERTING ) = 1;
1612  
1613 30 ▶ my $db_data = $self->_db_data
Branch: 1 branch
BranchTrueFalseTotal
my $db_data = $self->_db_data or die "object doesn't have db-data!? - can't cope" 0 30 30
1614 or die "object doesn't have db-data!? - can't cope";
1615  
1616 30 ▶ @props = $self->is_changed
Branch: 1 branch
BranchTrueFalseTotal
unless @props 29 1 30
1617 unless @props;
1618  
1619 30 for (@props) {
1620  
1621 21 ▶ if (exists $db_data->{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $db_data->{$_}) { } 16 5 21
1622 16 $self->{$_} = $db_data->{$_};
1623 }
1624 else {
1625 5 delete $self->{$_};
1626 }
1627 }
1628  
1629 30 return $self;
1630 }
1631  
1632 sub _not_available {
1633 0 ▶ my $self = shift;
Subroutine _not_available: not called CC=1 SCAR=6.9
1634  
1635 0 croak "this operation is not available for ". $self->entity_name;
1636 }
1637  
1638 #
1639 # Shared subtypes
1640 #
1641 BEGIN {
1642  
1643 subtype 'HiResDate'
1644 => as 'Int'
1645 19 ▶ => where {m{^-?\d+$}
Subroutine __ANON__: called CC=5 SCAR=17.8
Branch: 1 branch
BranchTrueFalseTotal
/^-?\d+$/ and /^0+$/ || length $_ > 10 && !/-/ || Carp::carp("doesn't look like a hi-res date: $_") 19 0 19
Condition66%8/12
/^-?\d+$/ && (/^0+$/ || length $_ > 10 && !/-/ || Carp::carp("doesn't look like a hi-res date: $_"))
!ll&&!rl&&r
0019
/^0+$/ or length $_ > 10 and not /-/ or Carp::carp("doesn't look like a hi-res date: $_")
l!l&&r!l&&!r
1720
/^0+$/ or length $_ > 10 and not /-/
l!l&&r!l&&!r
0172
length $_ > 10 and not /-/
!ll&&!rl&&r
1117
MC/DC0%0/5
/^-?\d+$/ && (/^0+$/ || length $_ > 10 && !/-/ || Carp::carp("doesn't look like a hi-res date: $_"))
/^-?\d+$/ /^0+$/ length $_ > 10 not /-/ Carp::carp("doesn't look like a hi-res date: $_")
Truth table42%3/7
A && B or C and not(D) or E
A= /^-?\d+$/B= /^0+$/C= length $_ > 10D= not /-/E= Carp::carp("doesn't look like a hi-res date: $_")
ABCDEresult
0XXXX0
100X00
101000
11XXX1
1011X1
100X11
101011
1646 && (m{^0+$} || (length($_) > 10 && !m{-})
1647 or Carp::carp "doesn't look like a hi-res date: $_")}
1648 37 ▶ => message {"invalid date: $_"};
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
1649 }
1650  
1651 sub can {
1652 2864 ▶ my ($class, $method) = @_;
Subroutine can: called CC=5 SCAR=16.1
Pod undocumented
1653  
1654 2864 ▶ my $subref = try { $class->SUPER::can($method) };
Subroutine __ANON__: called CC=1 SCAR=0.0
1655  
1656 2864 ▶ unless ($subref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($subref) 525 2339 2864
1657  
1658 525 ▶ my $aliases = try { $class->_aliases };
Subroutine __ANON__: called CC=1 SCAR=0.0
1659  
1660 525 ▶ if ($aliases && $aliases->{$method}
Branch: 1 branch
BranchTrueFalseTotal
if ($aliases and $aliases->{$method} and my $alias_to = $aliases->{$method}{"to"}) 2 523 525
Condition83%5/6
$aliases and $aliases->{$method} and my $alias_to = $aliases->{$method}{"to"}
!ll&&!rl&&r
52302
$aliases and $aliases->{$method}
!ll&&!rl&&r
1663572
MC/DC66%2/3
$aliases and $aliases->{$method} and my $alias_to = $aliases->{$method}{"to"}
$aliases $aliases->{$method} my $alias_to = $aliases->{$method}{"to"}
Truth table75%3/4
A and B and C
A= $aliasesB= $aliases->{$method}C= my $alias_to = $aliases->{$method}{"to"}
ABCresult
0XX0
10X0
1100
1111
1661 && (my $alias_to = $aliases->{$method}{to})) {
1662 2 $subref = $class->SUPER::can($alias_to);
1663 }
1664 }
1665  
1666 2864 return $subref;
1667 }
1668  
1669 sub AUTOLOAD {
1670 1 ▶ my @class_path = split('::', ${Elive::DAO::AUTOLOAD});
Subroutine AUTOLOAD: called CC=4 SCAR=14.6
1671  
1672 1 my $method = pop(@class_path);
1673 1 my $class = join('::', @class_path);
1674  
1675 1 ▶ die "Autoload Dispatch Error: ".${Elive::DAO::AUTOLOAD}
Branch: 1 branch
BranchTrueFalseTotal
unless $class and $method 0 1 1
Condition33%1/3
$class and $method
!ll&&!rl&&r
001
MC/DC0%0/2
$class and $method
$class $method
Truth table33%1/3
A and B
A= $classB= $method
ABresult
0X0
100
111
1676 unless $class && $method;
1677  
1678 1 ▶ if (my $subref = $class->can($method)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $subref = $class->can($method)) { } 1 0 1
1679 37 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1680 1 *{$class.'::'.$method} = $subref;
1681  
1682 1 goto $subref;
1683 }
1684 else {
1685 0 Carp::croak $class.": unknown method $method";
1686 }
1687 }
1688  
1689 sub DEMOLISH {
1690 1178 ▶ my ($self) = shift;
Subroutine DEMOLISH: called CC=10 SCAR=24.3
Pod documented
1691 1178 my $class = ref($self);
1692  
1693 1178 ▶ warn 'DEMOLISH '.$self->url.': db_data='.($self->_db_data||'(null)')."\n"
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug || 0) >= 5 0 1178 1178
Condition25%1/4
$self->debug || 0
l!l
01178
$self->_db_data || '(null)'
l!l
00
MC/DC0%0/1
$self->debug || 0
$self->debug
MC/DC0%0/1
$self->_db_data || '(null)'
$self->_db_data
Truth table50%1/2
A
A= $self->debug
Aresult
11
00
Truth table0%0/2
A
A= $self->_db_data
Aresult
11
00
1694 if ($self->debug||0) >= 5;
1695  
1696 1178 ▶ if (my $db_data = $self->_db_data) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $db_data = $self->_db_data) 233 945 1178
1697 233 ▶ if (!$REVERTING
Branch: 1 branch
BranchTrueFalseTotal
if (not $Elive::DAO::REVERTING and my(@changed) = $self->is_changed and not $self->_deleted) 1 232 233
Condition83%5/6
not $Elive::DAO::REVERTING and my(@changed) = $self->is_changed and not $self->_deleted
!ll&&!rl&&r
23201
not $Elive::DAO::REVERTING and my(@changed) = $self->is_changed
!ll&&!rl&&r
62261
MC/DC66%2/3
not $Elive::DAO::REVERTING and my(@changed) = $self->is_changed and not $self->_deleted
not $Elive::DAO::REVERTING my(@changed) = $self->is_changed not $self->_deleted
Truth table75%3/4
not(A) and B and not(C)
A= not $Elive::DAO::REVERTINGB= my(@changed) = $self->is_changedC= not $self->_deleted
ABCresult
0XX0
10X0
1100
1111
1698 && (my @changed = $self->is_changed)
1699 && ! $self->_deleted) {
1700 1 my $self_string = Elive::Util::string($self);
1701 1 Carp::carp("$class $self_string destroyed without saving or reverting changes to: "
1702 . join(', ', @changed));
1703  
1704 1 ▶ warn YAML::Syck::Dump {self => $self, db_data => $db_data}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug || 0) >= 6 0 1 1
Condition50%1/2
$self->debug || 0
l!l
01
MC/DC0%0/1
$self->debug || 0
$self->debug
Truth table50%1/2
A
A= $self->debug
Aresult
11
00
1705 if ($self->debug||0) >= 6;
1706 }
1707 #
1708 # Destroy this objects data
1709 #
1710 233 $self->_db_data(undef);
1711 }
1712 }
1713  
1714 =head1 ADVANCED
1715  
1716 =head2 Object Management
1717  
1718 L<Elive::DAO> keeps a reference table to all current database objects. This
1719 is primarily used to detect errors, such as destroying or overwriting objects
1720 with unsaved changes.
1721  
1722 You can also reuse objects from this cache by passing C<reuse =E<gt> 1> to the
1723 C<fetch> method.
1724  
1725 my $user = Elive::Entity::User->retrieve(11223344);
1726 #
1727 # returns the same reference, but refetches from the database
1728 #
1729 my $user_copy = Elive::Entity::User->retrieve(11223344);
1730 #
1731 # same as above, however don't refetch if we already have a copy
1732 #
1733 my $user_copy2 = Elive::Entity::User->retrieve(11223344, reuse => 1);
1734  
1735 You can access the in-memory cache using the C<live_entity> and C<live_entities>
1736 methods.
1737  
1738 =head2 Entity Manipulation
1739  
1740 All objects are simply blessed structures that contain data and nothing else.
1741 You may choose to use the accessors, or work directly with the object data.
1742  
1743 The following are all equivalent, and are all ok:
1744  
1745 my $p_list = Elive::Entity::ParticipantList->retrieve(98765);
1746 my $user = Elive::Entity::User->retrieve(11223344);
1747  
1748 $p_list->participants->add($user);
1749 push (@{ $p_list->participants }, $user);
1750 push (@{ $p_list->{participants} }, $user);
1751 push (@{ $p_list->get('participants') }, $user);
1752  
1753 =cut
1754  
1755 =head1 SEE ALSO
1756  
1757 =over 4
1758  
1759 =item L<Mouse> (base class) - Middle-weight L<Moose> like class system
1760  
1761 =back
1762  
1763 =cut
1764  
1765 37 1;
« Elive/Connection.pm ↑ Summary Elive/Entity/ServerParameters.pm »