This file is indexed.

/usr/share/perl5/PGObject/Simple.pm is in libpgobject-simple-perl 3.000002-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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
package PGObject::Simple;

use 5.010;
use strict;
use warnings;
use Carp;
use PGObject;
use parent 'Exporter';

=head1 NAME

PGObject::Simple - Minimalist stored procedure mapper based on LedgerSMB's DBObject

=head1 VERSION

Version 3.0.2

=cut

our $VERSION = 3.000002;

=head1 SYNOPSIS

  use PGObject::Simple;
  my $obj = PGObject::Simple->new(%myhash);
  $obj->set_dbh($dbh); # Database connection

To call a stored procedure with enumerated arguments.

  my @results = $obj->call_procedure(
      funcname     => $funcname,
      funcschema   => $funcname,
      args         => [$arg1, $arg2, $arg3],
  );

You can add something like a running total as well:

  my @results = $obj->call_procedure(
      funcname      => $funcname,
      funcschema    => $funcname,
      args          => [$arg1, $arg2, $arg3],
      running_funcs => [{agg => 'sum(amount)', alias => 'total'}],
  );

To call a stored procedure with named arguments from a hashref.  This is 
typically done when mapping object properties in to stored procedure arguments.

  my @results = $obj->call_dbmethod(
      funcname      => $funcname,
      funcschema    => $funcname,
      running_funcs => [{agg => 'sum(amount)', alias => 'total'}],
  );

To call a stored procedure with named arguments from a hashref with overrides.

  my @results = $obj->call_dbmethod(
      funcname      => 'customer_save',
      funcschema    => 'public',
      running_funcs => [{agg => 'sum(amount)', alias => 'total'}],
      args          => { id => undef }, # force to create new!
  );


=head1 EXPORTS

We now allow various calls to be exported.  We recommend using the tags.

=head2 One-at-a-time Exports

=over

=item call_dbmethod

=item call_procedure

=item set_dbh

=item _set_funcprefix

=item _set_funcschema

=item _set_registry

=back

=head2 Export Tags

Below are the export tags listed including the leading ':' used to invoke them.

=over

=item :mapper
	    call_dbmethod, call_procedure, and set_dbh

=item :full
	    All methods that can be exported at once.

=back

=cut

our @EXPORT_OK = qw(call_dbmethod call_procedure set_dbh associate dbh
                   _set_funcprefix
                    _set_funcschema _set_registry);

our %EXPORT_TAGS = (mapper => [qw(call_dbmethod call_procedure set_dbh dbh)],
                    full => \@EXPORT_OK);

=head1 DESCRIPTION

PGObject::Simple a top-half object system for PGObject which is simple and
inspired by (and a subset functionally speaking of) the simple stored procedure
object method system of LedgerSMB 1.3. The framework discovers stored procedure
APIs and dispatches to them and can therefore be a base for application-specific
object models and much more.

PGObject::Simple is designed to be light-weight and yet robust glue between your
object model and the RDBMS's stored procedures. It works by looking up the
stored procedure arguments, stripping them of the conventional prefix 'in_', and
mapping what is left to object property names. Properties can be
overridden by passing in a hashrefs in the args named argument. Named arguments
there will be used in place of object properties.

This system is quite flexible, perhaps too much so, and it relies on the
database encapsulating its own logic behind self-documenting stored procedures
using consistent conventions. No function which is expected to be discovered can
be overloaded, and all arguments must be named for their object properties. For
this reason the use of this module fundamentally changes the contract of the
stored procedure from that of a fixed number of arguments in fixed types
contract to one where the name must be unique and the stored procedures must be
coded to the application's interface. This inverts the way we typically think
about stored procedures and makes them much more application friendly.

=head1 SUBROUTINES/METHODS

=head2 new

This constructs a new object.  Basically it copies the incoming hash (one level
deep) and then blesses it.  If the hash passed in has a dbh member, the dbh 
is set to that.  This does not set the function prefix, as this is assumed to 
be done implicitly by subclasses.

