PGObject::Simple(3pm)

๐Ÿ“ฆ
version 3.000002 ยท 2 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

๐Ÿ’– PGObject::Simple ๐Ÿ’–

okay bestie, welcome to the PGObject::Simple era โœจ it's a 3pm-style Perl manual page โ€” you know the vibe, User Contributed Perl Documentation, and it's about to be your new hyperfixation. grab a boba, we're doing stored procedure mapping ๐Ÿง‹


๐ŸŒธ NAME ๐ŸŒธ

PGObject::Simple โ€” a minimalist stored procedure mapper based on LedgerSMB's DBObject. that's it, that's the whole name, no cap ๐Ÿ’…

๐ŸŽ€ VERSION ๐ŸŽ€

Version 3.0.2 โ€” she's grown, she's stable, she's iconic ๐Ÿ‘‘

โœจ SYNOPSIS โœจ

okay so first things first, you load her and make an object:

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],
);

and 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 the move when you're mapping object properties into 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 โ€” the spicy version ๐ŸŒถ๏ธ:

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

๐Ÿฆ‹ EXPORTS ๐Ÿฆ‹

so we now allow various calls to be exported ๐Ÿ“ฆ and honestly? we recommend using the tags. tags are the moment, tags are the vibe ๐Ÿซถ

๐Ÿ’ซ One-at-a-time Exports ๐Ÿ’ซ

  • call_dbmethod
  • call_procedure
  • set_dbh
  • _set_funcprefix
  • _set_funcschema
  • _set_registry

๐ŸŒˆ Export Tags ๐ŸŒˆ

Below are the export tags listed including the leading : used to invoke them. do not forget the colon bestie, the colon is load-bearing ๐Ÿ˜ญ

  • :mapper โ€” call_dbmethod, call_procedure, and set_dbh
  • :full โ€” All methods that can be exported at once.

๐Ÿ‘‘ DESCRIPTION ๐Ÿ‘‘

PGObject::Simple is a top-half object system for PGObject which is simple, and inspired by โ€” and, functionally speaking, a subset 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 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 ๐Ÿซถ


๐Ÿงš SUBROUTINES/METHODS ๐Ÿงš

๐Ÿ“ 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. ๐Ÿ˜Œ

๐Ÿ“ set_dbh($dbh) ๐Ÿ“

Sets the database handle (needs DBD::Pg 2.0 or later) to $dbh. yes bestie, 2.0 or later, don't come at me with the old ones ๐Ÿ™ˆ

๐Ÿ“ dbh ๐Ÿ“

Returns the database handle for the object. that's it, she just hands it over ๐Ÿคฒ

๐Ÿ“ associate($pgobject) ๐Ÿ“

Sets the db handle to that from the $pgobject. sibling energy, sharing the connection ๐Ÿ’ž

๐Ÿ“ _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. mind the boundaries bestie ๐Ÿšง

๐Ÿ“ _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). ๐Ÿ˜Œ

๐Ÿ“ _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. ๐Ÿค

๐Ÿ“ 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. glow up fr fr โœจ

๐Ÿ“ 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. ๐Ÿฅ‡


๐Ÿ’œ 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],
);

๐ŸŒธ AUTHOR ๐ŸŒธ

Chris Travers, "<chris.travers at gmail.com>" โ€” thank you for your service king ๐Ÿซก๐Ÿ’–

๐Ÿ’€ BUGS ๐Ÿ’€

Please report any bugs or feature requests to "bug-pgobject-simple at rt.cpan.org", or through the web interface at <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. bestie we're in this together, mutual updates only ๐Ÿ’Œ

๐Ÿซถ SUPPORT ๐Ÿซถ

You can find documentation for this module with the perldoc command.

perldoc PGObject::Simple

You can also look for information at:

  • RT: CPAN's request tracker (report bugs here)

<http://rt.cpan.org/NoAuth/Bugs.html?Dist=PGObject-Simple>

  • AnnoCPAN: Annotated CPAN documentation

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

  • CPAN Ratings

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

  • Search CPAN

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

๐ŸŒˆ ACKNOWLEDGEMENTS ๐ŸŒˆ

(the original page has this heading with nothing under it โ€” so, iconic of her honestly, an empty section is a mood ๐Ÿ’… nothing to report, no notes โœจ)

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:

  • 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.
  • 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.
๐Ÿšจ 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. ๐Ÿšจ

all caps legalese but make it cute, we love a legally-binding queen ๐Ÿ’–


and that's the whole page bestie! this beauty was built by perl v5.34.0 on 2022-06-16, and yes it's still serving PGObject::Simple(3pm) realness. go map some stored procedures, you absolute legend ๐Ÿฆ‹โœจ๐Ÿ‘‘๐Ÿ’