ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> perl-dbi-dev
perl-dbi-dev
Function Calling Methods
by David E. Wheeler other posts by this author
May 14 2008 9:39AM messages near this date
Re: Sort hash keys in C... | Re: Function Calling Methods
Howdy dbi-devers,

More and more lately, I'm writing database functions in PL/pgSQL (in  
PostgreSQL) or SQL (in MySQL and others) to do the heavy lifting of  
interacting with database tables. I've been thinking that I'd really  
love a DBI method to call these functions without having to do the  
usual prepare / execute / fetch drill. Even using do() or  
fetchrow_array() seems a bit silly in this context:

     my ($source_id) = $dbh-> fetchrow_array(
         'SELECT get_source_id(?)',
         undef,
         $source,
     );

What I'd love is a couple of DBI methods to do this for me. I  
recognize that this is currently not defined by the DBI, but I'm  
wondering whether it might be time. I've no idea whether JDBC  
implements such an interface, but I was thinking of something like  
this for function calls:

     sub call {
         my $dbh = shift;
         my $func = shift;
         my $places = join ', ', ('?') x @_;
         return $dbh-> fetchrow_array(
             "SELECT $func( $places )",
             undef,
             @_
         );
     }

This would allow me to call a function like so:

   my $val = $dbh-> call('get_source_id', $source );

Which is a much nicer syntax. Drivers might have to modify it, of  
course; for MySQL, it should use CALL rather than SELECT.

For functions or procedures that happen to return sets or a cursor,  
perhaps we could have a separate method that just returns a statement  
handle that's ready to be fetched from?

     sub cursor {
         my $dbh = shift;
         my $func = shift;
         my $places = join ', ', ('?') x @_;
         my $sth = $dbh-> prepare( "SELECT $func( $places )" );
         $sth-> execute(@_);
         return $sth;
     }

Just some ideas. I'm sure that there are more complications than this,  
but even if we could just have something that handles simple functions  
(think last_insert_id() -- eliminate this special case!), I think it'd  
go a long way toward not only simplifying the use of database  
functions in the DBI, but also toward encouraging DBI users to  
actually make more use of database functions.

Thoughts?

Thanks,

David
Thread:
David E. Wheeler
Darren Duncan
David E. Wheeler
Martin Evans

Privacy Policy | Email Opt-out | Feedback | Syndication
© 2004 ActiveState, a division of Sophos All rights reserved