Skip to content

Commit fd778c6

Browse files
committed
Added an option to return resultsets as an array type.
1 parent 5cf53df commit fd778c6

File tree

6 files changed

+69
-3
lines changed

6 files changed

+69
-3
lines changed

Readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ issue [#501](https://github.com/mysqljs/mysql/issues/501). (Default: `false`)
224224
also possible to blacklist default ones. For more information, check
225225
[Connection Flags](#connection-flags).
226226
* `ssl`: object with ssl parameters or a string containing name of ssl profile. See [SSL options](#ssl-options).
227+
* `rowsAsArray`: If `true`, resultsets return as an array type instead of an object. (Default: `false`)
227228

228229

229230
In addition to passing these options as an object, you can also use a url

lib/ConnectionConfig.js

+2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ function ConnectionConfig(options) {
5858
// Set the client flags
5959
var defaultFlags = ConnectionConfig.getDefaultFlags(options);
6060
this.clientFlags = ConnectionConfig.mergeFlags(defaultFlags, options.flags);
61+
62+
this.rowsAsArray = options.rowsAsArray || false;
6163
}
6264

6365
ConnectionConfig.mergeFlags = function mergeFlags(defaultFlags, userFlags) {

lib/protocol/Parser.js

+9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ function Parser(options) {
2121
this._nextPacketNumber = 0;
2222
this._encoding = 'utf-8';
2323
this._paused = false;
24+
this._globalRowsAsArray = options.config && options.config.rowsAsArray;
2425
}
2526

2627
Parser.prototype.write = function write(chunk) {
@@ -410,6 +411,14 @@ Parser.prototype.packetLength = function packetLength() {
410411
return this._packetHeader.length + this._longPacketBuffers.size;
411412
};
412413

414+
Parser.prototype.setArrayRowMode = function(rowsAsArray) {
415+
this._isArrayRowMode = typeof rowsAsArray === 'undefined' ? this._globalRowsAsArray : rowsAsArray;
416+
};
417+
418+
Parser.prototype.isArrayRowMode = function() {
419+
return this._isArrayRowMode;
420+
};
421+
413422
Parser.prototype._combineNextBuffers = function _combineNextBuffers(bytes) {
414423
var length = this._buffer.length - this._offset;
415424

lib/protocol/packets/RowDataPacket.js

+24-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,22 @@ Object.defineProperty(RowDataPacket.prototype, 'parse', {
1313
value : parse
1414
});
1515

16+
Object.defineProperty(RowDataPacket.prototype, 'isArray', {
17+
configurable : true,
18+
enumerable : false,
19+
value : function() {
20+
return typeof this._row !== 'undefined';
21+
}
22+
});
23+
24+
Object.defineProperty(RowDataPacket.prototype, 'getArrayValue', {
25+
configurable : true,
26+
enumerable : false,
27+
value : function() {
28+
return this._row;
29+
}
30+
});
31+
1632
Object.defineProperty(RowDataPacket.prototype, '_typeCast', {
1733
configurable : true,
1834
enumerable : false,
@@ -25,6 +41,11 @@ function parse(parser, fieldPackets, typeCast, nestTables, connection) {
2541
return self._typeCast(fieldPacket, parser, connection.config.timezone, connection.config.supportBigNumbers, connection.config.bigNumberStrings, connection.config.dateStrings);
2642
};
2743

44+
var isArrayRowMode = parser.isArrayRowMode();
45+
if (isArrayRowMode) {
46+
this._row = [];
47+
}
48+
2849
for (var i = 0; i < fieldPackets.length; i++) {
2950
var fieldPacket = fieldPackets[i];
3051
var value;
@@ -39,7 +60,9 @@ function parse(parser, fieldPackets, typeCast, nestTables, connection) {
3960
: parser.parseLengthCodedString() );
4061
}
4162

42-
if (typeof nestTables === 'string' && nestTables.length) {
63+
if (isArrayRowMode) {
64+
this._row.push(value);
65+
} else if (typeof nestTables === 'string' && nestTables.length) {
4366
this[fieldPacket.table + nestTables + fieldPacket.name] = value;
4467
} else if (nestTables) {
4568
this[fieldPacket.table] = this[fieldPacket.table] || {};

lib/protocol/sequences/Query.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ function Query(options, callback) {
1717
? true
1818
: options.typeCast;
1919
this.nestTables = options.nestTables || false;
20+
this.rowsAsArray = options.rowsAsArray;
2021

2122
this._resultSet = null;
2223
this._results = [];
@@ -138,12 +139,16 @@ Query.prototype._handleFinalResultPacket = function(packet) {
138139
};
139140

140141
Query.prototype['RowDataPacket'] = function(packet, parser, connection) {
142+
parser.setArrayRowMode(this.rowsAsArray);
143+
141144
packet.parse(parser, this._resultSet.fieldPackets, this.typeCast, this.nestTables, connection);
142145

146+
var row = packet.isArray() ? packet.getArrayValue() : packet;
147+
143148
if (this._callback) {
144-
this._resultSet.rows.push(packet);
149+
this._resultSet.rows.push(row);
145150
} else {
146-
this.emit('result', packet, this._index);
151+
this.emit('result', row, this._index);
147152
}
148153
};
149154

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var assert = require('assert');
2+
var common = require('../../common');
3+
4+
common.getTestConnection({ rowsAsArray: true }, function (err, connection) {
5+
assert.ifError(err);
6+
7+
connection.query('SELECT 1', function (err, rows) {
8+
assert.ifError(err);
9+
assert.deepEqual(rows, [[1]]);
10+
});
11+
12+
connection.query({ sql: 'SELECT ?' }, [ 1 ], function (err, rows) {
13+
assert.ifError(err);
14+
assert.deepEqual(rows, [[1]]);
15+
});
16+
17+
connection.query({
18+
sql: 'SELECT ?',
19+
rowsAsArray: false
20+
}, [ 1 ], function (err, rows) {
21+
assert.ifError(err);
22+
assert.deepEqual(rows, [{1: 1}]);
23+
});
24+
25+
connection.end(assert.ifError);
26+
});

0 commit comments

Comments
 (0)