Skip to content

Commit

Permalink
Properly format arrays (for e.g. ".. WHERE id IN (?)", [ array ] quer…
Browse files Browse the repository at this point in the history
…ies)

Ported changes made by https://github.com/blkcat on his fork to latest mysql version.
Updated legacy test to work with new way of handling arrays as commented on issue #85
#85
  • Loading branch information
Roger Castells committed Mar 11, 2012
1 parent 0383ec5 commit c79c3f3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lib/client.js
Expand Up @@ -162,6 +162,8 @@ Client.prototype.format = function(sql, params) {
};

Client.prototype.escape = function(val) {
var escape = this.escape;

if (val === undefined || val === null) {
return 'NULL';
}
Expand All @@ -171,6 +173,10 @@ Client.prototype.escape = function(val) {
case 'number': return val+'';
}

if (Array.isArray(val)) {
var sanitized = val.map( function( v ) { return escape( v ); } );
return "'" + sanitized.join( "','" ) + "'";
}
if (typeof val === 'object') {
val = (typeof val.toISOString === 'function')
? val.toISOString()
Expand Down
2 changes: 1 addition & 1 deletion test/unit/legacy/test-client.js
Expand Up @@ -41,7 +41,7 @@ test(function escape() {
assert.equal(client.escape(true), 'true');
assert.equal(client.escape(5), '5');
assert.equal(client.escape({foo:'bar'}), "'[object Object]'");
assert.equal(client.escape([1,2,3]), "'1,2,3'");
assert.equal(client.escape([1,2,3]), "'1','2','3'");
assert.equal(client.escape(new Date(Date.UTC(2011,6,6,6,6,6,6))), "'2011-07-06T06:06:06.006Z'");

assert.equal(client.escape('Super'), "'Super'");
Expand Down

0 comments on commit c79c3f3

Please sign in to comment.