Skip to content

Commit 04d95e0

Browse files
Merge pull request sailshq#7 from treelinehq/normalize-bindings-pt1
Begins the process of normalizing bindings to match spec. Still needs input added and the old code that uses the old way removed.
2 parents e48e2de + d5e4f7f commit 04d95e0

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

machines/send-native-query.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,27 @@ 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+
124+
// e.g. `'$3'` => `'3'` => `3` => `2`
125+
var idx = +( substr.slice(1) ) - 1;
126+
127+
// If no such binding exists, then just leave the original
128+
// template string (e.g. "$3") alone.
129+
if (idx >= bindings.length) {
130+
return substr;
131+
}
132+
133+
// But otherwise, replace it with the escaped binding.
134+
return inputs.connection.escape(bindings[idx]);
135+
});
136+
137+
debug('Compiled (final) SQL: ' + sql);
138+
120139
// Send native query to the database using node-mysql.
121-
inputs.connection.query({
122-
sql: sql,
123-
values: bindings
124-
}, function query() {
140+
inputs.connection.query(sql, function query() {
125141
// The exact format of the arguments for this callback are not part of
126142
// the officially documented behavior of node-mysql (at least not as
127143
// of March 2016 when this comment is being written).

0 commit comments

Comments
 (0)