Is it somehow possible to use named placeholders where DBI allows @bind_values? E. g., I would like to make statements like:
my $s = $DB->selectcol_arrayref ("SELECT a FROM b
WHERE c = ? OR d = ? OR e = ?;",
{},
$par1, $par2, $par1) or
die ($DB->errstr ());
less prone to mistakes. I’m using DBD::Pg and DBD::SQLite.
What sorts of placeholders (if any) are supported depends on the driver:
But you’re in luck, the PostgreSQL driver supports named or numbered parameters:
And the SQLite driver also supports them:
The downside is that you’ll end up using
bind_parama lot with the named parameters so you won’t be able to use conveniences likeselectcol_arrayrefand$sth->execute(1,2,3)(Note: If anyone knows how to use named placeholders withexecuteI’d appreciate some pointers in a comment, I’ve never figured out how to do it). However, you can use the various forms of number placeholders (such asselect c from t where x = $1for PostgreSQL orselect c from t where x = ?1for SQLite).Also be aware that PostgreSQL uses colons for array slices and question marks for some operators so sometimes the standard ? placeholders and :name named placeholders can cause problems. I’ve never had any problems with ? but I’ve never used the geometric operators either; I suspect that sensible use of whitespace would avoid any problems with ?. If you’re not using PostgreSQL arrays, then you probably don’t have to worry about array slices fighting with your
:namenamed placeholders.Executive Summary: You can’t use named placeholders with
selectcol_arrayrefor similar methods that work with@bind_params. However, with SQLite and Postgresql, you can use numbered placeholders ($1,$2, … for Postgresql or?1,?2, … for SQLite) with the methods that work with@bind_paramsor you can use named placeholders (:namefor both PostgreSQL and SQLite) if you’re happy using the longerprepare/bind_param/execute/fetchsequence of methods and you’ll have to be careful if you use PostgreSQL arrays in your queries.