Skip to content

Commit 31214eb

Browse files
authored
Merge pull request #1251 from oamaok/master
Allow unnamed placeholders even if the namedPlaceholders flag is enabled
2 parents 442d304 + b5ad716 commit 31214eb

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

documentation/Extras.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Named placeholders
44

5-
You can use named placeholders for parameters by setting `namedPlaceholders` config value or query/execute time option. Named placeholders are converted to unnamed `?` on the client (mysql protocol does not support named parameters). If you reference parameter multiple times under the same name it is sent to server multiple times.
5+
You can use named placeholders for parameters by setting `namedPlaceholders` config value or query/execute time option. Named placeholders are converted to unnamed `?` on the client (mysql protocol does not support named parameters). If you reference parameter multiple times under the same name it is sent to server multiple times. Unnamed placeholders can still be used by providing the values as an array instead of an object.
66

77
```js
88
connection.config.namedPlaceholders = true;
@@ -18,8 +18,14 @@ connection.execute('select :x + :x as z', { x: 1 }, (err, rows) => {
1818
connection.query('select :x + :x as z', { x: 1 }, (err, rows) => {
1919
// query select 1 + 1 as z
2020
});
21+
22+
// unnamed placeholders are still valid if the values are provided in an array
23+
connection.query('select ? + ? as z', [1, 1], (err, rows) => {
24+
// query select 1 + 1 as z
25+
});
2126
```
2227

28+
2329
## Receiving rows as array of columns instead of hash with column name as key:
2430

2531
```js

lib/connection.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,11 @@ class Connection extends EventEmitter {
488488
_resolveNamedPlaceholders(options) {
489489
let unnamed;
490490
if (this.config.namedPlaceholders || options.namedPlaceholders) {
491+
if (Array.isArray(options.values)) {
492+
// if an array is provided as the values, assume the conversion is not necessary.
493+
// this allows the usage of unnamed placeholders even if the namedPlaceholders flag is enabled.
494+
return
495+
}
491496
if (convertNamedPlaceholders === null) {
492497
convertNamedPlaceholders = require('named-placeholders')();
493498
}

test/integration/connection/test-named-paceholders.js renamed to test/integration/connection/test-named-placeholders.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,23 @@ connection.query('SELECT :a + :a as sum', { a: 2 }, (err, rows) => {
7171
connection.end();
7272
});
7373

74-
const sql = connection.format(
74+
const namedSql = connection.format(
7575
'SELECT * from test_table where num1 < :numParam and num2 > :lParam',
7676
{ lParam: 100, numParam: 2 }
7777
);
78-
assert.equal(sql, 'SELECT * from test_table where num1 < 2 and num2 > 100');
78+
assert.equal(
79+
namedSql,
80+
'SELECT * from test_table where num1 < 2 and num2 > 100'
81+
);
82+
83+
const unnamedSql = connection.format(
84+
'SELECT * from test_table where num1 < ? and num2 > ?',
85+
[2, 100]
86+
);
87+
assert.equal(
88+
unnamedSql,
89+
'SELECT * from test_table where num1 < 2 and num2 > 100'
90+
);
7991

8092
const pool = common.createPool();
8193
pool.config.connectionConfig.namedPlaceholders = true;

0 commit comments

Comments
 (0)