-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Added an option to return resultsets as an array type. #1637
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,22 @@ Object.defineProperty(RowDataPacket.prototype, 'parse', { | |
value : parse | ||
}); | ||
|
||
Object.defineProperty(RowDataPacket.prototype, 'isArray', { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can these be at least underscore-prefixed? Whatever we add here will shadow any column someone may be trying to query, so need to be careful adding anything new. |
||
configurable : true, | ||
enumerable : false, | ||
value : function() { | ||
return typeof this._row !== 'undefined'; | ||
} | ||
}); | ||
|
||
Object.defineProperty(RowDataPacket.prototype, 'getArrayValue', { | ||
configurable : true, | ||
enumerable : false, | ||
value : function() { | ||
return this._row; | ||
} | ||
}); | ||
|
||
Object.defineProperty(RowDataPacket.prototype, '_typeCast', { | ||
configurable : true, | ||
enumerable : false, | ||
|
@@ -25,6 +41,11 @@ function parse(parser, fieldPackets, typeCast, nestTables, connection) { | |
return self._typeCast(fieldPacket, parser, connection.config.timezone, connection.config.supportBigNumbers, connection.config.bigNumberStrings, connection.config.dateStrings); | ||
}; | ||
|
||
var isArrayRowMode = parser.isArrayRowMode(); | ||
if (isArrayRowMode) { | ||
this._row = []; | ||
} | ||
|
||
for (var i = 0; i < fieldPackets.length; i++) { | ||
var fieldPacket = fieldPackets[i]; | ||
var value; | ||
|
@@ -39,7 +60,9 @@ function parse(parser, fieldPackets, typeCast, nestTables, connection) { | |
: parser.parseLengthCodedString() ); | ||
} | ||
|
||
if (typeof nestTables === 'string' && nestTables.length) { | ||
if (isArrayRowMode) { | ||
this._row.push(value); | ||
} else if (typeof nestTables === 'string' && nestTables.length) { | ||
this[fieldPacket.table + nestTables + fieldPacket.name] = value; | ||
} else if (nestTables) { | ||
this[fieldPacket.table] = this[fieldPacket.table] || {}; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
var assert = require('assert'); | ||
var common = require('../../common'); | ||
|
||
common.getTestConnection({ rowsAsArray: true }, function (err, connection) { | ||
assert.ifError(err); | ||
|
||
connection.query('SELECT 1', function (err, rows) { | ||
assert.ifError(err); | ||
assert.deepEqual(rows, [[1]]); | ||
}); | ||
|
||
connection.query({ sql: 'SELECT ?' }, [ 1 ], function (err, rows) { | ||
assert.ifError(err); | ||
assert.deepEqual(rows, [[1]]); | ||
}); | ||
|
||
connection.query({ | ||
sql: 'SELECT ?', | ||
rowsAsArray: false | ||
}, [ 1 ], function (err, rows) { | ||
assert.ifError(err); | ||
assert.deepEqual(rows, [{1: 1}]); | ||
}); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a test that verifies the connection is reset back to the mode from the connection config here? |
||
connection.end(assert.ifError); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you hoist this to prevent the hidden class change this is triggering?