From dd921c3dac1c5dc1afbc2149be6d10665973b9f2 Mon Sep 17 00:00:00 2001 From: Christian Tellnes Date: Wed, 29 May 2013 01:49:00 +0200 Subject: [PATCH] add `stringifyObjects` option fixes #501 --- Readme.md | 2 ++ lib/Connection.js | 2 +- lib/ConnectionConfig.js | 1 + lib/protocol/SqlString.js | 4 ++-- test/unit/protocol/test-SqlString.js | 13 +++++++++++++ 5 files changed, 19 insertions(+), 3 deletions(-) diff --git a/Readme.md b/Readme.md index fe4361e66..e740d660b 100644 --- a/Readme.md +++ b/Readme.md @@ -140,6 +140,8 @@ When establishing a connection, you can set the following options: * `database`: Name of the database to use for this connection (Optional). * `charset`: The charset for the connection. (Default: `'UTF8_GENERAL_CI'`) * `timezone`: The timezone used to store local dates. (Default: `'local'`) +* `stringifyObjects`: Stringify objects instead of converting to values. See +issue [#501](https://github.com/felixge/node-mysql/issues/501). (Default: `'false'`) * `insecureAuth`: Allow connecting to MySQL instances that ask for the old (insecure) authentication method. (Default: `false`) * `typeCast`: Determines if column values should be converted to native diff --git a/lib/Connection.js b/lib/Connection.js index 018d86dd7..94677d8e3 100644 --- a/lib/Connection.js +++ b/lib/Connection.js @@ -150,7 +150,7 @@ Connection.prototype.format = function(sql, values) { if (typeof this.config.queryFormat == "function") { return this.config.queryFormat.call(this, sql, values, this.config.timezone); } - return SqlString.format(sql, values, this.config.timezone); + return SqlString.format(sql, values, this.config.stringifyObjects, this.config.timezone); }; Connection.prototype._handleNetworkError = function(err) { diff --git a/lib/ConnectionConfig.js b/lib/ConnectionConfig.js index b960192dd..fdacfea8a 100644 --- a/lib/ConnectionConfig.js +++ b/lib/ConnectionConfig.js @@ -18,6 +18,7 @@ function ConnectionConfig(options) { this.supportBigNumbers = options.supportBigNumbers || false; this.bigNumberStrings = options.bigNumberStrings || false; this.debug = options.debug; + this.stringifyObjects = options.stringifyObjects || false; this.timezone = options.timezone || 'local'; this.flags = options.flags || ''; this.queryFormat = options.queryFormat; diff --git a/lib/protocol/SqlString.js b/lib/protocol/SqlString.js index 674ebf1b8..98b3b3274 100644 --- a/lib/protocol/SqlString.js +++ b/lib/protocol/SqlString.js @@ -58,7 +58,7 @@ SqlString.arrayToList = function(array, timeZone) { }).join(', '); }; -SqlString.format = function(sql, values, timeZone) { +SqlString.format = function(sql, values, stringifyObjects, timeZone) { values = [].concat(values); return sql.replace(/\?\??/g, function(match) { @@ -69,7 +69,7 @@ SqlString.format = function(sql, values, timeZone) { if (match == "??") { return SqlString.escapeId(values.shift()); } - return SqlString.escape(values.shift(), false, timeZone); + return SqlString.escape(values.shift(), stringifyObjects, timeZone); }); }; diff --git a/test/unit/protocol/test-SqlString.js b/test/unit/protocol/test-SqlString.js index ed156be52..64423e528 100644 --- a/test/unit/protocol/test-SqlString.js +++ b/test/unit/protocol/test-SqlString.js @@ -129,4 +129,17 @@ test('SqlString.format', { var sql = SqlString.format('? and ?', ['hello?', 'b']); assert.equal(sql, "'hello?' and 'b'"); }, + + 'objects is converted to values': function () { + var sql = SqlString.format('?', { 'hello': 'world' }, false) + assert.equal(sql, "`hello` = 'world'") + }, + + 'objects is not converted to values': function () { + var sql = SqlString.format('?', { 'hello': 'world' }, true) + assert.equal(sql, "'[object Object]'") + + var sql = SqlString.format('?', { toString: function () { return 'hello' } }, true) + assert.equal(sql, "'hello'") + } });