Skip to content

Commit dd921c3

Browse files
committed
add stringifyObjects option
fixes #501
1 parent f2a8a50 commit dd921c3

File tree

5 files changed

+19
-3
lines changed

5 files changed

+19
-3
lines changed

Readme.md

+2
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ When establishing a connection, you can set the following options:
140140
* `database`: Name of the database to use for this connection (Optional).
141141
* `charset`: The charset for the connection. (Default: `'UTF8_GENERAL_CI'`)
142142
* `timezone`: The timezone used to store local dates. (Default: `'local'`)
143+
* `stringifyObjects`: Stringify objects instead of converting to values. See
144+
issue [#501](https://github.com/felixge/node-mysql/issues/501). (Default: `'false'`)
143145
* `insecureAuth`: Allow connecting to MySQL instances that ask for the old
144146
(insecure) authentication method. (Default: `false`)
145147
* `typeCast`: Determines if column values should be converted to native

lib/Connection.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ Connection.prototype.format = function(sql, values) {
150150
if (typeof this.config.queryFormat == "function") {
151151
return this.config.queryFormat.call(this, sql, values, this.config.timezone);
152152
}
153-
return SqlString.format(sql, values, this.config.timezone);
153+
return SqlString.format(sql, values, this.config.stringifyObjects, this.config.timezone);
154154
};
155155

156156
Connection.prototype._handleNetworkError = function(err) {

lib/ConnectionConfig.js

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ function ConnectionConfig(options) {
1818
this.supportBigNumbers = options.supportBigNumbers || false;
1919
this.bigNumberStrings = options.bigNumberStrings || false;
2020
this.debug = options.debug;
21+
this.stringifyObjects = options.stringifyObjects || false;
2122
this.timezone = options.timezone || 'local';
2223
this.flags = options.flags || '';
2324
this.queryFormat = options.queryFormat;

lib/protocol/SqlString.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ SqlString.arrayToList = function(array, timeZone) {
5858
}).join(', ');
5959
};
6060

61-
SqlString.format = function(sql, values, timeZone) {
61+
SqlString.format = function(sql, values, stringifyObjects, timeZone) {
6262
values = [].concat(values);
6363

6464
return sql.replace(/\?\??/g, function(match) {
@@ -69,7 +69,7 @@ SqlString.format = function(sql, values, timeZone) {
6969
if (match == "??") {
7070
return SqlString.escapeId(values.shift());
7171
}
72-
return SqlString.escape(values.shift(), false, timeZone);
72+
return SqlString.escape(values.shift(), stringifyObjects, timeZone);
7373
});
7474
};
7575

test/unit/protocol/test-SqlString.js

+13
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,17 @@ test('SqlString.format', {
129129
var sql = SqlString.format('? and ?', ['hello?', 'b']);
130130
assert.equal(sql, "'hello?' and 'b'");
131131
},
132+
133+
'objects is converted to values': function () {
134+
var sql = SqlString.format('?', { 'hello': 'world' }, false)
135+
assert.equal(sql, "`hello` = 'world'")
136+
},
137+
138+
'objects is not converted to values': function () {
139+
var sql = SqlString.format('?', { 'hello': 'world' }, true)
140+
assert.equal(sql, "'[object Object]'")
141+
142+
var sql = SqlString.format('?', { toString: function () { return 'hello' } }, true)
143+
assert.equal(sql, "'hello'")
144+
}
132145
});

0 commit comments

Comments
 (0)