Bounty update: Already got a very good answer from Mark. Adapted := into :, below. However, I'm still looking for similar schemes besides DBIx. I'm just interested in being compatible to anything.
I need advise on the syntax I've picked for "extended" placeholders in parameterized SQL statements. Because building some constructs (IN clauses) was bugging me, I decided on a few syntax shortcuts that automatically expand into ordinary ? placeholders.
I like them. But I want to package it up for distribution, and am asking myself if they are easily understandable.
Basically my new placeholders are
:? (enumerated params) and
:: (for named placeholders) with following use cases:
-> db(" SELECT * FROM all WHERE id IN (??) ", [$a, $b, $c, $d, $e])
?? expands into
?,?,?,?,?,... depending on the number of $args to my db() func. This one is pretty clear, and its syntax is already sort of standardized. Perls DBIx::Simple uses it too. So I'm pretty certain this is an acceptable idea.
-> db(" SELECT :? FROM any WHERE id>0 ", ["title", "frog", "id"]); // Note: not actually parameterized attr, needs cleanup regex
Admit it. I just liked the smiley. Basically this
:? placeholder expands an associative $args into plain column names. It throws away any $args values in fact. It's actually useful for INSERTs in conjunction with ??, and sometimes for IN clauses. But here I'm already wondering if this new syntax is sensible, or not just a misnomer because it mixes : and ? characters. But somehow it seems to match the syntax scheme well.
-> db(" UPDATE some SET :, WHERE :& AND (:|) ", $row, $keys, $or);
Here the mnemonic
:, expands into a list of
name=:name pairs separated by
, commas. Whereas the
:& is a column=:column list joined by
ANDs. For parity I've added
:|. The :& has other use cases out of UPDATE commands, though.
But my question is not about the usefulness, but if :, and :& appear to be rememberable?
-> db(" SELECT * FROM all WHERE name IN (::) ", $assoc);
After some though I also added
:: to interpolate a
:named,:value,:list very much like
?? expands to
?,?,?. Similar use cases, and sensible to have for uniformness.
Anyway, has anybody else implemented a scheme like that? Different placeholders? Or which would you recommend for simplicity? Update: I know that the PHP Oracle OCI interface can also bind array parameters, but doesn't use specific placeholders for it. And I'm looking for comparable placeholder syntaxes.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki