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

Status of Named Parameters? #73

Open
ScottFreeCode opened this issue Oct 1, 2016 · 3 comments
Open

Status of Named Parameters? #73

ScottFreeCode opened this issue Oct 1, 2016 · 3 comments

Comments

@ScottFreeCode
Copy link

I see there was some discussion of supporting named parameters in grncdr/node-any-db-adapter-spec#7, but it isn't entirely clear to me from the actual package readmes how to go about using them in the usual any-db paradigm of "write SQL here, get parameters from there, pass both to query" unless I'm supposed to build the SQL using the parameters every time with the any-db-parameters helper.

Ideally, what I'd like is to be able to write code like this:

function queryWithNamedParameters(connection, sql, parameters, callback, prefix) { // parameters is an object/hashmap
  if (!connection.supportsNamedParameters) { // HYPOTHETICAL: this is what I am looking for
    let parser = /([\s\S]*?)('(?:''|[^'])*'|"(?:""|[^"])*"|[[](?:]]|[^\]])*]|\/[*][\s\S]*?[*]\/|--.*?(?:\n|$)|$)/g // breaks SQL up into comments/strings and non-comment/string segments so we can replace variables only outside comments/strings
    let sqlTransformed = ""
    let parametersTransformed = []
    for (let match = parser.exec(sql); match[0] != ""; match = parser.exec(sql)) {
      const parameter = new RegExp(escapeRegExp(prefix || "$") + "([a-zA-Z]+)")
      while (parameter.test(match[1])) {
        parametersTransformed.push(parameters[parameter.exec(match[1])[1]])
        match[1] = match[1].replace(parameter, "?")
      }
      sqlTransformed += match[1] + match[2]
    }
    sql = sqlTransformed
    parameters = parametersTransformed
  }
  return connection.query(sql, parameters, callback)
}

...Which, in case it isn't clear, would basically turn named parameters into positional ones (assuming I've written it right), but only if the specific database doesn't already support positional parameters (in which case they would be used against the database directly).

@tbepdb
Copy link

tbepdb commented Oct 27, 2016

You can try this wrapper https://github.com/tbepdb/node-any-db-bind.

@ScottFreeCode
Copy link
Author

Thanks, I will keep that handy; but I'm really more concerned with whether there's a way to detect whether an any-db-compliant database adaptor already supports named parameters so I only have to use a converter for databases that don't.

@grncdr
Copy link
Owner

grncdr commented Jul 13, 2018

cc @aredridel: I'm consolidating all the any-db stuff into a monorepo, if you are still interested in this issue it will be addressed here.

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