PrePAN

Sign in to PrePAN

PGObject::Simple A simple object method to stored procedure mapper

Author
einhverfr@github
Date
URL
Status
Shipped
Good

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!
  );

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 a conventional prefix (by default '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.

Comments

Please sign up to post a review.