Skip to content
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

Parameterized query with a "WHERE [string] like" clause #503

Closed
saghm opened this issue Jan 10, 2014 · 4 comments
Closed

Parameterized query with a "WHERE [string] like" clause #503

saghm opened this issue Jan 10, 2014 · 4 comments

Comments

@saghm
Copy link

saghm commented Jan 10, 2014

I want to use the parameterized query feature to search a database for users whose emails contain a given search key. For example, if someone searches the string 'smith', I want to search the database with the query "select id, firstname, lastname from users where email like '%$1%' " with the value array ['smith']. For some reason, when my query is parsed, it doesn't recognize the parameter placeholder in the query.

Example:

var pg = require('pg').native;
var connect = 'my connection string';
var cl = new pg.Client(connect);

cl.connect(function(err) {
    if (err) {
        console.error("no connection: " + err);
        return cl.end();
    } else {
        return cl.query(
            "select id, firstname, lastname from users where email like '%$1%'",
            ['smith'],
            function(err, res) {
                if (err) {
                    console.error(err);
                    return cl.end();
                } else {
                    return console.log(res.rows);
                }
        });
    }
});

Running this (with the correct connection string) prints out the following error:

{ [Error: bind message supplies 1 parameters, but prepared statement "" requires 0]
severity: 'ERROR',
code: '08P01',
file: 'postgres.c',
line: '1500',
routine: 'exec_bind_message' }

However, if I change the query to check for "email like '%smith%' " and remove the array argument, it queries the database without any error and prints out the results.

Am I doing something dumb here that's causing this error? I don't have a lot of experience with node, so I wouldn't put it past me.

If not, I'm guessing this has to do with the way the query is parsed for parameter placeholders (i.e. that it doesn't check for them in the "like" expression). Is there by any chance a way around this? I'd prefer not to have to use a non-parameterized query and just stick the string in the middle, since then I'd have to protect against SQL injection myself (although I'm certainly open to it if there's no other option).

@benighted
Copy link
Contributor

The problem is that pg is reading the $1 as part of the literal string instead of a placeholder because it is wrapped in quotes. It will work if you call it like this without the quotes and with the wildcards in the values array element:

        return cl.query(
            "select id, firstname, lastname from users where email like $1",
            ['%smith%'],
            function(err, res) {
            ...

@saghm
Copy link
Author

saghm commented Jan 10, 2014

Thanks a bunch for the quick and helpful response!

@benighted
Copy link
Contributor

You're welcome 😄

@SamuelKoroh
Copy link

You can make it more robust by using dynamic value from a variable using JS string interpolation
return cl.query(
"select id, firstname, lastname from users where email like $1",
[${search}%],
function(err, res) {
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants