PGObject/Type/Registry.pm

filter: Statementstmt 73.3% 77 / 105 Branchbran 57.1% 32 / 56 Conditioncond 62.5% 5 / 8 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 80.0% 16 / 20 Podpod 100% 8 / 8 totaltotal 69.3% 140 / 202 CC 34 SCAR 25.8
CC
34
Cov
67%
rowhash_deserializer
56.0
register_type
13.5
deserializer
9.2
CRAP 13.2
« PGObject.pm ↑ Summary PGObject/Util/DBException.pm »
1  
2 =head1 NAME
3  
4 PGObject::Type::Registry - Registration of types for handing db types
5  
6 =head1 SYNOPSIS
7  
8 PGObject::Type::Registry->add_registry('myapp'); # required
9  
10 PGObject::Type::Registry->register_type(
11 registry => 'myapp', dbtype => 'int4',
12 apptype => 'PGObject::Type::BigFloat'
13 );
14  
15 # to get back a type:
16 my $number = PGObject::Type::Registry->deserialize(
17 registry => 'myapp', dbtype => 'int4',
18 dbstring => '1023'
19 );
20  
21 # To get registry data:
22 my %registry = PGObject::Type::Registry->inspect(registry => 'myapp');
23  
24 =cut
25  
26 package PGObject::Type::Registry;
27  
28 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31  
32 7 ▶use Carp::Clan qr/^PGObject\b/;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 7 ▶use List::MoreUtils qw(pairwise);
Subroutine BEGIN: called CC=1 SCAR=0.0
34 7 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
35 7 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37  
38 7 our $VERSION = '2.3.1';
39  
40 7 my %registry = ( default => {} );
41  
42 =head1 DESCRIPTION
43  
44 The PGObject type registry stores data for serialization and deserialization
45 relating to the database.
46  
47 =head1 USE
48  
49 Generally we like to separate applications into their own registries so that
50 different libraries can be used in a more harmonious way.
51  
52 =head1 CREATING A REGISTRY
53  
54 You must create a registry before using it. This is there to ensure that we
55 make sure that subtle problems are avoided and strings returned when serialized
56 types expected. This is idempotent and repeat calls are safe. There is no
57 abiltiy to remove an existing registry though you can loop through and remove
58 the existing registrations.
59  
60 =head2 new_registry(name)
61  
62 =cut
63  
64 13 ▶sub new_registry {
Subroutine new_registry: called CC=2 SCAR=6.9
Pod documented
65 13 my ( $self, $name ) = @_;
66 13 ▶ if ( not exists $registry{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $registry{$name}) 10 3 13
67 10 $registry{$name} = {};
68 }
69 }
70  
71 =head1 REGISTERING A TYPE
72  
73 =head2 register_type
74  
75 Args:
76  
77 registry => 'default', #warning thrown if not specified
78 dbtype => [required], #exception thrown if not specified
79 apptype => [required], #exception thrown if not specified
80  
81 Use:
82  
83 This registers a type for use by PGObject. PGObject calls with the same
84 registry key will serialize to this type, using the from_db method provided.
85  
86 from_db will be provided two arguments. The first is the string from the
87 database and the second is the type provided. The second argument is optional
88 and passed along for the db interface class's use.
89  
90 A warning is thrown if no
91  
92 =cut
93  
94 17 ▶sub register_type {
Subroutine register_type: called CC=13 SCAR=26.0
Pod documented
95 17 my ( $self, %args ) = @_;
96 17 my %defaults = ( registry => 'default' );
97 carp $log->warn( 'Using default registry' )
98 17 ▶ unless $args{registry};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"registry"} 6 11 17
99 croak $log->error( 'Missing dbtype arg' )
100 17 ▶ unless $args{dbtype};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"dbtype"} 0 17 17
101 croak $log->error( 'Missing apptype arg' )
102 17 ▶ unless $args{apptype};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"apptype"} 0 17 17
103 17 ▶ delete $args{registry} unless defined $args{registry};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"registry"} 6 11 17
104 17 %args = ( %defaults, %args );
105 croak $log->error( 'Registry does not exist yet' )
106 17 ▶ unless exists $registry{ $args{registry} };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $registry{$args{"registry"}} 2 15 17
107 croak $log->error( 'Type registered with different target' )
108 if exists $registry{ $args{registry} }->{ $args{dbtype} }
109 15 ▶ and $registry{ $args{registry} }->{ $args{dbtype} } ne $args{apptype};
Branch: 1 branch
BranchTrueFalseTotal
if exists $registry{$args{"registry"}}{$args{"dbtype"}} and $registry{$args{"registry"}}{$args{"dbtype"}} ne $args{"apptype"} 3 12 15
Condition100%3/3
exists $registry{$args{"registry"}}{$args{"dbtype"}} and $registry{$args{"registry"}}{$args{"dbtype"}} ne $args{"apptype"}
!ll&&!rl&&r
933
MC/DC100%2/2
exists $registry{$args{"registry"}}{$args{"dbtype"}} and $registry{$args{"registry"}}{$args{"dbtype"}} ne $args{"apptype"}
exists $registry{$args{"registry"}}{$args{"dbtype"}} $registry{$args{"registry"}}{$args{"dbtype"}} ne $args{"apptype"}
Truth table100%3/3
A and B
A= exists $registry{$args{"registry"}}{$args{"dbtype"}}B= $registry{$args{"registry"}}{$args{"dbtype"}} ne $args{"apptype"}
ABresult
0X0
100
111
110 12 $args{apptype} =~ /^(.*)::(\w*)$/;
111 12 my ( $parent, $final ) = ( $1, $2 );
112 12 ▶ $parent ||= '';
Condition50%1/2
$parent ||= ""
l!l
012
MC/DC0%0/1
$parent ||= ""
$parent
Truth table50%1/2
A
A= $parent
Aresult
11
00
113 12 ▶ $final ||= $args{apptype};
Condition33%1/3
$final ||= $args{"apptype"}
l!l&&r!l&&!r
0120
MC/DC0%0/2
$final ||= $args{"apptype"}
$final $args{"apptype"}
Truth table33%1/3
A ||= B
A= $finalB= $args{"apptype"}
ABresult
1X1
011
000
114 {
115 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
116 12 ▶ $parent = "${parent}::" if $parent;
Branch: 1 branch
BranchTrueFalseTotal
if $parent 0 12 12
117 croak "apptype not yet loaded ($args{apptype})"
118 12 ▶ unless exists ${"::${parent}"}{"${final}::"};
Branch: 1 branch
BranchTrueFalseTotal
unless exists ${"::$parent";}{"${final}::"} 2 10 12
119 croak 'apptype does not have from_db function'
120 10 ▶ unless $args{apptype}->can('from_db');
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"apptype"}->can("from_db") 0 10 10
121 }
122 10 %args = ( %defaults, %args );
123 10 $registry{ $args{registry} }->{ $args{dbtype} } = $args{apptype};
124 }
125  
126 =head1 UNREGISTERING A TYPE
127  
128 To unregister a type, you provide the dbtype and registry information, both
129 of which are required. Note that at this time this is rarely needed.
130  
131 =head2 unregister_type
132  
133 =cut
134  
135 3 ▶sub unregister_type {
Subroutine unregister_type: called CC=5 SCAR=16.2
Pod documented
136 3 my ( $self, %args ) = @_;
137 croak $log->error( 'Missing registry' )
138 3 ▶ unless $args{registry};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"registry"} 0 3 3
139 croak $log->error( 'Missing dbtype arg' )
140 3 ▶ unless $args{dbtype};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"dbtype"} 0 3 3
141 croak $log->error( 'Registry does not exist yet' )
142 3 ▶ unless exists $registry{ $args{registry} };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $registry{$args{"registry"}} 1 2 3
143 carp $log->warn( 'Type not registered' )
144 2 ▶ unless $registry{ $args{registry} }->{ $args{dbtype} };
Branch: 1 branch
BranchTrueFalseTotal
unless $registry{$args{"registry"}}{$args{"dbtype"}} 1 1 2
145 2 delete $registry{ $args{registry} }->{ $args{dbtype} };
146 }
147  
148 =head1 DESERIALIZING A VALUE
149  
150 =head2 deserialize
151  
152 This function deserializes a data from a db string.
153  
154 Mandatory args are dbtype and dbstring
155 The registry arg should be provided but if not, a warning will be issued and
156 'default' will be used.
157  
158 This function returns the output of the from_db method.
159  
160 =cut
161  
162 2 ▶sub deserialize {
Subroutine deserialize: called CC=2 SCAR=7.1
Pod documented
163 2 my ( $self, %args ) = @_;
164  
165 croak $log->error( "Missing dbstring arg" )
166 2 ▶ unless exists $args{dbstring};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"dbstring"} 0 2 2
167 2 return $self->deserializer( %args )->( $args{dbstring} );
168 }
169  
170 =head2 deserializer
171  
172 This returns a coderef to deserialize data from a db string. The coderef
173 should be called with a single argument: the argument that would be passed
174 as 'dbstring' into C<deserialize>. E.g.:
175  
176 my $deserializer = PGObject::Type::Registry->deserializer(dbtype => $type);
177 my $value = $deserializer->($dbvalue);
178  
179 Mandatory argument is dbtype.
180 The registry arg should be provided but if not, a warning will be issued and
181 'default' will be used.
182  
183 This function returns the output of the C<from_db> method of the registered
184 class.
185  
186 =cut
187  
188 4 ▶sub deserializer {
Subroutine deserializer: called CC=7 SCAR=22.1
Pod documented
189 4 my ( $self, %args ) = @_;
190 4 my %defaults = ( registry => 'default' );
191 carp $log->info( 'No registry specified, using default' )
192 4 ▶ unless exists $args{registry};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"registry"} 0 4 4
193 croak $log->error( "Missing dbtype arg" )
194 4 ▶ unless $args{dbtype};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"dbtype"} 0 4 4
195 4 %args = ( %defaults, %args );
196 4 my $arraytype = 0;
197 4 ▶ if ( $args{dbtype} =~ /^_/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"dbtype"} =~ /^_/) 0 4 4
198 0 $args{dbtype} =~ s/^_//;
199 0 $arraytype = 1;
200 }
201  
202 2 ▶ return $args{_unmapped_undef} ? undef : sub { shift }
Subroutine __ANON__: called CC=1 SCAR=0.0
203 4 ▶ unless $registry{ $args{registry} }->{ $args{dbtype} };
Branch: 2 branches
BranchTrueFalseTotal
unless $registry{$args{"registry"}}{$args{"dbtype"}} 2 2 4
$args{'_unmapped_undef'} ? : 0 2 2
204  
205 2 ▶ if ($arraytype) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arraytype) 0 2 2
206 0 my $deserializer = $self->deserializer( %args );
207 0 ▶ return sub { [ map { $deserializer->( $_ ) } @{ (shift) } ] };
Subroutine __ANON__: not called CC=1 SCAR=6.9
208 }
209  
210 2 my $clazz = $registry{ $args{registry} }->{ $args{dbtype} };
211 2 my $from_db = $clazz->can('from_db');
212 2 my $dbtype = $args{dbtype};
213 2 ▶ return sub { $from_db->($clazz, (shift), $dbtype); }
Subroutine __ANON__: called CC=1 SCAR=0.0
214 2 }
215  
216 =head2 rowhash_deserializer
217  
218 This returns a coderef to deserialize data from a call to e.g.
219 C<fetchrow_arrayref>. The coderef should be called with a single argument:
220 the hash that holds the row values with the keys being the column names.
221  
222 Mandatory argument is C<types>, which is either an arrayref or hashref.
223 In case of a hashref, the keys are the names of the columns to be expected
224 in the data hashrefs. The values are the types (same as the C<dbtype>
225 parameter of the C<deserialize> method). In case of an arrayref, an additional
226 argument C<columns> is required, containing the names of the columns in the
227 same order as C<types>.
228  
229 The registry arg should be provided but if not, a warning will be issued and
230 'default' will be used.
231  
232 This function returns the output of the C<from_db> method of the registered
233 class.
234  
235 =cut
236  
237 0 ▶sub rowhash_deserializer {
Subroutine rowhash_deserializer: not called CC=7 SCAR=40.3
Pod documented
238 0 my ( $self, %args ) = @_;
239 0 my %defaults = ( registry => 'default' );
240 carp $log->warn( 'No registry specified, using default' )
241 0 ▶ unless exists $args{registry};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"registry"} 0 0 0
242 croak $log->error( 'No types specied' )
243 0 ▶ unless exists $args{types};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"types"} 0 0 0
244  
245 0 %args = ( %defaults, %args );
246 0 my $types = $args{types};
247  
248 0 ▶ if (reftype $types eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($types) eq "ARRAY") 0 0 0
249 croak $log->error( 'No columns specified' )
250 0 ▶ unless exists $args{columns};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"columns"} 0 0 0
251  
252 0 ▶ $types = { pairwise { $a => $b } @{$args{columns}}, @$types };
Subroutine __ANON__: not called CC=1 SCAR=6.9
253 }
254  
255 my %column_deserializers =
256 0 map { $_ => $self->deserializer(dbtype => $types->{$_},
257 registry => $args{registry},
258 0 _unmapped_undef => 1) } keys %$types;
259 0 for (keys %column_deserializers) {
260 0 ▶ if (not defined $column_deserializers{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $column_deserializers{$_}) 0 0 0
261 0 delete $column_deserializers{$_}
262 }
263 }
264 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
265 0 my $row = shift;
266  
267 0 for my $col (keys %column_deserializers) {
268 $row->{$col} =
269 0 $column_deserializers{$col}->( $row->{$col} );
270 }
271 0 return $row;
272 }
273 0 }
274  
275 =head1 INSPECTING A REGISTRY
276  
277 Sometimes we need to see what types are registered. To do this, we can
278 request a copy of the registry.
279  
280 =head2 inspect($name)
281  
282 $name is required. If it does not exist an exception is thrown.
283  
284 =cut
285  
286 3 ▶sub inspect {
Subroutine inspect: called CC=3 SCAR=11.3
Pod documented
287 3 my ( $self, $name ) = @_;
288 3 ▶ croak $log->error( 'Must specify a name' )
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 3 3
289 unless $name;
290 croak $log->error( 'Registry does not exist' )
291 3 ▶ unless exists $registry{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $registry{$name} 0 3 3
292 3 return { %{ $registry{$name} } };
293 }
294  
295 =head2 list()
296  
297 Returns a list of existing registries.
298  
299 =cut
300  
301 1 ▶sub list {
Subroutine list: called CC=1 SCAR=0.0
Pod documented
302 1 return keys %registry;
303 }
304  
305 =head1 COPYRIGHT AND LICENSE
306  
307 COPYRIGHT (C) 2017-2021 The LedgerSMB Core Team
308  
309 Redistribution and use in source and compiled forms with or without
310 modification, are permitted provided that the following conditions are met:
311  
312 =over
313  
314 =item
315  
316 Redistributions of source code must retain the above
317 copyright notice, this list of conditions and the following disclaimer as the
318 first lines of this file unmodified.
319  
320 =item
321  
322 Redistributions in compiled form must reproduce the above copyright
323 notice, this list of conditions and the following disclaimer in the
324 source code, documentation, and/or other materials provided with the
325 distribution.
326  
327 =back
328  
329 THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) "AS IS" AND
330 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
331 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
332 DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR
333 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
334 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
335 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
336 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
337 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
338 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
339  
340  
341 =cut
342  
343 7 1;
« PGObject.pm ↑ Summary PGObject/Util/DBException.pm »