blib/lib/PGObject/Composite.pm

filter: Statementstmt 48.1% 51 / 106 Branchbran 12.5% 4 / 32 Conditioncond 11.1% 2 / 18 MC/DCmcdc 8.3% 1 / 12 Subroutinesub 55.1% 16 / 29 Podpod 100% 10 / 10 totaltotal 40.5% 84 / 207 CC 25 SCAR 21.8
CC
25
Cov
37%
_get_funcschema
20.0
_get_registry
20.0
_build_args
20.0
CRAP 8.9
↑ Summary
1 package PGObject::Composite;
2  
3 4 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use PGObject;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use PGObject::Type::Composite; # needed to import routines
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use parent 'Exporter', 'PGObject::Type::Composite';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 our @EXPORT_OK = qw(call_procedure to_db from_db call_dbmethod
13 _get_dbh _get_funcschema _get_funcprefix _get_schema
14 _get_registry _get_typename _get_typeschema
15 default_dbh default_schema default_prefix default_registry);
16 4 our %EXPORT_TAGS = (all => \@EXPORT_OK,
17 mapper => [qw{call_procedure to_db from_db call_dbmethod
18 default_dbh default_schema default_prefix
19 default_registry}],
20 );
21  
22 =head1 NAME
23  
24 PGObject::Composite - Composite Type Mapper for PGObject
25  
26 =head1 VERSION
27  
28 Version 1.0.2
29  
30 =cut
31  
32 4 our $VERSION = 1.000002;
33  
34  
35 =head1 SYNOPSIS
36  
37 This module provides a more object-oriented type of interface for writing
38 stored procedures for PostgreSQL than the Simple mapper. The Composite mapper
39 assumes that the object calling the call_dbmethod function usually wants its
40 type on the first argument. Thus we provide an extra function where this is
41 not the case (call_dbfunction).
42  
43 So we given a cumposite type:
44  
45 CREATE TYPE foo AS (bar int, baz text);
46  
47 and a stored procedure:
48  
49 CREATE OR REPLACE FUNCTION int(foo) returns int language sql as $$
50 SELECT length($1.baz) + $1.bar;
51 $$;
52  
53 We can have a package:
54  
55 package mycomposite;
56 use PGObject::Composite;
57 sub new {
58 my $pkg = shift;
59 bless shift, $pkg;
60 }
61  
62 sub to_int {
63 my $self = shift;
64 my ($ref) = $shelf->call_dbmethod(funcname => 'int');
65 return shift values %$ref;
66 }
67  
68 =head1 SUBROUTINES/METHODS
69  
70 =head2 new
71  
72 This constructs a new object. Basically it copies the incoming hash (one level
73 deep) and then blesses it. If the hash passed in has a dbh member, the dbh
74 is set to that. This does not set the function prefix, as this is assumed to
75 be done implicitly by subclasses.
76  
77 =cut
78  
79 2 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
80 2 my ($self) = shift @_;
81 2 my %args = @_;
82 2 my $ref = {};
83 2 $ref->{$_} = $args{$_} for keys %args;
84 2 bless ($ref, $self);
85 2 $ref->set_dbh($ref->{dbh});
86 2 $ref->_set_funcprefix($ref->{_funcprefix});
87 2 $ref->_set_funcschema($ref->{_funcschema});
88 2 $ref->_set_registry($ref->{_registry});
89 2 ▶ $ref->associate($self) if ref $self;
Branch: 1 branch
BranchTrueFalseTotal
if ref $self 0 2 2
90 2 return $ref;
91 }
92  
93 2 ▶sub _set_funcprefix {
Subroutine _set_funcprefix: called CC=1 SCAR=0.0
94 2 my ($self, $prefix) = @_;
95 2 $self->{_funcprefix} = $prefix;
96 }
97  
98 2 ▶sub _set_funcschema {
Subroutine _set_funcschema: called CC=1 SCAR=0.0
99 2 my ($self, $schema) = @_;
100 2 $self->{_funcschema} = $schema;
101 }
102  
103 2 ▶sub _set_registry {
Subroutine _set_registry: called CC=1 SCAR=0.0
104 2 my ($self, $registry) = @_;
105 2 $self->{_registry} = $registry;
106 }
107  
108 =head2 set_dbh
109  
110 Sets the database handle
111  
112 =cut
113  
114 4 ▶sub set_dbh {
Subroutine set_dbh: called CC=1 SCAR=0.0
Pod documented
115 4 my ($self, $dbh) = @_;
116 4 $self->{_dbh} = $dbh;
117 }
118  
119 0 ▶sub _set_dbh {
Subroutine _set_dbh: not called CC=1 SCAR=6.9
120 0 my ($self, $dbh) = @_;
121 0 $self->set_dbh($dbh);
122 }
123  
124 =head2 dbh
125  
126 returns the dbh of the object
127  
128 =cut
129  
130 4 ▶sub dbh {
Subroutine dbh: called CC=1 SCAR=0.0
Pod documented
131 4 my ($self) = @_;
132 4 return $self->_get_dbh;
133 }
134  
135 4 ▶sub _get_dbh {
Subroutine _get_dbh: called CC=4 SCAR=14.6
136 4 my ($self) = @_;
137 4 ▶ return $self->{_dbh} if ref $self and $self->{_dbh};
Branch: 1 branch
BranchTrueFalseTotal
if ref $self and $self->{"_dbh"} 3 1 4
Condition66%2/3
ref $self and $self->{"_dbh"}
!ll&&!rl&&r
013
MC/DC50%1/2
ref $self and $self->{"_dbh"}
ref $self $self->{"_dbh"}
Truth table66%2/3
A and B
A= ref $selfB= $self->{"_dbh"}
ABresult
0X0
100
111
138 1 ▶ return $self->default_dbh if ref $self;
Branch: 1 branch
BranchTrueFalseTotal
if ref $self 1 0 1
139 0 return "$self"->default_dbh;
140 }
141  
142 =head2 associate
143  
144 Assocates the current object with another PGObject-based class
145  
146 =cut
147  
148 1 ▶sub associate {
Subroutine associate: called CC=1 SCAR=0.0
Pod documented
149 1 my ($self, $other) = @_;
150 1 $self->set_dbh($other->dbh);
151 }
152  
153 =head2 default_dbh
154  
155 returns the dbh used by default. Subclasses must override.
156  
157 =cut
158  
159 1 ▶sub default_dbh {
Subroutine default_dbh: called CC=1 SCAR=0.0
Pod documented
160 1 croak 'Must override default dbh factory';
161 }
162  
163 0 ▶sub _get_funcschema {
Subroutine _get_funcschema: not called CC=4 SCAR=30.0
164 0 my ($self) = @_;
165 0 ▶ return $self->{_funcschema} if ref $self and $self->{_funcschema};
Branch: 1 branch
BranchTrueFalseTotal
if ref $self and $self->{"_funcschema"} 0 0 0
Condition0%0/3
ref $self and $self->{"_funcschema"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $self and $self->{"_funcschema"}
ref $self $self->{"_funcschema"}
Truth table0%0/3
A and B
A= ref $selfB= $self->{"_funcschema"}
ABresult
0X0
100
111
166 0 ▶ return $self->default_schema if ref $self;
Branch: 1 branch
BranchTrueFalseTotal
if ref $self 0 0 0
167 0 return "$self"->default_schema;
168 }
169  
170 =head2 default_schema
171  
172 returns the schema used by default. defaalt is 'public'
173  
174 =cut
175  
176 0 ▶sub default_schema { 'public' }
Subroutine default_schema: not called CC=1 SCAR=6.9
Pod documented
177  
178 0 ▶sub _get_funcprefix {
Subroutine _get_funcprefix: not called CC=3 SCAR=24.8
179 0 my ($self) = @_;
180 0 ▶ return $self->{_funcprefix} if ref $self and $self->{_funcprefix};
Branch: 1 branch
BranchTrueFalseTotal
if ref $self and $self->{"_funcprefix"} 0 0 0
Condition0%0/3
ref $self and $self->{"_funcprefix"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $self and $self->{"_funcprefix"}
ref $self $self->{"_funcprefix"}
Truth table0%0/3
A and B
A= ref $selfB= $self->{"_funcprefix"}
ABresult
0X0
100
111
181 0 return $self->default_prefix;
182 0 return "$self"->default_prefix;
183 }
184  
185 0 ▶sub _get_schema {
Subroutine _get_schema: not called CC=3 SCAR=24.8
186 0 my ($self) = @_;
187 0 ▶ return $self->{_funcprefix} if ref $self and $self->{_funcprefix};
Branch: 1 branch
BranchTrueFalseTotal
if ref $self and $self->{"_funcprefix"} 0 0 0
Condition0%0/3
ref $self and $self->{"_funcprefix"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $self and $self->{"_funcprefix"}
ref $self $self->{"_funcprefix"}
Truth table0%0/3
A and B
A= ref $selfB= $self->{"_funcprefix"}
ABresult
0X0
100
111
188 0 return $self->default_schema;
189 0 return "$self"->default_schema;
190 }
191  
192 0 ▶sub _get_typeschema {
Subroutine _get_typeschema: not called CC=3 SCAR=24.8
193 0 my ($self) = @_;
194 0 ▶ return $self->{_funcprefix} if ref $self and $self->{_funcprefix};
Branch: 1 branch
BranchTrueFalseTotal
if ref $self and $self->{"_funcprefix"} 0 0 0
Condition0%0/3
ref $self and $self->{"_funcprefix"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $self and $self->{"_funcprefix"}
ref $self $self->{"_funcprefix"}
Truth table0%0/3
A and B
A= ref $selfB= $self->{"_funcprefix"}
ABresult
0X0
100
111
195 0 return $self->default_schema;
196 0 return "$self"->default_schema;
197 }
198
199  
200 0 ▶sub _set_schema {
Subroutine _set_schema: not called CC=1 SCAR=6.9
201 0 my ($self, $schema);
202 0 $self->{_schema} = $schema;
203 }
204  
205 =head2 default_prefix
206  
207 returns the prefix used by default. Default is empty string
208  
209 =cut
210  
211 0 ▶sub default_prefix { '' }
Subroutine default_prefix: not called CC=1 SCAR=6.9
Pod documented
212  
213 0 ▶sub _get_registry {
Subroutine _get_registry: not called CC=4 SCAR=30.0
214 0 my ($self) = @_;
215 0 ▶ return $self->{_registry} if ref $self and $self->{_registry};
Branch: 1 branch
BranchTrueFalseTotal
if ref $self and $self->{"_registry"} 0 0 0
Condition0%0/3
ref $self and $self->{"_registry"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $self and $self->{"_registry"}
ref $self $self->{"_registry"}
Truth table0%0/3
A and B
A= ref $selfB= $self->{"_registry"}
ABresult
0X0
100
111
216 0 ▶ return $self->default_registry if ref $self;
Branch: 1 branch
BranchTrueFalseTotal
if ref $self 0 0 0
217 0 return "$self"->default_registry;
218 }
219  
220 =head2 default_registry
221  
222 Returns the registry used by default. Default is 'default'
223  
224 =cut
225  
226 0 ▶sub default_registry { 'default' }
Subroutine default_registry: not called CC=1 SCAR=6.9
Pod documented
227  
228 =head2 call_dbmethod
229  
230 Calls a mapped method with the current object as the argument named "self."
231  
232 This allows for stored procedurs to differentiate what is related to a related
233 type and what is not.
234  
235 =cut
236  
237 0 ▶sub _build_args {
Subroutine _build_args: not called CC=4 SCAR=30.0
238 0 my ($self, $args) = @_;
239 0 delete $args->{$_} for qw(typename typeschema); # invariants
240 0 my %args;
241 %args = (map {
242 0 my $f = "_get_$_";
243 0 ▶ $_ => (ref $self ? $self->$f() : "$self"->$f() )
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 0 0 0
244 }
245 qw(funcschema dbh funcprefix registry typename typeschema));
246 0 ▶ %args = (%args, %$args) if ref $args;
Branch: 1 branch
BranchTrueFalseTotal
if ref $args 0 0 0
247 0 return %args;
248 }
249  
250 0 ▶sub call_dbmethod {
Subroutine call_dbmethod: not called CC=3 SCAR=24.8
Pod documented
251 0 my $self = shift;
252 0 my %args = @_;
253 0 %args = _build_args($self, \%args);
254  
255 my $funcinfo = PGObject->function_info(
256 %args, (argtype1 => $args{typename},
257 argschema => $args{typeschema})
258 0 );
259 0 my @dbargs = (map { my $name = $_->{name};
260 0 $name =~ s/^in_//i;
261 0 ▶ $name eq 'self'? $self : $args{args}->{$name} ;
Branch: 1 branch
BranchTrueFalseTotal
$name eq 'self' ? : 0 0 0
262 0 } @{$funcinfo->{args}});
263 0 my @rows = PGObject->call_procedure(%args, ( args => \@dbargs ));
264 0 ▶ return shift @rows unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
265 0 return @rows;
266 }
267  
268 =head2 call_procedure
269  
270 Maps to PGObject::call_procedure with appropriate defaults.
271  
272 =cut
273  
274 0 ▶sub call_procedure {
Subroutine call_procedure: not called CC=3 SCAR=24.8
Pod documented
275 0 my ($self) = shift @_;
276 0 my %args = @_;
277 0 %args = _build_args($self, \%args);
278  
279 0 ▶ croak 'No DB handle provided' unless $args{dbh};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"dbh"} 0 0 0
280 0 my @rows = PGObject->call_procedure(%args);
281 0 ▶ return shift @rows unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
282 0 return @rows;
283 }
284  
285  
286 =head1 INTERFACES TO OVERRIDE
287  
288 =head2 _get_schema
289  
290 Defaults to public. This is the type's schema
291  
292 =head2 _get_funcschema
293  
294 defaults to public.
295  
296 =head2 _get_typename
297  
298 The name of the composite type. Must be set.
299  
300 =head2 _get_dbh
301  
302 The database connection to use. Must be set.
303  
304 =head1 AUTHOR
305  
306 Chris Travers, C<< <chris at efficito.com> >>
307  
308 =head1 BUGS
309  
310 Please report any bugs or feature requests to C<bug-pgobject-composite at rt.cpan.org>, or through
311 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=PGObject-Composite>. I will be notified, and then you'll
312 automatically be notified of progress on your bug as I make changes.
313  
314  
315  
316  
317 =head1 SUPPORT
318  
319 You can find documentation for this module with the perldoc command.
320  
321 perldoc PGObject::Composite
322  
323  
324 You can also look for information at:
325  
326 =over 4
327  
328 =item * RT: CPAN's request tracker (report bugs here)
329  
330 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=PGObject-Composite>
331  
332 =item * AnnoCPAN: Annotated CPAN documentation
333  
334 L<http://annocpan.org/dist/PGObject-Composite>
335  
336 =item * CPAN Ratings
337  
338 L<http://cpanratings.perl.org/d/PGObject-Composite>
339  
340 =item * Search CPAN
341  
342 L<http://search.cpan.org/dist/PGObject-Composite/>
343  
344 =back
345  
346  
347 =head1 ACKNOWLEDGEMENTS
348  
349  
350 =head1 LICENSE AND COPYRIGHT
351  
352 Copyright 2014 Chris Travers.
353  
354 This program is distributed under the (Revised) BSD License:
355 L<http://www.opensource.org/licenses/BSD-3-Clause>
356  
357 Redistribution and use in source and binary forms, with or without
358 modification, are permitted provided that the following conditions
359 are met:
360  
361 * Redistributions of source code must retain the above copyright
362 notice, this list of conditions and the following disclaimer.
363  
364 * Redistributions in binary form must reproduce the above copyright
365 notice, this list of conditions and the following disclaimer in the
366 documentation and/or other materials provided with the distribution.
367  
368 * Neither the name of Chris Travers's Organization
369 nor the names of its contributors may be used to endorse or promote
370 products derived from this software without specific prior written
371 permission.
372  
373 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
374 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
375 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
376 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
377 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
378 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
379 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
380 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
381 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
382 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
383 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
384  
385  
386 =cut
387  
388 4 1; # End of PGObject::Composite
↑ Summary