=cut

sub new {
    my ($self) = shift @_;
    my %args = @_;
    my $ref = {};
    $ref->{$_} = $args{$_} for keys %args;
    bless ($ref, $self);
    $ref->set_dbh($ref->{dbh});
    $ref->_set_funcprefix($ref->{_funcprefix});
    $ref->_set_funcschema($ref->{_funcschema});
    $ref->_set_registry($ref->{_registry});
    $ref->associate($self) if ref $self;
    return $ref;
}

=head2 set_dbh($dbh)

Sets the database handle (needs DBD::Pg 2.0 or later) to $dbh

=cut

sub set_dbh {
    my ($self, $dbh) = @_;
    $self->{_dbh} = $dbh;
}

=head2 dbh

Returns the database handle for the object.

=cut

sub dbh {
    my ($self) = @_;
    return ($self->{_dbh} or $self->{_DBH});
}

=head2 associate($pgobject)

Sets the db handle to that from the $pgobject.

=cut

sub associate {
    my ($self, $other) = @_;
    $self->set_dbh($other->dbh);
}

=head2 _set_funcprefix

This sets the default funcprefix for future calls.  The funcprefix can still be
overridden by passing in an explicit '' in a call.  This is used to "claim" a 
certain set of stored procedures in the database for use by an object.

It is semi-private, intended to be called by subclasses directly, perhaps in 
constructors, but not from outside the object.

=cut

sub _set_funcprefix {
    my ($self, $funcprefix) = @_;
    $self->{_func_prefix} = $funcprefix;
}

=head2 _set_funcschema 

This sets the default funcschema for future calls.  This is overwridden by 
per-call arguments, (PGObject::Util::DBMethod provides for such overrides on a
per-method basis).

=cut

sub _set_funcschema {
    my ($self, $funcschema) = @_;
    $self->{_func_schema} = $funcschema;
}

=head2 _set_registry

This sets the registry for future calls.  The idea here is that this allows for
application object model wrappers to set which registry they are using, both for
predictability and ensuring that interoperability is possible.

=cut

sub _set_registry {
    my ($self, $registry) = @_;
    $self->{_registry} = $registry;
}

=head2 call_dbmethod

Does a straight-forward mapping (as described below) to the stored procedure 
arguments.  Stored procedure arguments are looked up, a leading 'in_' is 
stripped off where it exists, and the remaining string mapped back to an 
object property.  The $args{args} hashref can be used to override arguments by
name.  Unknown properties are handled simply by passing a NULL in, so the
stored procedures should be prepared to handle these.

As with call_procedure below, this returns a single hashref when called in a
scalar context, and a list of hashrefs when called in a list context.

NEW IN 2.0: We now give preference to functions of the same name over 
properties.  So $obj->foo() will be used before $obj->{foo}.  This enables
better data encapsulation.

=cut

sub _arg_defaults {
    my ($self, %args) = @_;
    local $@;
    if (ref $self) {
        $args{dbh} ||= eval { $self->dbh } ;
        $args{funcprefix} //= eval { $self->funcprefix } ;
        $args{funcschema} //= eval { $self->funcschema } ;
        $args{funcprefix} //= $self->{_func_prefix};
        $args{funcschema} //= $self->{_func_schema};
        $args{funcprefix} //= eval {$self->_get_prefix() };
    } else { 
	# see if we have package-level reader/factories
        $args{dbh} ||= "$self"->dbh; # if eval {"$self"->dbh};
        $args{funcschema} //= "$self"->funcschema if eval {"$self"->funcschema};
        $args{funcprefix} //= "$self"->funcprefix if eval {"$self"->funcprefix};
    }
    $args{funcprefix} //= '';

    return %args
}

