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

Question mark params not being replaced in runSql #19

Open
cullylarson opened this issue May 23, 2019 · 2 comments
Open

Question mark params not being replaced in runSql #19

cullylarson opened this issue May 23, 2019 · 2 comments

Comments

@cullylarson
Copy link

cullylarson commented May 23, 2019

I have some code like this:

db.runSql('INSERT INTO authGroups (id, isActive, isLocked, name, description, created) VALUES(?, TRUE, TRUE, ?, ?, NOW())', [id, name, description])

I expect that the question marks in sql will be replaced with the values from params (as the documentation describes). But the sql is being run with the question marks still there. I'm getting this error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?, TRUE, TRUE, ?, ?, NOW())'

These are the values of id, name, description:

id: '4dfad450-7c17-11e9-8808-0242ac170002'
name: 'Users'
description: 'Test.'

Btw, I'm using promises, not the callback function.

What's strange is if I add a callback function as the third parameter, the placeholders are replaced. This seems like a bug.

If I use this function instead of db.runSql, it works fine:

const runSql = (db, query, params) => new Promise((resolve, reject) => {
    db.runSql(query, params, (err, data) => {
        if(err) reject(err)
        else resolve(data)
    })
})

For example:

runSql(db, 'INSERT INTO authGroups (id, isActive, isLocked, name, description, created) VALUES(?, TRUE, TRUE, ?, ?, NOW())', [id, name, description])

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@benbotto
Copy link

benbotto commented Aug 8, 2019

https://github.com/db-migrate/mysql/blob/master/index.js#L375 That function's called from runSql and it's not quite right. It shift the first argument (sql), then pops the last. It blindly expects the last argument to be callback. It should probably check if the last argument is an array and, if so, treat it as params. If I get some time I'll try to fix it and file a PR.

@patlillis
Copy link

I am running into this bug as well. This prevents runSql from being used with Promises and async/await, which is a huge pain.

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

3 participants