Skip to content

Commit fc30cc5

Browse files
committed
First pass at normalizing bindings (needs follow up to match latest from spec though)
1 parent e48e2de commit fc30cc5

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

machines/send-native-query.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,26 @@ module.exports = {
117117
debug('Bindings: ' + bindings);
118118
debug('Connection Id: ' + inputs.connection.id);
119119

120+
// Process SQL template, escaping bindings.
121+
// This converts `$1`, `$2`, etc. into the escaped binding.
122+
sql = sql.replace(/\$[1-9][0-9]*/g, function (substr){
123+
// e.g. `'$3'` => `'3'` => `3` => `2`
124+
var idx = +( substr.slice(1) ) - 1;
125+
126+
// If no such binding exists, then just leave the original
127+
// template string (e.g. "$3") alone.
128+
if (idx >= bindings.length) {
129+
return substr;
130+
}
131+
132+
// But otherwise, replace it with the escaped binding.
133+
return inputs.connection.escape(bindings[idx]);
134+
});
135+
136+
// console.log('Running compiled SQL:',sql);
137+
120138
// Send native query to the database using node-mysql.
121-
inputs.connection.query({
122-
sql: sql,
123-
values: bindings
124-
}, function query() {
139+
inputs.connection.query(sql, function query() {
125140
// The exact format of the arguments for this callback are not part of
126141
// the officially documented behavior of node-mysql (at least not as
127142
// of March 2016 when this comment is being written).

0 commit comments

Comments
 (0)