sub _self_to_arg { # refactored from map call, purely internal
    my ($self, $args, $argname) = @_;
    my $db_arg;
    $argname =~ s/^in_//;
    local $@;
    if (ref $self and $argname){
        if (eval { $self->can($argname) } ) {
            eval { $db_arg = $self->can($argname)->($self) };
        } else {
            $db_arg = $self->{$argname};
        }
    }
    $db_arg = $args->{args}->{$argname} if exists $args->{args}->{$argname};
    $db_arg = $db_arg->to_db if eval {$db_arg->can('to_db')};

    return $db_arg;
}

sub call_dbmethod {
    my ($self) = shift @_;
    my %args = @_;
    croak 'No function name provided' unless $args{funcname};
    %args = _arg_defaults($self, %args);
    my $info = PGObject->function_info(%args);

    my $arglist = [];
    @{$arglist} = map { _self_to_arg($self, \%args, $_->{name}) } 
                  @{$info->{args}};
    $args{args} = $arglist;

    # The conditional return is necessary since the object may carry a registry
    # --CT
    return $self->call_procedure(%args) if ref $self;
    return __PACKAGE__->call_procedure(%args);
}

=head2 call_procedure 

This is a lightweight wrapper around PGObject->call_procedure which merely
passes the currently attached db connection in.  We use the previously set 
funcprefix and dbh by default but other values can be passed in to override the
default object's values.

This returns a single hashref when called in a scalar context, and a list of 
hashrefs when called in a list context.  When called in a scalar context it 
simply returns the single first row returned.

=cut

sub call_procedure {
    my ($self, %args) = @_;
    %args = _arg_defaults($self, %args);
    croak 'No DB handle provided' unless $args{dbh};
    my @rows = PGObject->call_procedure(%args);
    return shift @rows unless wantarray;
    return @rows;
}

=head1 WRITING CLASSES WITH PGObject::Simple

Unlike PGObject, which is only loosely tied to the functionality in question
and presumes that relevant information will be passed over a functional 
interface, PGObject is a specific framework for object-oriented coding in Perl.
It can therefore be used alone or with other modules to provide quite a bit of
functionality.

A PGObject::Simple object is a blessed hashref with no gettors or setters.  This
is thus ideal for cases where you are starting and just need some quick mappings
of stored procedures to hashrefs.  You reference properties simply with the
$object->{property} syntax.  There is very little encapsulation in objects, and 
very little abstraction except when it comes to the actual stored procedure 
interfaces.   In essence, PGObject::Simple generally assumes that the actual
data structure is essentially a public interface between the database and 
whatever else is going on with the application.

The general methods can then wrap call_procedure and call_dbmethod calls,
mapping out to stored procedures in the database.

Stored procedures must be written to relatively exacting specifications.  
Arguments must be named, with names prefixed optionally with 'in_' (if the 
property name starts with 'in_' properly one must also prefix it).

An example of a simple stored procedure might be:

   CREATE OR REPLACE FUNCTION customer_get(in_id int) returns customer 
   RETURNS setof customer language sql as $$

   select * from customer where id = $1;

   $$;

This stored procedure could then be called with any of:

   $obj->call_dbmethod(
      funcname => 'customer_get', 
   ); # retrieve the customer with the $obj->{id} id

   $obj->call_dbmethod(
      funcname => 'customer_get',
      args     => {id => 3 },
   ); # retrieve the customer with the id of 3 regardless of $obj->{id}

   $obj->call_procedure(
      funcname => 'customer_get',
      args     => [3],
   );

=head1 AUTHOR

Chris Travers, C<< <chris.travers at gmail.com> >>

=head1 BUGS

Please report any bugs or feature requests to C<bug-pgobject-simple at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=PGObject-Simple>.  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


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>

=item * AnnoCPAN: Annotated CPAN documentation

L<http://annocpan.org/dist/PGObject-Simple>

=item * CPAN Ratings

L<http://cpanratings.perl.org/d/PGObject-Simple>

=item * Search CPAN

L<http://search.cpan.org/dist/PGObject-Simple/>

=back


=head1 ACKNOWLEDGEMENTS


=head1 LICENSE AND COPYRIGHT

Copyright 2013-2017 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