2

While setting up a nodejs server with a mariadb database, I found this:

While the recommended method is to use the question mark placeholder, you can alternatively allow named placeholders by setting this query option. Values given in the query must contain keys corresponding to the placeholder names.

This seems odd to me as the named placeholders seem more readable and the ability to use each instance multiple times makes it more flexible. For example, consider this with the ? method:

connection.query(
  "INSERT INTO t VALUES (?, ?, ?)",
  [1,"Mike","5/12/1945"]
)

A named version could look like

connection.query(
  { namedPlaceholders: true,
  "INSERT INTO t VALUES (:id, :name, :dob)" },
  { id: 1, name: "Mike", dob: "5/12/1945" }
)

It also seems much more likely for the data to be in an object format over an array anyway. So why recommend a less readable option? Why not make namedPlaceholders default to true instead?

3
  • 2
    I have no idea what the mariadb people are smoking, but named placeholders are vastly superior to unlabelled placeholders, imo. I can't think of any example when they would be preferable... Commented Jan 3, 2019 at 5:23
  • see Discuss this ${blog}
    – gnat
    Commented Jan 3, 2019 at 15:10
  • @gnat 1) This is from the official kb, not some random blog, so I don't think the intent is to create discussion. 2) I'm also not attempting to create discussion, this is something I am new to and wondering if I am missing something. 3) I'm not sure this is too broad, but potentially opinionated. Perhaps asking for an example the existing way is a better default would be better? It's honestly just something I can't think of a reason for, which I am apparently not the only one. Commented Jan 3, 2019 at 15:30

1 Answer 1

2

Performance.

As of your concern named placeholders are indeed superior for readability/reuse etc.

However, it's much more simple/faster to handle an array of parameters in-order for the insert.

2
  • 1
    Do you have any sources for this claim? Commented Jan 3, 2019 at 15:31
  • 2
    No, I responded purely of common sense. Then, I looked into the repository of mariadb-connector-nodejs and I reference you to their query parsing code. Please look on the difference between splitQuery and splitQueryPlaceholder.
    – nadir
    Commented Jan 3, 2019 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.