blib/lib/ObjectDB.pm

filter: Statementstmt 25.3% 86 / 339 Branchbran 13.7% 25 / 182 Conditioncond 22.7% 10 / 44 MC/DCmcdc 11.1% 3 / 27 Subroutinesub 38.3% 18 / 47 Podpod 77.4% 24 / 31 totaltotal 24.7% 166 / 670 SCAR 89.5
CC
125
Cov
21%
set_column
212.8
create
90.0
load
90.0
CRAP 7707.7
↑ Summary blib/lib/ObjectDB/Table.pm »
1 package ObjectDB;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use mro;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 require Carp;
8 5 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use SQL::Composer;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use ObjectDB::DBHPool;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use ObjectDB::Meta;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use ObjectDB::Quoter;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use ObjectDB::RelatedFactory;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use ObjectDB::Table;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use ObjectDB::With;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use ObjectDB::Util qw(execute filter_columns);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 5 our $VERSION = '3.27';
19  
20 5 $Carp::Internal{ (__PACKAGE__) }++;
21 5 $Carp::Internal{"ObjectDB::$_"}++ for qw/
22 With
23 Related
24 Related::ManyToOne
25 Related::OneToOne
26 Related::ManyToMany
27 Related::OneToMany
28 Meta::Relationship
29 Meta::Relationship::ManyToOne
30 Meta::Relationship::OneToOne
31 Meta::Relationship::ManyToMany
32 Meta::Relationship::OneToMany
33 Meta::RelationshipFactory
34 Table
35 Util
36 Quoter
37 DBHPool
38 Meta
39 RelationshipFactory
40 /;
41  
42 sub new {
43 8 ▶ my $class = shift;
Subroutine new: called CC=5 SCAR=16.2
Pod undocumented
44 8 ▶ $class = ref $class if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 8 8
45 8 my (%columns) = @_;
46  
47 8 my $self = {};
48 8 bless $self, $class;
49  
50 8 foreach my $column (keys %columns) {
51 4 ▶ if ( $self->meta->is_column($column)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->meta->is_column($column) or $self->meta->is_relationship($column)) 3 1 4
Condition66%2/3
$self->meta->is_column($column) or $self->meta->is_relationship($column)
l!l&&r!l&&!r
301
MC/DC50%1/2
$self->meta->is_column($column) or $self->meta->is_relationship($column)
$self->meta->is_column($column) $self->meta->is_relationship($column)
Truth table66%2/3
A or B
A= $self->meta->is_column($column)B= $self->meta->is_relationship($column)
ABresult
1X1
011
000
52 || $self->meta->is_relationship($column))
53 {
54 3 $self->set_column($column => $columns{$column});
55 }
56 }
57  
58 8 $self->{is_in_db} = 0;
59 8 $self->{is_modified} = 0;
60  
61 8 return $self;
62 }
63  
64 sub is_in_db {
65 0 ▶ my $self = shift;
Subroutine is_in_db: not called CC=2 SCAR=17.9
Pod documented
66  
67 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
68 0 $self->{is_in_db} = $_[0];
69 0 return $self;
70 }
71  
72 0 return $self->{is_in_db};
73 }
74  
75 sub is_modified {
76 0 ▶ my $self = shift;
Subroutine is_modified: not called CC=1 SCAR=6.9
Pod documented
77  
78 0 return $self->{is_modified};
79 }
80  
81 0 ▶sub dbh { &init_db }
Subroutine dbh: not called CC=1 SCAR=6.9
Pod undocumented
82  
83 sub init_db {
84 0 ▶ my $self = shift;
Subroutine init_db: not called CC=10 SCAR=45.0
Pod documented
85  
86 5 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
87  
88 0 ▶ my $class = ref($self) ? ref($self) : $self;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 0 0 0
89  
90 0 my $dbh;
91 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
92 0 ▶ if (@_ == 1 && ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0]) { } 0 0 0
Condition0%0/3
@_ == 1 and ref $_[0]
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 1 and ref $_[0]
@_ == 1 ref $_[0]
Truth table0%0/3
A and B
A= @_ == 1B= ref $_[0]
ABresult
0X0
100
111
93 0 ${"$class\::DBH"} = shift;
94 }
95 else {
96 0 ${"$class\::DBH"} = ObjectDB::DBHPool->new(@_);
97 }
98  
99 0 $dbh = ${"$class\::DBH"};
100 }
101 else {
102 0 $dbh = ${"$class\::DBH"};
103  
104 0 ▶ if (!$dbh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dbh) 0 0 0
105 0 my $parents = mro::get_linear_isa($class);
106 0 foreach my $parent (@$parents) {
107 0 ▶ if ($dbh = ${"$parent\::DBH"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh = ${"${parent}::DBH";}) 0 0 0
108 0 last;
109 }
110 }
111 }
112  
113 0 ▶ Carp::croak('Setup a dbh first') unless $dbh;
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh 0 0 0
114 }
115  
116 0 ▶ return $dbh->isa('ObjectDB::DBHPool')
Branch: 1 branch
BranchTrueFalseTotal
$dbh->isa('ObjectDB::DBHPool') ? : 0 0 0
117 ? $dbh->dbh
118 : $dbh;
119 }
120  
121 sub txn {
122 0 ▶ my $self = shift;
Subroutine txn: not called CC=2 SCAR=17.9
Pod documented
123 0 my ($cb) = @_;
124  
125 0 my $dbh = $self->init_db;
126  
127 0 my $retval;
128 eval {
129 0 $dbh->{AutoCommit} = 0;
130  
131 0 $retval = $cb->($self);
132  
133 0 $self->commit;
134 0 ▶ } || do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $dbh->{"AutoCommit"} = 0; $retval = &$cb($self); $self->commit } } or do { my $e = $@; $self->rollback; Carp::croak($e) } 0 0 0
135 0 my $e = $@;
136  
137 0 $self->rollback;
138  
139 0 Carp::croak($e);
140 };
141  
142 0 return $retval;
143 }
144  
145 sub commit {
146 0 ▶ my $self = shift;
Subroutine commit: not called CC=2 SCAR=17.9
Pod documented
147  
148 0 my $dbh = $self->init_db;
149  
150 0 ▶ if ($dbh->{AutoCommit} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh->{"AutoCommit"} == 0) 0 0 0
151 0 $dbh->commit;
152 0 $dbh->{AutoCommit} = 1;
153 }
154  
155 0 return $self;
156 }
157  
158 sub rollback {
159 0 ▶ my $self = shift;
Subroutine rollback: not called CC=2 SCAR=17.9
Pod documented
160  
161 0 my $dbh = $self->init_db;
162  
163 0 ▶ if ($dbh->{AutoCommit} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh->{"AutoCommit"} == 0) 0 0 0
164 0 $dbh->rollback;
165 0 $dbh->{AutoCommit} = 1;
166 }
167  
168 0 return $self;
169 }
170  
171 sub meta {
172 42 ▶ my $class = shift;
Subroutine meta: called CC=2 SCAR=6.9
Pod documented
173 42 ▶ $class = ref $class if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 33 9 42
174  
175 42 return ObjectDB::Meta->find_or_register_meta($class, @_);
176 }
177  
178 sub table {
179 0 ▶ my $self = shift;
Subroutine table: not called CC=2 SCAR=17.9
Pod undocumented
180 0 ▶ my $class = ref $self ? ref $self : $self;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 0 0 0
181  
182 0 return ObjectDB::Table->new(class => $class, dbh => $self->init_db);
183 }
184  
185 sub columns {
186 0 ▶ my $self = shift;
Subroutine columns: not called CC=3 SCAR=24.8
Pod documented
187  
188 0 my @columns;
189 0 foreach my $key ($self->meta->columns) {
190 0 ▶ if (exists $self->{columns}->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"columns"}{$key}) 0 0 0
191 0 push @columns, $key;
192 }
193 }
194  
195 0 return @columns;
196 }
197  
198 sub column {
199 0 ▶ my $self = shift;
Subroutine column: not called CC=4 SCAR=30.0
Pod documented
200  
201 0 ▶ $self->{columns} ||= {};
Condition0%0/2
$self->{"columns"} ||= {}
l!l
00
MC/DC0%0/1
$self->{"columns"} ||= {}
$self->{"columns"}
Truth table0%0/2
A
A= $self->{"columns"}
Aresult
11
00
202  
203 0 ▶ if (@_ == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1) { } 0 0 0
elsif (@_ == 2) { } 0 0 0
204 0 return $self->get_column(@_);
205 }
206 elsif (@_ == 2) {
207 0 $self->set_column(@_);
208 }
209  
210 0 return $self;
211 }
212  
213 sub get_column {
214 8 ▶ my $self = shift;
Subroutine get_column: called CC=12 SCAR=29.0
Pod documented
215 8 my ($name) = @_;
216  
217 8 ▶ if ($self->meta->is_column($name)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->meta->is_column($name)) { } 6 2 8
elsif ($self->meta->is_relationship($name)) { } 0 2 2
218 6 my $value;
219  
220 6 ▶ if (exists $self->{columns}->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'columns'}{$name}) { } 3 3 6
221 3 $value = $self->{columns}->{$name};
222 }
223 else {
224 3 ▶ if (exists $self->meta->get_column($name)->{default}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->meta->get_column($name)->{"default"}) 1 2 3
225 1 my $default = $self->meta->get_column($name)->{default};
226 1 ▶ $value = ref $default eq 'CODE' ? $default->() : $default;
Branch: 1 branch
BranchTrueFalseTotal
ref $default eq 'CODE' ? : 0 1 1
227 }
228 }
229  
230 6 ▶ if (defined $value && $value ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and $value ne "") 4 2 6
Condition66%2/3
defined $value and $value ne ""
!ll&&!rl&&r
204
MC/DC50%1/2
defined $value and $value ne ""
defined $value $value ne ""
Truth table66%2/3
A and B
A= defined $valueB= $value ne ""
ABresult
0X0
100
111
231 4 ▶ if (my $type = $self->meta->get_column($name)->{type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $type = $self->meta->get_column($name)->{"type"}) 0 4 4
232 0 ▶ if ($type eq 'number') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'number') { } 0 0 0
elsif ($type eq 'string') { } 0 0 0
233 0 $value += 0;
234 }
235 elsif ($type eq 'string') {
236 0 $value .= '';
237 }
238 }
239 }
240  
241 6 return $value;
242 }
243 elsif ($self->meta->is_relationship($name)) {
244 return exists $self->{relationships}->{$name}
245 0 ▶ ? $self->{relationships}->{$name}
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'relationships'}{$name} ? : 0 0 0
246 : undef;
247 }
248 else {
249 2 return $self->{virtual_columns}->{$name};
250 }
251 }
252  
253 sub set_columns {
254 1 ▶ my $self = shift;
Subroutine set_columns: called CC=3 SCAR=11.2
Pod documented
255 1 ▶ my %values = ref $_[0] ? %{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 1 1
256  
257 1 ▶ while (my ($key, $value) = each %values) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %values) 1 1 2
258 1 $self->set_column($key => $value);
259 }
260  
261 1 return $self;
262 }
263  
264 sub set_column {
265 7 ▶ my $self = shift;
Subroutine set_column: called CC=23 SCAR=53.6
Pod documented
266 7 my ($name, $value) = @_;
267  
268 7 ▶ if ($self->meta->is_column($name)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->meta->is_column($name)) { } 6 1 7
elsif ($self->meta->is_relationship($name)) { } 0 1 1
269 6 ▶ if ( !defined $value
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value or $self->meta->is_nullable($name)) 2 4 6
Condition66%2/3
defined $value or $self->meta->is_nullable($name)
l!l&&r!l&&!r
402
MC/DC50%1/2
defined $value or $self->meta->is_nullable($name)
defined $value $self->meta->is_nullable($name)
Truth table66%2/3
A or B
A= defined $valueB= $self->meta->is_nullable($name)
ABresult
1X1
011
000
270 && !$self->meta->is_nullable($name) )
271 {
272 2 return $self;
273 }
274  
275 4 ▶ if ( !exists $self->{columns}->{$name}
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"columns"}{$name} and (defined $self->{"columns"}{$name} and defined $value and $self->{"columns"}{$name} eq $value)) 4 0 4
Condition44%4/9
exists $self->{"columns"}{$name} and (defined $self->{"columns"}{$name} and defined $value and $self->{"columns"}{$name} eq $value)
!ll&&!rl&&r
310
defined $self->{"columns"}{$name} and defined $value and $self->{"columns"}{$name} eq $value
!ll&&!rl&&r
010
defined $self->{"columns"}{$name} and defined $value
!ll&&!rl&&r
001
MC/DC0%0/4
exists $self->{"columns"}{$name} and (defined $self->{"columns"}{$name} and defined $value and $self->{"columns"}{$name} eq $value)
exists $self->{"columns"}{$name} defined $self->{"columns"}{$name} defined $value $self->{"columns"}{$name} eq $value
Truth table40%2/5
A and B and C and D
A= exists $self->{"columns"}{$name}B= defined $self->{"columns"}{$name}C= defined $valueD= $self->{"columns"}{$name} eq $value
ABCDresult
0XXX0
10XX0
110X0
11100
11111
276 || !((defined $self->{columns}->{$name} && defined $value) && ($self->{columns}->{$name} eq $value)))
277 {
278 4 $self->{columns}->{$name} = $value;
279 4 $self->{is_modified} = 1;
280 }
281 }
282 elsif ($self->meta->is_relationship($name)) {
283 0 my $related_value;
284 0 ▶ if (Scalar::Util::blessed($value)) {
Branch: 3 branches
BranchTrueFalseTotal
if (builtin::blessed($value)) { } 0 0 0
elsif (ref $value eq 'ARRAY') { } 0 0 0
elsif (not $self->_is_empty_hash_ref($value)) { } 0 0 0
285 0 $related_value = $value;
286 }
287 elsif (ref $value eq 'ARRAY') {
288 0 $related_value = [];
289 0 foreach my $sub_value (@$value) {
290 0 ▶ next unless defined $sub_value && ref $sub_value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sub_value and ref $sub_value 0 0 0
Condition0%0/3
defined $sub_value and ref $sub_value
!ll&&!rl&&r
000
MC/DC0%0/2
defined $sub_value and ref $sub_value
defined $sub_value ref $sub_value
Truth table0%0/3
A and B
A= defined $sub_valueB= ref $sub_value
ABresult
0X0
100
111
291  
292 0 ▶ Carp::croak(qq{Value of related object(s) '$name' has to be a reference}) unless ref $sub_value;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $sub_value 0 0 0
293  
294 0 ▶ if (Scalar::Util::blessed($sub_value)) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($sub_value)) { } 0 0 0
elsif (ref $sub_value eq 'HASH') { } 0 0 0
295 0 push @$related_value, $sub_value;
296 }
297 elsif (ref($sub_value) eq 'HASH') {
298 0 ▶ if (!$self->_is_empty_hash_ref($sub_value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_empty_hash_ref($sub_value)) 0 0 0
299 0 push @$related_value, $self->meta->get_relationship($name)->class->new(%$sub_value);
300 }
301 }
302 else {
303 0 Carp::croak(qq{Unexpected reference found } . qq{when setting '$name' related object});
304 }
305 }
306  
307 0 ▶ undef $related_value unless @$related_value;
Branch: 1 branch
BranchTrueFalseTotal
unless @$related_value 0 0 0
308 }
309 elsif (!$self->_is_empty_hash_ref($value)) {
310 0 $related_value = $self->meta->get_relationship($name)->class->new(%$value);
311 }
312  
313 0 ▶ if ($related_value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($related_value) 0 0 0
314 0 ▶ if ($self->meta->get_relationship($name)->is_multi
Branch: 1 branch
BranchTrueFalseTotal
if ($self->meta->get_relationship($name)->is_multi and ref $related_value ne "ARRAY") 0 0 0
Condition0%0/3
$self->meta->get_relationship($name)->is_multi and ref $related_value ne "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$self->meta->get_relationship($name)->is_multi and ref $related_value ne "ARRAY"
$self->meta->get_relationship($name)->is_multi ref $related_value ne "ARRAY"
Truth table0%0/3
A and B
A= $self->meta->get_relationship($name)->is_multiB= ref $related_value ne "ARRAY"
ABresult
0X0
100
111
315 && ref($related_value) ne 'ARRAY')
316 {
317 0 $related_value = [$related_value];
318 }
319  
320 0 $self->{relationships}->{$name} = $related_value;
321 }
322 }
323 else {
324 1 $self->{virtual_columns}->{$name} = $value;
325 }
326  
327 5 return $self;
328 }
329  
330 sub clone {
331 0 ▶ my $self = shift;
Subroutine clone: not called CC=4 SCAR=30.0
Pod documented
332  
333 0 my %columns;
334 0 foreach my $column ($self->meta->columns) {
335 next
336 0 ▶ if $self->meta->is_primary_key($column)
Branch: 1 branch
BranchTrueFalseTotal
if $self->meta->is_primary_key($column) or $self->meta->is_unique_key($column) 0 0 0
Condition0%0/3
$self->meta->is_primary_key($column) or $self->meta->is_unique_key($column)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->meta->is_primary_key($column) or $self->meta->is_unique_key($column)
$self->meta->is_primary_key($column) $self->meta->is_unique_key($column)
Truth table0%0/3
A or B
A= $self->meta->is_primary_key($column)B= $self->meta->is_unique_key($column)
ABresult
1X1
011
000
337 || $self->meta->is_unique_key($column);
338 0 $columns{$column} = $self->column($column);
339 }
340  
341 0 return (ref $self)->new->set_columns(%columns);
342 }
343  
344 sub create {
345 0 ▶ my $self = shift;
Subroutine create: not called CC=9 SCAR=45.0
Pod documented
346  
347 0 ▶ Carp::croak(q{Calling 'create' on already created object})
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_in_db 0 0 0
348 if $self->is_in_db;
349  
350 0 my $dbh = $self->init_db;
351  
352 my $sql = SQL::Composer->build(
353 'insert',
354 driver => $dbh->{Driver}->{Name},
355 into => $self->meta->table,
356 0 values => [ map { $_ => $self->{columns}->{$_} } $self->columns ]
357 );
358  
359 0 my $rv = execute($dbh, $sql);
360  
361 0 ▶ if (my $auto_increment = $self->meta->auto_increment) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $auto_increment = $self->meta->auto_increment) 0 0 0
362 0 $self->set_column(
363 $auto_increment => $dbh->last_insert_id(undef, undef, $self->meta->table, $auto_increment));
364 }
365  
366 0 $self->{is_in_db} = 1;
367 0 $self->{is_modified} = 0;
368  
369 0 foreach my $rel_name (keys %{ $self->meta->relationships }) {
370 0 ▶ if (my $rel_values = $self->{relationships}->{$rel_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rel_values = $self->{"relationships"}{$rel_name}) 0 0 0
371 0 ▶ if (ref $rel_values eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rel_values eq 'ARRAY') { } 0 0 0
372 0 @$rel_values = grep { !$_->is_in_db } @$rel_values;
373 0 ▶ next unless @$rel_values;
Branch: 1 branch
BranchTrueFalseTotal
unless @$rel_values 0 0 0
374 }
375 else {
376 0 ▶ next if $rel_values->is_in_db;
Branch: 1 branch
BranchTrueFalseTotal
if $rel_values->is_in_db 0 0 0
377 }
378  
379 0 my $rel = $self->meta->get_relationship($rel_name);
380 0 my @related = $self->create_related($rel_name, $rel_values);
381  
382 0 ▶ $self->{relationships}->{$rel_name} =
Branch: 1 branch
BranchTrueFalseTotal
$rel->is_multi ? : 0 0 0
383 $rel->is_multi ? \@related : $related[0];
384 }
385 }
386  
387 0 return $self;
388 }
389  
390 sub save {
391 0 ▶ my $self = shift;
Subroutine save: not called CC=2 SCAR=17.9
Pod undocumented
392  
393 0 ▶ if ($self->is_in_db) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_in_db) { } 0 0 0
394 0 return $self->update;
395 }
396 else {
397 0 return $self->create;
398 }
399 }
400  
401 0 ▶sub find { shift->table->find(@_) }
Subroutine find: not called CC=1 SCAR=6.9
Pod undocumented
402  
403 sub load {
404 0 ▶ my $self = shift;
Subroutine load: not called CC=9 SCAR=45.0
Pod documented
405 0 my (%params) = @_;
406  
407 0 my @columns;
408  
409 0 foreach my $name ($self->columns) {
410 0 ▶ push @columns, $name if $self->meta->is_primary_key($name);
Branch: 1 branch
BranchTrueFalseTotal
if $self->meta->is_primary_key($name) 0 0 0
411 }
412  
413 0 ▶ if (!@columns) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@columns) 0 0 0
414 0 foreach my $name ($self->columns) {
415 0 ▶ push @columns, $name if $self->meta->is_unique_key($name);
Branch: 1 branch
BranchTrueFalseTotal
if $self->meta->is_unique_key($name) 0 0 0
416 }
417 }
418  
419 0 ▶ Carp::croak(ref($self) . ': no primary or unique keys specified')
Branch: 1 branch
BranchTrueFalseTotal
unless @columns 0 0 0
420 unless @columns;
421  
422 0 my $where = [ map { $_ => $self->{columns}->{$_} } @columns ];
423  
424 0 my $with = ObjectDB::With->new(meta => $self->meta, with => $params{with});
425  
426 0 my $columns = filter_columns([ $self->meta->get_columns ], \%params);
427  
428 my $select = SQL::Composer->build(
429 'select',
430 driver => $self->init_db->{Driver}->{Name},
431 columns => $columns,
432 from => $self->meta->table,
433 where => $where,
434 join => $with->to_joins,
435 for_update => $params{for_update},
436 0 );
437  
438 0 my ($rv, $sth) = execute($self->init_db, $select);
439  
440 0 my $rows = $sth->fetchall_arrayref;
441 0 ▶ return unless $rows && @$rows;
Branch: 1 branch
BranchTrueFalseTotal
unless $rows and @$rows 0 0 0
Condition0%0/3
$rows and @$rows
!ll&&!rl&&r
000
MC/DC0%0/2
$rows and @$rows
$rows @$rows
Truth table0%0/3
A and B
A= $rowsB= @$rows
ABresult
0X0
100
111
442  
443 0 my $row_object = $select->from_rows($rows)->[0];
444  
445 0 $self->{columns} = {};
446 0 $self->{relationships} = {};
447  
448 0 $self->set_columns(%$row_object);
449  
450 0 $self->{is_modified} = 0;
451 0 $self->{is_in_db} = 1;
452  
453 0 return $self;
454 }
455  
456 sub load_or_create {
457 0 ▶ my $self = shift;
Subroutine load_or_create: not called CC=8 SCAR=42.8
Pod undocumented
458  
459 0 my @columns;
460 0 foreach my $name ($self->columns) {
461 0 ▶ push @columns, $name if $self->meta->is_primary_key($name);
Branch: 1 branch
BranchTrueFalseTotal
if $self->meta->is_primary_key($name) 0 0 0
462 }
463  
464 0 ▶ if (!@columns) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@columns) 0 0 0
465 0 foreach my $name ($self->columns) {
466 0 ▶ push @columns, $name if $self->meta->is_unique_key($name);
Branch: 1 branch
BranchTrueFalseTotal
if $self->meta->is_unique_key($name) 0 0 0
467 }
468 }
469  
470 0 my $object;
471 0 ▶ $object = $self->load if @columns;
Branch: 1 branch
BranchTrueFalseTotal
if @columns 0 0 0
472 0 ▶ $object ||= $self->create;
Condition0%0/3
$object ||= $self->create
l!l&&r!l&&!r
000
MC/DC0%0/2
$object ||= $self->create
$object $self->create
Truth table0%0/3
A ||= B
A= $objectB= $self->create
ABresult
1X1
011
000
473  
474 0 return $object;
475 }
476  
477 sub update {
478 0 ▶ my $self = shift;
Subroutine update: not called CC=9 SCAR=45.0
Pod documented
479  
480 0 ▶ return $self unless $self->is_modified;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_modified 0 0 0
481  
482 0 my %where;
483 0 foreach my $name ($self->columns) {
484 0 ▶ $where{$name} = $self->{columns}->{$name}
Branch: 1 branch
BranchTrueFalseTotal
if $self->meta->is_primary_key($name) 0 0 0
485 if $self->meta->is_primary_key($name);
486 }
487  
488 0 ▶ if (!keys %where) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %where) 0 0 0
489 0 foreach my $name ($self->columns) {
490 0 ▶ $where{$name} = $self->{columns}->{$name}
Branch: 1 branch
BranchTrueFalseTotal
if $self->meta->is_unique_key($name) 0 0 0
491 if $self->meta->is_unique_key($name);
492 }
493 }
494  
495 0 ▶ Carp::croak(ref($self) . ': no primary or unique keys specified')
Branch: 1 branch
BranchTrueFalseTotal
unless keys %where 0 0 0
496 unless keys %where;
497  
498 0 my @columns = grep { !$self->meta->is_primary_key($_) } $self->columns;
499 0 my @values = map { $self->{columns}->{$_} } @columns;
500  
501 0 my %columns_set;
502 0 @columns_set{@columns} = @values;
503 my $sql = SQL::Composer->build(
504 'update',
505 driver => $self->init_db->{Driver}->{Name},
506 0 table => $self->meta->table,
507 values => [%columns_set],
508 where => [%where]
509 );
510  
511 0 my $rv = execute($self->init_db, $sql);
512  
513 0 ▶ Carp::croak('No rows were affected') if $rv eq '0E0';
Branch: 1 branch
BranchTrueFalseTotal
if $rv eq "0E0" 0 0 0
514  
515 0 $self->{is_modified} = 0;
516 0 $self->{is_in_db} = 1;
517  
518 0 return $self;
519 }
520  
521 sub delete : method {
522 0 ▶ my $self = shift;
Subroutine delete: not called CC=8 SCAR=42.8
Pod documented
523  
524 0 my %where;
525 0 foreach my $name ($self->columns) {
526 0 ▶ $where{$name} = $self->{columns}->{$name}
Branch: 1 branch
BranchTrueFalseTotal
if $self->meta->is_primary_key($name) 0 0 0
527 if $self->meta->is_primary_key($name);
528 }
529  
530 0 ▶ if (!keys %where) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %where) 0 0 0
531 0 foreach my $name ($self->columns) {
532 0 ▶ $where{$name} = $self->{columns}->{$name}
Branch: 1 branch
BranchTrueFalseTotal
if $self->meta->is_unique_key($name) 0 0 0
533 if $self->meta->is_unique_key($name);
534 }
535 }
536  
537 0 ▶ Carp::croak(ref($self) . ': no primary or unique keys specified')
Branch: 1 branch
BranchTrueFalseTotal
unless keys %where 0 0 0
538 unless keys %where;
539  
540 my $sql = SQL::Composer->build(
541 'delete',
542 driver => $self->init_db->{Driver}->{Name},
543 0 from => $self->meta->table,
544 where => [%where]
545 );
546  
547 0 my $rv = execute($self->init_db, $sql);
548  
549 0 ▶ Carp::croak('No rows were affected') if $rv eq '0E0';
Branch: 1 branch
BranchTrueFalseTotal
if $rv eq "0E0" 0 0 0
550  
551 0 %$self = ();
552  
553 0 return $self;
554 }
555  
556 sub to_hash {
557 0 ▶ my $self = shift;
Subroutine to_hash: not called CC=9 SCAR=45.0
Pod documented
558  
559 0 my $hash = {};
560  
561 0 foreach my $key ($self->meta->get_columns) {
562 0 ▶ if (exists $self->{columns}->{$key}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $self->{'columns'}{$key}) { } 0 0 0
elsif (exists $self->meta->get_column($key)->{'default'}) { } 0 0 0
563 0 $hash->{$key} = $self->get_column($key);
564 }
565 elsif (exists $self->meta->get_column($key)->{default}) {
566 0 $hash->{$key} = $self->get_column($key);
567 }
568 }
569  
570 0 foreach my $key (keys %{ $self->{virtual_columns} }) {
571 0 $hash->{$key} = $self->get_column($key);
572 }
573  
574 0 foreach my $name (keys %{ $self->{relationships} }) {
575 0 my $rel = $self->{relationships}->{$name};
576 0 ▶ next unless defined $rel;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rel 0 0 0
577  
578 0 ▶ Carp::croak("unknown '$name' relationship") unless $rel;
Branch: 1 branch
BranchTrueFalseTotal
unless $rel 0 0 0
579  
580 0 ▶ if (ref $rel eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rel eq 'ARRAY') { } 0 0 0
581 0 $hash->{$name} = [ map { $_->to_hash } @$rel ];
582 }
583 else {
584 0 $hash->{$name} = $rel->to_hash;
585 }
586 }
587  
588 0 return $hash;
589 }
590  
591 sub is_related_loaded {
592 0 ▶ my $self = shift;
Subroutine is_related_loaded: not called CC=1 SCAR=6.9
Pod documented
593 0 my ($name) = @_;
594  
595 0 return exists $self->{relationships}->{$name};
596 }
597  
598 sub related {
599 0 ▶ my $self = shift;
Subroutine related: not called CC=5 SCAR=34.0
Pod documented
600 0 my ($name) = shift;
601  
602 0 my $rel = $self->meta->get_relationship($name);
603  
604 0 ▶ if (!$self->{relationships}->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"relationships"}{$name}) 0 0 0
605 0 ▶ $self->{relationships}->{$name} =
Branch: 1 branch
BranchTrueFalseTotal
$rel->is_multi ? : 0 0 0
606 $rel->is_multi
607 ? [ $self->find_related($name, @_) ]
608 : $self->find_related($name, @_);
609 }
610  
611 0 my $related = $self->{relationships}->{$name};
612  
613 return
614 wantarray
615 0 ▶ ? ref $related eq 'ARRAY'
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 0 0 0
ref $related eq 'ARRAY' ? : 0 0 0
616 ? @$related
617 : ($related)
618 : $related;
619 }
620  
621 0 ▶sub find_related { shift->_do_related('find', @_) }
Subroutine find_related: not called CC=1 SCAR=6.9
Pod documented
622 0 ▶sub update_related { shift->_do_related('update', @_) }
Subroutine update_related: not called CC=1 SCAR=6.9
Pod documented
623 0 ▶sub count_related { shift->_do_related('count', @_) }
Subroutine count_related: not called CC=1 SCAR=6.9
Pod undocumented
624 0 ▶sub delete_related { shift->_do_related('delete', @_) }
Subroutine delete_related: not called CC=1 SCAR=6.9
Pod documented
625  
626 sub create_related {
627 0 ▶ my $self = shift;
Subroutine create_related: not called CC=4 SCAR=30.0
Pod documented
628 0 my $name = shift;
629  
630 0 ▶ my @related = @_ == 1 ? ref $_[0] eq 'ARRAY' ? @{ $_[0] } : ($_[0]) : ({@_});
Branch: 2 branches
BranchTrueFalseTotal
@_ == 1 ? : 0 0 0
ref $_[0] eq 'ARRAY' ? : 0 0 0
631  
632 0 my @rv = $self->_do_related('create', $name, \@related);
633 0 ▶ return @rv == 1 ? $rv[0] : @rv;
Branch: 1 branch
BranchTrueFalseTotal
@rv == 1 ? : 0 0 0
634 }
635  
636 sub _do_related {
637 0 ▶ my $self = shift;
Subroutine _do_related: not called CC=2 SCAR=17.9
638 0 my $action = shift;
639 0 my $name = shift;
640  
641 0 ▶ Carp::croak('Relationship name is required') unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
642  
643 0 my $related = $self->_build_related($name);
644  
645 0 my $method = "$action\_related";
646 0 return $related->$method($self, @_);
647 }
648  
649 sub _build_related {
650 0 ▶ my $self = shift;
Subroutine _build_related: not called CC=1 SCAR=6.9
651 0 my ($name) = @_;
652  
653 0 my $meta = $self->meta->get_relationship($name);
654  
655 0 return ObjectDB::RelatedFactory->new->build($meta->type, meta => $meta);
656 }
657  
658 sub _is_empty_hash_ref {
659 0 ▶ my $self = shift;
Subroutine _is_empty_hash_ref: not called CC=8 SCAR=42.8
660 0 my ($hash_ref) = @_;
661  
662 0 ▶ return 1 unless defined $hash_ref && ref $hash_ref eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hash_ref and ref $hash_ref eq "HASH" 0 0 0
Condition0%0/3
defined $hash_ref and ref $hash_ref eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $hash_ref and ref $hash_ref eq "HASH"
defined $hash_ref ref $hash_ref eq "HASH"
Truth table0%0/3
A and B
A= defined $hash_refB= ref $hash_ref eq "HASH"
ABresult
0X0
100
111
663  
664 0 foreach my $key (keys %$hash_ref) {
665 0 ▶ if (defined $hash_ref->{$key} && $hash_ref->{$key} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hash_ref->{$key} and $hash_ref->{$key} ne "") 0 0 0
Condition0%0/3
defined $hash_ref->{$key} and $hash_ref->{$key} ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $hash_ref->{$key} and $hash_ref->{$key} ne ""
defined $hash_ref->{$key} $hash_ref->{$key} ne ""
Truth table0%0/3
A and B
A= defined $hash_ref->{$key}B= $hash_ref->{$key} ne ""
ABresult
0X0
100
111
666 0 ▶ if (ref($hash_ref->{$key}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $hash_ref->{$key} eq 'HASH') { } 0 0 0
667 0 my $is_empty = $self->_is_empty_hash_ref($hash_ref->{$key});
668 0 ▶ return 0 unless $is_empty;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_empty 0 0 0
669 }
670 else {
671 0 return 0;
672 }
673 }
674 }
675  
676 0 return 1;
677 }
678  
679 5 1;
680 __END__
↑ Summary blib/lib/ObjectDB/Table.pm »