Skip to content

Commit 55c20da

Browse files
committed
Add mysql.raw() to generate pre-escaped values
closes #877 closes #1821
1 parent 5d139b2 commit 55c20da

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

Changes.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ you spot any mistakes.
77
## HEAD
88

99
* Add new Amazon RDS ca-central-1 certificate CA to Amazon RDS SSL profile #1809
10+
* Add `mysql.raw()` to generate pre-escaped values #877 #1821
1011
* Fix "changedRows" to work on non-English servers #1819
1112
* Fix typo in insecure auth error message
1213
* Support `mysql_native_password` auth switch request for Azure #1396 #1729 #1730

Readme.md

+13
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,19 @@ var sql = mysql.format('UPDATE posts SET modified = ? WHERE id = ?', [CURRENT_TI
737737
console.log(sql); // UPDATE posts SET modified = CURRENT_TIMESTAMP() WHERE id = 42
738738
```
739739

740+
To generate objects with a `toSqlString` method, the `mysql.raw()` method can
741+
be used. This creates an object that will be left un-touched when using in a `?`
742+
placeholder, useful for using functions as dynamic values:
743+
744+
**Caution** The string provided to `mysql.raw()` will skip all escaping
745+
functions when used, so be careful when passing in unvalidated input.
746+
747+
```js
748+
var CURRENT_TIMESTAMP = mysql.raw('CURRENT_TIMESTAMP()');
749+
var sql = mysql.format('UPDATE posts SET modified = ? WHERE id = ?', [CURRENT_TIMESTAMP, 42]);
750+
console.log(sql); // UPDATE posts SET modified = CURRENT_TIMESTAMP() WHERE id = 42
751+
```
752+
740753
If you feel the need to escape queries by yourself, you can also use the escaping
741754
function directly:
742755

index.js

+12
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,18 @@ exports.format = function format(sql, values, stringifyObjects, timeZone) {
9494
return SqlString.format(sql, values, stringifyObjects, timeZone);
9595
};
9696

97+
/**
98+
* Wrap raw SQL strings from escape overriding.
99+
* @param {string} sql The raw SQL
100+
* @return {object} Wrapped object
101+
* @public
102+
*/
103+
exports.raw = function raw(sql) {
104+
var SqlString = loadClass('SqlString');
105+
106+
return SqlString.raw(sql);
107+
};
108+
97109
/**
98110
* The type constants.
99111
* @public

test/unit/test-Mysql.js

+10
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ test('Mysql.format', {
2020
}
2121
});
2222

23+
test('Mysql.raw', {
24+
'generate object format will not escape': function() {
25+
var now = Mysql.raw('NOW()');
26+
assert.equal(
27+
Mysql.format('SELECT * FROM ?? WHERE ?? >= ?', ['table', 'property', now]),
28+
'SELECT * FROM `table` WHERE `property` >= NOW()'
29+
);
30+
}
31+
});
32+
2333
test('Mysql.Types', {
2434
'exported object of types': function() {
2535
assert.equal(typeof Mysql.Types, 'object');

0 commit comments

Comments
 (0)