/usr/share/perl5/PGObject/Util/DBMethod.pm is in libpgobject-util-dbmethod-perl 1.00.002-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 | package PGObject::Util::DBMethod;
use 5.008;
use strict;
use warnings;
use Exporter 'import';
=head1 NAME
PGObject::Util::DBMethod - Declarative stored procedure <-> object mappings for
the PGObject Framework
=head1 VERSION
Version 1.00.002
=cut
our $VERSION = '1.00.002';
=head1 SYNOPSIS
Without PGObject::Util::DBobject, you would:
sub mymethod {
my ($self) = @_;
return $self->call_dbmethod(funcname => 'foo');
}
With this you'd do this instead:
dbmethod mymethod => (funcname => 'foo');
=head1 EXPORT
This exports only dbmethod, which it always exports.
=cut
our @EXPORT = qw(dbmethod);
=head1 SUBROUTINES/METHODS
=head2 dbmethod
use as dbmethod (name => (default_arghash))
For example:
package MyObject;
use PGObject::Utils::DBMethod;
dbmethod save => (
strict_args => 0,
funcname => 'save_user',
funcschema => 'public',
args => { admin => 0 },
);
$MyObject->save(args => {username => 'foo', password => 'bar'});
Special arguments are:
=over
=item arg_lit
It set must point to a hashref. Used to allow mapping of function arguments
to arg hash elements. If this is set then funcname, funcschema, etc, cannot be
overwritten on the call.
=item strict_args
If true, args override args provided by user.
=item returns_objects
If true, bless returned hashrefs before returning them.
=item merge_back
If true, merges the first record back to the $self at the end before returning,
and returns $self. Note this is a copy only one layer deep which is fine for
the use case of merging return values from the database into the current
object.
=back
=cut
sub dbmethod {
my $name = shift;
my %defaultargs = @_;
my ($target) = caller;
my $coderef = sub {
my $self = shift @_;
my %args;
if ($defaultargs{arg_list}){
%args = ( args => _process_args($defaultargs{arg_list}, @_) );
} else {
%args = @_;
}
for my $key (keys %{$defaultargs{args}}){
$args{args}->{$key} = $defaultargs{args}->{$key}
unless $args{args}->{$key} or $defaultargs{strict_args};
$args{args}->{$key} = $defaultargs{args}->{$key}
if $defaultargs{strict_args};
}
for my $key(keys %defaultargs){
next if grep(/^$key$/, qw(strict_args args returns_objects));
$args{$key} = $defaultargs{$key} if $defaultargs{$key};
}
my @results = $self->call_dbmethod(%args);
if ($defaultargs{returns_objects}){
for my $ref(@results){
$ref = "$target"->new(%$ref);
}
}
if ($defaultargs{merge_back}){
_merge($self, shift @results);
return $self;
}
return shift @results unless wantarray;
return @results;
};
no strict 'refs';
*{"${target}::${name}"} = $coderef;
}
# private function _merge($dest, $src)
# used to merge incoming db rows to a hash ref.
# hash table entries in $src overwrite those in $dest.
# Since this is an incoming row, we can generally assume we are not having to
# do a deep copy.
sub _merge {
my ($dest, $src) = @_;
if (eval {$dest->can('has') and $dest->can('extends')}){
# Moo or Moose. Use accessors, though better would be to just return
# objects in this case.
for my $att (keys %$src){
$dest->can($att)->($dest, $src->{$att}) if $dest->can($att);
}
} else {
$dest->{$_} = $src->{$_} for (keys %$src);
}
}
# private method _process_args.
# first arg $arrayref of argnames
# after that we just pass in @_ from the function call
# then we return a hash with the args as specified.
sub _process_args {
my $arglist = shift @_;
my @args = @_;
my $arghref = {};
my $maxlen = scalar @_;
my $it = 1;
for my $argname (@$arglist){
last if $it > $maxlen;
$arghref->{$argname} = shift @args;
++$it;
}
return $arghref;
}
=head1 AUTHOR
Chris Travers, C<< <chris.travers at gmail.com> >>
=head1 BUGS
Please report any bugs or feature requests to C<bug-pgobject-util-dbmethod at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=PGObject-Util-DBMethod>. 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::Util::DBMethod
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-Util-DBMethod>
=item * AnnoCPAN: Annotated CPAN documentation
L<http://annocpan.org/dist/PGObject-Util-DBMethod>
=item * CPAN Ratings
L<http://cpanratings.perl.org/d/PGObject-Util-DBMethod>
=item * Search CPAN
L<http://search.cpan.org/dist/PGObject-Util-DBMethod/>
=back
=head1 ACKNOWLEDGEMENTS
=head1 LICENSE AND COPYRIGHT
Copyright 2014 Chris Travers.
This program is released under the following license: BSD
=cut
1; # End of PGObject::Util::DBMethod
|