sqlfirebirdfirebird2.5node-firebird

How to use a WHERE ... IN ... clause with parameterized arrays in node-firebird?


I'm writing an SQL query using node-firebird with a WHERE ... IN ... clause and a parameterized array, like this:

SELECT * FROM MY_TABLE WHERE MY_FIELD IN ?

And passing the parameter:

[['string1', 'string2']]

However, this gives me the following error:

Dynamic SQL Error, SQL error code = -104, Token unknown - line 1, column 48, ?

(Note: the table and field names in the actual database are different, so the column index mentioned in the error may not be accurate.) What I've Tried:

Wrapping the question mark in parentheses like this:

SELECT * FROM MY_TABLE WHERE MY_FIELD IN (?)

But this resulted in another error:

Dynamic SQL Error, SQL error code = -303, Arithmetic exception, numeric overflow, or string truncation, string right truncation

Queries not involving WHERE ... IN ... work perfectly fine. Hard-coding the array values directly into the query works, and using JetBrains Database Manager to make the parameterized query works as well (array passed as ('myString1', 'myString2')).

Code context:

Here's a bit more context of how I'm executing the query:

pool.get(function (err: any, db) {
  if (err) throw err
  db.query(
    `SELECT * FROM MY_TABLE WHERE MY_FIELD IN ?`,
    [['string1', 'string2']],
    function (err, result) {
      if (err) {
        console.error(err)
      }
      console.debug('result: ' + JSON.stringify(result, null, 2))
      db.detach()
    }
  )
})

Documentation and similar issues:

I checked the node-firebird documentation, but found no specific mention of parameterized IN clauses. A similar question was asked in 2018 on GitHub, but there hasn't been an answer: https://github.com/hgourvest/node-firebird/issues/144

What is the correct syntax for using a WHERE ... IN ... clause with parameterized arrays in node-firebird? Is there an alternative approach to achieve this?


Solution

  • You'll need to embed the correct number of placeholders in your query, contained in parentheses. You can build this part yourself:

    params = ['this', 'that', 'the other', ...];
    
    query = 'SELECT * FROM MY_TABLE WHERE MY_FIELD IN ('
            + Array(params.length).fill('?').join(', ')
            + ')';