On Fri, Feb 14, 2014 at 08:33:49AM -0800, Andy Balholm wrote:
> With PostgreSQL, at least, you have the option of passing the entire array
> as a string, using a single placeholder:
>
> db.Query("select 1 = any($1::integer[])", "{1,2,3}")
>
> That way, you can use a single query string, and all the string
> concatenation is confined to the parameter. And if the parameter is
> malformed, you don't get an SQL injection; you just get something like:
> ERROR: invalid input syntax for integer: "xyz"
Right. Note that from the client point of view, we are just passing a string
mapped to a parameter, so any invalid or malicious input is still correctly
quoted by the driver. So even if the input strings came in as
[]string{"1", "2", "3;--drop table xxx"}
and you strings.Join() manually, you're still safe.
However, the whole thing looks rather inelegant to me.
The array coercion occurs much later, relatively speaking.
> I don't know if MySQL can do something like this or not. I doubt the syntax
> is portable, but there may be some equivalent.
The syntax is definitely not portable, but is occasionally useful.
MySQL does not support arrays. I have seen people stuff json arrays and
other delimited data in text fields, but those are all blobs as far the
database is concerned unless you write extensions to grok it.
-Gyepi