/usr/share/perl5/PGObject/Simple/Role.pm is in libpgobject-simple-role-perl 1.12.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | package PGObject::Simple::Role;
use 5.006;
use strict;
use warnings;
use Moo::Role;
use PGObject::Simple;
use Carp;
=head1 NAME
PGObject::Simple::Role - Moo/Moose mappers for minimalist PGObject framework
=head1 VERSION
Version 1.12.0
=cut
our $VERSION = '1.12.0';
=head1 SYNOPSIS
Take the following (Moose) class:
package MyAPP::Foo;
use PGObject::Util::DBMethod;
use Moose;
with 'PGObject::Simple::Role';
has id => (is => 'ro', isa => 'Int', required => 0);
has foo => (is => 'ro', isa => 'Str', required => 0);
has bar => (is => 'ro', isa => 'Str', required => 0);
has baz => (is => 'ro', isa => 'Int', required => 0);
sub get_dbh {
return DBI->connect('dbi:Pg:dbname=foobar');
}
# PGObject::Util::DBMethod exports this
dbmethod int => (funcname => 'foo_to_int');
And a stored procedure:
CREATE OR REPLACE FUNCTION foo_to_int
(in_id int, in_foo text, in_bar text, in_baz int)
RETURNS INT LANGUAGE SQL AS
$$
select char_length($2) + char_length($3) + $1 * $4;
$$;
Then the following Perl code would work to invoke it:
my $foobar = MyApp->foo(id => 3, foo => 'foo', bar => 'baz', baz => 33);
$foobar->call_dbmethod(funcname => 'foo_to_int');
The following will also work since you have the dbmethod call above:
my $int = $foobar->int;
The full interface of call_dbmethod and call_procedure from PGObject::Simple are
supported, and call_dbmethod is effectively wrapped by dbmethod(), allowing a
declarative mapping.
=head1 DESCRIPTION
=head1 ATTRIBUTES AND LAZY GETTERS
=cut
# Private attribute for database handle, not intended to be directly set.
has _DBH => (
is => 'lazy',
isa => sub {
croak "Expected a database handle. Got $_[0] instead"
unless eval {$_[0]->isa('DBI::db')};
},
);
sub _build__DBH {
my ($self) = @_;
return $self->_get_dbh;
}
has _Registry => (is => 'lazy');
sub _build__Registry {
return _get_registry();
}
=head2 _get_registry
This is a method the consuming classes can override in order to set the
registry of the calls for type mapping purposes.
=cut
sub _get_registry{
return undef;
}
has _funcschema => (is => 'lazy');
=head2 _get_schema
Returns the default schema associated with the object.
=cut
sub _build__funcschema {
return $_[0]->_get_schema;
}
sub _get_schema {
return undef;
}
has _funcprefix => (is => 'lazy');
=head2 _get_prefix
Returns string, default is an empty string, used to set a prefix for mapping
stored prcedures to an object class.
=cut
sub _build__funcprefix {
return $_[0]->_get_prefix;
}
sub _get_prefix {
return '';
}
has _PGObject_Simple => (
is => 'lazy',
);
sub _build__PGObject_Simple {
my ($self) = @_;
return PGObject::Simple->new() unless ref $self;
$self->_DBH;
$self->_funcprefix;
my $obj = PGObject::Simple->new(%$self);
$obj->_set_registry($self->_registry);
return $obj;
}
has _registry => ( is => 'lazy' );
sub _build__registry {
return _get_registry();
}
=head2 _get_dbh
Subclasses or sub-roles MUST implement a function which returns a DBI database
handle (DBD::Pg 2.0 or hgher required). If this is not overridden an exception
will be raised.
=cut
sub _get_dbh {
croak 'Subclasses MUST set their own get_dbh methods!';
}
=head2 call_procedure
Identical interface to PGObject::Simple->call_procedure.
This can be used on objects or on the packages themselves. I.e.
mypackage->call_procedure() and $myobject->call_procedure() both work.
=cut
sub call_procedure {
my $self = shift @_;
my %args = @_;
my $obj = _build__PGObject_Simple($self);
$obj->{_DBH} = $self->_DBH if ref $self and !$args{dbh};
$obj->{_DBH} = "$self"->_get_dbh unless ref $self or $args{dbh};
if (ref $self){
$args{funcprefix} = $self->_funcprefix
unless defined $args{funcprefix} or !ref $self;
$args{funcschema} = $self->_funcschema
unless defined $args{funcschema} or !ref $self;
} else {
$args{funcprefix} = "$self"->_get_prefix
unless defined $args{funcprefix} or ref $self;
$args{funcschema} = "$self"->_get_schema
unless defined $args{funcschema} or ref $self;
}
my @rows = $obj->call_procedure(%args);
for my $row (@rows){
for (keys %$row){
delete $row->{$_} unless defined $row->{$_};
}
}
return @rows if wantarray;
return shift @rows;
}
=head2 call_dbmethod
Identical interface to PGObject::Simple->call_dbmethod
This can be used on objects or on the packages themselves. I.e.
mypackage->call_dbmethod() and $myobject->call_dbmethod() both work.
=cut
sub call_dbmethod {
my $self = shift @_;
my %args = @_;
croak 'No function name provided' unless $args{funcname};
$args{dbh} = $self->_DBH if ref $self and !$args{dbh};
$args{dbh} = "$self"->_get_dbh() unless $args{dbh};
if (ref $self){
$args{funcprefix} = $self->_funcprefix unless defined $args{funcprefix};
$args{funcschema} = $self->_funcschema unless $args{funcschema};
} else {
$args{funcprefix} = "$self"->_get_prefix
unless defined $args{funcprefix};
$args{funcschema} = "$self"->_get_schema unless $args{funcschema};
}
$args{funcprefix} ||= '';
my $info = PGObject->function_info(%args);
my $dbargs = [];
@$dbargs = map {
my $argname = $_->{name};
my $db_arg;
$argname =~ s/^in_//;
local $@;
eval { $db_arg = $self->can($argname)->($self) } if ref $self and $argname;
$db_arg = $args{args}->{$argname} if $args{args}->{$argname};
$db_arg = $db_arg->to_db if eval {$db_arg->can('to_db')};
$db_arg = { type => 'bytea', value => $db_arg} if $_->{type} eq 'bytea';
$db_arg;
} @{$info->{args}};
$args{args} = $dbargs;
my @rows;
if (ref $self){
@rows = $self->call_procedure(%args);
} else {
@rows = "$self"->call_procedure(%args);
}
return @rows if wantarray;
return shift @rows;
}
=head1 REMOVED METHODS
These methods were once part of this package but have been removed due to
the philosophy of not adding framework dependencies when an application
dependency can work just as well.
=head2 dbmethod
Included in versions 0.50 - 0.71.
Instead of using this directly, use:
use PGObject::Util::DBMethod;
instead. Ideally this should be done in your actual class since that will
allow you to dispense with the extra parentheses. However, if you need a
backwards-compatible and central solution, since PGObject::Simple::Role
generally assumes sub-roles will be created for managing db connections etc.
you can put the use statement there and it will have the same impact as it did
here when it was removed with the benefit of better testing.
=head1 AUTHOR
Chris Travers,, C<< <chris.travers at gmail.com> >>
=head1 BUGS
Please report any bugs or feature requests to C<bug-pgobject-simple-role at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=PGObject-Simple-Role>. I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc PGObject::Simple::Role
You can also look for information at:
=over 4
=item * RT: CPAN's request tracker (report bugs here)
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=PGObject-Simple-Role>
=item * AnnoCPAN: Annotated CPAN documentation
L<http://annocpan.org/dist/PGObject-Simple-Role>
=item * CPAN Ratings
L<http://cpanratings.perl.org/d/PGObject-Simple-Role>
=item * Search CPAN
L<http://search.cpan.org/dist/PGObject-Simple-Role/>
=back
=head1 ACKNOWLEDGEMENTS
=head1 LICENSE AND COPYRIGHT
Copyright 2013-2014 Chris Travers,.
Redistribution and use in source and compiled forms with or without
modification, are permitted provided that the following conditions are met:
=over
=item
Redistributions of source code must retain the above
copyright notice, this list of conditions and the following disclaimer as the
first lines of this file unmodified.
=item
Redistributions in compiled form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
source code, documentation, and/or other materials provided with the
distribution.
=back
THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
=cut
1; # End of PGObject::Simple::Role
|