Skip to content

Commit d75ff3c

Browse files
committed
Merge pull request mysqljs#608 from Loddan/identifiers-array
Identifiers array
2 parents d3a8d98 + 89ee4e2 commit d75ff3c

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

Readme.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -490,9 +490,13 @@ Alternatively, you can use `??` characters as placeholders for identifiers you w
490490
like to have escaped like this:
491491

492492
```js
493-
connection.query('SELECT * FROM ?? WHERE id = ?', ['users', userId], function(err, results) {
493+
var userId = 1;
494+
var columns = ['username', 'email'];
495+
var query = connection.query('SELECT ?? FROM ?? WHERE id = ?', [columns, 'users', userId], function(err, results) {
494496
// ...
495497
});
498+
499+
console.log(query.sql); // SELECT `username`, `email` FROM `users` WHERE id = 1
496500
```
497501
**Please note that this last character sequence is experimental and syntax might change**
498502

lib/protocol/SqlString.js

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
var SqlString = exports;
22

33
SqlString.escapeId = function (val, forbidQualified) {
4+
if (Array.isArray(val)) {
5+
return val.map(function(v) {
6+
return SqlString.escapeId(v, forbidQualified);
7+
}).join(', ');
8+
}
9+
410
if (forbidQualified) {
511
return '`' + val.replace(/`/g, '``') + '`';
612
}

test/unit/protocol/test-SqlString.js

+10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ var test = require('utest');
33
var assert = require('assert');
44
var SqlString = require(common.lib + '/protocol/SqlString');
55

6+
test('SqlString.escapeId', {
7+
'arrays are turned into lists': function() {
8+
assert.equal(SqlString.escapeId(['a', 'b', 't.c']), "`a`, `b`, `t`.`c`");
9+
},
10+
11+
'nested arrays are flattened': function() {
12+
assert.equal(SqlString.escapeId(['a', ['b', ['t.c']]]), "`a`, `b`, `t`.`c`");
13+
},
14+
});
15+
616
test('SqlString.escape', {
717
'undefined -> NULL': function() {
818
assert.equal(SqlString.escape(undefined), 'NULL');

0 commit comments

Comments
 (0)