Easy/Record.pm

filter: Statementstmt 75.4% 123 / 163 Branchbran 46.7% 29 / 62 Conditioncond 35.7% 20 / 56 MC/DCmcdc 6.2% 2 / 32 Subroutinesub 75.0% 15 / 20 Podpod 0.0% 0 / 13 totaltotal 54.6% 189 / 346 CC 57 SCAR 30.8
CC
57
Cov
61%
apply_request_params
110.0
validation_errors
30.0
create
11.2
CRAP 21.7
« Easy/Record/Collection.pm ↑ Summary Easy/DBH.pm »
1 package DBI::Easy::Record;
2 # $Id: Record.pm,v 1.6 2009/07/20 18:00:08 apla Exp $
3  
4 6 ▶use Class::Easy;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use DBI::Easy;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use base qw(DBI::Easy);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 our $wrapper = 1;
10  
11 61 ▶sub _init {
Subroutine _init: called CC=4 SCAR=14.0
12 61 my $class = shift;
13
14 61 my $params;
15
16 61 ▶ if (@_ == 1 && ref $_[0] && ref $_[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] and ref $_[0] eq 'HASH') { } 18 43 61
Condition66%4/6
@_ == 1 and ref $_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
43018
@_ == 1 and ref $_[0]
!ll&&!rl&&r
43018
MC/DC33%1/3
@_ == 1 and ref $_[0] and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] ref $_[0] eq "HASH"
Truth table50%2/4
A and B and C
A= @_ == 1B= ref $_[0]C= ref $_[0] eq "HASH"
ABCresult
0XX0
10X0
1100
1111
17 # old school
18 18 $params->{field_values} = $_[0];
19 } else {
20 43 $params = {@_};
21 }
22
23 61 return $params;
24 }
25  
26 6 ▶sub save {
Subroutine save: called CC=6 SCAR=18.7
Pod undocumented
27 6 my $self = shift;
28
29 6 my $result;
30
31 6 ▶ return unless $self->field_values;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->field_values 0 6 6
32
33 6 my $pk_column = $self->_pk_column_;
34
35 6 ▶ if ($pk_column and $pk_column ne '' and defined $self->column_values and $self->column_values->{$pk_column}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pk_column and $pk_column ne '' and defined $self->column_values and $self->column_values->{$pk_column}) { } 2 4 6
Condition55%5/9
$pk_column and $pk_column ne "" and defined $self->column_values and $self->column_values->{$pk_column}
!ll&&!rl&&r
402
$pk_column and $pk_column ne "" and defined $self->column_values
!ll&&!rl&&r
042
$pk_column and $pk_column ne ""
!ll&&!rl&&r
006
MC/DC25%1/4
$pk_column and $pk_column ne "" and defined $self->column_values and $self->column_values->{$pk_column}
$pk_column $pk_column ne "" defined $self->column_values $self->column_values->{$pk_column}
Truth table40%2/5
A and B and C and D
A= $pk_columnB= $pk_column ne ""C= defined $self->column_valuesD= $self->column_values->{$pk_column}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
36 # try to update
37 2 $result = $self->update_by_pk;
38 } else {
39 4 $result = $self->create;
40 }
41
42 6 return $result;
43 }
44  
45 0 ▶sub fetched {
Subroutine fetched: not called CC=2 SCAR=17.9
Pod undocumented
46 0 ▶ return 1 if defined shift->{field_values};
Branch: 1 branch
BranchTrueFalseTotal
if defined +(shift())->{"field_values"} 0 0 0
47 }
48  
49 # update by pk
50 3 ▶sub update_by_pk {
Subroutine update_by_pk: called CC=8 SCAR=23.6
Pod undocumented
51 3 my $self = shift;
52 3 my %params = @_;
53
54 # there we make decision:
55 # a) programmmer can provide update values
56 # we simply reject field values
57 # b) field_values already contains update values
58
59 3 my $column_values;
60
61 3 ▶ if (exists $params{set} and ref $params{set} and ref $params{set} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{'set'} and ref $params{'set'} and ref $params{'set'} eq 'HASH') { } 0 3 3
Condition33%2/6
exists $params{"set"} and ref $params{"set"} and ref $params{"set"} eq "HASH"
!ll&&!rl&&r
300
exists $params{"set"} and ref $params{"set"}
!ll&&!rl&&r
300
MC/DC0%0/3
exists $params{"set"} and ref $params{"set"} and ref $params{"set"} eq "HASH"
exists $params{"set"} ref $params{"set"} ref $params{"set"} eq "HASH"
Truth table25%1/4
A and B and C
A= exists $params{"set"}B= ref $params{"set"}C= ref $params{"set"} eq "HASH"
ABCresult
0XX0
10X0
1100
1111
62 0 $column_values = $self->fields_to_columns ($params{set});
63 } else {
64 3 $column_values = $self->fields_to_columns;
65 }
66
67 3 my ($sql, $bind) = $self->sql_update_by_pk (%params);
68
69 3 ▶ return unless defined $sql;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sql 0 3 3
70
71 3 ▶ debug "sql: $sql => " . (defined $bind and scalar @$bind ? join ', ', @$bind : '[]');
Branch: 1 branch
BranchTrueFalseTotal
scalar @$bind ? : 3 0 3
Condition33%1/3
defined $bind && (scalar @$bind ? join(', ', @$bind) : '[]')
!ll&&!rl&&r
003
MC/DC0%0/2
defined $bind && (scalar @$bind ? join(', ', @$bind) : '[]')
defined $bind (scalar @$bind ? join(', ', @$bind) : '[]')
Truth table33%1/3
A && B
A= defined $bindB= (scalar @$bind ? join(', ', @$bind) : '[]')
ABresult
0X0
100
111
72
73 3 my $result = $self->no_fetch ($sql, $bind);
74
75 3 foreach my $k (keys %$column_values) {
76 3 $self->column_values->{$k} = $column_values->{$k};
77 }
78  
79 3 delete $self->{field_values};
80
81 3 return $result
82 }
83  
84 # delete by pk
85 1 ▶sub delete_by_pk {
Subroutine delete_by_pk: called CC=3 SCAR=12.0
Pod undocumented
86 1 my $self = shift;
87
88 1 my ($sql, $bind) = $self->sql_delete_by_pk (@_);
89
90 1 ▶ debug "sql: $sql => " . (defined $bind and scalar @$bind ? join ', ', @$bind : '[]');
Branch: 1 branch
BranchTrueFalseTotal
scalar @$bind ? : 1 0 1
Condition33%1/3
defined $bind && (scalar @$bind ? join(', ', @$bind) : '[]')
!ll&&!rl&&r
001
MC/DC0%0/2
defined $bind && (scalar @$bind ? join(', ', @$bind) : '[]')
defined $bind (scalar @$bind ? join(', ', @$bind) : '[]')
Truth table33%1/3
A && B
A= defined $bindB= (scalar @$bind ? join(', ', @$bind) : '[]')
ABresult
0X0
100
111
91
92 1 return $self->no_fetch ($sql, $bind);
93
94 }
95  
96 16 ▶sub create {
Subroutine create: called CC=9 SCAR=24.1
Pod undocumented
97 16 my $self = shift;
98
99 16 my $t = timer ('fields to columns translation');
100
101 16 my $column_values = $self->fields_to_columns;
102
103 16 $t->lap ('sql generation');
104
105 16 my ($sql, $bind) = $self->sql_insert ($column_values);
106
107 16 ▶ debug "sql: $sql => " . (defined $bind and scalar @$bind ? join ', ', @$bind : '[]');
Branch: 1 branch
BranchTrueFalseTotal
scalar @$bind ? : 16 0 16
Condition33%1/3
defined $bind && (scalar @$bind ? join(', ', @$bind) : '[]')
!ll&&!rl&&r
0016
MC/DC0%0/2
defined $bind && (scalar @$bind ? join(', ', @$bind) : '[]')
defined $bind (scalar @$bind ? join(', ', @$bind) : '[]')
Truth table33%1/3
A && B
A= defined $bindB= (scalar @$bind ? join(', ', @$bind) : '[]')
ABresult
0X0
100
111
108
109 16 $t->lap ('insert');
110
111 # sequence is available for oracle insertions
112 16 my $pk_col = $self->_pk_column_;
113 16 my $seq;
114
115 16 ▶ if ($pk_col and exists $column_values->{"_$pk_col"} and $column_values->{"_$pk_col"} =~ /^\s*(\w+)\.nextval\s*$/si) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pk_col and exists $$column_values{"_$pk_col"} and $$column_values{"_$pk_col"} =~ /^\s*(\w+)\.nextval\s*$/is) 0 16 16
Condition33%2/6
$pk_col and exists $$column_values{"_$pk_col"} and $$column_values{"_$pk_col"} =~ /^\s*(\w+)\.nextval\s*$/is
!ll&&!rl&&r
1600
$pk_col and exists $$column_values{"_$pk_col"}
!ll&&!rl&&r
0160
MC/DC0%0/3
$pk_col and exists $$column_values{"_$pk_col"} and $$column_values{"_$pk_col"} =~ /^\s*(\w+)\.nextval\s*$/is
$pk_col exists $$column_values{"_$pk_col"} $$column_values{"_$pk_col"} =~ /^\s*(\w+)\.nextval\s*$/is
Truth table25%1/4
A and B and C
A= $pk_colB= exists $$column_values{"_$pk_col"}C= $$column_values{"_$pk_col"} =~ /^\s*(\w+)\.nextval\s*$/is
ABCresult
0XX0
10X0
1100
1111
116 0 $seq = $1;
117 }
118  
119 16 my $id = $self->no_fetch ($sql, $bind, $seq);
120
121 16 $t->lap ('perl wrapper for id');
122
123 16 ▶ return unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 16 16
124
125 16 delete $self->{field_values};
126 16 $self->{column_values} = $column_values;
127
128 16 ▶ return $id if $id =~ /^0E\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $id =~ /^0E\d+$/ 0 16 16
129
130 16 ▶ $self->{column_values}->{$pk_col} = $id
Branch: 1 branch
BranchTrueFalseTotal
if $pk_col 16 0 16
131 if $pk_col; # sometimes no primary keys in table
132  
133 16 $t->end;
134
135 16 $t->total;
136
137 16 return 1;
138 }
139  
140 7 ▶sub fetch {
Subroutine fetch: called CC=2 SCAR=6.9
Pod undocumented
141 7 my $class = shift;
142 7 my $params = shift;
143 7 my $cols = shift;
144
145 7 my $prefixed_params = $class->fields_to_columns ($params);
146
147 7 my ($statement, $bind) = $class->sql_select (where => $prefixed_params, fieldset => $cols);
148
149 7 debug "sql: '$statement'";
150
151 7 my $record = $class->fetch_row ($statement, $bind);
152
153 return
154 7 ▶ unless ref $record;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $record 3 4 7
155
156 4 return $class->new (
157 column_values => $record
158 );
159
160 }
161  
162 2 ▶sub fetch_or_create {
Subroutine fetch_or_create: called CC=2 SCAR=7.0
Pod undocumented
163 2 my $class = shift;
164 2 my $params = shift;
165
166 2 my $record = $class->fetch ($params);
167
168 2 ▶ unless (defined $record) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $record) 2 0 2
169 2 $record = $class->new ($params);
170 2 $record->create;
171 }
172
173 2 return $record;
174 }
175  
176 6 ▶sub hash {
Subroutine hash: called CC=7 SCAR=19.7
Pod undocumented
177 6 my $self = shift;
178
179 6 my $result = {};
180
181 # we need to return everything we got from db + changes
182 1 my $result = {map {$_ => $self->{field_values}->{$_}}
183 30 grep {defined $self->{field_values}->{$_}}
184 6 keys %{$self->fields}};
185
186 6 foreach my $col_name (keys %{$self->{column_values}}) {
187 21 my $col_meta = $self->columns->{$col_name};
188 21 my $col_value = $self->{column_values}->{$col_name};
189
190 21 ▶ next unless defined $col_value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $col_value 3 18 21
191
192 $result->{$col_name} = $col_value, next
193 18 ▶ if ! defined $col_meta and ! exists $result->{$col_meta->{field_name}};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $col_meta or exists $$result{$col_meta->{"field_name"}} 0 18 18
Condition33%1/3
defined $col_meta or exists $$result{$col_meta->{"field_name"}}
l!l&&r!l&&!r
1800
MC/DC0%0/2
defined $col_meta or exists $$result{$col_meta->{"field_name"}}
defined $col_meta exists $$result{$col_meta->{"field_name"}}
Truth table33%1/3
A or B
A= defined $col_metaB= exists $$result{$col_meta->{"field_name"}}
ABresult
1X1
011
000
194
195 $result->{$col_meta->{field_name}} = (
196 exists $col_meta->{decoder} ? $col_meta->{decoder}->($self): $col_value
197 18 ▶ ) if ! exists $result->{$col_meta->{field_name}};
Branch: 2 branches
BranchTrueFalseTotal
unless exists $$result{$col_meta->{"field_name"}} 17 1 18
exists $col_meta->{'decoder'} ? : 0 17 17
198 }
199
200 6 return {%{$self->{embed}}, %$result};
201 }
202  
203 6 *TO_JSON = \&hash;
204 6 *TO_XML = \&hash;
205  
206 1 ▶sub embed {
Subroutine embed: called CC=4 SCAR=15.9
Pod undocumented
207 1 my $self = shift;
208 1 my $what = shift;
209
210 1 ▶ if (@_ == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1) { } 1 0 1
elsif (@_ > 1) { } 0 0 0
211 die "cannot embed '$what' into ". ref $self
212 1 ▶ if exists $self->fields->{$what};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->fields->{$what} 0 1 1
213 1 $self->{embed}->{$what} = $_[0];
214 } elsif (@_ > 1) {
215 0 die "too many parameters";
216 }
217
218 1 return $self->{embed}->{$what};
219
220 }
221  
222 # example usage: $domain->is_related_to ('contacts', {
223 # isa => 'My::Entity::Contact::Collection',
224 # relation => [domain_key => domain_key_in_contacts], # optional, by default natural join
225 # many_to_many => 'My::Entity::Domain_Contact::Collection',
226 # filter => {}
227 # });
228  
229 # памятка использования is_related_to
230 #$ref->is_related_to (
231 # ‘entity’, # название сущности, доступной у объекта
232 # # после вызова этого метода
233 # ‘entity_pack’, # имя класса, корое используется в
234 # # качестве фабрики для сущностей
235 # filter => {}, # хэш фильтров для ограничения выборки
236 # relation => ['key_in_ref', 'key_in_entity'] # отношение
237 #);
238  
239 4 ▶sub is_related_to {
Subroutine is_related_to: called CC=7 SCAR=20.3
Pod undocumented
240 4 my $ref = shift;
241 4 my $entity = shift;
242 4 my $pack = shift;
243 4 my %params = @_;
244  
245 4 my $t = timer ('all');
246
247 4 debug "$entity";
248
249 4 ▶ my $filter = $params{filter} || {};
Condition50%1/2
$params{'filter'} || {}
l!l
04
MC/DC0%0/1
$params{'filter'} || {}
$params{'filter'}
Truth table50%1/2
A
A= $params{'filter'}
Aresult
11
00
250
251 $params{relation} = []
252 4 ▶ unless defined $params{relation};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"relation"} 4 0 4
253
254 4 ▶ my $column = $params{relation}->[0] || $ref->_pk_;
Condition33%1/3
$params{'relation'}[0] || $ref->_pk_
l!l&&r!l&&!r
040
MC/DC0%0/2
$params{'relation'}[0] || $ref->_pk_
$params{'relation'}[0] $ref->_pk_
Truth table33%1/3
A || B
A= $params{'relation'}[0]B= $ref->_pk_
ABresult
1X1
011
000
255 4 ▶ my $ref_column = $params{relation}->[1] || ($ref->column_prefix
Branch: 1 branch
BranchTrueFalseTotal
$ref->column_prefix ? : 4 0 4
Condition33%1/3
$params{'relation'}[1] || ($ref->column_prefix ? $ref->column_prefix : $ref->table_name . '_') . $column
l!l&&r!l&&!r
040
MC/DC0%0/2
$params{'relation'}[1] || ($ref->column_prefix ? $ref->column_prefix : $ref->table_name . '_') . $column
$params{'relation'}[1] ($ref->column_prefix ? $ref->column_prefix : $ref->table_name . '_') . $column
Truth table33%1/3
A || B
A= $params{'relation'}[1]B= ($ref->column_prefix ? $ref->column_prefix : $ref->table_name . '_') . $column
ABresult
1X1
011
000
256 ? $ref->column_prefix
257 : $ref->table_name . '_'
258 ) . $column;
259
260 4 try_to_use ($pack);
261
262 # warn "column $column from table ".$ref->table_name." is related to column $ref_column from table ". $pack->table_name;
263
264 4 my $sub;
265 my $ref_sub;
266
267
268 4 ▶ if ($pack->is_collection) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pack->is_collection) { } 2 2 4
269 4 ▶ $sub = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
270 4 my $self = shift;
271
272 4 return $pack->new ({filter => {%$filter, $ref_column => $self->$column}});
273 2 };
274 0 ▶ $ref_sub = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
275 0 my $self = shift;
276
277 0 return $pack->new ({filter => {%$filter, $ref_column => $self->$column}});
278 2 };
279 } else {
280
281 2 ▶ $sub = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
282 2 my $self = shift;
283
284 2 return $pack->fetch_or_create ({%$filter, $ref_column => $self->$column});
285 2 };
286 }
287
288 4 make_accessor ($ref, $entity, default => $sub);
289
290 4 $t->end;
291 }
292  
293 0 ▶sub validation_errors {
Subroutine validation_errors: not called CC=5 SCAR=34.0
Pod undocumented
294 0 my $self = shift;
295
296 0 my $errors = {};
297
298 0 debug "field validation";
299
300 0 foreach my $field (keys %{$self->fields}) {
301 # first, we need to validate throught db schema
302 # TODO
303 0 if (0) {
304 $errors->{$field} = 'schema-validation-error';
305 }
306 # second, we validate throught custom validators
307 0 my $method = "${field}_valid";
308 0 ▶ if ($self->can ($method)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($method)) 0 0 0
309 0 debug "custom validation for $field";
310 0 my $error_code = $self->$method;
311 0 ▶ if ($error_code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error_code) 0 0 0
312 0 $errors->{$field} = $error_code;
313 0 debug "failed: $error_code";
314 }
315 }
316 }
317
318 0 ▶ return unless scalar keys %$errors;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar keys %$errors 0 0 0
319
320 0 return $errors;
321 }
322  
323 0 ▶sub dump_fields_exclude {
Subroutine dump_fields_exclude: not called CC=1 SCAR=0.0
Pod undocumented
324 #TODO
325 }
326  
327 0 ▶sub apply_request_params {
Subroutine apply_request_params: not called CC=10 SCAR=47.0
Pod undocumented
328 0 my $self = shift;
329 0 my $request = shift;
330
331 0 foreach my $field (keys %{$self->fields}) {
332 # TODO: check for primary key. we don't want primary key value here
333 0 my $value = $request->param ($field);
334 0 ▶ next if !defined $value or $value eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or $value eq "" 0 0 0
Condition0%0/3
not defined $value or $value eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $value or $value eq ""
not defined $value $value eq ""
Truth table0%0/3
not(A) or B
A= not defined $valueB= $value eq ""
ABresult
1X1
011
000
335 0 $self->{$field} = $value;
336 }
337  
338 0 my $values = {};
339
340 0 foreach my $field (keys %{$self->columns}) {
341 # TODO: check for primary key. we don't want primary key value here
342 0 my $value = $request->param ($field);
343 0 ▶ next if !defined $value or $value eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or $value eq "" 0 0 0
Condition0%0/3
not defined $value or $value eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $value or $value eq ""
not defined $value $value eq ""
Truth table0%0/3
not(A) or B
A= not defined $valueB= $value eq ""
ABresult
1X1
011
000
344 0 $values->{$field} = $value;
345 }
346
347 0 my $fields = $self->columns_to_fields ($values);
348  
349 0 foreach my $field (keys %{$fields}) {
350 0 my $value = $fields->{$field};
351 0 ▶ next if !defined $value or $value eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or $value eq "" 0 0 0
Condition0%0/3
not defined $value or $value eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $value or $value eq ""
not defined $value $value eq ""
Truth table0%0/3
not(A) or B
A= not defined $valueB= $value eq ""
ABresult
1X1
011
000
352 0 $self->{$field} = $value;
353 }
354 }
355  
356 6 1;
« Easy/Record/Collection.pm ↑ Summary Easy/DBH.pm »