Skip to content

Commit 485caf6

Browse files
committed
Fix incorrectly replacing non-placeholders in SQL
fixes #31
1 parent 362fbb6 commit 485caf6

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

HISTORY.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
unreleased
2+
==========
3+
4+
* Fix incorrectly replacing non-placeholders in SQL
5+
16
2.3.0 / 2017-10-01
27
==================
38

lib/SqlString.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,19 @@ SqlString.format = function format(sql, values, stringifyObjects, timeZone) {
8383
}
8484

8585
var chunkIndex = 0;
86-
var placeholdersRegex = /\?\??/g;
86+
var placeholdersRegex = /\?+/g;
8787
var result = '';
8888
var valuesIndex = 0;
8989
var match;
9090

9191
while (valuesIndex < values.length && (match = placeholdersRegex.exec(sql))) {
92-
var value = match[0] === '??'
92+
var len = match[0].length;
93+
94+
if (len > 2) {
95+
continue;
96+
}
97+
98+
var value = len === 2
9399
? SqlString.escapeId(values[valuesIndex])
94100
: SqlString.escape(values[valuesIndex], stringifyObjects, timeZone);
95101

test/unit/test-SqlString.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,11 @@ test('SqlString.format', {
257257
assert.equal(sql, 'SELECT * FROM `table` WHERE id = 42');
258258
},
259259

260+
'triple question marks are ignored': function () {
261+
var sql = SqlString.format('? or ??? and ?', ['foo', 'bar', 'fizz', 'buzz']);
262+
assert.equal(sql, "'foo' or ??? and 'bar'");
263+
},
264+
260265
'extra question marks are left untouched': function() {
261266
var sql = SqlString.format('? and ?', ['a']);
262267
assert.equal(sql, "'a' and ?");

0 commit comments

Comments
 (0)