Skip to content

Commit

Permalink
add stringifyObjects option
Browse files Browse the repository at this point in the history
  • Loading branch information
tellnes committed May 28, 2013
1 parent f2a8a50 commit dd921c3
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'`)

This comment has been minimized.

Copy link
@Simran-B

Simran-B Aug 25, 2016

The default is actually false (bool), not 'false' (string).

* `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
Expand Down
2 changes: 1 addition & 1 deletion lib/Connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions lib/ConnectionConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions lib/protocol/SqlString.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
});
};

Expand Down
13 changes: 13 additions & 0 deletions test/unit/protocol/test-SqlString.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'")
}
});

0 comments on commit dd921c3

Please sign in to comment.