ORM.pm

filter: Statementstmt 76.6% 532 / 694 Branchbran 56.2% 206 / 366 Conditioncond 49.0% 75 / 153 MC/DCmcdc 26.4% 23 / 87 Subroutinesub 72.6% 69 / 95 Podpod 75.8% 22 / 29 totaltotal 65.1% 927 / 1424 CC 253 SCAR 32.8
CC
253
Cov
67%
find_or_new
90.0
stat
76.1
_derive
51.1
CRAP 26.6
« ORM/MetapropBuilder.pm ↑ Summary ORM/Db/DBI/MySQL.pm »
1 #
2 # DESCRIPTION
3 # PerlORM - Object relational mapper (ORM) for Perl. PerlORM is Perl
4 # library that implements object-relational mapping. Its features are
5 # much similar to those of Java's Hibernate library, but interface is
6 # much different and easier to use.
7 #
8 # AUTHOR
9 # Alexey V. Akimov <akimov_alexey@sourceforge.net>
10 #
11 # COPYRIGHT
12 # Copyright (C) 2005-2006 Alexey V. Akimov
13 #
14 # This library is free software; you can redistribute it and/or
15 # modify it under the terms of the GNU Lesser General Public
16 # License as published by the Free Software Foundation; either
17 # version 2.1 of the License, or (at your option) any later version.
18 #
19 # This library is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 # Lesser General Public License for more details.
23 #
24 # You should have received a copy of the GNU Lesser General Public
25 # License along with this library; if not, write to the Free Software
26 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 #
28  
29 package ORM;
30  
31 5 ▶use 5.006001;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 5 ▶use base 'Class::Data::Inheritable';
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 5 ▶use ORM::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 5 ▶use ORM::Cache;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 5 ▶use ORM::Broken;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 5 ▶use ORM::Date;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 5 ▶use ORM::Datetime;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 5 ▶use ORM::Ta;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 5 ▶use ORM::Const;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 5 ▶use ORM::Ident;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 5 ▶use ORM::Expr;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 5 ▶use ORM::Order;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 5 ▶use ORM::Metaprop;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 5 ▶use ORM::MetapropBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 5 ▶use ORM::ResultSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 5 ▶use ORM::StatResultSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 5 our $VERSION = 0.83;
53  
54 5 ORM->mk_classdata( '_class_hier' );
55 5 ORM->mk_classdata( '_db' );
56 5 ORM->mk_classdata( '_history_class' );
57 5 ORM->mk_classdata( '_default_prefer_lazy_load' );
58 5 ORM->mk_classdata( '_emulate_foreign_keys' );
59 5 ORM->mk_classdata( '_default_cache_size' );
60 5 ORM->mk_classdata( '_current_transaction' );
61  
62 ##
63 ## CONSTRUCTORS
64 ##
65  
66 ## use: $obj = $class->new
67 ## (
68 ## prop => { prop => [string|OBJECT] ... },
69 ## error => ORM::Error,
70 ## temporary => boolean,
71 ## suspended => boolean,
72 ## history => boolean,
73 ## )
74 ##
75 ## 'temporary' - if set to true, then created object will
76 ## not be stored in database.
77 ## You can store that kind of objects later using method
78 ## $object->make_permanent.
79 ##
80 ## 'suspended' - if set to true, then constructor's behavior
81 ## is similar to those with 'temporary'=1 but after creation
82 ## object appended to the internal list of suspended objects.
83 ##
84 ## Later you can flush all suspended objects into database
85 ## at one time by calling $class->flush_suspended. This allows to
86 ## optimize write of objects into database by means of database
87 ## server, e.g. ORM::Db::DBI::MySQL storage engine will use
88 ## multiple-rows form of INSERT statement:
89 ##
90 ## INSERT INTO table (a,b,c) VALUES (1,1,1),(2,2,2),(3,3,3)...
91 ##
92 sub new
93 21 ▶{
Subroutine new: called CC=16 SCAR=31.4
Pod documented
94 21 my $class = shift;
95 21 my %arg = @_;
96 21 my $error = ORM::Error->new;
97 21 my $ta = $class->new_transaction( error=>$error );
98 21 my $self = {};
99 21 ▶ my $history = defined $arg{history} ? $arg{history} : $class->history_is_enabled;
Branch: 1 branch
BranchTrueFalseTotal
defined $arg{'history'} ? : 0 21 21
100  
101 21 ▶ if( $class->_is_intermediate )
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_is_intermediate) 0 21 21
102 {
103 0 $error->add_fatal( "Can't create instance of intermediate class" );
104 }
105  
106 21 ▶ unless( $error->fatal )
Branch: 1 branch
BranchTrueFalseTotal
unless ($error->fatal) 21 0 21
107 {
108 21 my $prop;
109  
110 21 bless $self, $class;
111  
112 21 ▶ $self->{_ORM_tpm} = 1 if( $arg{temporary} );
Branch: 1 branch
BranchTrueFalseTotal
if $arg{"temporary"} 0 21 21
113  
114 # Extract required DB properties from %arg
115 21 for $prop ( $class->_not_mandatory_props )
116 {
117 $self->{_ORM_data}{$prop} = $self->_normalize_prop_to_db_value
118 (
119 name => $prop,
120 error => $error,
121 value =>
122 (
123 exists $arg{prop}{$prop}
124 160 ▶ ? $arg{prop}{$prop}
Branch: 1 branch
BranchTrueFalseTotal
exists $arg{'prop'}{$prop} ? : 160 0 160
125 : $class->_prop_default_value( $prop )
126 ),
127 );
128 }
129 }
130  
131 21 ▶ unless( $error->fatal )
Branch: 1 branch
BranchTrueFalseTotal
unless ($error->fatal) 21 0 21
132 {
133 # Check validity of object data
134 21 $self->_validate_prop( prop=>$self->{_ORM_data}, method=>'new', error=>$error );
135 }
136  
137 21 ▶ if( ! $arg{temporary} && ! $error->fatal )
Branch: 1 branch
BranchTrueFalseTotal
unless ($arg{"temporary"} or $error->fatal) 21 0 21
Condition33%1/3
$arg{"temporary"} or $error->fatal
l!l&&r!l&&!r
0021
MC/DC0%0/2
$arg{"temporary"} or $error->fatal
$arg{"temporary"} $error->fatal
Truth table33%1/3
A or B
A= $arg{"temporary"}B= $error->fatal
ABresult
1X1
011
000
138 {
139 $self->{_ORM_data}{id} = $class->_db->insert_object
140 (
141 id => $arg{repair_id},
142 21 object => $self,
143 error => $error,
144 );
145 21 ▶ if( ! $error->fatal && ! $self->{_ORM_data}{id} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($error->fatal or $self->{"_ORM_data"}{"id"}) 0 21 21
Condition33%1/3
$error->fatal or $self->{"_ORM_data"}{"id"}
l!l&&r!l&&!r
0210
MC/DC0%0/2
$error->fatal or $self->{"_ORM_data"}{"id"}
$error->fatal $self->{"_ORM_data"}{"id"}
Truth table33%1/3
A or B
A= $error->fatalB= $self->{"_ORM_data"}{"id"}
ABresult
1X1
011
000
146 {
147 0 $error->add_fatal( "Failed to detect id of newly created object of class '$class'" );
148 }
149  
150 # Make record in history
151 21 ▶ if( !$error->fatal && $history )
Branch: 1 branch
BranchTrueFalseTotal
if (not $error->fatal and $history) 2 19 21
Condition66%2/3
not $error->fatal and $history
!ll&&!rl&&r
0192
MC/DC50%1/2
not $error->fatal and $history
not $error->fatal $history
Truth table66%2/3
not(A) and B
A= not $error->fatalB= $history
ABresult
0X0
100
111
152 {
153 2 $class->_history_class->new( obj=>$self, created=>1, error=>$error );
154 }
155  
156 # Cache object
157 21 ▶ $self->_cache->add( $self ) unless( $error->fatal );
Branch: 1 branch
BranchTrueFalseTotal
unless $error->fatal 21 0 21
158 }
159  
160 21 $error->upto( $arg{error} );
161 21 ▶ return $error->fatal ? undef : $self;
Branch: 1 branch
BranchTrueFalseTotal
$error->fatal ? : 0 21 21
162 }
163  
164 ## use: $count = $class->count
165 ## (
166 ## filter => ORM::Filter,
167 ## error => ORM::Error,
168 ## )
169 ##
170 sub count
171 2 ▶{
Subroutine count: called CC=1 SCAR=0.0
Pod documented
172 2 my $class = shift;
173 2 $class->_db->count( class=>$class, @_ );
174 }
175  
176 sub exists
177 0 ▶{
Subroutine exists: not called CC=1 SCAR=6.9
Pod undocumented
178 0 my $class = shift;
179 0 my %arg = @_;
180  
181 return $class->count
182 (
183 filter => ( $class->M->id == $arg{id} ),
184 error => $arg{error},
185 0 );
186 }
187  
188 ## use: @obj = $class->find
189 ## (
190 ## filter => ORM::Filter,
191 ## order => ORM::Order,
192 ## lazy_load => boolean,
193 ## page => integer,
194 ## pagesize => integer,
195 ## error => ORM::Error,
196 ## return_ref => boolean,
197 ## return_res => boolean,
198 ## )
199 ##
200 ## If called in scalar context returns first object from result set.
201 ##
202 ## If called in array context returns array of found objects.
203 ##
204 ## If 'return_ref' is true then return value is reference to the array
205 ## of found objects with no respect to context.
206 ##
207 ## If 'return_res' is true then return value is object of class ORM::ResultSet,
208 ## found objects can be accesed one by one via $result->next. It is useful to
209 ## retrieve large amount of objects. Pays no respect to context and 'return_ref'.
210 ##
211 ## If 'pagesize' and 'page' is specified then result set is divided to pages
212 ## with 'pagesize' object per page and only page numbered 'page' will be returned.
213 ## First page number is 1.
214 ##
215 ## If 'lazy_load' specified then only data from tables corresponding to base class
216 ## $class will be loaded initially.
217 ##
218 sub find
219 2 ▶{
Subroutine find: called CC=20 SCAR=35.5
Pod documented
220 2 my $class = shift;
221 2 my %arg = @_;
222 2 my $error = ORM::Error->new;
223 2 ▶ my $page = defined $arg{page} && int( $arg{page} );
Condition33%1/3
defined $arg{'page'} && int $arg{'page'}
!ll&&!rl&&r
200
MC/DC0%0/2
defined $arg{'page'} && int $arg{'page'}
defined $arg{'page'} int $arg{'page'}
Truth table33%1/3
A && B
A= defined $arg{'page'}B= int $arg{'page'}
ABresult
0X0
100
111
224 2 ▶ my $pagesize = defined $arg{pagesize} && int( $arg{pagesize} );
Condition33%1/3
defined $arg{'pagesize'} && int $arg{'pagesize'}
!ll&&!rl&&r
200
MC/DC0%0/2
defined $arg{'pagesize'} && int $arg{'pagesize'}
defined $arg{'pagesize'} int $arg{'pagesize'}
Truth table33%1/3
A && B
A= defined $arg{'pagesize'}B= int $arg{'pagesize'}
ABresult
0X0
100
111
225 2 ▶ my $lazy_load = defined $arg{lazy_load} ? $arg{lazy_load} : $class->prefer_lazy_load;
Branch: 1 branch
BranchTrueFalseTotal
defined $arg{'lazy_load'} ? : 2 0 2
226 0 ▶ my $order = ( ref $arg{order} eq 'ARRAY' ) ? ORM::Order->new( @{$arg{order}} ) : $arg{order};
Branch: 1 branch
BranchTrueFalseTotal
ref $arg{'order'} eq 'ARRAY' ? : 0 2 2
227 2 my @obj;
228 my $res;
229  
230 2 ▶ if( !wantarray && !$arg{return_ref} && !$arg{return_res} )
Branch: 1 branch
BranchTrueFalseTotal
unless (wantarray or $arg{"return_ref"} or $arg{"return_res"}) 2 0 2
Condition33%2/6
wantarray or $arg{"return_ref"} or $arg{"return_res"}
l!l&&r!l&&!r
002
wantarray or $arg{"return_ref"}
l!l&&r!l&&!r
002
MC/DC0%0/3
wantarray or $arg{"return_ref"} or $arg{"return_res"}
wantarray $arg{"return_ref"} $arg{"return_res"}
Truth table25%1/4
A or B or C
A= wantarrayB= $arg{"return_ref"}C= $arg{"return_res"}
ABCresult
1XX1
01X1
0011
0000
231 {
232 2 $page = ($page-1)*$pagesize+1;
233 2 $pagesize = 1;
234 }
235  
236 2 ▶ if( $class->_is_sealed || $lazy_load || $arg{return_res} )
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_is_sealed or $lazy_load or $arg{'return_res'}) { } 1 1 2
Condition66%4/6
$class->_is_sealed or $lazy_load or $arg{"return_res"}
l!l&&r!l&&!r
101
$class->_is_sealed or $lazy_load
l!l&&r!l&&!r
011
MC/DC33%1/3
$class->_is_sealed or $lazy_load or $arg{"return_res"}
$class->_is_sealed $lazy_load $arg{"return_res"}
Truth table50%2/4
A or B or C
A= $class->_is_sealedB= $lazy_loadC= $arg{"return_res"}
ABCresult
1XX1
01X1
0011
0000
237 {
238 $res = ORM::ResultSet->new
239 (
240 class => $class,
241 result => $class->_db->select_base
242 (
243 class => $class,
244 filter => $arg{filter},
245 1 order => $order,
246 page => $page,
247 pagesize => $pagesize,
248 error => $error,
249 ),
250 );
251 1 ▶ unless( $arg{return_res} || $error->fatal )
Branch: 1 branch
BranchTrueFalseTotal
unless ($arg{"return_res"} or $error->fatal) 1 0 1
Condition33%1/3
$arg{"return_res"} or $error->fatal
l!l&&r!l&&!r
001
MC/DC0%0/2
$arg{"return_res"} or $error->fatal
$arg{"return_res"} $error->fatal
Truth table33%1/3
A or B
A= $arg{"return_res"}B= $error->fatal
ABresult
1X1
011
000
252 {
253 1 my $obj;
254 1 ▶ while( $obj = $res->next ) { push @obj, $obj; }
Branch: 1 branch
BranchTrueFalseTotal
if ($obj = $res->next) 1 1 2
255 }
256 }
257 else
258 {
259 $res = $class->_db->select_full
260 (
261 class => $class,
262 filter => $arg{filter},
263 1 order => $order,
264 page => $page,
265 pagesize => $pagesize,
266 error => $error,
267 );
268 1 ▶ unless( $error->fatal )
Branch: 1 branch
BranchTrueFalseTotal
unless ($error->fatal) 1 0 1
269 {
270 1 my $data;
271 my $obj;
272 1 ▶ while( $data = $res->next_row )
Branch: 1 branch
BranchTrueFalseTotal
if ($data = $res->next_row) 1 1 2
273 {
274 1 ▶ if( ref $data eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq 'HASH') { } 0 1 1
275 {
276 0 $obj = bless { _ORM_data=>$data }, $data->{class};
277 0 delete $obj->{_ORM_data}{class};
278 0 $class->_cache->add( $obj );
279 }
280 else
281 {
282 1 $obj = $data;
283 }
284 1 push @obj, $obj;
285 }
286 }
287 }
288  
289 2 $error->upto( $arg{error} );
290
291 return
292 $arg{return_res}
293 ? $res
294 2 ▶ : ( $arg{return_ref} ? \@obj : ( wantarray ? ( @obj ) : $obj[0] ) );
Branch: 3 branches
BranchTrueFalseTotal
$arg{'return_res'} ? : 0 2 2
$arg{'return_ref'} ? : 0 2 2
wantarray ? : 0 2 2
295 }
296  
297 ## use: $obj = $class->find_id
298 ## (
299 ## id => integer,
300 ## lazy_load => boolean,
301 ## error => ORM::Error,
302 ## );
303 ##
304 sub find_id
305 3 ▶{
Subroutine find_id: called CC=8 SCAR=21.4
Pod documented
306 3 my $class = shift;
307 3 my %arg = @_;
308 3 my $self;
309  
310 3 $self = $class->_cache->get( $arg{id} );
311  
312 3 ▶ unless( $self )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self) 3 0 3
313 {
314 3 $self = { _ORM_data=>{ id=>$arg{id} } };
315 3 for my $table ( $class->_db_tables )
316 {
317 4 ▶ if( scalar $class->_db_table_fields( $table ) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar $class->_db_table_fields($table)) 4 0 4
318 {
319 4 $self->{_ORM_missing_tables}{$table} = 1;
320 }
321 }
322 3 bless $self, $class;
323  
324 3 ▶ unless( $arg{lazy_load} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($arg{"lazy_load"}) 1 2 3
325 {
326 1 my $error = ORM::Error->new;
327 1 $self->finish_loading( error=>$error );
328 1 ▶ $self = undef if( ref $self eq 'ORM::Broken' || $error->fatal );
Branch: 1 branch
BranchTrueFalseTotal
if ref $self eq "ORM::Broken" or $error->fatal 0 1 1
Condition33%1/3
ref $self eq "ORM::Broken" or $error->fatal
l!l&&r!l&&!r
001
MC/DC0%0/2
ref $self eq "ORM::Broken" or $error->fatal
ref $self eq "ORM::Broken" $error->fatal
Truth table33%1/3
A or B
A= ref $self eq "ORM::Broken"B= $error->fatal
ABresult
1X1
011
000
329 1 $error->upto( $arg{error} );
330 }
331  
332 3 ▶ $self && $class->_cache->add( $self );
Branch: 1 branch
BranchTrueFalseTotal
$self and $class->_cache->add($self) 3 0 3
333 }
334  
335 3 return $self;
336 }
337  
338 ## use: $obj = $class->find_or_new
339 ## (
340 ## prop => { prop_name => [string|OBJECT] ... },
341 ## lazy_load => boolean,
342 ## history => boolean,
343 ## error => ORM::Error,
344 ## )
345 ##
346 sub find_or_new
347 0 ▶{
Subroutine find_or_new: not called CC=9 SCAR=45.0
Pod documented
348 0 my $class = shift;
349 0 my %arg = @_;
350 0 my $error = ORM::Error->new;
351 0 my $filter = ORM::Expr->_and;
352 0 my @obj;
353  
354 0 for my $prop ( keys %{$arg{prop}} )
355 {
356 0 ▶ if( $class->_has_prop( $prop ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_has_prop($prop)) { } 0 0 0
357 {
358 0 $filter->add_expr( $class->M->_prop( $prop ) == $arg{prop}{$prop} );
359 }
360 else
361 {
362 0 $error->add_fatal( "Non-existing prop '$prop' specified" );
363 0 last;
364 }
365 }
366  
367 0 ▶ unless( $error->fatal )
Branch: 1 branch
BranchTrueFalseTotal
unless ($error->fatal) 0 0 0
368 {
369 @obj = $class->find
370 (
371 filter => $filter,
372 error => $error,
373 pagesize => 2,
374 lazy_load => $arg{lazy_load},
375 0 );
376 }
377 0 ▶ unless( $error->fatal )
Branch: 1 branch
BranchTrueFalseTotal
unless ($error->fatal) 0 0 0
378 {
379 0 ▶ if( @obj > 1 )
Branch: 1 branch
BranchTrueFalseTotal
if (@obj > 1) 0 0 0
380 {
381 0 $error->add_fatal( "More than 1 object were found" );
382 }
383 }
384 0 ▶ unless( $error->fatal )
Branch: 1 branch
BranchTrueFalseTotal
unless ($error->fatal) 0 0 0
385 {
386 0 ▶ if( ! @obj )
Branch: 1 branch
BranchTrueFalseTotal
unless (@obj) 0 0 0
387 {
388 0 $obj[0] = $class->new( prop=>$arg{prop}, history=>$arg{history}, error=>$error );
389 }
390 }
391  
392 0 $error->upto( $arg{error} );
393 0 ▶ return $error->fatal ? undef : $obj[0];
Branch: 1 branch
BranchTrueFalseTotal
$error->fatal ? : 0 0 0
394 }
395  
396 ##
397 ## OBJECT METHODS
398 ##
399  
400 ## use: $ta = $class->new_transaction( error=>ORM::Error );
401 ##
402 ## Begins transaction.
403 ## Transaction commits when object $ta is destroyed.
404 ##
405 sub new_transaction
406 57 ▶{
Subroutine new_transaction: called CC=1 SCAR=0.0
Pod documented
407 57 my $class = shift;
408 57 my $iclass = $class->initial_class;
409 57 my %arg = @_;
410  
411 57 ORM::Ta->new( class=>$iclass, error=>$arg{error} );
412 }
413  
414 ## use: $self->update
415 ## (
416 ## prop => HASH,
417 ## old_prop => HASH,
418 ## history => boolean,
419 ## error => ORM::Error,
420 ## )
421 ##
422 sub update
423 6 ▶{
Subroutine update: called CC=31 SCAR=35.1
Pod documented
424 6 my $self = shift;
425 6 my $class = ref $self;
426 6 my %arg = @_;
427 6 my $error = ORM::Error->new;
428 6 my $ta = $class->new_transaction( error=>$error );
429 6 ▶ my $history = defined $arg{history} ? $arg{history} : $class->history_is_enabled;
Branch: 1 branch
BranchTrueFalseTotal
defined $arg{'history'} ? : 0 6 6
430 6 my %changed_prop;
431 my %expr_prop;
432 6 my %old_prop;
433  
434 6 $self->finish_loading( error=>$error );
435  
436 # Check if current properties match to those in 'old_prop' argument
437 6 ▶ unless( $error->fatal )
Branch: 1 branch
BranchTrueFalseTotal
unless ($error->fatal) 6 0 6
438 {
439 6 %old_prop = %{$self->{_ORM_data}};
440 6 ▶ if( $arg{old_prop} )
Branch: 1 branch
BranchTrueFalseTotal
if ($arg{"old_prop"}) 2 4 6
441 {
442 2 for my $prop ( keys %{$arg{old_prop}} )
443 {
444 my $old_normalized = $self->_normalize_prop_to_db_value
445 (
446 name => $prop,
447 5 value => $arg{old_prop}{$prop},
448 error => $error,
449 );
450 5 ▶ last if( $error->fatal );
Branch: 1 branch
BranchTrueFalseTotal
if $error->fatal 0 5 5
451 5 ▶ if( $self->_values_are_not_equal( $self->{_ORM_data}{$prop}, $old_normalized ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_values_are_not_equal($self->{"_ORM_data"}{$prop}, $old_normalized)) 1 4 5
452 {
453 1 $error->add_fatal
454 (
455 'Current properties of object #'.$self->id
456 . ' of class "'.$class.'" do not match '
457 . 'properties assumed by user',
458 );
459 1 last;
460 }
461 }
462 }
463 }
464  
465 # Detect data changes
466 6 ▶ unless( $error->fatal )
Branch: 1 branch
BranchTrueFalseTotal
unless ($error->fatal) 5 1 6
467 {
468 5 for my $prop ( $class->_not_mandatory_props )
469 {
470 21 ▶ if( exists $arg{prop}{$prop} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $arg{"prop"}{$prop}) 7 14 21
471 {
472 7 ▶ if( UNIVERSAL::isa( $arg{prop}{$prop}, 'ORM::Expr' ) )
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($arg{'prop'}{$prop}, 'ORM::Expr')) { } 1 6 7
473 {
474 1 $expr_prop{$prop} = $arg{prop}{$prop};
475 }
476 else
477 {
478 my $new_normalized = $self->_normalize_prop_to_db_value
479 (
480 name => $prop,
481 6 value => $arg{prop}{$prop},
482 error => $error,
483 );
484 6 ▶ last if( $error->fatal );
Branch: 1 branch
BranchTrueFalseTotal
if $error->fatal 0 6 6
485 6 ▶ if( $self->_values_are_not_equal( $self->{_ORM_data}{$prop}, $new_normalized ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_values_are_not_equal($self->{"_ORM_data"}{$prop}, $new_normalized)) 6 0 6
486 {
487 6 $changed_prop{$prop} = 1;
488 6 $self->{_ORM_data}{$prop} = $new_normalized;
489 6 delete $self->{_ORM_cache}{$prop};
490 }
491 }
492 }
493 }
494 }
495 # User validations
496 6 ▶ if( %changed_prop && !$error->fatal )
Branch: 1 branch
BranchTrueFalseTotal
if (%changed_prop and not $error->fatal) 4 2 6
Condition66%2/3
%changed_prop and not $error->fatal
!ll&&!rl&&r
204
MC/DC50%1/2
%changed_prop and not $error->fatal
%changed_prop not $error->fatal
Truth table66%2/3
A and not(B)
A= %changed_propB= not $error->fatal
ABresult
0X0
100
111
497 {
498 4 $self->_validate_prop( prop=>\%changed_prop, old=>\%old_prop, method=>'update', error=>$error );
499 }
500 # Detect data changes again to consider changes in _validate_prop
501 6 ▶ unless( $error->fatal )
Branch: 1 branch
BranchTrueFalseTotal
unless ($error->fatal) 5 1 6
502 {
503 5 %changed_prop = ();
504 5 for my $prop ( $class->_not_mandatory_props )
505 {
506 21 ▶ if( $self->_values_are_not_equal( $old_prop{$prop}, $self->{_ORM_data}{$prop} ) )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_values_are_not_equal($old_prop{$prop}, $self->{'_ORM_data'}{$prop})) { } 6 15 21
elsif (exists $expr_prop{$prop}) { } 1 14 15
507 {
508 6 $changed_prop{$prop} = $self->{_ORM_data}{$prop};
509 6 ▶ delete $expr_prop{$prop} if( exists $expr_prop{$prop} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $expr_prop{$prop} 0 6 6
510 }
511 elsif( exists $expr_prop{$prop} )
512 {
513 1 $changed_prop{$prop} = $expr_prop{$prop};
514 }
515 }
516 }
517  
518 6 ▶ if( !$self->is_temporary && !$error->fatal && scalar( %changed_prop ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->is_temporary || $error->fatal and scalar %changed_prop) 5 1 6
Condition80%4/5
not $self->is_temporary || $error->fatal and scalar %changed_prop
!ll
15
$self->is_temporary || $error->fatal
l!l&&r!l&&!r
015
MC/DC50%1/2
not $self->is_temporary || $error->fatal and scalar %changed_prop
$self->is_temporary $error->fatal
Truth table66%2/3
not(A || B)
A= $self->is_temporaryB= $error->fatal
ABresult
1X0
010
001
519 {
520 5 for my $prop ( keys %expr_prop )
521 {
522 1 $self->{_ORM_missing_tables}{ $class->_prop2table($prop) }{$prop} = 1;
523 }
524  
525 # Update object
526 5 ▶ unless( $error->fatal )
Branch: 1 branch
BranchTrueFalseTotal
unless ($error->fatal) 5 0 5
527 {
528 5 $class->_db->update_object
529 (
530 object => $self,
531 values => \%changed_prop,
532 old_values => \%old_prop,
533 error => $error,
534 );
535 }
536  
537 # Save changes to history
538 5 ▶ if( $history && !$error->fatal )
Branch: 1 branch
BranchTrueFalseTotal
if ($history and not $error->fatal) 5 0 5
Condition33%1/3
$history and not $error->fatal
!ll&&!rl&&r
005
MC/DC0%0/2
$history and not $error->fatal
$history not $error->fatal
Truth table33%1/3
A and not(B)
A= $historyB= not $error->fatal
ABresult
0X0
100
111
539 {
540 5 $self->finish_loading( error=>$error );
541 }
542 5 ▶ if( $history && !$error->fatal )
Branch: 1 branch
BranchTrueFalseTotal
if ($history and not $error->fatal) 5 0 5
Condition33%1/3
$history and not $error->fatal
!ll&&!rl&&r
005
MC/DC0%0/2
$history and not $error->fatal
$history not $error->fatal
Truth table33%1/3
A and not(B)
A= $historyB= not $error->fatal
ABresult
0X0
100
111
543 {
544 5 my %history;
545 5 for my $prop_name ( keys %changed_prop )
546 {
547 $history{$prop_name} =
548 [
549 $old_prop{$prop_name},
550 7 $self->{_ORM_data}{$prop_name}
551 ];
552 }
553 $class->_history_class->new
554 (
555 5 error => $error,
556 obj => $self,
557 changed => \%history,
558 );
559 }
560 }
561  
562 6 ▶ if( $error->fatal )
Branch: 1 branch
BranchTrueFalseTotal
if ($error->fatal) 1 5 6
563 {
564 # Roll back update action if error occured
565 1 $self->{_ORM_data} = \%old_prop;
566 }
567  
568 6 $error->upto( $arg{error} );
569 6 return undef;
570 }
571  
572 ## use: $self->delete( error=>ORM::Error, history=>boolean )
573 ##
574 sub delete
575 2 ▶{
Subroutine delete: called CC=7 SCAR=20.1
Pod documented
576 2 my $self = shift;
577 2 my $class = ref $self;
578 2 my %arg = @_;
579 2 my $error = ORM::Error->new;
580 2 my $ta = $class->new_transaction( error=>$error );
581 2 ▶ my $history = defined $arg{history} ? $arg{history} : $class->history_is_enabled;
Branch: 1 branch
BranchTrueFalseTotal
defined $arg{'history'} ? : 0 2 2
582  
583 2 ▶ unless( $self->is_temporary )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_temporary) 2 0 2
584 {
585 2 ▶ unless( $error->fatal )
Branch: 1 branch
BranchTrueFalseTotal
unless ($error->fatal) 2 0 2
586 {
587 # Make record in history
588 2 ▶ if( $history )
Branch: 1 branch
BranchTrueFalseTotal
if ($history) 2 0 2
589 {
590 2 $class->_history_class->new( obj=>$self, deleted=>1, error=>$error );
591 }
592 }
593 2 ▶ unless( $error->fatal )
Branch: 1 branch
BranchTrueFalseTotal
unless ($error->fatal) 2 0 2
594 {
595 2 $class->_db->delete_object
596 (
597 object => $self,
598 error => $error,
599 emulate_foreign_keys => $class->_emulate_foreign_keys,
600 );
601 }
602 2 ▶ unless( $error->fatal )
Branch: 1 branch
BranchTrueFalseTotal
unless ($error->fatal) 2 0 2
603 {
604 2 $self->_rebless_to_broken( deleted=>1 );
605 }
606 }
607  
608 2 $error->upto( $arg{error} );
609 2 return undef;
610 }
611  
612 ## use: $object->refresh( error=>ORM::Error );
613 ##
614 sub refresh
615 0 ▶{
Subroutine refresh: not called CC=3 SCAR=24.8
Pod documented
616 0 my $self = shift;
617 0 my $class = ref $self;
618 0 my %arg = @_;
619  
620 0 $self->{_ORM_data} = { id=>$self->id };
621 0 delete $self->{_ORM_cache};
622 0 for my $table ( $class->_db_tables )
623 {
624 0 ▶ if( scalar $class->_db_table_fields( $table ) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar $class->_db_table_fields($table)) 0 0 0
625 {
626 0 $self->{_ORM_missing_tables}{$table} = 1;
627 }
628 }
629  
630 0 $self->finish_loading( error=>$arg{error} );
631 }
632  
633 ## use: $object->finish_loading
634 ## or
635 ## use: $object->finish_loading( error=>ORM::Error );
636 ##
637 ## First form will rebless object to 'ORM::Broken' in case of error.
638 ##
639 sub finish_loading
640 16 ▶{
Subroutine finish_loading: called CC=19 SCAR=33.2
Pod documented
641 16 my $self = shift;
642 16 my $class = ref $self;
643 16 my %arg = @_;
644 16 my $new_class;
645 16 my $prop = $arg{prop};
646 16 ▶ my $prop_table = $prop && $class->_prop2table( $prop );
Condition66%2/3
$prop && $class->_prop2table($prop)
!ll&&!rl&&r
1303
MC/DC50%1/2
$prop && $class->_prop2table($prop)
$prop $class->_prop2table($prop)
Truth table66%2/3
A && B
A= $propB= $class->_prop2table($prop)
ABresult
0X0
100
111
647  
648 16 ▶ if
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_ORM_missing_tables"} and !defined($prop) || defined $prop_table && $self->{"_ORM_missing_tables"}{$prop_table} && (!(ref $self->{"_ORM_missing_tables"}{$prop_table} eq "HASH") || $self->{"_ORM_missing_tables"}{$prop_table}{$prop})) 7 9 16
Condition46%7/15
exists $self->{"_ORM_missing_tables"} and !defined($prop) || defined $prop_table && $self->{"_ORM_missing_tables"}{$prop_table} && (!(ref $self->{"_ORM_missing_tables"}{$prop_table} eq "HASH") || $self->{"_ORM_missing_tables"}{$prop_table}{$prop})
!ll&&!rl&&r
907
not defined $prop or defined $prop_table and $self->{"_ORM_missing_tables"}{$prop_table} and !(ref $self->{"_ORM_missing_tables"}{$prop_table} eq "HASH") || $self->{"_ORM_missing_tables"}{$prop_table}{$prop}
l!l&&r!l&&!r
430
defined $prop_table and $self->{"_ORM_missing_tables"}{$prop_table} and !(ref $self->{"_ORM_missing_tables"}{$prop_table} eq "HASH") || $self->{"_ORM_missing_tables"}{$prop_table}{$prop}
!ll&&!rl&&r
003
defined $prop_table and $self->{"_ORM_missing_tables"}{$prop_table}
!ll&&!rl&&r
003
not ref $self->{"_ORM_missing_tables"}{$prop_table} eq "HASH" or $self->{"_ORM_missing_tables"}{$prop_table}{$prop}
l!l&&r!l&&!r
300
MC/DC16%1/6
exists $self->{"_ORM_missing_tables"} and !defined($prop) || defined $prop_table && $self->{"_ORM_missing_tables"}{$prop_table} && (!(ref $self->{"_ORM_missing_tables"}{$prop_table} eq "HASH") || $self->{"_ORM_missing_tables"}{$prop_table}{$prop})
exists $self->{"_ORM_missing_tables"} not defined $prop defined $prop_table $self->{"_ORM_missing_tables"}{$prop_table} not ref $self->{"_ORM_missing_tables"}{$prop_table} eq "HASH" $self->{"_ORM_missing_tables"}{$prop_table}{$prop}
Truth table42%3/7
A and not(B) or C and D and not(E) or F
A= exists $self->{"_ORM_missing_tables"}B= not defined $propC= defined $prop_tableD= $self->{"_ORM_missing_tables"}{$prop_table}E= not ref $self->{"_ORM_missing_tables"}{$prop_table} eq "HASH"F= $self->{"_ORM_missing_tables"}{$prop_table}{$prop}
ABCDEFresult
0XXXXX0
100XXX0
1010XX0
1011000
11XXXX1
10111X1
1011011
649 (
650 exists $self->{_ORM_missing_tables}
651 &&
652 (
653 ! defined $prop
654 ||
655 (
656 defined $prop_table
657 && $self->{_ORM_missing_tables}{$prop_table}
658 &&
659 (
660 !( ref $self->{_ORM_missing_tables}{$prop_table} eq 'HASH' )
661 || $self->{_ORM_missing_tables}{$prop_table}{$prop}
662 )
663 )
664 )
665 )
666 {
667 7 my $error = ORM::Error->new;
668 my $data = $class->_db->select_tables
669 (
670 id => $self->qc( $self->id ),
671 tables => $self->{_ORM_missing_tables},
672 7 error => $error,
673 );
674  
675 7 ▶ $data = $data && $data->next_row;
Condition33%1/3
$data && $data->next_row
!ll&&!rl&&r
007
MC/DC0%0/2
$data && $data->next_row
$data $data->next_row
Truth table33%1/3
A && B
A= $dataB= $data->next_row
ABresult
0X0
100
111
676  
677 7 ▶ if( $error->fatal )
Branch: 2 branches
BranchTrueFalseTotal
if ($error->fatal) { } 0 7 7
elsif (not $data) { } 0 7 7
678 {
679 0 ▶ if( $arg{error} )
Branch: 1 branch
BranchTrueFalseTotal
if ($arg{'error'}) { } 0 0 0
680 {
681 0 $arg{error}->add( error=>$error );
682 }
683 else
684 {
685 0 $self->_rebless_to_broken( error=>$error );
686 }
687 }
688 elsif( !$data )
689 {
690 0 $self->_rebless_to_broken( deleted=>1 );
691 }
692 else
693 {
694 7 delete $self->{_ORM_missing_tables};
695  
696 # Fetch loaded properties
697 7 ▶ if( exists $data->{class} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"class"}) 3 4 7
698 {
699 3 $new_class = $data->{class};
700 3 delete $data->{class};
701 }
702 7 for my $prop ( keys %$data )
703 {
704 24 $self->{_ORM_data}{$prop} = $data->{$prop};
705 }
706 }
707 }
708  
709 # If actual class of object is different than blessed class,
710 # then rebless object and upload residual tables if needed
711 16 ▶ if( $new_class && $new_class ne $class )
Branch: 1 branch
BranchTrueFalseTotal
if ($new_class and $new_class ne $class) 2 14 16
Condition100%3/3
$new_class and $new_class ne $class
!ll&&!rl&&r
1312
MC/DC100%2/2
$new_class and $new_class ne $class
$new_class $new_class ne $class
Truth table100%3/3
A and B
A= $new_classB= $new_class ne $class
ABresult
0X0
100
111
712 {
713 2 $class->_load_ORM_class( $new_class );
714  
715 2 ▶ if( UNIVERSAL::isa( $new_class, $class ) )
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($new_class, $class)) { } 2 0 2
716 {
717 2 bless $self, $new_class;
718  
719 2 my $base_class_tables = $class->_db_tables_count;
720 2 my $class_tables = $new_class->_db_tables_count;
721  
722 2 ▶ for( my $i=$base_class_tables; $i<$class_tables; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $class_tables) 2 2 4
723 {
724 2 $self->{_ORM_missing_tables}{$new_class->_db_table($i)} = 1;
725 }
726  
727 2 ▶ $self->finish_loading( error=>$arg{error} ) unless( defined $prop );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prop 1 1 2
728 }
729 else
730 {
731 0 $self->_rebless_to_broken( deleted=>1 );
732 }
733 }
734 }
735  
736 ##
737 ## PROPERTIES
738 ##
739  
740 89 ▶sub id { $_[0]->{_ORM_data}{id}; }
Subroutine id: called CC=1 SCAR=0.0
Pod documented
741 0 ▶sub class { ref $_[0] || $_[0]; }
Subroutine class: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] or $_[0] 0 0 0
742 8 ▶sub is_temporary { $_[0]->{_ORM_tpm}; }
Subroutine is_temporary: called CC=1 SCAR=0.0
Pod documented
743  
744 0 ▶sub __ORM_db_value { $_[0]->{_ORM_data}{id}; }
Subroutine __ORM_db_value: not called CC=1 SCAR=6.9
745 sub __ORM_new_db_value
746 0 ▶{
Subroutine __ORM_new_db_value: not called CC=2 SCAR=17.9
747 0 my $class = shift;
748 0 my %arg = @_;
749 0 my $self;
750  
751 0 ▶ if( defined $arg{value} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg{"value"}) 0 0 0
752 {
753 0 $self = $class->find_id( id=>$arg{value}, error=>$arg{error}, lazy_load=>$arg{lazy_load} );
754 }
755  
756 0 return $self;
757 }
758  
759 sub _class_info
760 1363 ▶{
Subroutine _class_info: called CC=2 SCAR=7.1
761 1363 ▶ my $class = ref $_[0] || $_[0];
Condition66%2/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
2713360
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table66%2/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
762 1363 $class->_class_hier->{$class};
763 }
764  
765 0 ▶sub base_class { $_[0]->_class_info->{BASE_CLASS}; }
Subroutine base_class: not called CC=1 SCAR=6.9
Pod documented
766 40 ▶sub primary_class { $_[0]->_class_info->{PRIMARY_CLASS}; }
Subroutine primary_class: called CC=1 SCAR=0.0
Pod documented
767 57 ▶sub initial_class { $_[0]->_is_initial ? $_[0] : $_[0]->_class_info->{INITIAL_CLASS}; }
Subroutine initial_class: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->_is_initial ? : 0 57 57
768  
769 sub M
770 21 ▶{
Subroutine M: called CC=3 SCAR=11.0
Pod documented
771 21 my $self = shift;
772 21 ▶ my $class = ref $self || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
1470
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
773 21 my $prop = shift;
774  
775 21 ▶ if( $prop )
Branch: 1 branch
BranchTrueFalseTotal
if ($prop) { } 7 14 21
776 {
777 7 ORM::Metaprop->_new( prop_class=>$class, prop=>$prop );
778 }
779 else
780 {
781 14 ORM::Metaprop->_new_flat( class=>$class );
782 }
783 }
784  
785 ## use: $value = -$object->P( error=>$error )->prop1->prop2->prop3;
786 ##
787 sub P
788 0 ▶{
Subroutine P: not called CC=1 SCAR=6.9
Pod documented
789 0 my $self = shift;
790 0 my %arg = @_;
791  
792 ORM::MetapropBuilder->new
793 (
794 prop_class => (ref $self),
795 need_value => $self,
796 error => $arg{error},
797 0 );
798 }
799  
800 18 ▶sub metaprop_class { $_[0]->_class_info->{METAPROP_CLASS}; }
Subroutine metaprop_class: called CC=1 SCAR=0.0
Pod undocumented
801  
802 0 ▶sub ql { $_[0]->_db->ql( $_[1] ); }
Subroutine ql: not called CC=1 SCAR=6.9
Pod undocumented
803 25 ▶sub qc { $_[0]->_db->qc( $_[1] ); }
Subroutine qc: called CC=1 SCAR=0.0
Pod undocumented
804 1 ▶sub qi { $_[0]->_db->qi( $_[1] ); }
Subroutine qi: called CC=1 SCAR=0.0
Pod undocumented
805 94 ▶sub qt { $_[0]->_db->qt( $_[1] ); }
Subroutine qt: called CC=1 SCAR=0.0
Pod undocumented
806 0 ▶sub qf { $_[0]->_db->qf( $_[1] ); }
Subroutine qf: not called CC=1 SCAR=6.9
Pod undocumented
807  
808 ## use: $state = $class->history_is_enabled;
809 ## use: $state = $class->history_is_enabled( $new_state );
810 ##
811 ## If $new_state is specified then value of flag
812 ## 'history_is_enabled' will be replaced to $new_state.
813 ## $new_state can be undef, in that case global default value
814 ## will be used instead.
815 ##
816 sub history_is_enabled
817 29 ▶{
Subroutine history_is_enabled: called CC=6 SCAR=28.1
Pod documented
818 29 my $class = shift;
819  
820 29 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 29 29
821 {
822 0 ▶ if( defined $_[0] )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) { } 0 0 0
823 {
824 0 ▶ if( $class->_class_info )
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_class_info) { } 0 0 0
825 {
826 0 $class->_class_info->{HISTORY_IS_ENABLED} = $_[0];
827 }
828 else
829 {
830 0 croak "Can't change global history settings";
831 }
832 }
833 else
834 {
835 0 ▶ delete $class->_class_info->{HISTORY_IS_ENABLED} if( $class->_class_info );
Branch: 1 branch
BranchTrueFalseTotal
if $class->_class_info 0 0 0
836 }
837 }
838  
839 exists $class->_class_info->{HISTORY_IS_ENABLED}
840 ? $class->_class_info->{HISTORY_IS_ENABLED}
841 29 ▶ : $class->_history_class;
Branch: 1 branch
BranchTrueFalseTotal
exists $class->_class_info->{'HISTORY_IS_ENABLED'} ? : 19 10 29
842 }
843  
844 ## use: $state = $class->prefer_lazy_load;
845 ## use: $state = $class->prefer_lazy_load( $new_state );
846 ##
847 ## If $new_state is specified then value of flag
848 ## 'prefer_lazy_load' will be replaced to $new_state.
849 ## $new_state can be undef, in that case global default value
850 ## will be used instead.
851 ##
852 sub prefer_lazy_load
853 0 ▶{
Subroutine prefer_lazy_load: not called CC=4 SCAR=30.0
Pod documented
854 0 my $class = shift;
855  
856 0 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
857 {
858 0 ▶ if( defined $_[0] )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) { } 0 0 0
859 {
860 0 $class->_class_info->{PREFER_LAZY_LOAD} = $_[0];
861 }
862 else
863 {
864 0 delete $class->_class_info->{PREFER_LAZY_LOAD};
865 }
866 }
867  
868 exists $class->_class_info->{PREFER_LAZY_LOAD}
869 ? $class->_class_info->{PREFER_LAZY_LOAD}
870 0 ▶ : $class->_default_prefer_lazy_load;
Branch: 1 branch
BranchTrueFalseTotal
exists $class->_class_info->{'PREFER_LAZY_LOAD'} ? : 0 0 0
871 }
872  
873 sub _plain_prop
874 196 ▶{
Subroutine _plain_prop: called CC=2 SCAR=7.1
875 196 my $class = shift;
876 196 my $prop = shift;
877  
878 exists( $class->_class_info->{PROP}{$prop} )
879 196 ▶ && ( ! $class->_class_info->{PROP}{$prop} );
Branch: 1 branch
BranchTrueFalseTotal
exists $class->_class_info->{"PROP"}{$prop} and not $class->_class_info->{"PROP"}{$prop} 196 0 196
880 }
881 sub _prop_is_ref
882 71 ▶{
Subroutine _prop_is_ref: called CC=3 SCAR=11.0
883 71 my $class = shift;
884 71 my $prop = shift;
885 71 my $pclass = $class->_prop_class( $prop );
886  
887 71 ▶ $pclass && $class->_class_hier->{$pclass} && $pclass;
Branch: 1 branch
BranchTrueFalseTotal
$pclass and $class->_class_hier->{$pclass} and $pclass 23 48 71
Condition100%3/3
$pclass and $class->_class_hier->{$pclass}
!ll&&!rl&&r
291923
MC/DC100%2/2
$pclass and $class->_class_hier->{$pclass}
$pclass $class->_class_hier->{$pclass}
Truth table100%3/3
A and B
A= $pclassB= $class->_class_hier->{$pclass}
ABresult
0X0
100
111
888 }
889  
890 30 ▶sub _is_sealed { $_[0]->_class_info->{SEALED}; }
Subroutine _is_sealed: called CC=1 SCAR=0.0
891 133 ▶sub _prop_class { $_[0]->_class_info->{PROP}{$_[1]}; }
Subroutine _prop_class: called CC=1 SCAR=0.0
892 0 ▶sub _prop_default_value { $_[0]->_class_info->{PROP_DEFAULT_VALUE}{$_[1]}; }
Subroutine _prop_default_value: not called CC=1 SCAR=6.9
893 212 ▶sub _has_prop { exists $_[0]->_class_info->{PROP}{$_[1]}; }
Subroutine _has_prop: called CC=1 SCAR=0.0
894 73 ▶sub _prop2table { $_[0]->_class_info->{PROP2TABLE_MAP}{$_[1]}; }
Subroutine _prop2table: called CC=1 SCAR=0.0
895 0 ▶sub _prop2field { $_[0]->_class_info->{PROP2FIELD_MAP}{$_[1]}; }
Subroutine _prop2field: not called CC=1 SCAR=6.9
896 21 ▶sub _is_intermediate { $_[0]->_class_info->{INTERMEDIATE}; }
Subroutine _is_intermediate: called CC=1 SCAR=0.0
897 96 ▶sub _is_initial { !$_[0]->_class_info; }
Subroutine _is_initial: called CC=1 SCAR=0.0
898 10 ▶sub _db_table { $_[0]->_class_info->{TABLE}[$_[1]]; }
Subroutine _db_table: called CC=1 SCAR=0.0
899 0 ▶sub _db_tables_str { $_[0]->_class_info->{TABLES_STR}; }
Subroutine _db_tables_str: not called CC=1 SCAR=6.9
900 7 ▶sub _db_tables_count { scalar( @{$_[0]->_class_info->{TABLE}} ); }
Subroutine _db_tables_count: called CC=1 SCAR=0.0
901 65 ▶sub _db_tables { @{$_[0]->_class_info->{TABLE}}; }
Subroutine _db_tables: called CC=1 SCAR=0.0
902 2 ▶sub _db_tables_ref { $_[0]->_class_info->{TABLE}; }
Subroutine _db_tables_ref: called CC=1 SCAR=0.0
903 26 ▶sub _db_table_fields { keys %{$_[0]->_class_info->{TABLE_STRUCT}{$_[1]}}; }
Subroutine _db_table_fields: called CC=1 SCAR=0.0
904 0 ▶sub _db_tables_inner_join { $_[0]->_class_info->{TABLES_INNER_JOIN}; }
Subroutine _db_tables_inner_join: not called CC=1 SCAR=6.9
905 33 ▶sub _not_mandatory_props { keys %{$_[0]->_class_info->{PROP2FIELD_MAP}}; }
Subroutine _not_mandatory_props: called CC=1 SCAR=0.0
906 4 ▶sub _all_props { ( 'id', 'class', keys %{$_[0]->_class_info->{PROP2FIELD_MAP}} ); }
Subroutine _all_props: called CC=1 SCAR=0.0
907 40 ▶sub _cache { $_[0]->primary_class->_class_info->{CACHE}; }
Subroutine _cache: called CC=1 SCAR=0.0
908  
909 sub _rev_refs
910 6 ▶{
Subroutine _rev_refs: called CC=2 SCAR=6.9
911 6 my $class = shift;
912 6 my @refs = values %{$class->_class_info->{REV_REFS}};
913  
914 6 ▶ if( $class->_class_info->{BASE_CLASS} )
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_class_info->{"BASE_CLASS"}) 2 4 6
915 {
916 2 push @refs, $class->_class_info->{BASE_CLASS}->_rev_refs;
917 }
918  
919 6 return @refs;
920 }
921  
922 sub _has_rev_ref
923 0 ▶{
Subroutine _has_rev_ref: not called CC=5 SCAR=34.0
924 0 my $class = shift;
925 0 my $rev_class = shift;
926 0 my $rev_prop = shift;
927  
928 0 ▶ $class->_class_info->{REV_REFS}{ $rev_class.' '.$rev_prop }
Branch: 1 branch
BranchTrueFalseTotal
$class->_class_info->{"REV_REFS"}{$rev_class . ' ' . $rev_prop} or $rev_class->base_class and $class->_has_rev_ref($rev_class->base_class, $rev_prop) or $class->base_class and $class->base_class->_has_rev_ref($rev_class, $rev_prop) 0 0 0
Condition0%0/12
$class->_class_info->{'REV_REFS'}{$rev_class . ' ' . $rev_prop} || $rev_class->base_class && $class->_has_rev_ref($rev_class->base_class, $rev_prop) || $class->base_class && $class->base_class->_has_rev_ref($rev_class, $rev_prop)
l!l&&r!l&&!r
000
$class->_class_info->{"REV_REFS"}{$rev_class . ' ' . $rev_prop} or $rev_class->base_class and $class->_has_rev_ref($rev_class->base_class, $rev_prop)
l!l&&r!l&&!r
000
$rev_class->base_class and $class->_has_rev_ref($rev_class->base_class, $rev_prop)
!ll&&!rl&&r
000
$class->base_class and $class->base_class->_has_rev_ref($rev_class, $rev_prop)
!ll&&!rl&&r
000
MC/DC0%0/5
$class->_class_info->{'REV_REFS'}{$rev_class . ' ' . $rev_prop} || $rev_class->base_class && $class->_has_rev_ref($rev_class->base_class, $rev_prop) || $class->base_class && $class->base_class->_has_rev_ref($rev_class, $rev_prop)
$class->_class_info->{"REV_REFS"}{$rev_class . ' ' . $rev_prop} $rev_class->base_class $class->_has_rev_ref($rev_class->base_class, $rev_prop) $class->base_class $class->base_class->_has_rev_ref($rev_class, $rev_prop)
Truth table0%0/8
A or B and C || D and E
A= $class->_class_info->{"REV_REFS"}{$rev_class . ' ' . $rev_prop}B= $rev_class->base_classC= $class->_has_rev_ref($rev_class->base_class, $rev_prop)D= $class->base_classE= $class->base_class->_has_rev_ref($rev_class, $rev_prop)
ABCDEresult
1XXXX1
011XX1
00X111
010111
00X0X0
00X100
0100X0
010100
929 || (
930 $rev_class->base_class
931 && $class->_has_rev_ref( $rev_class->base_class, $rev_prop )
932 )
933 || (
934 $class->base_class
935 && $class->base_class->_has_rev_ref( $rev_class, $rev_prop )
936 );
937 }
938  
939 ## use: $class->stat
940 ## (
941 ## data => { alias=>ORM::Expr, ... },
942 ## preload => { alias=>boolean, ... },
943 ## filter => ORM::Expr,
944 ## group_by => [ ORM::Ident|ORM::Metaprop, ... ],
945 ## post_filter => ORM::Expr,
946 ## order => ORM::Order,
947 ## lazy_load => boolean,
948 ## page => integer,
949 ## pagesize => integer,
950 ## count => boolean,
951 ## error => ORM::Error,
952 ## return_res => boolean,
953 ## )
954 ##
955 sub stat
956 1 ▶{
Subroutine stat: called CC=24 SCAR=43.3
Pod documented
957 1 my $class = shift;
958 1 my %arg = @_;
959 1 my $error = ORM::Error->new;
960 1 ▶ my $page = defined $arg{page} && int( $arg{page} );
Condition33%1/3
defined $arg{'page'} && int $arg{'page'}
!ll&&!rl&&r
100
MC/DC0%0/2
defined $arg{'page'} && int $arg{'page'}
defined $arg{'page'} int $arg{'page'}
Truth table33%1/3
A && B
A= defined $arg{'page'}B= int $arg{'page'}
ABresult
0X0
100
111
961 1 ▶ my $pagesize = defined $arg{pagesize} && int( $arg{pagesize} );
Condition33%1/3
defined $arg{'pagesize'} && int $arg{'pagesize'}
!ll&&!rl&&r
100
MC/DC0%0/2
defined $arg{'pagesize'} && int $arg{'pagesize'}
defined $arg{'pagesize'} int $arg{'pagesize'}
Truth table33%1/3
A && B
A= defined $arg{'pagesize'}B= int $arg{'pagesize'}
ABresult
0X0
100
111
962 0 ▶ my $order = ( ref $arg{order} eq 'ARRAY' ) ? ORM::Order->new( @{$arg{order}} ) : $arg{order};
Branch: 1 branch
BranchTrueFalseTotal
ref $arg{'order'} eq 'ARRAY' ? : 0 1 1
963 1 ▶ my %preload = $arg{preload} ? %{$arg{preload}} : ();
Branch: 1 branch
BranchTrueFalseTotal
$arg{'preload'} ? : 1 0 1
964 1 my %data;
965 my %conv;
966 1 my $res;
967  
968 1 ▶ if( ! %{$arg{data}} )
Branch: 1 branch
BranchTrueFalseTotal
unless (%{$arg{"data"};}) 0 1 1
969 {
970 0 $error->add_fatal( "'data' argument is missing" );
971 }
972  
973 1 ▶ unless( $error->fatal )
Branch: 1 branch
BranchTrueFalseTotal
unless ($error->fatal) 1 0 1
974 {
975 # Prepare type converstions
976 1 ▶ if( $arg{count} )
Branch: 2 branches
BranchTrueFalseTotal
if ($arg{'count'}) { } 0 1 1
elsif (%preload) { } 1 0 1
977 {
978 0 %data = %{$arg{data}};
979 }
980 elsif( %preload )
981 {
982 1 for my $name ( keys %{$arg{data}} )
983 {
984 2 ▶ if( ! UNIVERSAL::isa( $arg{data}{$name}, 'ORM::Metaprop' ) )
Branch: 2 branches
BranchTrueFalseTotal
if (not UNIVERSAL::isa($arg{'data'}{$name}, 'ORM::Metaprop')) { } 0 2 2
elsif ($arg{'data'}{$name}->_prop_ref_class and $preload{$name}) { } 2 0 2
985 {
986 0 $conv{$name} = undef;
987 0 $data{$name} = $arg{data}{$name};
988 0 delete $preload{$name};
989 }
990 ▶ elsif( $arg{data}{$name}->_prop_ref_class && $preload{$name} )
Condition33%1/3
$arg{"data"}{$name}->_prop_ref_class and $preload{$name}
!ll&&!rl&&r
002
MC/DC0%0/2
$arg{"data"}{$name}->_prop_ref_class and $preload{$name}
$arg{"data"}{$name}->_prop_ref_class $preload{$name}
Truth table33%1/3
A and B
A= $arg{"data"}{$name}->_prop_ref_classB= $preload{$name}
ABresult
0X0
100
111
991 {
992 2 $conv{$name} = $arg{data}{$name}->_prop_class;
993 2 for my $prop ( $arg{data}{$name}->_prop_ref_class->_all_props )
994 {
995 13 ▶ if( $prop eq 'id' )
Branch: 1 branch
BranchTrueFalseTotal
if ($prop eq 'id') { } 2 11 13
996 {
997 2 $data{$name} = $arg{data}{$name}->_prop( $prop );
998 }
999 else
1000 {
1001 11 $data{"_${name} ${prop}"} = $arg{data}{$name}->_prop( $prop );
1002 }
1003 }
1004 }
1005 else
1006 {
1007 0 $conv{$name} = $arg{data}{$name}->_prop_class;
1008 0 $data{$name} = $arg{data}{$name};
1009 0 delete $preload{$name};
1010 }
1011 }
1012 }
1013 else
1014 {
1015 0 %data = %{$arg{data}};
1016 0 for my $name ( keys %data )
1017 {
1018 0 ▶ if
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($data{$name}, 'ORM::Metaprop') and $data{$name}->_prop_class) { } 0 0 0
Condition0%0/3
UNIVERSAL::isa($data{$name}, "ORM::Metaprop") and $data{$name}->_prop_class
!ll&&!rl&&r
000
MC/DC0%0/2
UNIVERSAL::isa($data{$name}, "ORM::Metaprop") and $data{$name}->_prop_class
UNIVERSAL::isa($data{$name}, "ORM::Metaprop") $data{$name}->_prop_class
Truth table0%0/3
A and B
A= UNIVERSAL::isa($data{$name}, "ORM::Metaprop")B= $data{$name}->_prop_class
ABresult
0X0
100
111
1019 (
1020 UNIVERSAL::isa( $data{$name}, 'ORM::Metaprop' )
1021 && $data{$name}->_prop_class
1022 )
1023 {
1024 0 $conv{$name} = $data{$name}->_prop_class;
1025 }
1026 else
1027 {
1028 0 $conv{$name} = undef;
1029 }
1030 }
1031 }
1032  
1033 # Fetch result set
1034 $res = $class->_db->select_stat
1035 (
1036 class => $class,
1037 data => \%data,
1038 filter => $arg{filter},
1039 post_filter => $arg{post_filter},
1040 group_by => $arg{group_by},
1041 1 order => $order,
1042 page => $page,
1043 pagesize => $pagesize,
1044 error => $error,
1045 );
1046 }
1047  
1048 # Final step, prepare resulting data
1049 1 ▶ if( $res && !$error->fatal )
Branch: 1 branch
BranchTrueFalseTotal
if ($res and not $error->fatal) 1 0 1
Condition33%1/3
$res and not $error->fatal
!ll&&!rl&&r
001
MC/DC0%0/2
$res and not $error->fatal
$res not $error->fatal
Truth table33%1/3
A and not(B)
A= $resB= not $error->fatal
ABresult
0X0
100
111
1050 {
1051 1 ▶ if( $arg{count} )
Branch: 1 branch
BranchTrueFalseTotal
if ($arg{'count'}) { } 0 1 1
1052 {
1053 0 $res = $res->rows;
1054 }
1055 else
1056 {
1057 $res = ORM::StatResultSet->new
1058 (
1059 class => $class,
1060 result => $res,
1061 preload => \%preload,
1062 conv => \%conv,
1063 lazy_load => $arg{lazy_load},
1064 1 );
1065 1 ▶ if( !$arg{return_res} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($arg{"return_res"}) 1 0 1
1066 {
1067 1 my @stat;
1068 my $stat;
1069  
1070 1 ▶ while( $stat = $res->next( error=>$error ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($stat = $res->next("error", $error)) 1 1 2
1071 {
1072 1 ▶ if( $error->fatal )
Branch: 1 branch
BranchTrueFalseTotal
if ($error->fatal) 0 1 1
1073 {
1074 0 @stat = ();
1075 0 last;
1076 }
1077 1 push @stat, $stat;
1078 }
1079  
1080 1 $res = \@stat;
1081 }
1082 }
1083 }
1084  
1085 1 $error->upto( $arg{error} );
1086 1 return $res;
1087 }
1088  
1089 ## use: $prop = $obj->_property
1090 ## (
1091 ## name => string,
1092 ## error => ORM::Error,
1093 ## );
1094 ##
1095 ## 'name' - is name of the property corresponding to field name in DB table
1096 ##
1097 ## $prop - is either plain property,
1098 ## either object referenced by id in DB,
1099 ## or object referenced by value in DB
1100 ##
1101 0 ▶sub _prop { shift->_property( @_ ); }
Subroutine _prop: not called CC=1 SCAR=6.9
1102 sub _property
1103 25 ▶{
Subroutine _property: called CC=12 SCAR=32.8
1104 25 my $self = shift;
1105 25 ▶ my %arg = ( @_ == 1 ) ? () : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 25 25
1106 25 ▶ my $prop = ( @_ == 1 ) ? $_[0] : $arg{name};
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 25 25
1107 25 my $class = ref $self;
1108 25 my $error = ORM::Error->new;
1109 25 my $res;
1110 my $pclass;
1111  
1112  
1113 25 ▶ if( exists $arg{new_value} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $arg{'new_value'}) { } 0 25 25
1114 {
1115 0 $self->update( prop=>{ $prop=>$arg{new_value} }, error=>$error );
1116 }
1117 else
1118 {
1119 25 ▶ if( exists $self->{_ORM_missing_tables} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_ORM_missing_tables"}) 3 22 25
1120 {
1121 3 $self->finish_loading( prop=>$prop, error=>$error );
1122 }
1123  
1124 25 ▶ unless( $error->fatal )
Branch: 1 branch
BranchTrueFalseTotal
unless ($error->fatal) 25 0 25
1125 {
1126 25 ▶ if( $prop eq 'class' && $class->_is_sealed )
Branch: 3 branches
BranchTrueFalseTotal
if ($prop eq 'class' and $class->_is_sealed) { } 0 25 25
elsif ($class->_plain_prop($prop)) { } 25 0 25
elsif ($pclass = $class->_prop_class($prop)) { } 0 0 0
Condition33%1/3
$prop eq "class" and $class->_is_sealed
!ll&&!rl&&r
2500
MC/DC0%0/2
$prop eq "class" and $class->_is_sealed
$prop eq "class" $class->_is_sealed
Truth table33%1/3
A and B
A= $prop eq "class"B= $class->_is_sealed
ABresult
0X0
100
111
1127 {
1128 0 $res = $class;
1129 }
1130 elsif( $class->_plain_prop( $prop ) )
1131 {
1132 25 $res = $self->{_ORM_data}{$prop};
1133 }
1134 elsif( $pclass = $class->_prop_class( $prop ) )
1135 {
1136 0 ▶ if( defined $self->{_ORM_data}{$prop} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_ORM_data"}{$prop}) 0 0 0
1137 {
1138 0 ▶ unless( exists $self->{_ORM_cache}{$prop} )
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_ORM_cache"}{$prop}) 0 0 0
1139 {
1140 $self->{_ORM_cache}{$prop} = $pclass->__ORM_new_db_value
1141 (
1142 0 value => $self->{_ORM_data}{$prop},
1143 error => $error,
1144 );
1145 }
1146 0 $res = $self->{_ORM_cache}{$prop};
1147 }
1148 }
1149 }
1150 }
1151  
1152 25 $error->upto( $arg{error} );
1153 25 return $res;
1154 }
1155  
1156 ## use: $prop = $obj->_property_id
1157 ## (
1158 ## name => string,
1159 ## error => ORM::Error,
1160 ## );
1161 ##
1162 ## 'name' - is name of the property corresponding to field name in DB table
1163 ##
1164 ## $prop - is either plain property,
1165 ## either object referenced by id in DB,
1166 ## or object referenced by value in DB
1167 ##
1168 0 ▶sub _prop_id { shift->_property_id( @_ ); }
Subroutine _prop_id: not called CC=1 SCAR=6.9
1169 sub _property_id
1170 160 ▶{
Subroutine _property_id: called CC=4 SCAR=15.7
1171 160 my $self = shift;
1172 160 my %arg;
1173 my $prop;
1174 160 my $value;
1175  
1176 160 ▶ if( @_ == 1 )
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 160 0 160
1177 {
1178 160 $prop = $_[0];
1179 }
1180 else
1181 {
1182 0 %arg = @_;
1183 0 $prop = $arg{name};
1184 }
1185  
1186 160 ▶ if( $prop eq 'class' )
Branch: 1 branch
BranchTrueFalseTotal
if ($prop eq 'class') { } 0 160 160
1187 {
1188 0 $value = $self->class;
1189 }
1190 else
1191 {
1192 160 ▶ if( exists $self->{_ORM_missing_tables} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_ORM_missing_tables"}) 0 160 160
1193 {
1194 0 $self->finish_loading( prop=>$prop, error=>$arg{error} );
1195 }
1196 160 $value = $self->{_ORM_data}{$prop};
1197 }
1198  
1199 160 return $value;
1200 }
1201  
1202 0 ▶sub _rev { shift->_rev_prop( @_ ); }
Subroutine _rev: not called CC=1 SCAR=6.9
1203 sub _rev_prop
1204 0 ▶{
Subroutine _rev_prop: not called CC=2 SCAR=17.9
1205 0 my $self = shift;
1206 0 my $rev_class = shift;
1207 0 my $rev_prop = shift;
1208 0 my %arg = @_;
1209  
1210 0 ▶ if( (ref $self)->_has_rev_ref( $rev_class, $rev_prop ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref($self)->_has_rev_ref($rev_class, $rev_prop)) 0 0 0
1211 {
1212 0 $arg{filter} = $arg{filter} & ( $rev_class->M->_prop( $rev_prop ) == $self );
1213 0 $rev_class->find( %arg );
1214 }
1215 }
1216  
1217 0 ▶sub _rev_count { shift->_rev_prop_count( @_ ); }
Subroutine _rev_count: not called CC=1 SCAR=6.9
1218 sub _rev_prop_count
1219 0 ▶{
Subroutine _rev_prop_count: not called CC=2 SCAR=17.9
1220 0 my $self = shift;
1221 0 my $rev_class = shift;
1222 0 my $rev_prop = shift;
1223 0 my %arg = @_;
1224  
1225 0 ▶ if( (ref $self)->_has_rev_ref( $rev_class, $rev_prop ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref($self)->_has_rev_ref($rev_class, $rev_prop)) 0 0 0
1226 {
1227 0 $arg{filter} = $arg{filter} & ( $rev_class->M->_prop( $rev_prop ) == $self );
1228 0 $rev_class->count( %arg );
1229 }
1230 }
1231  
1232 ## use: $prop = $obj->prop( error=>ORM::Error, new_value=>SCALAR );
1233 ##
1234 ## 'prop' - is name of the property corresponding to field name in DB table
1235 ##
1236 ## If 'new_value' is specified, then $obj will be updated with this value
1237 ## and new value will be returned.
1238 ##
1239 sub AUTOLOAD
1240 25 ▶{
Subroutine AUTOLOAD: called CC=3 SCAR=11.2
1241 25 ▶ if( $ORM::AUTOLOAD =~ /^(.+)::(.+)$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($ORM::AUTOLOAD =~ /^(.+)::(.+)$/) 25 0 25
1242 {
1243 25 my $prop = $2;
1244 25 my $self = shift;
1245 25 my %arg = @_;
1246  
1247 25 ▶ croak "Called undefined static method '$ORM::AUTOLOAD' of class '$self'" unless( ref $self );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 25 25
1248  
1249 25 $self->_property( name=>$prop, %arg );
1250 }
1251 }
1252  
1253 ##
1254 ## CLASS METHODS
1255 ##
1256  
1257 sub optimize_storage
1258 0 ▶{
Subroutine optimize_storage: not called CC=1 SCAR=6.9
Pod documented
1259 0 my $class = shift;
1260 0 $class->_db->optimize_tables( class=>$class );
1261 }
1262  
1263 ##
1264 ## PROTECTED METHODS
1265 ##
1266  
1267 sub _find_constructor
1268 3 ▶{
Subroutine _find_constructor: called CC=5 SCAR=16.3
1269 3 my $class = shift;
1270 3 my $prop = shift;
1271 3 my $result_tables = shift;
1272 3 my $self;
1273  
1274 3 ▶ if( $prop->{id} )
Branch: 1 branch
BranchTrueFalseTotal
if ($prop->{"id"}) 3 0 3
1275 {
1276 3 ▶ if( $prop->{class} )
Branch: 1 branch
BranchTrueFalseTotal
if ($prop->{'class'}) { } 3 0 3
1277 {
1278 3 $class->_load_ORM_class( $prop->{class} );
1279 3 $self = bless { _ORM_data => $prop }, $prop->{class};
1280  
1281 3 ▶ if( $result_tables )
Branch: 1 branch
BranchTrueFalseTotal
if ($result_tables) 3 0 3
1282 {
1283 3 my $class_tables_count = $prop->{class}->_db_tables_count;
1284 3 my $loaded_tables_count = scalar( @$result_tables );
1285 3 ▶ for( my $i=$loaded_tables_count; $i<$class_tables_count; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $class_tables_count) 1 3 4
1286 {
1287 1 $self->{_ORM_missing_tables}{$prop->{class}->_db_table($i)} = 1;
1288 }
1289 }
1290  
1291 3 delete $self->{_ORM_data}{class};
1292 }
1293 else
1294 {
1295 0 $self = bless { _ORM_data => $prop }, $class;
1296 }
1297 }
1298  
1299 3 return $self;
1300 }
1301  
1302 sub _rebless_to_broken
1303 2 ▶{
Subroutine _rebless_to_broken: called CC=4 SCAR=15.4
1304 2 my $self = shift;
1305 2 my %arg = @_;
1306
1307 2 $self->_cache->delete( $self );
1308  
1309 2 $self->{class} = ref $self;
1310 2 $self->{id} = $self->id;
1311  
1312 2 ▶ if( $arg{deleted} )
Branch: 2 branches
BranchTrueFalseTotal
if ($arg{'deleted'}) { } 2 0 2
elsif ($arg{'error'} and $arg{'error'}->fatal) { } 0 0 0
1313 {
1314 2 $self->{deleted} = 1;
1315 }
1316 ▶ elsif( $arg{error} && $arg{error}->fatal )
Condition0%0/3
$arg{"error"} and $arg{"error"}->fatal
!ll&&!rl&&r
000
MC/DC0%0/2
$arg{"error"} and $arg{"error"}->fatal
$arg{"error"} $arg{"error"}->fatal
Truth table0%0/3
A and B
A= $arg{"error"}B= $arg{"error"}->fatal
ABresult
0X0
100
111
1317 {
1318 0 $self->{error} = $arg{error};
1319 }
1320  
1321 2 delete $self->{_ORM_tmp};
1322 2 delete $self->{_ORM_data};
1323 2 delete $self->{_ORM_cache};
1324 2 delete $self->{_ORM_missing_tables};
1325  
1326 2 bless $self, 'ORM::Broken';
1327 }
1328  
1329 ## use: $self->_normalize_prop_to_db_value( name=>STRING, value=>SCALAR, error=>ORM::Error )
1330 ##
1331 ## Normalize specified value to be able to store it in database table.
1332 ## All arguments are necessary.
1333 ##
1334 sub _normalize_prop_to_db_value
1335 171 ▶{
Subroutine _normalize_prop_to_db_value: called CC=14 SCAR=32.8
1336 171 my $self = shift;
1337 171 my $class = ref $self;
1338 171 my %arg = @_;
1339 171 my $error = ORM::Error->new;
1340 171 my $prop_name = $arg{name};
1341 171 my $prop_value = $arg{value};
1342 171 my $prop_ref = ref $prop_value;
1343  
1344 171 ▶ if( ! $class->_has_prop( $prop_name ) )
Branch: 3 branches
BranchTrueFalseTotal
if (not $class->_has_prop($prop_name)) { } 0 171 171
elsif ($class->_plain_prop($prop_name)) { } 133 38 171
elsif ($class->_prop_is_ref($prop_name)) { } 19 19 38
1345 {
1346 0 $error->add_fatal( "Superfluous property '$prop_name'" );
1347 }
1348 elsif( $class->_plain_prop( $prop_name ) )
1349 {
1350 133 ▶ if( $prop_ref )
Branch: 1 branch
BranchTrueFalseTotal
if ($prop_ref) 0 133 133
1351 {
1352 0 $error->add_fatal
1353 (
1354 "Property '$prop_name' should be scalar, not reference"
1355 );
1356 }
1357 }
1358 elsif( $class->_prop_is_ref( $prop_name ) )
1359 {
1360 19 ▶ if( ! defined $prop_value )
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $prop_value) { } 9 10 19
elsif (not $prop_ref) { } 0 10 10
elsif (UNIVERSAL::isa($prop_ref, $class->_prop_class($prop_name))) { } 10 0 10
1361 {
1362 # leave NULL value
1363 }
1364 elsif( ! $prop_ref )
1365 {
1366 0 my $obj = $class->_prop_class( $prop_name )->exists
1367 (
1368 id => $prop_value,
1369 error => $error,
1370 );
1371 0 ▶ unless( $obj )
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj) 0 0 0
1372 {
1373 0 $error->add_fatal
1374 (
1375 "Property '$prop_name' of type '"
1376 . $class->_prop_class( $prop_name )
1377 . "' with id='$prop_value' was not found"
1378 );
1379 }
1380 }
1381 elsif( UNIVERSAL::isa( $prop_ref, $class->_prop_class( $prop_name ) ) )
1382 {
1383 10 $prop_value = $prop_value->id;
1384 }
1385 else
1386 {
1387 0 $error->add_fatal
1388 (
1389 "Property '$prop_name' should be of type "
1390 . "'" . $class->_prop_class( $prop_name ) . "' not '"
1391 . (ref $prop_value) . "'"
1392 );
1393 }
1394 }
1395 else # if( $class->_prop_class( $prop_name ) && ! $class->_prop_is_ref( $prop_name ) )
1396 {
1397 19 ▶ if( ! defined $prop_value )
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $prop_value) { } 0 19 19
elsif (not $prop_ref) { } 19 0 19
elsif (UNIVERSAL::isa($prop_ref, $class->_prop_class($prop_name))) { } 0 0 0
1398 {
1399 # leave undef value
1400 }
1401 elsif( ! $prop_ref )
1402 {
1403 19 my $obj = $class->_prop_class( $prop_name )->__ORM_new_db_value
1404 (
1405 value => $prop_value,
1406 error => $error,
1407 );
1408 19 ▶ $prop_value = defined $obj ? $obj->__ORM_db_value : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $obj ? : 19 0 19
1409 }
1410 elsif( UNIVERSAL::isa( $prop_ref, $class->_prop_class( $prop_name ) ) )
1411 {
1412 0 $prop_value = $prop_value->__ORM_db_value;
1413 }
1414 else
1415 {
1416 0 $error->add_fatal
1417 (
1418 "Property '$prop_name' should be of type "
1419 . "'" . $class->_prop_class( $prop_name ) . "' not '"
1420 . (ref $prop_value) . "'"
1421 );
1422 }
1423 }
1424  
1425 171 $arg{error}->add( error=>$error );
1426 171 ▶ return $arg{error}->fatal ? undef : $prop_value;
Branch: 1 branch
BranchTrueFalseTotal
$arg{'error'}->fatal ? : 0 171 171
1427 }
1428  
1429 ## use: $self->_validate_prop( prop=>HASH, method=>string, error=>ORM::Error )
1430 ##
1431 25 ▶sub _validate_prop {}
Subroutine _validate_prop: called CC=1 SCAR=0.0
1432  
1433 ## use: $self->_fix_prop( prop=>HASH, error=>ORM::Error )
1434 ##
1435 ## May be called from _validate_prop to change values of
1436 ## properties before commiting them to database.
1437 ##
1438 sub _fix_prop
1439 0 ▶{
Subroutine _fix_prop: not called CC=3 SCAR=24.8
1440 0 my $self = shift;
1441 0 my %arg = @_;
1442 0 my $error = ORM::Error->new;
1443  
1444 0 for my $prop ( keys %{$arg{prop}} )
1445 {
1446 0 ▶ if( (ref $self)->_has_prop( $prop ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref($self)->_has_prop($prop)) 0 0 0
1447 {
1448 0 delete $self->{_ORM_cache}{$prop};
1449 $self->{_ORM_data}{$prop} = $self->_normalize_prop_to_db_value
1450 (
1451 name => $prop,
1452 0 value => $arg{prop}{$prop},
1453 error => $error,
1454 );
1455 }
1456 }
1457  
1458 0 $error->upto( $arg{error} );
1459 0 return undef;
1460 }
1461  
1462 ## use: ORM->_init
1463 ## (
1464 ## db => ORM::Db,
1465 ## history_class => string||undef,
1466 ## prefer_lazy_load => boolean,
1467 ## emulate_foreign_keys => boolean,
1468 ## default_cache_size => integer,
1469 ## )
1470 ##
1471 sub _init
1472 4 ▶{
Subroutine _init: called CC=7 SCAR=20.2
1473 4 my $class = shift;
1474 4 my %arg = @_;
1475  
1476 4 ▶ die "'db' argument not specified" unless( exists $arg{db} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $arg{"db"} 0 4 4
1477 4 ▶ die "'db' argument is specified but undefined" unless( $arg{db} );
Branch: 1 branch
BranchTrueFalseTotal
unless $arg{"db"} 0 4 4
1478 4 ▶ die "'db' argument specified is not descendant of 'ORM::Db'" unless( UNIVERSAL::isa( $arg{db}, 'ORM::Db' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($arg{"db"}, "ORM::Db") 0 4 4
1479 4 ▶ die "'prefer_lazy_load' argument not specified" unless( exists $arg{prefer_lazy_load} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $arg{"prefer_lazy_load"} 0 4 4
1480 4 ▶ die "'emulate_foreign_keys' argument not specified" unless( exists $arg{emulate_foreign_keys} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $arg{"emulate_foreign_keys"} 0 4 4
1481 4 ▶ die "'default_cache_size' argument not specified" unless( exists $arg{default_cache_size} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $arg{"default_cache_size"} 0 4 4
1482  
1483 4 $class->_class_hier( {} );
1484 4 $class->_db( $arg{db} );
1485 4 $class->_history_class( $arg{history_class} );
1486 4 $class->_default_prefer_lazy_load( $arg{prefer_lazy_load} );
1487 4 $class->_emulate_foreign_keys( $arg{emulate_foreign_keys} );
1488 4 $class->_default_cache_size( $arg{default_cache_size} );
1489 4 $class->_current_transaction( undef );
1490 }
1491  
1492 ## use: $base_class->_derive
1493 ## (
1494 ## derived_class => string,
1495 ## intermediate => boolean,
1496 ## table => string,
1497 ##
1498 ## history_is_enabled => boolean,
1499 ## prefer_lazy_load => boolean,
1500 ## )
1501 ##
1502 sub _derive
1503 10 ▶{
Subroutine _derive: called CC=47 SCAR=39.3
1504 10 my $class = shift;
1505 10 my %arg = @_;
1506 10 my $error = ORM::Error->new;
1507 10 my $base = $class->_class_info;
1508 10 my $derived;
1509 my $struct;
1510 10 my $defaults;
1511 10 my $table;
1512  
1513 10 $derived = {};
1514 10 $class->_class_hier->{$arg{derived_class}} = $derived;
1515  
1516 # Copy SQL configuration from base class
1517 10 ▶ if( $base )
Branch: 1 branch
BranchTrueFalseTotal
if ($base) { } 5 5 10
1518 {
1519 5 ▶ if( $class->_is_sealed )
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_is_sealed) { } 0 5 5
1520 {
1521 0 $error->add_fatal
1522 (
1523 "You cannot create class derived from '$class'"
1524 . " because '$class' is sealed. If you want to derive"
1525 . " from '$class' you should add column 'class' to"
1526 . " table '".$class->_db_table(0)."' and fill it with"
1527 . " '$class' values."
1528 );
1529 }
1530 else
1531 {
1532 5 $derived->{BASE_CLASS} = $class;
1533 5 $derived->{INITIAL_CLASS} = $base->{INITIAL_CLASS};
1534 5 $derived->{PRIMARY_CLASS} = $base->{PRIMARY_CLASS};
1535 5 $derived->{TABLES_STR} = $base->{TABLES_STR};
1536 5 $derived->{TABLES_INNER_JOIN} = $base->{TABLES_INNER_JOIN};
1537 5 %{$derived->{PROP2FIELD_MAP}} = %{$base->{PROP2FIELD_MAP}};
1538 5 %{$derived->{PROP2TABLE_MAP}} = %{$base->{PROP2TABLE_MAP}};
1539 5 %{$derived->{TABLE_STRUCT}} = %{$base->{TABLE_STRUCT}};
1540 5 %{$derived->{PROP}} = %{$base->{PROP}};
1541 5 %{$derived->{PROP_DEFAULT_VALUE}} = %{$base->{PROP_DEFAULT_VALUE}};
1542 5 @{$derived->{TABLE}} = @{$base->{TABLE}};
1543 }
1544 }
1545 else
1546 {
1547 5 $derived->{INITIAL_CLASS} = $class;
1548 5 $derived->{PRIMARY_CLASS} = $arg{derived_class};
1549 5 ▶ $derived->{CACHE} = ORM::Cache->new( size=>($arg{cache_size}||$class->_default_cache_size) );
Condition33%1/3
$arg{'cache_size'} || $class->_default_cache_size
l!l&&r!l&&!r
050
MC/DC0%0/2
$arg{'cache_size'} || $class->_default_cache_size
$arg{'cache_size'} $class->_default_cache_size
Truth table33%1/3
A || B
A= $arg{'cache_size'}B= $class->_default_cache_size
ABresult
1X1
011
000
1550 }
1551  
1552 10 ▶ unless( $error->fatal )
Branch: 1 branch
BranchTrueFalseTotal
unless ($error->fatal) 10 0 10
1553 {
1554 10 $derived->{REV_REFS} = {};
1555 10 $derived->{INTERMEDIATE} = $arg{intermediate};
1556  
1557 # History configuration
1558 10 ▶ if( exists $arg{history_is_enabled} )
Branch: 2 branches
BranchTrueFalseTotal
if (exists $arg{'history_is_enabled'}) { } 2 8 10
elsif (exists $base->{'HISTORY_IS_ENABLED'}) { } 0 8 8
1559 {
1560 2 $derived->{HISTORY_IS_ENABLED} = $arg{history_is_enabled};
1561 }
1562 elsif( exists $base->{HISTORY_IS_ENABLED} )
1563 {
1564 0 $derived->{HISTORY_IS_ENABLED} = $base->{HISTORY_IS_ENABLED};
1565 }
1566  
1567 # Lazy load configuration
1568 10 ▶ if( exists $arg{prefer_lazy_load} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $arg{"prefer_lazy_load"}) 0 10 10
1569 {
1570 0 $derived->{PREFER_LAZY_LOAD} = $arg{prefer_lazy_load};
1571 }
1572  
1573 # Detect db table name
1574 10 ▶ $table = $arg{table} || $class->_guess_table_name( $arg{derived_class} );
Condition33%1/3
$arg{'table'} || $class->_guess_table_name($arg{'derived_class'})
l!l&&r!l&&!r
0100
MC/DC0%0/2
$arg{'table'} || $class->_guess_table_name($arg{'derived_class'})
$arg{'table'} $class->_guess_table_name($arg{'derived_class'})
Truth table33%1/3
A || B
A= $arg{'table'}B= $class->_guess_table_name($arg{'derived_class'})
ABresult
1X1
011
000
1575 }
1576  
1577 10 ▶ if( $table )
Branch: 1 branch
BranchTrueFalseTotal
if ($table) 10 0 10
1578 {
1579 ( $struct, $defaults ) = $class->_db->table_struct
1580 (
1581 class => $arg{derived_class},
1582 10 table => $table,
1583 error => $error,
1584 );
1585 10 ▶ if( $class->_history_class && $arg{derived_class} eq $class->_history_class )
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_history_class and $arg{"derived_class"} eq $class->_history_class) 2 8 10
Condition66%2/3
$class->_history_class and $arg{"derived_class"} eq $class->_history_class
!ll&&!rl&&r
082
MC/DC50%1/2
$class->_history_class and $arg{"derived_class"} eq $class->_history_class
$class->_history_class $arg{"derived_class"} eq $class->_history_class
Truth table66%2/3
A and B
A= $class->_history_classB= $arg{"derived_class"} eq $class->_history_class
ABresult
0X0
100
111
1586 {
1587 2 $struct->{slaved_by} = $class->_history_class;
1588 }
1589 # Check whether table exists
1590 10 ▶ if( ! scalar( %$struct ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar %$struct) 1 9 10
1591 {
1592 1 $error->add_fatal
1593 (
1594 "Table '$table' for class '$arg{derived_class}' not found."
1595 );
1596 1 $table = undef;
1597 }
1598 }
1599 10 ▶ if( $table )
Branch: 1 branch
BranchTrueFalseTotal
if ($table) 9 1 10
1600 {
1601 # Check whether table format is correct
1602 9 ▶ unless( $error->fatal )
Branch: 1 branch
BranchTrueFalseTotal
unless ($error->fatal) 9 0 9
1603 {
1604 9 ▶ if( ! exists $struct->{id} )
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $struct->{"id"}) 0 9 9
1605 {
1606 0 $error->add_fatal( "Table '$table' should contain 'id' column" );
1607 }
1608 }
1609 9 ▶ unless( $error->fatal )
Branch: 1 branch
BranchTrueFalseTotal
unless ($error->fatal) 9 0 9
1610 {
1611 9 ▶ if
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_class_is_primary($arg{"derived_class"}) and not exists $struct->{"class"}) 2 7 9
Condition100%3/3
$class->_class_is_primary($arg{"derived_class"}) and not exists $struct->{"class"}
!ll&&!rl&&r
432
MC/DC100%2/2
$class->_class_is_primary($arg{"derived_class"}) and not exists $struct->{"class"}
$class->_class_is_primary($arg{"derived_class"}) not exists $struct->{"class"}
Truth table100%3/3
A and not(B)
A= $class->_class_is_primary($arg{"derived_class"})B= not exists $struct->{"class"}
ABresult
0X0
100
111
1612 (
1613 $class->_class_is_primary( $arg{derived_class} )
1614 && ! exists $struct->{class}
1615 )
1616 {
1617 2 $derived->{SEALED} = 1;
1618 }
1619 }
1620 # Initialize $derived->{TABLES_INNER_JOIN}
1621 9 ▶ unless( $error->fatal )
Branch: 1 branch
BranchTrueFalseTotal
unless ($error->fatal) 9 0 9
1622 {
1623 9 ▶ if( !$class->_class_is_primary( $arg{derived_class} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->_class_is_primary($arg{"derived_class"})) 4 5 9
1624 {
1625 4 ▶ $derived->{TABLES_INNER_JOIN} .= ' AND ' if( $derived->{TABLES_INNER_JOIN} );
Branch: 1 branch
BranchTrueFalseTotal
if $derived->{"TABLES_INNER_JOIN"} 0 4 4
1626 $derived->{TABLES_INNER_JOIN} .=
1627 4 $class->_db->qt( $table ).'.id = '.$class->_db->qt( $derived->{TABLE}[0] ).'.id';
1628 }
1629 }
1630 # Initialize
1631 # $derived->{PROP},
1632 # $derived->{PROP_DEFAULT_VALUE},
1633 # $derived->{PROP2FIELD_MAP},
1634 # $derived->{PROP2TABLE_MAP}
1635 9 ▶ unless( $error->fatal )
Branch: 1 branch
BranchTrueFalseTotal
unless ($error->fatal) 9 0 9
1636 {
1637 9 my $prop;
1638 9 for $prop ( keys %$struct )
1639 {
1640 43 $derived->{PROP}{$prop} = $struct->{$prop};
1641 43 $derived->{PROP_DEFAULT_VALUE}{$prop} = $defaults->{$prop};
1642 }
1643  
1644 9 ▶ $derived->{PROP2TABLE_MAP}{id} = $table unless( $derived->{PROP2TABLE_MAP}{id} );
Branch: 1 branch
BranchTrueFalseTotal
unless $derived->{"PROP2TABLE_MAP"}{"id"} 5 4 9
1645 9 delete $struct->{id};
1646  
1647 9 for my $field ( keys %$struct )
1648 {
1649 34 ▶ unless( $derived->{PROP2FIELD_MAP}{$field} )
Branch: 1 branch
BranchTrueFalseTotal
if ($derived->{'PROP2FIELD_MAP'}{$field}) { } 0 34 34
1650 {
1651 34 $derived->{PROP2TABLE_MAP}{$field} = $table;
1652 34 ▶ if( $field ne 'class' )
Branch: 1 branch
BranchTrueFalseTotal
if ($field ne "class") 31 3 34
1653 {
1654 31 $derived->{PROP2FIELD_MAP}{$field} =
1655 $class->_db->qt( $table ) . '.' . $class->_db->qf( $field );
1656 }
1657 }
1658 else
1659 {
1660 0 $error->add_fatal
1661 (
1662 "Duplicate columns "
1663 . "'$derived->{PROP2FIELD_MAP}{$field}',"
1664 . " '".$class->_db->qt($table).'.'.$class->_db->qf($field)."'"
1665 );
1666 0 last;
1667 }
1668 }
1669 }
1670 # Initialize
1671 # $derived->{TABLE},
1672 # $derived->{TABLE_STR},
1673 # $derived->{TABLE_STRUCT},
1674 9 delete $struct->{class};
1675 9 ▶ unless( $error->fatal )
Branch: 1 branch
BranchTrueFalseTotal
unless ($error->fatal) 9 0 9
1676 {
1677 9 ▶ if( !$class->_class_is_primary( $arg{derived_class} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->_class_is_primary($arg{"derived_class"})) 4 5 9
1678 {
1679 4 $derived->{TABLES_STR} .= ',';
1680 }
1681 9 $derived->{TABLES_STR} .= $class->_db->qt( $table );
1682 9 $derived->{TABLE_STRUCT}{$table} = $struct;
1683 9 push @{$derived->{TABLE}}, $table;
1684 }
1685 }
1686  
1687 10 ▶ unless( $error->fatal )
Branch: 1 branch
BranchTrueFalseTotal
unless ($error->fatal) 9 1 10
1688 {
1689 # Load self metaprop class
1690 9 $derived->{METAPROP_CLASS} = "ORM::Meta::$arg{derived_class}";
1691 9 ▶ if( ! eval "require $derived->{METAPROP_CLASS}" )
Branch: 1 branch
BranchTrueFalseTotal
unless (eval qq[require $derived->{"METAPROP_CLASS"}]) 9 0 9
1692 {
1693 9 ▶ if( $derived->{BASE_CLASS} )
Branch: 1 branch
BranchTrueFalseTotal
if ($derived->{'BASE_CLASS'}) { } 4 5 9
1694 {
1695 4 $derived->{METAPROP_CLASS} = $base->{METAPROP_CLASS};
1696 }
1697 else
1698 {
1699 5 $derived->{METAPROP_CLASS} = 'ORM::Metaprop';
1700 }
1701 }
1702 }
1703  
1704 10 my %require;
1705  
1706 10 ▶ unless( $error->fatal )
Branch: 1 branch
BranchTrueFalseTotal
unless ($error->fatal) 9 1 10
1707 {
1708 # Load referenced and referencing classes
1709 # and initialize reverse props
1710 9 for my $prop ( keys %{$derived->{TABLE_STRUCT}{$table}} )
1711 {
1712 31 my $pclass = $derived->{PROP}{$prop};
1713 31 ▶ if( $pclass && !$class->_class_hier->{$pclass} )
Branch: 1 branch
BranchTrueFalseTotal
if ($pclass and not $class->_class_hier->{$pclass}) 3 28 31
Condition100%3/3
$pclass and not $class->_class_hier->{$pclass}
!ll&&!rl&&r
2533
MC/DC100%2/2
$pclass and not $class->_class_hier->{$pclass}
$pclass not $class->_class_hier->{$pclass}
Truth table100%3/3
A and not(B)
A= $pclassB= not $class->_class_hier->{$pclass}
ABresult
0X0
100
111
1714 {
1715 3 $require{$pclass} = 1;
1716 }
1717 }
1718 9 for my $pclass ( $class->_db->referencing_classes( class=>$arg{derived_class}, error=>$error ) )
1719 {
1720 2 ▶ $require{$pclass->{class}} = 1 unless( $class->_class_hier->{$pclass->{class}} );
Branch: 1 branch
BranchTrueFalseTotal
unless $class->_class_hier->{$pclass->{"class"}} 1 1 2
1721 $derived->{REV_REFS}{ $pclass->{class}.' '.$pclass->{prop} }
1722 2 = [ $pclass->{class}, $pclass->{prop} ];
1723 }
1724 ## Following pease of code make sence only in mod_perl environment,
1725 ## it is necessary to avoid the following problem:
1726 ##
1727 ## If you have created and loaded new ORM-class My::Class2 that contain
1728 ## referencing property to class My::Class1, then My::Class1 does not
1729 ## know about new referer and therefore My::Class1->_rev_refs returns
1730 ## outdated data.
1731 ##
1732 9 for my $prop ( keys %{$derived->{TABLE_STRUCT}{$table}} )
1733 {
1734 31 my $pclass = $derived->{PROP}{$prop};
1735 31 my $key = "$arg{derived_class} $prop";
1736 31 ▶ if( $pclass && $class->_class_hier->{$pclass} && !$pclass->_class_info->{REV_REFS}{$key} )
Branch: 1 branch
BranchTrueFalseTotal
if ($pclass and $class->_class_hier->{$pclass} and not $pclass->_class_info->{"REV_REFS"}{$key}) 2 29 31
Condition100%6/6
$pclass and $class->_class_hier->{$pclass} and not $pclass->_class_info->{"REV_REFS"}{$key}
!ll&&!rl&&r
2812
$pclass and $class->_class_hier->{$pclass}
!ll&&!rl&&r
2533
MC/DC100%3/3
$pclass and $class->_class_hier->{$pclass} and not $pclass->_class_info->{"REV_REFS"}{$key}
$pclass $class->_class_hier->{$pclass} not $pclass->_class_info->{"REV_REFS"}{$key}
Truth table100%4/4
A and B and not(C)
A= $pclassB= $class->_class_hier->{$pclass}C= not $pclass->_class_info->{"REV_REFS"}{$key}
ABCresult
0XX0
10X0
1100
1111
1737 {
1738 2 $pclass->_class_info->{REV_REFS}{$key} = [ $arg{derived_class}, $prop ];
1739 }
1740 }
1741  
1742 # Load metaclasses of not ORM classes
1743 9 for my $prop ( keys %{$derived->{TABLE_STRUCT}{$table}} )
1744 {
1745 31 my $pclass = $derived->{PROP}{$prop};
1746 31 ▶ if( $pclass && !$class->_class_hier->{$pclass} )
Branch: 1 branch
BranchTrueFalseTotal
if ($pclass and not $class->_class_hier->{$pclass}) 3 28 31
Condition100%3/3
$pclass and not $class->_class_hier->{$pclass}
!ll&&!rl&&r
2533
MC/DC100%2/2
$pclass and not $class->_class_hier->{$pclass}
$pclass not $class->_class_hier->{$pclass}
Truth table100%3/3
A and not(B)
A= $pclassB= not $class->_class_hier->{$pclass}
ABresult
0X0
100
111
1747 {
1748 3 ORM::Metaprop->_class2metaclass( $pclass );
1749 }
1750 }
1751 }
1752  
1753 # Print error message and exit if necessary
1754 10 ▶ die $error->text if( $error->any );
Branch: 1 branch
BranchTrueFalseTotal
if $error->any 1 9 10
1755  
1756 9 return keys %require;
1757 }
1758  
1759 ##
1760 ## PRIVATE METHODS
1761 ##
1762  
1763 sub _values_are_not_equal
1764 32 ▶{
Subroutine _values_are_not_equal: called CC=5 SCAR=16.6
1765 32 my $self = shift;
1766 32 my $val1 = shift;
1767 32 my $val2 = shift;
1768  
1769 32 ▶ ( ( defined $val1 ) xor ( defined $val2 ) )
Branch: 1 branch
BranchTrueFalseTotal
defined $val1 xor defined $val2 or defined $val1 and defined $val2 and $val1 ne $val2 3 29 32
Condition69%9/13
defined $val1 ^^ defined $val2 || defined $val1 && defined $val2 && $val1 ne $val2
l!l&&r!l&&!r
31019
defined $val1 xor defined $val2
l&&rl&&!r!l&&r!l&&!r
29210
defined $val1 and defined $val2 and $val1 ne $val2
!ll&&!rl&&r
01910
defined $val1 and defined $val2
!ll&&!rl&&r
0029
MC/DC60%3/5
defined $val1 ^^ defined $val2 || defined $val1 && defined $val2 && $val1 ne $val2
defined $val1 defined $val2 defined $val1 defined $val2 $val1 ne $val2
Truth table40%4/10
A xor B || C and D and E
A= defined $val1B= defined $val2C= defined $val1D= defined $val2E= $val1 ne $val2
ABCDEresult
10XXX1
01XXX1
111111
001111
110XX0
1110X0
111100
000XX0
0010X0
001100
1770 || ( defined $val1 && defined $val2 && ( $val1 ne $val2 ) );
1771 }
1772  
1773 ##
1774 ## METHODS AND PROPERTIES TO USE DURING CLASS INITIALISATION
1775 ## ( ORM->_derive )
1776 ##
1777  
1778 27 ▶sub _class_is_primary { ! exists $_[1]->_class_info->{TABLE}; }
Subroutine _class_is_primary: called CC=1 SCAR=0.0
1779  
1780 ## use: $table_name = $class->_guess_table_name( $obj_class );
1781 ##
1782 sub _guess_table_name
1783 0 ▶{
Subroutine _guess_table_name: not called CC=1 SCAR=6.9
1784 0 my $class = shift;
1785 0 my $table = shift;
1786  
1787 0 $table =~ s/::/_/g;
1788  
1789 0 return $table;
1790 }
1791  
1792 ## use: $prop_class = $class->_db_type_to_class( $db_field_name, $db_type_name );
1793 ##
1794 sub _db_type_to_class
1795 43 ▶{
Subroutine _db_type_to_class: called CC=4 SCAR=14.6
1796 43 my $class = shift;
1797 43 my $field = shift;
1798 43 my $type = shift;
1799 43 my $prop_class;
1800  
1801 ## These classes will be used by default for columns
1802 ## of type 'date' and 'datetime' in database.
1803 ##
1804 ## '__ORM_new_db_value' method of classes should
1805 ## be able to return object constructed by value
1806 ## of 'time' function.
1807 ##
1808 ## This means:
1809 ##
1810 ## $class->__ORM_new_db_value( value=>1125850389 )->__ORM_db_value
1811 ## should return '2005-09-04 22:13:09'
1812 ##
1813 43 ▶ if( ( lc $type ) eq 'date' )
Branch: 3 branches
BranchTrueFalseTotal
if (lc $type eq 'date') { } 0 43 43
elsif (lc $type eq 'datetime') { } 2 41 43
elsif (lc $type eq 'timestamp') { } 0 41 41
1814 {
1815 0 $prop_class = 'ORM::Date';
1816 }
1817 elsif( ( lc $type ) eq 'datetime' )
1818 {
1819 2 $prop_class = 'ORM::Datetime';
1820 }
1821 elsif( ( lc $type ) eq 'timestamp' )
1822 {
1823 0 $prop_class = 'ORM::Datetime';
1824 }
1825  
1826 43 return $prop_class;
1827 }
1828  
1829 ## use: $class->_load_ORM_class( $class );
1830 ##
1831 sub _load_ORM_class
1832 5 ▶{
Subroutine _load_ORM_class: called CC=2 SCAR=9.2
1833 5 my $class = shift;
1834 5 my $load_class = shift;
1835  
1836 5 ▶ unless( $class->_class_hier->{$load_class} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->_class_hier->{$load_class}) 0 5 5
1837 {
1838 0 $load_class .= '.pm';
1839 0 $load_class =~ s(::)(/)g;
1840 0 require $load_class;
1841 }
1842 }
1843  
1844 sub DESTROY
1845 0 ▶{
Subroutine DESTROY: not called CC=3 SCAR=24.8
1846 0 ▶ exists $_[0]->_class_hier->{PRIMARY_CLASS} && $_[0]->_cache && $_[0]->_cache->delete( $_[0] );
Branch: 1 branch
BranchTrueFalseTotal
exists $_[0]->_class_hier->{"PRIMARY_CLASS"} and $_[0]->_cache and $_[0]->_cache->delete($_[0]) 0 0 0
Condition0%0/3
exists $_[0]->_class_hier->{"PRIMARY_CLASS"} and $_[0]->_cache
!ll&&!rl&&r
000
MC/DC0%0/2
exists $_[0]->_class_hier->{"PRIMARY_CLASS"} and $_[0]->_cache
exists $_[0]->_class_hier->{"PRIMARY_CLASS"} $_[0]->_cache
Truth table0%0/3
A and B
A= exists $_[0]->_class_hier->{"PRIMARY_CLASS"}B= $_[0]->_cache
ABresult
0X0
100
111
1847 }
1848  
1849 5 1;
1850 __END__
« ORM/MetapropBuilder.pm ↑ Summary ORM/Db/DBI/MySQL.pm »