Skip to content

Commit e953232

Browse files
committed
escapeQuery shouldn't modify provided array
1 parent 48815f3 commit e953232

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

lib/Helpers.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
// into:
55
// "name LIKE 'John' AND age > 23"
66
module.exports.escapeQuery = function (Dialect, query, args) {
7-
return query.replace(/[?]+/g, function (match) {
7+
var pos = 0;
8+
9+
return query.replace(/\?{1,2}/g, function (match) {
810
if (match == '?') {
9-
return Dialect.escapeVal(args.shift());
11+
return Dialect.escapeVal(args[pos++]);
1012
} else if (match == '??') {
11-
return Dialect.escapeId(args.shift());
13+
return Dialect.escapeId(args[pos++]);
1214
}
1315
});
1416
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"sql",
77
"query"
88
],
9-
"version": "0.1.19",
9+
"version": "0.1.20",
1010
"license": "MIT",
1111
"repository": {
1212
"url": "http://github.com/dresende/node-sql-query"

test/lib/test-helpers.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,16 @@ assert.equal(
3333
Helpers.escapeQuery(Dialect, "SELECT * FROM abc WHERE LOWER(abc.??) LIKE ? AND ?? == ?", ['stuff', 'peaches', 'number']),
3434
"SELECT * FROM abc WHERE LOWER(abc.`stuff`) LIKE 'peaches' AND `number` == NULL"
3535
);
36+
37+
// Should match at most 2 '?' at a time
38+
assert.equal(
39+
Helpers.escapeQuery(Dialect, "?????", ['a', 'b', 'c']),
40+
"`a``b`'c'"
41+
);
42+
43+
// Should not modify provided array
44+
var arr = ['a', 'b', 'c'];
45+
assert.equal(
46+
arr.join(','),
47+
'a,b,c'
48+
)

0 commit comments

Comments
 (0)