lib/DBIx/EAV.pm

filter: Statementstmt 98.8% 168 / 170 Branchbran 75.8% 44 / 58 Conditioncond 70.0% 14 / 20 MC/DCmcdc 33.3% 4 / 12 Subroutinesub 96.5% 28 / 29 Podpod 80.0% 4 / 5 totaltotal 89.1% 262 / 294 SCAR 38.4
CC
45
Cov
91%
_register_entity_type
17.7
register_types
9.0
connect
5.6
CRAP 46.4
« lib/DBIx/EAV/EntityType.pm ↑ Summary lib/DBIx/EAV/ResultSet.pm »
1 package DBIx::EAV;
2  
3 10 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use Lingua::EN::Inflect ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use DBIx::EAV::EntityType;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use DBIx::EAV::Entity;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use DBIx::EAV::ResultSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use DBIx::EAV::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use Carp qw' croak confess ';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 10 ▶use Class::Load qw' try_load_class ';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 10 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 10 our $VERSION = "0.08";
18  
19 # required
20 10 has 'dbh', is => 'ro', required => 1;
21  
22 # options
23 10 has 'default_attribute_type', is => 'ro', default => 'varchar';
24 0 ▶has 'schema_config', is => 'ro', default => sub { {} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
25 12 ▶has 'entity_namespaces', is => 'ro', default => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
26 12 ▶has 'resultset_namespaces', is => 'ro', default => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
27  
28 # internal
29 10 has 'schema', is => 'ro', lazy => 1, builder => 1, init_arg => undef, handles => [qw/ table dbh_do /];
30 13 ▶has '_types', is => 'ro', default => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
31 13 ▶has '_types_by_id', is => 'ro', default => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
32  
33 # group schema_config params
34 around BUILDARGS => sub {
35 13 ▶ my ( $orig, $class, @args ) = @_;
Subroutine __ANON__: called CC=4 SCAR=13.9
36 13 ▶ my $params = @args == 1 && ref $args[0] ? $args[0] : { @args };
Branch: 1 branch
BranchTrueFalseTotal
@args == 1 && ref $args[0] ? : 1 12 13
Condition66%2/3
@args == 1 and ref $args[0]
!ll&&!rl&&r
1201
MC/DC50%1/2
@args == 1 and ref $args[0]
@args == 1 ref $args[0]
Truth table66%2/3
A and B
A= @args == 1B= ref $args[0]
ABresult
0X0
100
111
37 13 ▶ my $schema_config = delete $params->{schema_config} || {};
Condition50%1/2
delete $params->{'schema_config'} || {}
l!l
013
MC/DC0%0/1
delete $params->{'schema_config'} || {}
delete $params->{'schema_config'}
Truth table50%1/2
A
A= delete $params->{'schema_config'}
Aresult
11
00
38  
39 13 my @schema_params = grep { exists $params->{$_} } qw/
40 tenant_id data_types database_cascade_delete static_attributes
41 table_prefix id_type default_attribute_type
42 /;
43  
44 13 @{$schema_config}{@schema_params} = delete @{$params}{@schema_params};
45  
46 13 $class->$orig(%$params, schema_config => $schema_config);
47 10 };
48  
49  
50 sub _build_schema {
51 12 ▶ my $self = shift;
Subroutine _build_schema: called CC=1 SCAR=0.0
52 12 DBIx::EAV::Schema->new(%{$self->schema_config}, dbh => $self->dbh);
53 }
54  
55 sub connect {
56 1 ▶ my ($class, $dsn, $user, $pass, $attrs, $constructor_params) = @_;
Subroutine connect: called CC=5 SCAR=17.2
Pod documented
57  
58 1 ▶ croak 'Missing $dsn argument for connect()' unless $dsn;
Branch: 1 branch
BranchTrueFalseTotal
unless $dsn 0 1 1
59  
60 1 ▶ croak "connect() must be called as a class method."
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 1 1
61 if ref $class;
62  
63 1 ▶ $constructor_params //= {};
Condition50%1/2
$constructor_params //= {}
l!l
10
MC/DC0%0/1
$constructor_params //= {}
$constructor_params
Truth table50%1/2
A
A= $constructor_params
Aresult
11
00
64  
65 1 ▶ $constructor_params->{dbh} = DBI->connect($dsn, $user, $pass, $attrs)
Branch: 1 branch
BranchTrueFalseTotal
$constructor_params->{"dbh"} = "DBI"->connect($dsn, $user, $pass, $attrs) or die $DBI::errstr 0 1 1
66 or die $DBI::errstr;
67  
68 1 $class->new($constructor_params);
69 }
70  
71 sub type {
72 238 ▶ my ($self, $value) = @_;
Subroutine type: called CC=5 SCAR=16.1
Pod documented
73  
74 return $self->_types->{$value}
75 238 ▶ if exists $self->_types->{$value};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->_types->{$value} 231 7 238
76  
77 7 my $type = $self->_load_type('name', $value);
78  
79 # not registered, try to find a custom entity class and register it
80 7 ▶ if (!$type && (my $entity_class = $self->_resolve_entity_class($value))) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $type and my $entity_class = $self->_resolve_entity_class($value)) 3 3 6
Condition100%3/3
not $type and my $entity_class = $self->_resolve_entity_class($value)
!ll&&!rl&&r
133
MC/DC100%2/2
not $type and my $entity_class = $self->_resolve_entity_class($value)
not $type my $entity_class = $self->_resolve_entity_class($value)
Truth table100%3/3
not(A) and B
A= not $typeB= my $entity_class = $self->_resolve_entity_class($value)
ABresult
0X0
100
111
81  
82 3 ($type) = $self->register_types({$value => $entity_class->type_definition});
83 }
84  
85 6 ▶ confess "EntityType '$value' does not exist."
Branch: 1 branch
BranchTrueFalseTotal
unless $type 2 4 6
86 unless $type;
87  
88 4 $type;
89 }
90  
91 sub type_by_id {
92 47 ▶ my ($self, $value) = @_;
Subroutine type_by_id: called CC=3 SCAR=11.1
Pod undocumented
93  
94 return $self->_types_by_id->{$value}
95 47 ▶ if exists $self->_types_by_id->{$value};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->_types_by_id->{$value} 44 3 47
96  
97 3 ▶ $self->_load_type('id', $value)
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_type("id", $value) or confess("EntityType 'id=${value}' does not exist.") 0 3 3
98 or confess "EntityType 'id=$value' does not exist.";
99 }
100  
101 sub _load_type {
102 10 ▶ my ($self, $field, $value) = @_;
Subroutine _load_type: called CC=2 SCAR=6.9
103  
104 10 my $type_row = $self->table('entity_types')->select_one({ $field => $value });
105 10 ▶ return unless $type_row;
Branch: 1 branch
BranchTrueFalseTotal
unless $type_row 6 4 10
106  
107 4 my $type = DBIx::EAV::EntityType->load({ %$type_row, core => $self});
108 4 $self->_types->{$type->name} = $type;
109 4 $self->_types_by_id->{$type->id} = $type;
110 4 $type;
111 }
112  
113 sub _resolve_entity_class {
114 109 ▶ my ($self, $name) = @_;
Subroutine _resolve_entity_class: called CC=4 SCAR=13.9
115  
116 109 foreach my $ns (@{ $self->entity_namespaces }) {
117  
118 5 my $entity_class = join '::', $ns, $name;
119 5 my ($is_loaded, $error) = try_load_class $entity_class;
120  
121 5 ▶ return $entity_class if $is_loaded;
Branch: 1 branch
BranchTrueFalseTotal
if $is_loaded 4 1 5
122  
123 # rethrow compilation errors
124 1 ▶ die $error if $error =~ /^Can't locate .* in \@INC/;
Branch: 1 branch
BranchTrueFalseTotal
if $error =~ /^Can't locate .* in \@INC/ 1 0 1
125 }
126  
127 104 return;
128 }
129  
130 sub _resolve_resultset_class {
131 118 ▶ my ($self, $name) = @_;
Subroutine _resolve_resultset_class: called CC=3 SCAR=11.2
132  
133 118 foreach my $ns (@{ $self->resultset_namespaces }) {
134  
135 2 my $class = join '::', $ns, $name;
136 2 my ($is_loaded, $error) = try_load_class $class;
137  
138 2 ▶ return $class if $is_loaded;
Branch: 1 branch
BranchTrueFalseTotal
if $is_loaded 2 0 2
139  
140 # rethrow compilation errors
141 0 die $class;
142 }
143  
144 116 return;
145 }
146  
147 sub resultset {
148 118 ▶ my ($self, $name) = @_;
Subroutine resultset: called CC=2 SCAR=6.9
Pod documented
149  
150 118 ▶ my $rs_class = $self->_resolve_resultset_class($name)
Condition100%2/2
$self->_resolve_resultset_class($name) || 'DBIx::EAV::ResultSet'
l!l
2116
MC/DC100%1/1
$self->_resolve_resultset_class($name) || 'DBIx::EAV::ResultSet'
$self->_resolve_resultset_class($name)
Truth table100%2/2
A
A= $self->_resolve_resultset_class($name)
Aresult
11
00
151 || 'DBIx::EAV::ResultSet';
152  
153 118 $rs_class->new({
154 eav => $self,
155 type => $self->type($name),
156 });
157 }
158  
159  
160 sub register_types {
161 12 ▶ my ($self, $schema) = @_;
Subroutine register_types: called CC=9 SCAR=22.0
Pod documented
162 12 my %skip;
163  
164 # register only not-installed entities to
165 # allow multiple calls to this method
166 12 my @new_types = grep { not exists $self->_types->{$_} } keys %$schema;
167  
168 # create or update each entity type on database
169 12 my @registered_types;
170 12 foreach my $name (@new_types) {
171 41 ▶ next if exists $self->_types->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->_types->{$name} 1 40 41
172 push @registered_types,
173 40 $self->_register_entity_type($name, $schema->{$name}, $schema);
174 }
175  
176 # register relationships
177 12 foreach my $name (@new_types) {
178  
179 41 my $spec = $schema->{$name};
180 41 my $entity_type = $self->type($name);
181  
182 41 foreach my $reltype (qw/ has_one has_many many_to_many /) {
183  
184 123 ▶ next unless defined $spec->{$reltype};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spec->{$reltype} 89 34 123
185  
186 34 ▶ $spec->{$reltype} ||= [];
Condition50%1/2
$spec->{$reltype} ||= []
l!l
340
MC/DC0%0/1
$spec->{$reltype} ||= []
$spec->{$reltype}
Truth table50%1/2
A
A= $spec->{$reltype}
Aresult
11
00
187 $spec->{$reltype} = [$spec->{$reltype}]
188 34 ▶ unless ref $spec->{$reltype} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $spec->{$reltype} eq "ARRAY" 13 21 34
189  
190 34 foreach my $rel (@{$spec->{$reltype}}) {
191 34 $entity_type->register_relationship($reltype, $rel);
192 }
193 }
194 }
195  
196 12 @registered_types;
197 }
198  
199  
200 sub _register_entity_type {
201 41 ▶ my ($self, $name, $spec, $schema) = @_;
Subroutine _register_entity_type: called CC=17 SCAR=28.7
202  
203 # parent type first
204 41 my $parent_type;
205 41 ▶ if ($spec->{extends}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"extends"}) 5 36 41
206  
207 5 ▶ unless ($parent_type = $self->_types->{$spec->{extends}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parent_type = $self->_types->{$spec->{"extends"}}) 1 4 5
208  
209 die "Unknown type '$spec->{extends}' specified in 'extents' option for type '$name'."
210 1 ▶ unless exists $schema->{$spec->{extends}};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$schema{$spec->{"extends"}} 0 1 1
211  
212 1 $parent_type = $self->_register_entity_type($spec->{extends}, $schema->{$spec->{extends}}, $schema);
213 }
214 }
215  
216 # find or create entity type
217 41 my $types_table = $self->table('entity_types');
218 41 my $hierarchy_table = $self->table('type_hierarchy');
219 41 my $type = $types_table->select_one({ name => $name });
220  
221 41 ▶ if (defined $type) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type) { } 0 41 41
222  
223 # change parent
224 }
225 else {
226  
227 # TODO implement rename
228 # if ($spec->{rename_from}) { ... }
229  
230 41 my $id = $types_table->insert({ name => $name });
231 41 $type = $types_table->select_one({ id => $id });
232 41 ▶ die "Error inserting entity type '$name'!" unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 41 41
233  
234 41 ▶ if ($parent_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent_type) 5 36 41
235 $hierarchy_table->insert({
236 parent_type_id => $parent_type->{id},
237 child_type_id => $type->{id}
238 5 });
239  
240 5 $type->{parent} = $parent_type;
241 }
242 }
243  
244 # update or create attributes
245 41 my $attributes = $self->table('attributes');
246 41 my %static_attributes = map { $_ => {name => $_, is_static => 1} } @{$self->table('entities')->columns};
247 41 $type->{attributes} = {};
248  
249 41 ▶ my %inherited_attributes = $parent_type ? map { $_->{name} => $_ } $parent_type->attributes( no_static => 1 )
Branch: 1 branch
BranchTrueFalseTotal
$parent_type ? : 5 36 41
250 : ();
251  
252 41 foreach my $attr_spec (@{$spec->{attributes}}) {
253  
254 # expand string to name/type
255 112 ▶ unless (ref $attr_spec) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $attr_spec) 45 67 112
256 45 my ($name, $type) = split ':', $attr_spec;
257 45 ▶ $attr_spec = {
Condition66%2/3
$type || $self->default_attribute_type
l!l&&r!l&&!r
32130
MC/DC0%0/2
$type || $self->default_attribute_type
$type $self->default_attribute_type
Truth table66%2/3
A || B
A= $typeB= $self->default_attribute_type
ABresult
1X1
011
000
258 name => $name,
259 type => $type || $self->default_attribute_type
260 };
261 }
262  
263 die sprintf("Error registering attribute '%s' for entity '%s'. Can't use names of static attributes (real table columns).", $attr_spec->{name}, $type->{name})
264 112 ▶ if exists $static_attributes{$attr_spec->{name}};
Branch: 1 branch
BranchTrueFalseTotal
if exists $static_attributes{$attr_spec->{"name"}} 0 112 112
265  
266 printf STDERR "[warn] entity '%s' is overriding inherited attribute '%s'", $name, $attr_spec->{name}
267 112 ▶ if $inherited_attributes{$attr_spec->{name}};
Branch: 1 branch
BranchTrueFalseTotal
if $inherited_attributes{$attr_spec->{"name"}} 0 112 112
268  
269 my $attr = $attributes->select_one({
270 entity_type_id => $type->{id},
271 name => $attr_spec->{name}
272 112 });
273  
274 112 ▶ if (defined $attr) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $attr) { } 0 112 112
275 # update
276 }
277 else {
278 112 delete $attr_spec->{id}; # safety
279  
280 112 my %data = %$attr_spec;
281  
282 112 $data{entity_type_id} = $type->{id};
283 112 ▶ $data{data_type} = delete($data{type}) || $self->default_attribute_type;
Condition66%2/3
delete $data{'type'} || $self->default_attribute_type
l!l&&r!l&&!r
96160
MC/DC0%0/2
delete $data{'type'} || $self->default_attribute_type
delete $data{'type'} $self->default_attribute_type
Truth table66%2/3
A || B
A= delete $data{'type'}B= $self->default_attribute_type
ABresult
1X1
011
000
284  
285 die sprintf("Attribute '%s' has unknown data type '%s'.", $data{name}, $data{data_type})
286 112 ▶ unless $self->schema->has_data_type($data{data_type});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->schema->has_data_type($data{"data_type"}) 0 112 112
287  
288 112 $attributes->insert(\%data);
289 112 $attr = $attributes->select_one(\%data);
290 112 ▶ die "Error inserting attribute '$attr_spec->{name}'!" unless $attr;
Branch: 1 branch
BranchTrueFalseTotal
unless $attr 0 112 112
291 }
292  
293 112 $type->{attributes}{$attr->{name}} = $attr;
294 }
295  
296 $self->_types->{$name} =
297 41 $self->_types_by_id->{$type->{id}} = DBIx::EAV::EntityType->new(%$type, core => $self);
298 }
299  
300  
301  
302 10 1;
303  
304 __END__
« lib/DBIx/EAV/EntityType.pm ↑ Summary lib/DBIx/EAV/ResultSet.pm »