Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: RFC: Placeholder creation for SQL statements

by erix (Prior)
on Aug 29, 2022 at 11:34 UTC ( [id://11146484]=note: print w/replies, xml ) Need Help??


in reply to RFC: Placeholder creation for SQL statements

#prepare my $sth = $dbh->prepare('SELECT * FROM people WHERE lastname = ? AND f +irstname = ?'); #execute with list of bindvars $sth->execute( $lastname, $firstname ); [download] But it's a bit cumbersome to adjust the bind values if the order chang +es.

Re placeholders: PostgreSQL's DBD::Pg has an attribute 'pg_placeholder_dollaronly' which when set to 1 makes placeholdering (ordering or repeating) a little easier (fsvo easy):

$dbh->{pg_placeholder_dollaronly} = 1; # default is 0 my $sth = $dbh->prepare('SELECT * FROM people WHERE lastname = $1 AND +firstname = $2');

Replies are listed 'Best First'.
Re^2: RFC: Placeholder creation for SQL statements
by kikuchiyo (Hermit) on Aug 29, 2022 at 12:23 UTC
    Native Postgresql uses the dollar-integer syntax for placeholders in prepared queries and doesn't support question marks at all, that is emulated by DBD::Pg. That is why you have to take care when you use question marks in SQL comments or as an operator, you have to escape them.

      for ?-containing operators, of course.

      But I can't find a way via DBI to let a postgres server complain about a questionmark in comments. (tried server versions 14, 15, 16). This kinda makes sense because '--'-style comments used to be removed, although '/* */'-style comments were kept. (btw, this will be changed (to both included) in next Pg version, 15)

      (It would be another good reason to use $dbh->{pg_placeholder_dollaronly} = 1;. To me the operator-with-questionmark case is reason enough to use it)

        Hmm, indeed, it seems that DBD::Pg handles question marks in comments correctly now (3.16.0).

        I distinctly remember that this used to be a problem a few years ago.

        But you're right about the operator-with-questionmark case: there is actually a "?" operator in postgres (to test for a key in a jsonb object), and you have to escape it if you want to use it in a query with DBD::Pg (which makes the SQL incompatible if you want to reuse it with e.g. raw psql or Go/sqlc).

Re^2: RFC: Placeholder creation for SQL statements
by LanX (Saint) on Aug 29, 2022 at 21:19 UTC
    as far as I understand you will still need to do $sth->execute( $lastname, $firstname );

    hence I don't know how this is easier, apart of (probably) being able to change the order?

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2026-04-14 12:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.