Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

DBD::Pg - function declaration and using alias $n

by lestrrat (Deacon)
on Feb 15, 2005 at 01:57 UTC ( [id://431006]=perlquestion: print w/replies, xml ) Need Help??

lestrrat has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to create a function (stored procedure) on PostgreSQL using DBI, but I'm hitting a roadblock because DBD::Pg apparently recognizes $1, $2, $3 ... as placeholder variables.

For example, the following code produces an error:

$dbh->do(q{ CREATE FUNCTION poo (INTEGER) RETURNS INTEGER AS $func$ DECLARE val ALIAS FOR $1; BEGIN RETURN val * 2; END }); # Error: DBD::Pg::db do failed: execute called with an unbound placeholder [f +or Statement " CREATE FUNCTION poo (INTEGER) RETURNS INTEGER AS $func$ DECLARE val ALIAS FOR $1; BEGIN RETURN val * 2; END "]

$1 is apparently being recognized as a placeholder, but it's an alias. Is there anyway to circumvent this ??

Replies are listed 'Best First'.
Re: DBD::Pg - function declaration and using alias $n
by Thilosophy (Curate) on Feb 15, 2005 at 08:19 UTC
    Slightly off-topic and a shameless (?) plug, but once you have managed to create your stored procedure, please have a look at DBIx::ProcedureCall, which creates Perl wrappers for stored procedures, eliminating the need for writing SQL by hand.

    I just added PostgreSQL support yesterday, and am looking for testers. With it you could call your function like so

    use DBIx::ProcedureCall qw( poo ); my $result = poo ($dbh, 12345);

    As for your problem, I can only offer a work-around: Create the function with the psql command line tool (not with Perl/DBI). You only have to do this once (when installing your program), right?

      Those functions might be nicer if they live in their own application specific namespace. Consider this.

      $dbh->My::App::poo( 12345 ); package My::App; use DBIx::ProcedureCall qw( poo );
        Those functions might be nicer if they live in their own application specific namespace.

        The code you gave above should work, actually. Thanks for pointing out the interesting syntax.

        $dbh->My::App::poo( 12345 );
        I had no idea this works (calling an object with a fully qualified "method" name). I am going to mention this pattern in the module's docs.
Re: DBD::Pg - function declaration and using alias $n
by diotalevi (Canon) on Feb 15, 2005 at 21:04 UTC

    See DBD::Pg and the $dbh->func( ... 'putline' ) function. This will allow you to send arbitrary text to the PostgreSQL server.

    $dbh->func( q{ CREATE FUNCTION poo (INTEGER) RETURNS INTEGER AS $func$ DECLARE val ALIAS FOR $1; BEGIN RETURN val * 2; END } => 'putline' )

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://431006]
Approved by sgifford
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-19 19:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found