Skip to content

Bug: Non-Alphanumeric characters in property names breaks the lib #14

Open
@julius-e

Description

@julius-e

I have columns in my database with . characters in them. named-placeholders breaks when I pass it objects that contain non-alphanum chars in their property names.

i.e.

{
  "my.funky.col.id": 1234
}

The following is a change I made to the param regex that seems to have addressed my specific issue:

const RE_PARAM = /(?:\?)|(?::(\d+|(?:[a-zA-Z][a-zA-Z0-9_]*)))/g

to

const RE_PARAM = /(?:\?)|(?::(\d+|(?:[a-zA-Z][a-zA-Z0-9_\.]*)))/g, // I added \. to the regex

My question is, how should named-placeholders handle non-alphanum chars in column names, since MySQL allows them as long as they're escaped with backticks? Currently RE_PARAM is private, and it seems that there is no one-size-fits-all regex. Expose it as an option on createCompiler?

I saw you changed the regex in #6 so maybe that is the way you want to go with this. Please advise. I'm ready and willing to submit a PR.

Full repro of issue:

var namedPlaceholders = require("named-placeholders")
var np = namedPlaceholders();
np('select * from test where `my.funky.col.id` = :my.funky.col.id', {"my.funky.col.id": 1234})

Returns

["select * from test where `my.funky.col.id` = ?.funky.col.id", [undefined]]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions