Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add stringifyObjects option #502

Merged
merged 1 commit into from
May 29, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'`)
* `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'")
}
});