Skip to content

Commit

Permalink
Allow custom escaping mode
Browse files Browse the repository at this point in the history
People can now use connection config to define an `queryFormat`
function that is used instead of connection.format.
  • Loading branch information
dresende committed Nov 16, 2012
1 parent 3ec47b7 commit ab18e26
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/Connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ Connection.prototype.escape = function(value) {
};

Connection.prototype.format = function(sql, values) {
if (typeof this.config.queryFormat == "function") {
return this.config.queryFormat(sql, values, this.config.timezone);
}
return SqlString.format(sql, values, this.config.timezone);
};

Expand Down
2 changes: 2 additions & 0 deletions lib/ConnectionConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ function ConnectionConfig(options) {
this.debug = options.debug;
this.timezone = options.timezone || 'local';
this.flags = options.flags || '';
this.queryFormat = options.queryFormat;
this.typeCast = (options.typeCast === undefined)
? true
: options.typeCast;

if (this.timezone[0] == " ") {
// "+" is a url encoded char for space so it
// gets translated to space when giving a
Expand Down
15 changes: 15 additions & 0 deletions test/integration/connection/test-custom-query-format.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var Mysql = require('../../../');
var common = require('../../common');
var connection = common.createConnection();
var assert = require('assert');

connection.config.queryFormat = function (query, values, tz) {
return query.replace(/\:(\w+)/g, function (txt, key) {
if (values.hasOwnProperty(key)) {
return connection.escape(values[key]);
}
return txt;
});
};

assert.equal(connection.format("SELECT :a1, :a2", { a1: 1, a2: 'two' }), "SELECT 1, 'two'");

0 comments on commit ab18e26

Please sign in to comment.