Skip to content

Commit 4c1f4a9

Browse files
kevinmartindougwilson
authored andcommitted
Add .toSqlString() escape overriding
closes mysqljs#9
1 parent 7aeeffe commit 4c1f4a9

File tree

5 files changed

+33
-0
lines changed

5 files changed

+33
-0
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+
* Add `.toSqlString()` escape overriding
5+
16
2.2.0 / 2016-11-01
27
==================
38

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ Different value types are escaped differently, here is how:
7070
* Arrays are turned into list, e.g. `['a', 'b']` turns into `'a', 'b'`
7171
* Nested arrays are turned into grouped lists (for bulk inserts), e.g. `[['a',
7272
'b'], ['c', 'd']]` turns into `('a', 'b'), ('c', 'd')`
73+
* Objects that have a `toSqlString` method will have `.toSqlString()` called
74+
and the returned value is used as the raw SQL.
7375
* Objects are turned into `key = 'val'` pairs for each enumerable property on
7476
the object. If the property's value is a function, it is skipped; if the
7577
property's value is an object, toString() is called on it and the returned
@@ -87,6 +89,14 @@ var sql = SqlString.format('INSERT INTO posts SET ?', post);
8789
console.log(sql); // INSERT INTO posts SET `id` = 1, `title` = 'Hello MySQL'
8890
```
8991

92+
And the `toSqlString` method allows you to form complex queries with functions:
93+
94+
```js
95+
var CURRENT_TIMESTAMP = { toSqlString: function() { return 'CURRENT_TIMESTAMP()'; } };
96+
var sql = SqlString.format('UPDATE posts SET modified = ? WHERE id = ?', [CURRENT_TIMESTAMP, 42]);
97+
console.log(sql); // UPDATE posts SET modified = CURRENT_TIMESTAMP() WHERE id = 42
98+
```
99+
90100
If you feel the need to escape queries by yourself, you can also use the escaping
91101
function directly:
92102

lib/SqlString.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ SqlString.escape = function escape(val, stringifyObjects, timeZone) {
4545
return SqlString.arrayToList(val, timeZone);
4646
} else if (Buffer.isBuffer(val)) {
4747
return SqlString.bufferToString(val);
48+
} else if (typeof val.toSqlString === 'function') {
49+
return String(val.toSqlString());
4850
} else if (stringifyObjects) {
4951
return escapeString(val.toString());
5052
} else {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"Adri Van Houdt <[email protected]>",
77
"Douglas Christopher Wilson <[email protected]>",
88
"fengmk2 <[email protected]> (http://fengmk2.github.com)",
9+
"Kevin Jose Martin <[email protected]>",
910
"Nathan Woltman <[email protected]>"
1011
],
1112
"license": "MIT",

test/unit/test-SqlString.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,18 @@ test('SqlString.escape', {
7474
assert.equal(SqlString.escape({a: 'b', c: function() {}}), "`a` = 'b'");
7575
},
7676

77+
'object values toSqlString is called': function() {
78+
assert.equal(SqlString.escape({id: { toSqlString: function() { return 'LAST_INSERT_ID()'; } }}), '`id` = LAST_INSERT_ID()');
79+
},
80+
81+
'objects toSqlString is called': function() {
82+
assert.equal(SqlString.escape({ toSqlString: function() { return '@foo_id'; } }), '@foo_id');
83+
},
84+
85+
'objects toSqlString is not quoted': function() {
86+
assert.equal(SqlString.escape({ toSqlString: function() { return 'CURRENT_TIMESTAMP()'; } }), 'CURRENT_TIMESTAMP()');
87+
},
88+
7789
'nested objects are cast to strings': function() {
7890
assert.equal(SqlString.escape({a: {nested: true}}), "`a` = '[object Object]'");
7991
},
@@ -272,6 +284,9 @@ test('SqlString.format', {
272284

273285
var sql = SqlString.format('?', { toString: function () { return 'hello'; } }, true);
274286
assert.equal(sql, "'hello'");
287+
288+
var sql = SqlString.format('?', { toSqlString: function () { return '@foo'; } }, true);
289+
assert.equal(sql, '@foo');
275290
},
276291

277292
'sql is untouched if no values are provided': function () {

0 commit comments

Comments
 (0)