Skip to content

Commit 3ce9704

Browse files
dovidgefdougwilson
authored andcommitted
Add .toSqlString() escapeId overriding
closes #57 closes #58
1 parent eac0f22 commit 3ce9704

File tree

4 files changed

+19
-0
lines changed

4 files changed

+19
-0
lines changed

HISTORY.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
unreleased
2+
==========
3+
4+
* Add `.toSqlString()` escapeId overriding
5+
16
2.3.3 / 2022-03-06
27
==================
38

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@ console.log(sql); // SELECT `username`, `email` FROM `users` WHERE id = 1
172172
```
173173
**Please note that this last character sequence is experimental and syntax might change**
174174

175+
To skip escaping one or more of the columns names that you pass to `SqlString.escapeId()`
176+
you may use `SqlString.raw()` similarly to how it is used with `SqlString.escape()`.
177+
See above for more details.
178+
175179
When you pass an Object to `.escape()` or `.format()`, `.escapeId()` is used to avoid SQL injection in object keys.
176180

177181
### Formatting queries

lib/SqlString.js

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ SqlString.escapeId = function escapeId(val, forbidQualified) {
2424
}
2525

2626
return sql;
27+
} else if (typeof val.toSqlString === 'function') {
28+
return String(val.toSqlString());
2729
} else if (forbidQualified) {
2830
return '`' + String(val).replace(ID_GLOBAL_REGEXP, '``') + '`';
2931
} else {

test/unit/test-SqlString.js

+8
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ test('SqlString.escapeId', {
4646

4747
'nested arrays are flattened': function() {
4848
assert.equal(SqlString.escapeId(['a', ['b', ['t.c']]]), '`a`, `b`, `t`.`c`');
49+
},
50+
51+
'raw not escaped': function () {
52+
assert.equal(SqlString.escapeId(SqlString.raw('*')), '*');
53+
},
54+
55+
'raw within array not escaped': function () {
56+
assert.equal(SqlString.escapeId(['a', SqlString.raw('*'), 'b']), '`a`, *, `b`');
4957
}
5058
});
5159

0 commit comments

Comments
 (0)