Skip to content

Commit de837a1

Browse files
committed
introducing a possibility to ignore values in escape and escapeId
1 parent 107b66d commit de837a1

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lib/SqlString.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ SqlString.escapeId = function escapeId(val, forbidQualified) {
2323
return sql;
2424
}
2525

26+
if (val instanceof Escaped) {
27+
return String(val);
28+
}
29+
2630
if (forbidQualified) {
2731
return '`' + String(val).replace(/`/g, '``') + '`';
2832
}
@@ -39,7 +43,9 @@ SqlString.escape = function escape(val, stringifyObjects, timeZone) {
3943
case 'boolean': return (val) ? 'true' : 'false';
4044
case 'number': return val + '';
4145
case 'object':
42-
if (val instanceof Date) {
46+
if (val instanceof Escaped) {
47+
return String(val);
48+
} else if (val instanceof Date) {
4349
return SqlString.dateToString(val, timeZone || 'local');
4450
} else if (Array.isArray(val)) {
4551
return SqlString.arrayToList(val, timeZone);
@@ -54,6 +60,10 @@ SqlString.escape = function escape(val, stringifyObjects, timeZone) {
5460
}
5561
};
5662

63+
SqlString.escaped = function escaped(val) {
64+
return new Escaped(val);
65+
};
66+
5767
SqlString.arrayToList = function arrayToList(array, timeZone) {
5868
var sql = '';
5969

@@ -216,3 +226,9 @@ function convertTimezone(tz) {
216226
}
217227
return false;
218228
}
229+
230+
function Escaped(val) {
231+
this.toString = function toString() {
232+
return String(val);
233+
};
234+
}

test/unit/test-SqlString.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ test('SqlString.escapeId', {
3333

3434
'nested arrays are flattened': function() {
3535
assert.equal(SqlString.escapeId(['a', ['b', ['t.c']]]), "`a`, `b`, `t`.`c`");
36+
},
37+
38+
'instances of Escaped are not quoted': function() {
39+
assert.equal(SqlString.escapeId(SqlString.escaped('@a := 42')), "@a := 42");
3640
}
3741
});
3842

@@ -82,6 +86,10 @@ test('SqlString.escape', {
8286
assert.equal(SqlString.escape('Super'), "'Super'");
8387
},
8488

89+
'instances of Escaped get not escaped': function() {
90+
assert.equal(SqlString.escape({a: SqlString.escaped('@a')}), "`a` = @a");
91+
},
92+
8593
'\0 gets escaped': function() {
8694
assert.equal(SqlString.escape('Sup\0er'), "'Sup\\0er'");
8795
assert.equal(SqlString.escape('Super\0'), "'Super\\0'");

0 commit comments

Comments
 (0)