.\" Automatically generated by Pod::Man 2.28 (Pod::Simple 3.28) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{ . if \nF \{ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "PGObject::Simple 3pm" .TH PGObject::Simple 3pm "2014-08-23" "perl v5.20.1" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" PGObject::Simple \- Minimalist stored procedure mapper based on LedgerSMB's DBObject .SH "VERSION" .IX Header "VERSION" Version 1.8 .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 3 \& use PGObject::Simple; \& my $obj = PGObject::Simple\->new(%myhash); \& $obj\->set_dbh($dbh); # Database connection .Ve .PP To call a stored procedure with enumerated arguments. .PP .Vb 5 \& my @results = $obj\->call_procedure( \& funcname => $funcname, \& funcschema => $funcname, \& args => [$arg1, $arg2, $arg3], \& ); .Ve .PP You can add something like a running total as well: .PP .Vb 6 \& my @results = $obj\->call_procedure( \& funcname => $funcname, \& funcschema => $funcname, \& args => [$arg1, $arg2, $arg3], \& running_funcs => [{agg => \*(Aqsum(amount)\*(Aq, alias => \*(Aqtotal\*(Aq}], \& ); .Ve .PP To call a stored procedure with named arguments from a hashref. This is typically done when mapping object properties in to stored procedure arguments. .PP .Vb 5 \& my @results = $obj\->call_dbmethod( \& funcname => $funcname, \& funcschema => $funcname, \& running_funcs => [{agg => \*(Aqsum(amount)\*(Aq, alias => \*(Aqtotal\*(Aq}], \& ); .Ve .PP To call a stored procedure with named arguments from a hashref with overrides. .PP .Vb 6 \& my @results = $obj\->call_dbmethod( \& funcname => \*(Aqcustomer_save\*(Aq, \& funcschema => \*(Aqpublic\*(Aq, \& running_funcs => [{agg => \*(Aqsum(amount)\*(Aq, alias => \*(Aqtotal\*(Aq}], \& args => { id => undef }, # force to create new! \& ); .Ve .SH "DESCRIPTION" .IX Header "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. .PP PGObject::Simple is designed to be light-weight and yet robust glue between your object model and the \s-1RDBMS\s0'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. .PP 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. .SH "SUBROUTINES/METHODS" .IX Header "SUBROUTINES/METHODS" .SS "new" .IX Subsection "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. .SS "set_dbh($dbh)" .IX Subsection "set_dbh($dbh)" Sets the database handle (needs DBD::Pg 2.0 or later) to \f(CW$dbh\fR .SS "_set_funcprefix" .IX Subsection "_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 \*(L"claim\*(R" a certain set of stored procedures in the database for use by an object. .PP It is semi-private, intended to be called by subclasses directly, perhaps in constructors, but not from outside the object. .SS "_set_funcschema" .IX Subsection "_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). .SS "_set_registry" .IX Subsection "_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. .SS "call_dbmethod" .IX Subsection "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 \f(CW$args\fR{args} hashref can be used to override arguments by name. Unknown properties are handled simply by passing a \s-1NULL\s0 in, so the stored procedures should be prepared to handle these. .PP 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. .SS "call_procedure" .IX Subsection "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. .PP 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. .SH "WRITING CLASSES WITH PGObject::Simple" .IX Header "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. .PP 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 \&\f(CW$object\fR\->{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. .PP The general methods can then wrap call_procedure and call_dbmethod calls, mapping out to stored procedures in the database. .PP 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). .PP An example of a simple stored procedure might be: .PP .Vb 2 \& CREATE OR REPLACE FUNCTION customer_get(in_id int) returns customer \& RETURNS setof customer language sql as $$ \& \& select * from customer where id = $1; \& \& $$; .Ve .PP This stored procedure could then be called with any of: .PP .Vb 3 \& $obj\->call_dbmethod( \& funcname => \*(Aqcustomer_get\*(Aq, \& ); # retrieve the customer with the $obj\->{id} id \& \& $obj\->call_dbmethod( \& funcname => \*(Aqcustomer_get\*(Aq, \& args => {id => 3 }, \& ); # retrieve the customer with the id of 3 regardless of $obj\->{id} \& \& $obj\->call_procedure( \& funcname => \*(Aqcustomer_get\*(Aq, \& args => [3], \& ); .Ve .SH "AUTHOR" .IX Header "AUTHOR" Chris Travers, \f(CW\*(C`\*(C'\fR .SH "BUGS" .IX Header "BUGS" Please report any bugs or feature requests to \f(CW\*(C`bug\-pgobject\-simple at rt.cpan.org\*(C'\fR, or through the web interface at . I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. .SH "SUPPORT" .IX Header "SUPPORT" You can find documentation for this module with the perldoc command. .PP .Vb 1 \& perldoc PGObject::Simple .Ve .PP You can also look for information at: .IP "\(bu" 4 \&\s-1RT: CPAN\s0's request tracker (report bugs here) .Sp .IP "\(bu" 4 AnnoCPAN: Annotated \s-1CPAN\s0 documentation .Sp .IP "\(bu" 4 \&\s-1CPAN\s0 Ratings .Sp .IP "\(bu" 4 Search \s-1CPAN\s0 .Sp .SH "ACKNOWLEDGEMENTS" .IX Header "ACKNOWLEDGEMENTS" .SH "LICENSE AND COPYRIGHT" .IX Header "LICENSE AND COPYRIGHT" Copyright 2013\-2014 Chris Travers. .PP Redistribution and use in source and compiled forms with or without modification, are permitted provided that the following conditions are met: .IP "\(bu" 4 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. .IP "\(bu" 4 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. .PP \&\s-1THIS SOFTWARE IS PROVIDED BY THE AUTHOR\s0(S) \*(L"\s-1AS IS\*(R" 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\s0(S) \s-1BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES \&\s0(\s-1INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES\s0; \&\s-1LOSS OF USE, DATA, OR PROFITS\s0; \s-1OR BUSINESS INTERRUPTION\s0) \s-1HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT \&\s0(\s-1INCLUDING NEGLIGENCE OR OTHERWISE\s0) \s-1ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\s0