-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SELECT * FROM table WHERE id IN ($1) #129
Comments
node-postgres uses parameterized queries, so the actual text of the query string is what is passed to postgresql, $1 included, and the parameters are passed separately. Escaping is done by the postgresql server. If you had an array of f.e. ['larry', 'curly', 'moe'] and wanted to search from the table 'stooges' on the column 'name', you would do this:
for queries where the IN value is of arbitrary length, you'll need to construct the "$1, $2, $3" string manually. There are several dozen ways to do this, none of which particularly efficient or elegant. I prefer coffeescript, and this is what I do: inSet = ['larry', 'moe', 'curly']
inSetStr = ("$#{i}" for i in [1..inSet.length]).join(', ') but if you're using straight javascript you might be well served by an enumeration utility library such as underscore. |
@mattly thanks mattly. I need to write this up in the wiki...I think this is the 3rd issue on the subject. |
@brianc of course; I'm just getting into using postgresql from node (as opposed to using it from ruby, or using redis/mongo from node) myself, and recently had to do some research into the issue. To be fair, there's not much documentation on using parameterized queries in general. |
Thanks, @mattly, though this is disappointing. @brianc, I take it that this would be the canonical solution?
as the generated lists of parameters would need to start at different indexes depending on the lengths of all the previous lists. |
Just for the record, this is also a valid request: SELECT * FROM table WHERE col1 IN ($1, $2, $3) AND col2 IN ($1, $2, $3) OR col3 IN ($1, $2, $3) Some people use a regex to create the indexes of the query. Use a special character in the text part of the query and replace it with ascending numbers. |
@booo I'm well aware of that, I do mean different values for each parameter in my example. |
Yes, that's the canonical solution. Other databases have similar stories for doing IN clauses with parameters. |
node-postgres supports arrays (now?), so: SELECT * FROM table WHERE id = ANY ($1) |
The throwOnRelease() function does not appear to be exposed anywhere, and it does not appear to make any sense to have it as a standalone func, as it ovecomplicates things and makes function call as non-returning. Inlined it.
I could not figure how to approach the kind of query stated in the title. I have a dynamic list of values, and I need to use it in an IN operator in the query. Apparently node-postgres does not handle array parameters in such cases; also, I could not find any value escaping methods that would allow me to construct the query without using placeholders in this case. Constructing the query template and parameter list dynamically, adding as many placeholders as needed, seems unacceptably burdensome. What is the proper way to perform such a query?
The text was updated successfully, but these errors were encountered: