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

Nested arrays aren't formatted correctly #26

Closed
c24w opened this issue Oct 25, 2017 · 1 comment
Closed

Nested arrays aren't formatted correctly #26

c24w opened this issue Oct 25, 2017 · 1 comment
Assignees
Labels

Comments

@c24w
Copy link

c24w commented Oct 25, 2017

Nested arrays are turned into grouped lists (for bulk inserts), e.g. [['a', 'b'], ['c', 'd']] turns into ('a', 'b'), ('c', 'd')

However, this is the behaviour:

require('sqlstring').format('?', [ [12, 34], [56, 78] ])
// => '12, 34'

It works correctly when using escape:

require('sqlstring').escape([ [12, 34], [56, 78] ])
// => '(12, 34), (56, 78)'
@dougwilson
Copy link
Member

So the issue is that the format function takes two arguments: first is the sting of SQL with ? placeholders and the second is an array where each element corresponds to each ?. You see, you called it as the following:

require('sqlstring').format('?', [ [12, 34], [56, 78] ])

Where that deconstructs as:

let arg1 = '?'
let arg2 = [ [12, 34], [56, 78] ]
require('sqlstring').format(arg1, arg2)

So the first ? in your arg1 will be the first element in the array arg2, which is arg2[0], which is just [12, 34]. Really you wanted that to be the entire thing.

The correct call is the following:

require('sqlstring').format('?', [ [ [12, 34], [56, 78] ] ])
// => '(12, 34), (56, 78)'

@dougwilson dougwilson self-assigned this Jan 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants