blib/lib/DBIx/QuickORM/Map.pm

filter: Statementstmt 58.4% 59 / 101 Branchbran 50.0% 30 / 60 Conditioncond 44.4% 8 / 18 MC/DCmcdc 41.6% 5 / 12 Subroutinesub 63.1% 12 / 19 Podpod 83.3% 10 / 12 totaltotal 55.8% 124 / 222 CC 39 SCAR 29.1
CC
39
Cov
54%
normalize_pk
35.6
normalize_pk
35.6
_copy
8.1
CRAP 18.4
↑ Summary blib/lib/DBIx/QuickORM/AggregateRow.pm »
1 package DBIx::QuickORM::Map;
2 261 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 261 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 261 our $VERSION = '0.000031';
6  
7 261 ▶use Carp qw/croak confess/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 261 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 261 ▶use Object::HashBase qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
11 <tables
12 261 };
13  
14 =pod
15  
16 =encoding UTF-8
17  
18 =head1 NAME
19  
20 DBIx::QuickORM::Map - The database map: tables, fields, and keys as plain data.
21  
22 =head1 DESCRIPTION
23  
24 One object over plain nested data (strings, hashes, and arrays only, plus a
25 field's C<format> code reference), keyed by table name. The ORM builds it in
26 its constructor. After that, treat the map and everything its methods return
27 as read-only.
28  
29 {
30 users => {
31 fields => {
32 id => {
33 affinity => 'numeric',
34 db_type => 'integer',
35 type => 'integer',
36 },
37 name => {
38 affinity => 'string',
39 db_type => 'varchar(255)',
40 type => 'varchar',
41 size => 255,
42 },
43 prefs => {
44 affinity => 'string',
45 db_type => 'jsonb',
46 type => 'jsonb',
47 orm_type => 'DBIx::QuickORM::Type::JSON',
48 },
49 },
50 pk => ['id'], # undef when the table has none
51 # each sorted, and the primary key is not repeated
52 unique => [['email']],
53 fk => {orgs => [{org_id => 'id'}]}, # several links may name one table
54 default_fields => ['id', 'name'], # absent: every field
55 # absent: the ORM's default, undef: hashrefs
56 row_class => 'My::User',
57 },
58 }
59  
60 C<db_type> is the field's type as the database reports it, C<type> its
61 lowercased base name, C<size> its declared length or precision when it has
62 one, and C<affinity> one of C<string>, C<numeric>, C<binary>, or C<boolean>.
63 C<orm_type> is the type class chosen for the field, if any, and C<format> and
64 C<generate> its hints (see L<DBIx::QuickORM::Role::Type/Field hints>).
65 C<passthrough> is 1 when that type's C<inflate> returns the raw value
66 unchanged for the field (C<inflate_is_identity>). The ORM sets it, and rows
67 then skip inflating the field. C<db_fills> is 1 when the database fills the
68 column when an insert leaves it out, and C<db_refuses_value> is 1 when it
69 rejects an explicit value for it. Both come from the scan and are absent
70 otherwise. A hand-written map without them reads as "no", so C<prefer_db>
71 generates and the C<prefer_orm> build croak cannot fire. C<pk_types> holds
72 the C<orm_type> of each primary key field, in key order, for the lookup
73 table. The ORM sets it only when a key field has a type.
74  
75 A scanned field record also holds its own C<table> and C<name> (left out
76 above), so messages can name the field.
77  
78 Names are exact and case-sensitive, and a table or field named C<0> works. A
79 foreign key may name a table that is not in the map.
80  
81 =head1 SYNOPSIS
82  
83 my $map = $orm->db_map;
84  
85 if (my $field = $map->field(users => 'name')) { ... }
86 my $pk = $map->pk('users');
87  
88 my $copy = $map->clone;
89  
90 =head1 ATTRIBUTES
91  
92 =over 4
93  
94 =item \%tables = $map->tables
95  
96 The data, as shown above. Required.
97  
98 =back
99  
100 =cut
101  
102 329 ▶sub init {
Subroutine init: called CC=2 SCAR=9.2
Pod undocumented
103 0 ▶ my $self = shift;
Subroutine init: not called CC=2 SCAR=9.2
Pod undocumented
104 0 ▶ croak "'tables' must be a hashref" unless ref($self->{+TABLES}) eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $self->{"tables"} eq "HASH" 2 327 329
unless ref $self->{"tables"} eq "HASH" 0 0 0
105 }
106  
107 =head1 PUBLIC METHODS
108  
109 =over 4
110  
111 =item $map = DBIx::QuickORM::Map->new(tables => \%tables)
112  
113 Holds C<\%tables> as given, without copying it. Croaks unless it is a hashref.
114  
115 =item $copy = $map->clone
116  
117 A new map over a deep copy of the data. Code references (a field's
118 C<format>) are kept as they are. Any other reference but a plain hash or
119 array croaks.
120  
121 =back
122  
123 =cut
124  
125 19 ▶sub clone {
Subroutine clone: called CC=1 SCAR=1.2
Pod documented
126 0 ▶ my $self = shift;
Subroutine clone: not called CC=1 SCAR=1.2
Pod documented
127 0 return ref($self)->new(tables => $self->_copy($self->{+TABLES}));
128 }
129  
130 =pod
131  
132 =over 4
133  
134 =item \%table = $map->table($name)
135  
136 =item \%field = $map->field($table, $name)
137  
138 The record, or undef when there is none.
139  
140 =back
141  
142 =cut
143  
144 18718 ▶sub table {
Subroutine table: called CC=1 SCAR=1.2
Pod documented
145 0 ▶ my $self = shift;
Subroutine table: not called CC=1 SCAR=1.2
Pod documented
146 0 my ($name) = @_;
147 0 return $self->{+TABLES}->{$name};
148 }
149  
150 479 ▶sub field {
Subroutine field: called CC=2 SCAR=9.2
Pod documented
151 0 ▶ my $self = shift;
Subroutine field: not called CC=2 SCAR=9.2
Pod documented
152 0 my ($table, $name) = @_;
153 0 ▶ my $t = $self->{+TABLES}->{$table} or return undef;
Branch: 2 branches
BranchTrueFalseTotal
my $t = $self->{"tables"}{$table} or return undef 478 1 479
my $t = $self->{"tables"}{$table} or return undef 0 0 0
154 0 return $t->{fields}->{$name};
155 }
156  
157 =pod
158  
159 =over 4
160  
161 =item \@fields = $map->pk($table)
162  
163 The primary key fields in key order, or undef when the table has none. Croaks
164 when there is no such table.
165  
166 =back
167  
168 =cut
169  
170 1245 ▶sub pk {
Subroutine pk: called CC=2 SCAR=9.2
Pod documented
171 0 ▶ my $self = shift;
Subroutine pk: not called CC=2 SCAR=9.2
Pod documented
172 0 my ($table) = @_;
173 0 ▶ my $t = $self->{+TABLES}->{$table} or croak "The table '$table' is not in the database map";
Branch: 2 branches
BranchTrueFalseTotal
my $t = $self->{"tables"}{$table} or croak("The table '${table}' is not in the database map") 1242 3 1245
my $t = $self->{"tables"}{$table} or croak("The table '${table}' is not in the database map") 0 0 0
174 0 return $t->{pk};
175 }
176  
177 =pod
178  
179 =over 4
180  
181 =item \@values = $map->normalize_pk($table, $pk)
182  
183 The primary key values of C<$table>, in key order, from any key form. It
184 judges the form, not the values: they come back as given, and deflating them
185 is the caller's job (the ORM's key arguments do it).
186  
187 =over 4
188  
189 =item *
190  
191 A hashref holding the key fields. Other fields are ignored, so row data works.
192  
193 =item *
194  
195 A row object (any object with C<row_data>), taken as its row data.
196  
197 =item *
198  
199 An arrayref of the values, in the primary key's declared order.
200  
201 =item *
202  
203 A single value, for a single-field key. Any blessed object that is not a row
204 is a value, such as a C<DateTime> or a value marker.
205  
206 =back
207  
208 Croaks when the table has no primary key, on a single value for a compound
209 key, on an arrayref of the wrong length, on a missing or undef key value, and
210 on an unblessed reference that is not a hash or an array.
211  
212 =back
213  
214 =cut
215  
216 407 ▶sub normalize_pk {
Subroutine normalize_pk: called CC=13 SCAR=35.7
Pod documented
217 0 ▶ my $self = shift;
Subroutine normalize_pk: not called CC=13 SCAR=35.7
Pod documented
218 0 my ($table, $pk) = @_;
219  
220 0 my $fields = $self->pk($table);
221 0 ▶ confess "The table '$table' has no primary key" unless $fields && @$fields;
Branch: 2 branches
BranchTrueFalseTotal
unless $fields and @$fields 4 403 407
unless $fields and @$fields 0 0 0
Condition33%2/6
$fields and @$fields
!ll&&!rl&&r
40403
$fields and @$fields
!ll&&!rl&&r
000
MC/DC50%1/2
$fields and @$fields
$fields @$fields
MC/DC0%0/2
$fields and @$fields
$fields @$fields
Truth table66%2/3
A and B
A= $fieldsB= @$fields
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $fieldsB= @$fields
ABresult
0X0
100
111
222 0 my $names = join(', ' => @$fields);
223  
224 0 ▶ $pk = $pk->row_data if blessed($pk) && $pk->can('row_data');
Branch: 2 branches
BranchTrueFalseTotal
if builtin::blessed($pk) and $pk->can("row_data") 28 375 403
if builtin::blessed($pk) and $pk->can("row_data") 0 0 0
Condition50%3/6
builtin::blessed($pk) and $pk->can("row_data")
!ll&&!rl&&r
3601528
builtin::blessed($pk) and $pk->can("row_data")
!ll&&!rl&&r
000
MC/DC100%2/2
builtin::blessed($pk) and $pk->can("row_data")
builtin::blessed($pk) $pk->can("row_data")
MC/DC0%0/2
builtin::blessed($pk) and $pk->can("row_data")
builtin::blessed($pk) $pk->can("row_data")
Truth table100%3/3
A and B
A= builtin::blessed($pk)B= $pk->can("row_data")
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= builtin::blessed($pk)B= $pk->can("row_data")
ABresult
0X0
100
111
225  
226 0 my @values;
227 0 ▶ if (ref($pk) eq 'HASH') {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $pk eq 'HASH') { } 272 131 403
elsif (ref $pk eq 'ARRAY') { } 14 117 131
if (ref $pk eq 'HASH') { } 0 0 0
elsif (ref $pk eq 'ARRAY') { } 0 0 0
228 0 @values = map { $pk->{$_} } @$fields;
229 }
230 elsif (ref($pk) eq 'ARRAY') {
231 0 ▶ confess "The table '$table' has " . @$fields . " primary key field(s) ($names), but the arrayref holds " . @$pk . " value(s)" unless @$pk == @$fields;
Branch: 2 branches
BranchTrueFalseTotal
unless @$pk == @$fields 4 10 14
unless @$pk == @$fields 0 0 0
232 0 @values = @$pk;
233 }
234 else {
235 0 ▶ confess "Primary key must be a value, an arrayref, a hashref, or a row; got $pk" if ref($pk) && !blessed($pk);
Branch: 2 branches
BranchTrueFalseTotal
if ref $pk and not builtin::blessed($pk) 3 114 117
if ref $pk and not builtin::blessed($pk) 0 0 0
Condition50%3/6
ref $pk and not builtin::blessed($pk)
!ll&&!rl&&r
99153
ref $pk and not builtin::blessed($pk)
!ll&&!rl&&r
000
MC/DC100%2/2
ref $pk and not builtin::blessed($pk)
ref $pk not builtin::blessed($pk)
MC/DC0%0/2
ref $pk and not builtin::blessed($pk)
ref $pk not builtin::blessed($pk)
Truth table100%3/3
A and not(B)
A= ref $pkB= not builtin::blessed($pk)
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= ref $pkB= not builtin::blessed($pk)
ABresult
0X0
100
111
236 0 ▶ confess "The table '$table' has a compound primary key ($names); give a hashref of its fields or an arrayref of its values in that order" if @$fields > 1;
Branch: 2 branches
BranchTrueFalseTotal
if @$fields > 1 4 110 114
if @$fields > 1 0 0 0
237 0 @values = ($pk);
238 }
239  
240 0 for my $i (0 .. $#$fields) {
241 0 ▶ confess "The primary key field '$fields->[$i]' of the table '$table' is missing or undefined" unless defined $values[$i];
Branch: 2 branches
BranchTrueFalseTotal
unless defined $values[$i] 5 403 408
unless defined $values[$i] 0 0 0
242 }
243  
244 0 return \@values;
245 }
246  
247 =head1 PRIVATE METHODS
248  
249 =over 4
250  
251 =item $copy = $map->_copy($data)
252  
253 A deep copy of map data, with the croak C<clone> documents.
254  
255 =back
256  
257 =cut
258  
259 1308 ▶sub _copy {
Subroutine _copy: called CC=5 SCAR=20.9
260 0 ▶ my $self = shift;
Subroutine _copy: not called CC=5 SCAR=20.9
261 0 my ($data) = @_;
262  
263 0 ▶ my $type = ref($data) or return $data;
Branch: 2 branches
BranchTrueFalseTotal
my $type = ref $data or return $data 449 859 1308
my $type = ref $data or return $data 0 0 0
264 0 ▶ return $data if $type eq 'CODE'; # a field's format, which nothing changes
Branch: 2 branches
BranchTrueFalseTotal
if $type eq "CODE" 1 448 449
if $type eq "CODE" 0 0 0
265 0 ▶ return {map { $_ => $self->_copy($data->{$_}) } keys %$data} if $type eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
if $type eq "HASH" 327 121 448
if $type eq "HASH" 0 0 0
266 0 ▶ return [map { $self->_copy($_) } @$data] if $type eq 'ARRAY';
Branch: 2 branches
BranchTrueFalseTotal
if $type eq "ARRAY" 119 2 121
if $type eq "ARRAY" 0 0 0
267 0 croak "The database map holds only strings, hashes, arrays, and code references, found a '$type' reference";
268 }
269  
270 261 1;
271  
272 __END__
↑ Summary blib/lib/DBIx/QuickORM/AggregateRow.pm »