lib/DBIx/EAV/ResultSet.pm

filter: Statementstmt 87.8% 130 / 148 Branchbran 81.0% 47 / 58 Conditioncond 85.7% 12 / 14 MC/DCmcdc 88.8% 8 / 9 Subroutinesub 85.2% 29 / 34 Podpod 36.8% 7 / 19 totaltotal 82.6% 233 / 282 SCAR 37.4
CC
38
Cov
86%
delete
14.6
_merge_options
8.0
update
6.0
CRAP 42.0
« lib/DBIx/EAV.pm ↑ Summary lib/DBIx/EAV/Schema.pm »
1 package DBIx::EAV::ResultSet;
2  
3 10 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use DBIx::EAV::Entity;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use DBIx::EAV::Cursor;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use Carp qw/ croak confess /;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 use overload
9 10 '0+' => "_to_num",
10 'bool' => "_to_bool",
11 10 ▶ fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 10 my $sql = SQL::Abstract->new;
14  
15 10 has 'eav', is => 'ro', required => 1;
16 10 has 'type', is => 'ro', required => 1;
17 118 ▶has '_query', is => 'rw', default => sub { [] }, init_arg => 'query';
Subroutine __ANON__: called CC=1 SCAR=0.0
18 118 ▶has '_options', is => 'rw', default => sub { {} }, init_arg => 'options';
Subroutine __ANON__: called CC=1 SCAR=0.0
19 10 has 'cursor', is => 'rw',
20 lazy => 1,
21 init_arg => undef,
22 predicate => '_has_cursor',
23 clearer => '_clear_cursor',
24 builder => '_build_cursor';
25  
26 has 'entity_class', is => 'ro', init_arg => undef, lazy => 1, default => sub {
27 103 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
28 103 ▶ $self->eav->_resolve_entity_class($self->type->name) || 'DBIx::EAV::Entity';
Branch: 1 branch
BranchTrueFalseTotal
$self->eav->_resolve_entity_class($self->type->name) or "DBIx::EAV::Entity" 102 1 103
29 10 };
30  
31 3 ▶sub _to_num { $_[0]->count }
Subroutine _to_num: called CC=1 SCAR=0.0
32  
33 0 ▶sub _to_bool { 1 }
Subroutine _to_bool: not called CC=1 SCAR=6.9
34  
35  
36 sub _build_cursor {
37 99 ▶ my $self = shift;
Subroutine _build_cursor: called CC=1 SCAR=0.0
38  
39 99 DBIx::EAV::Cursor->new(
40 eav => $self->eav,
41 type => $self->type,
42 query => $self->_query,
43 options => $self->_options,
44 );
45 }
46  
47  
48 sub new_entity {
49 92 ▶ my ($self, $data) = @_;
Subroutine new_entity: called CC=2 SCAR=7.0
Pod documented
50 92 my $entity = $self->entity_class->new( eav => $self->eav, type => $self->type );
51 92 ▶ $entity->set($data) if ref $data eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $data eq "HASH" 92 0 92
52 92 $entity;
53 }
54  
55  
56 sub inflate_entity {
57 111 ▶ my ($self, $data) = @_;
Subroutine inflate_entity: called CC=3 SCAR=11.0
Pod undocumented
58 111 my $type = $self->type;
59 $type = $self->eav->type_by_id($data->{entity_type_id})
60 111 ▶ if $data->{entity_type_id} && $data->{entity_type_id} != $type->id;
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"entity_type_id"} and $data->{"entity_type_id"} != $type->id 9 102 111
Condition100%3/3
$data->{"entity_type_id"} and $data->{"entity_type_id"} != $type->id
!ll&&!rl&&r
3999
MC/DC100%2/2
$data->{"entity_type_id"} and $data->{"entity_type_id"} != $type->id
$data->{"entity_type_id"} $data->{"entity_type_id"} != $type->id
Truth table100%3/3
A and B
A= $data->{"entity_type_id"}B= $data->{"entity_type_id"} != $type->id
ABresult
0X0
100
111
61  
62 111 my $entity = $self->entity_class->new( eav => $self->eav, type => $type, raw => $data );
63 111 $entity->load_attributes;
64 111 $entity;
65 }
66  
67  
68 {
69 10 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 10 *create = \&insert;
71 }
72  
73 sub insert {
74 87 ▶ my ($self, $data) = @_;
Subroutine insert: called CC=1 SCAR=0.0
Pod documented
75 87 $self->new_entity($data)->save;
76 }
77  
78  
79 sub populate {
80 16 ▶ my ($self, $data) = @_;
Subroutine populate: called CC=4 SCAR=13.9
Pod documented
81 16 ▶ die 'Call populate(\@items)' unless ref $data eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "ARRAY" 0 16 16
82  
83 16 my @result;
84 16 foreach my $item (@$data) {
85 46 push @result, $self->insert($item);
86 }
87  
88 16 ▶ return wantarray ? @result : \@result;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 14 16
89 }
90  
91  
92 sub update {
93 0 ▶ my ($self, $data, $where) = @_;
Subroutine update: not called CC=2 SCAR=17.9
Pod undocumented
94  
95 0 ▶ $where //= {};
Condition0%0/2
$where //= {}
l!l
00
MC/DC0%0/1
$where //= {}
$where
Truth table0%0/2
A
A= $where
Aresult
11
00
96 0 $where->{entity_type_id} = $self->type->id;
97  
98 # do a direct update for static attributes
99  
100 }
101  
102  
103 sub delete {
104 3 ▶ my $self = shift;
Subroutine delete: called CC=5 SCAR=26.8
Pod documented
105 3 my $eav = $self->eav;
106 3 my $type = $self->type;
107 3 my $entities_table = $eav->table('entities');
108  
109 # Call delete_all for SQLite since it doesn't
110 # support delete with joins.
111 # Better solution welcome.
112 3 ▶ return $self->delete_all if
Branch: 1 branch
BranchTrueFalseTotal
if $self->eav->schema->db_driver_name eq "SQLite" 3 0 3
113 $self->eav->schema->db_driver_name eq 'SQLite';
114  
115 0 ▶ unless ($eav->schema->database_cascade_delete) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($eav->schema->database_cascade_delete) 0 0 0
116  
117 # delete links by relationship id
118 0 my @ids = map { $_->{id} } $type->relationships;
119  
120 0 $eav->table('entity_relationships')->delete(
121 {
122 relationship_id => \@ids,
123 $entities_table->name.'.entity_type_id' => $type->id
124 },
125 { join => { $entities_table->name => [{ 'me.left_entity_id' => 'their.id' }, { 'me.right_entity_id' => 'their.id' }] } }
126 );
127  
128 # delete attributes:
129 # - group attrs by data type so only one DELETE command is sent per data type
130 # - restrict by entity_type_id so we dont delete parent/sibiling/child data
131 0 my %types;
132 0 push @{ $types{$_->{data_type}} }, $_->{id}
133 0 for $type->attributes(no_static => 1);
134  
135 0 ▶ while (my ($data_type, $ids) = each %types) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %types) 0 0 0
136  
137 0 my $value_table = $eav->table('value_'.$data_type);
138 0 $value_table->delete(
139 {
140 attribute_id => $ids,
141 $entities_table->name.'.entity_type_id' => $type->id
142 },
143 { join => { $entities_table->name => { 'me.entity_id' => 'their.id' } } }
144 );
145 }
146 }
147  
148 0 $entities_table->delete({ entity_type_id => $type->id });
149 }
150  
151  
152 sub delete_all {
153 6 ▶ my $self = shift;
Subroutine delete_all: called CC=3 SCAR=11.0
Pod documented
154  
155 6 ▶ my $rs = scalar @_ > 0 ? $self->search_rs(@_) : $self;
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ > 0 ? : 0 6 6
156 6 my $i = 0;
157  
158 6 ▶ while (my $entity = $rs->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $entity = $rs->next) 14 6 20
159 14 $entity->delete;
160 14 $i++;
161 }
162  
163 6 $i;
164 }
165  
166  
167 sub find {
168 11 ▶ my ($self, $criteria, $options) = @_;
Subroutine find: called CC=5 SCAR=16.1
Pod undocumented
169  
170 11 ▶ croak "Missing find() criteria."
Branch: 1 branch
BranchTrueFalseTotal
unless defined $criteria 0 11 11
171 unless defined $criteria;
172  
173 # simple id search
174 11 ▶ return $self->search_rs({ id => $criteria }, $options)->next
Branch: 1 branch
BranchTrueFalseTotal
unless ref $criteria 1 10 11
175 unless ref $criteria;
176  
177 10 my $rs = $self->search_rs($criteria, $options);
178 10 my $result = $rs->next;
179  
180 # criteria is a search query, die if this query returns multiple items
181 10 ▶ croak "find() returned more than one entity. If this is what you want, use search or search_rs."
Branch: 1 branch
BranchTrueFalseTotal
if defined $result and defined $rs->cursor->next 1 9 10
Condition100%3/3
defined $result and defined $rs->cursor->next
!ll&&!rl&&r
181
MC/DC100%2/2
defined $result and defined $rs->cursor->next
defined $result defined $rs->cursor->next
Truth table100%3/3
A and B
A= defined $resultB= defined $rs->cursor->next
ABresult
0X0
100
111
182 if defined $result && defined $rs->cursor->next;
183  
184 9 $result;
185 }
186  
187  
188 sub search {
189 66 ▶ my ($self, $query, $options) = @_;
Subroutine search: called CC=2 SCAR=6.9
Pod undocumented
190  
191 66 my $rs = $self->search_rs($query, $options);
192  
193 66 ▶ return wantarray ? $rs->all : $rs;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 64 66
194 }
195  
196 sub search_rs {
197 78 ▶ my ($self, $query, $options) = @_;
Subroutine search_rs: called CC=2 SCAR=6.9
Pod undocumented
198  
199 # simple combine queries using AND
200 78 my @new_query = @{ $self->_query };
201 78 ▶ push @new_query, $query if $query;
Branch: 1 branch
BranchTrueFalseTotal
if $query 64 14 78
202  
203 # merge options
204 78 my $merged_options = $self->_merge_options($options);
205  
206 78 (ref $self)->new(
207 eav => $self->eav,
208 type => $self->type,
209 query => \@new_query,
210 options => $merged_options
211 );
212 }
213  
214  
215 sub _merge_options {
216 78 ▶ my ($self, $options) = @_;
Subroutine _merge_options: called CC=8 SCAR=20.8
217  
218 78 my %merged = %{ $self->_options };
219  
220 78 ▶ return \%merged
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options 59 19 78
221 unless defined $options;
222  
223 19 ▶ confess "WTF" if $options eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $options eq "" 0 19 19
224  
225 19 foreach my $opt (keys %$options) {
226  
227 # doesnt even exist, just copy
228 39 ▶ if (not exists $merged{$opt}) {
Branch: 3 branches
BranchTrueFalseTotal
if (not exists $merged{$opt}) { } 36 3 39
elsif ($opt eq 'having') { } 1 2 3
elsif (ref $merged{$opt} eq 'ARRAY') { } 1 1 2
229 36 $merged{$opt} = $options->{$opt};
230 }
231  
232 # having: combine queries using AND
233 elsif ($opt eq 'having') {
234  
235 1 $merged{$opt} = [$merged{$opt}, $options->{$opt}];
236 }
237  
238 # merge array
239 elsif (ref $merged{$opt} eq 'ARRAY') {
240  
241 $merged{$opt} = [
242 1 ▶ @{$merged{$opt}},
Branch: 1 branch
BranchTrueFalseTotal
ref $options->{$opt} eq 'ARRAY' ? : 1 0 1
243 1 ref $options->{$opt} eq 'ARRAY' ? @{$options->{$opt}} : $options->{$opt}
244 ];
245 }
246  
247 else {
248 1 $merged{$opt} = $options->{$opt};
249 }
250 }
251  
252 19 \%merged;
253 }
254  
255  
256 sub count {
257 17 ▶ my $self = shift;
Subroutine count: called CC=6 SCAR=17.9
Pod documented
258 17 ▶ return $self->search(@_)->count if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 3 14 17
259  
260 # from DBIx::Class::ResultSet::count()
261 # this is a little optimization - it is faster to do the limit
262 # adjustments in software, instead of a subquery
263 14 my $options = $self->_options;
264 14 my ($limit, $offset) = @$options{qw/ limit offset /};
265  
266 14 my $count = $self->_count_rs($options)->cursor->next->{count};
267  
268 14 ▶ $count -= $offset if $offset;
Branch: 1 branch
BranchTrueFalseTotal
if $offset 2 12 14
269 14 ▶ $count = 0 if $count < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $count < 0 1 13 14
270 14 ▶ $count = $limit if $limit && $count > $limit;
Branch: 1 branch
BranchTrueFalseTotal
if $limit and $count > $limit 1 13 14
Condition100%3/3
$limit and $count > $limit
!ll&&!rl&&r
1121
MC/DC100%2/2
$limit and $count > $limit
$limit $count > $limit
Truth table100%3/3
A and B
A= $limitB= $count > $limit
ABresult
0X0
100
111
271  
272 14 $count;
273 }
274  
275  
276 sub _count_rs {
277 14 ▶ my ($self, $options) = @_;
Subroutine _count_rs: called CC=3 SCAR=11.0
278 14 my %tmp_options = ( %$options, select => [\'COUNT(*) AS count'] );
279  
280 # count using subselect if needed
281 $tmp_options{from} = $self->as_query
282 14 ▶ if $options->{group_by} || $options->{distinct};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"group_by"} or $options->{"distinct"} 3 11 14
Condition100%3/3
$options->{"group_by"} or $options->{"distinct"}
l!l&&r!l&&!r
1211
MC/DC100%2/2
$options->{"group_by"} or $options->{"distinct"}
$options->{"group_by"} $options->{"distinct"}
Truth table100%3/3
A or B
A= $options->{"group_by"}B= $options->{"distinct"}
ABresult
1X1
011
000
283  
284 14 delete @tmp_options{qw/ limit offset order_by group_by distinct /};
285  
286 (ref $self)->new(
287 eav => $self->eav,
288 type => $self->type,
289 14 query => [@{ $self->_query }],
290 options => \%tmp_options
291 );
292 }
293  
294  
295 sub as_query {
296 4 ▶ my $self = shift;
Subroutine as_query: called CC=1 SCAR=0.0
Pod undocumented
297 4 $self->cursor->as_query;
298 }
299  
300  
301 sub reset {
302 54 ▶ my $self = shift;
Subroutine reset: called CC=1 SCAR=0.0
Pod undocumented
303 54 $self->_clear_cursor;
304 54 $self;
305 }
306  
307 sub first {
308 3 ▶ $_[0]->reset->next;
Subroutine first: called CC=1 SCAR=0.0
Pod undocumented
309 }
310  
311 sub next {
312 165 ▶ my $self = shift;
Subroutine next: called CC=2 SCAR=6.9
Pod undocumented
313  
314 # fetch next
315 165 my $entity_row = $self->cursor->next;
316 165 ▶ return unless defined $entity_row;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $entity_row 54 111 165
317  
318 # instantiate entity
319 111 $self->inflate_entity($entity_row);
320 }
321  
322 sub all {
323 25 ▶ my $self = shift;
Subroutine all: called CC=3 SCAR=11.0
Pod undocumented
324 25 my @entities;
325  
326 25 $self->reset;
327  
328 25 ▶ while (my $entity = $self->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $entity = $self->next) 69 25 94
329 69 push @entities, $entity;
330 }
331  
332 25 $self->reset;
333  
334 25 ▶ return wantarray ? @entities : \@entities;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 24 1 25
335 }
336  
337 sub pager {
338 0 ▶ die "pager() not implemented";
Subroutine pager: not called CC=1 SCAR=6.9
Pod undocumented
339 }
340  
341 sub distinct {
342 0 ▶ die "distinct() not implemented";
Subroutine distinct: not called CC=1 SCAR=6.9
Pod documented
343 }
344  
345 sub storage_size {
346 0 ▶ die "storage_size() not implemented";
Subroutine storage_size: not called CC=1 SCAR=6.9
Pod undocumented
347 }
348  
349  
350 10 1;
351  
352 __END__
« lib/DBIx/EAV.pm ↑ Summary lib/DBIx/EAV/Schema.pm »