3 ms·
Do you have an example? I can only imagine three scenarios: - SQL logic. We should be able to write this as a literal query, parameterised as needed, e.g. (mad
by chriswarbo 5y ago
Do you have an example? I can only imagine three scenarios:
- SQL logic. We should be able to write this as a literal query, parameterised as needed, e.g. (made up syntax)
query(
addParam(
"MyParam",
$myParam,
SQL"SELECT foo FROM tbl WHERE @MyParam IS NULL OR bar = @MyParam"
)
)
- PHP logic. This is just ordinary control flow, like anything else, e.g.
query(
($myParam === null)
? SQL"SELECT foo FROM tbl"
: addParam("MyParam", $myParam, SQL"SELECT foo FROM tbl WHERE bar = @MyParam")
)
- A mixture of SQL logic and PHP logic. This seems inherently unsafe to me, so it's not surprising that safety guarantees can't be proven. My point is that such things should be be made difficult ("artificially", if needed), such that nobody would choose to go down that route when another option is available.
- michaelmior 5y agoI'm not quite sure what you're asking for an example of. But suppose you have a table tbl(foo, bar, baz). Depending on user input, you may want to query on any combination of foo, bar, and baz. With this and larger number of columns, it becomes impractical to have conditions for every combination. One approach would be to construct a list of conditions as well as a list of parameters to be substituted. Shown below without any particular language syntax, but hopefully comprehensible. conditions = ("foo=?", "bar>?") parameters = (fooValue, barValue) Then when building the SQL, you join the conditions together with AND and substitute in the parameters. This works in the sense that you are still prevented from injection. But it's rather messy. I suppose perhaps you can actually do what I'm discussing with some ORMs in a reasonably clean way. But my point is that most SQL interfaces make it easy to parameterize a single set of fixed values, but hard to do so for table and column names. Arguably this is a feature not a bug since you probably want to avoid such parameterization anyway. But having a safer way to do so would be nice.