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?

  • 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... – Maybe_Factor Jan 03 '19 at 05:23
  • see [Discuss this ${blog}](https://softwareengineering.meta.stackexchange.com/a/6418/31260) – gnat Jan 03 '19 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. – David Starkey Jan 03 '19 at 15:30

1 Answers1

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.

nadir
  • 773
  • 1
  • 4
  • 10
  • 1
    Do you have any sources for this claim? – David Starkey Jan 03 '19 at 15:31
  • 2
    No, I responded purely of common sense. Then, I looked into the repository of [mariadb-connector-nodejs](https://github.com/MariaDB/mariadb-connector-nodejs) and I reference you to their [query parsing code](https://github.com/MariaDB/mariadb-connector-nodejs/blob/master/lib/misc/parse.js). Please look on the difference between splitQuery and splitQueryPlaceholder. – nadir Jan 03 '19 at 16:10