Skip to content

Commit b00e626

Browse files
committed
Added array row support.
1 parent cf5d1e3 commit b00e626

File tree

6 files changed

+57
-6
lines changed

6 files changed

+57
-6
lines changed

Readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ issue [#501](https://github.com/mysqljs/mysql/issues/501). (Default: `false`)
212212
(insecure) authentication method. (Default: `false`)
213213
* `typeCast`: Determines if column values should be converted to native
214214
JavaScript types. (Default: `true`)
215+
* `arrayRow`: Determines if rows should be an array or an object. (Default: `false`)
215216
* `queryFormat`: A custom query format function. See [Custom format](#custom-format).
216217
* `supportBigNumbers`: When dealing with big numbers (BIGINT and DECIMAL columns) in the database,
217218
you should enable this option (Default: `false`).

lib/Connection.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,16 @@ Connection.prototype.query = function query(sql, values, cb) {
183183
var query = Connection.createQuery(sql, values, cb);
184184
query._connection = this;
185185

186-
if (!(typeof sql === 'object' && 'typeCast' in sql)) {
186+
if (typeof sql === 'object') {
187+
if (!('typecast' in sql)) {
188+
query.typeCast = this.config.typeCast;
189+
}
190+
if (!('arrayRow' in sql)) {
191+
query.arrayRow = this.config.arrayRow;
192+
}
193+
} else {
187194
query.typeCast = this.config.typeCast;
195+
query.arrayRow = this.config.arrayRow;
188196
}
189197

190198
if (query.sql) {

lib/ConnectionConfig.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ function ConnectionConfig(options) {
3737
this.typeCast = (options.typeCast === undefined)
3838
? true
3939
: options.typeCast;
40+
this.arrayRow = options.arrayRow || false;
4041

4142
if (this.timezone[0] === ' ') {
4243
// "+" is a url encoded char for space so it

lib/Pool.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,16 @@ Pool.prototype.end = function (cb) {
190190
Pool.prototype.query = function (sql, values, cb) {
191191
var query = Connection.createQuery(sql, values, cb);
192192

193-
if (!(typeof sql === 'object' && 'typeCast' in sql)) {
193+
if (typeof sql === 'object') {
194+
if (!('typecast' in sql)) {
195+
query.typeCast = this.config.connectionConfig.typeCast;
196+
}
197+
if (!('arrayRow' in sql)) {
198+
query.arrayRow = this.config.connectionConfig.arrayRow;
199+
}
200+
} else {
194201
query.typeCast = this.config.connectionConfig.typeCast;
202+
query.arrayRow = this.config.connectionConfig.arrayRow;
195203
}
196204

197205
if (this.config.connectionConfig.trace) {

lib/protocol/packets/RowDataPacket.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ Object.defineProperty(RowDataPacket.prototype, 'parse', {
1313
value : parse
1414
});
1515

16+
Object.defineProperty(RowDataPacket.prototype, 'parseAsArray', {
17+
configurable : true,
18+
enumerable : false,
19+
value : parseAsArray
20+
});
21+
1622
Object.defineProperty(RowDataPacket.prototype, '_typeCast', {
1723
configurable : true,
1824
enumerable : false,
@@ -50,6 +56,28 @@ function parse(parser, fieldPackets, typeCast, nestTables, connection) {
5056
}
5157
}
5258

59+
function parseAsArray(parser, fieldPackets, typeCast, connection) {
60+
var self = this;
61+
var next = function () {
62+
return self._typeCast(fieldPacket, parser, connection.config.timezone, connection.config.supportBigNumbers, connection.config.bigNumberStrings, connection.config.dateStrings);
63+
};
64+
var row = [];
65+
for (var i = 0; i < fieldPackets.length; i++) {
66+
var fieldPacket = fieldPackets[i];
67+
68+
if (typeof typeCast === 'function') {
69+
row[i] = typeCast.apply(connection, [ new Field({ packet: fieldPacket, parser: parser }), next ]);
70+
} else {
71+
row[i] = (typeCast)
72+
? this._typeCast(fieldPacket, parser, connection.config.timezone, connection.config.supportBigNumbers, connection.config.bigNumberStrings, connection.config.dateStrings)
73+
: ( (fieldPacket.charsetNr === Charsets.BINARY)
74+
? parser.parseLengthCodedBuffer()
75+
: parser.parseLengthCodedString() );
76+
}
77+
}
78+
return row;
79+
}
80+
5381
function typeCast(field, parser, timeZone, supportBigNumbers, bigNumberStrings, dateStrings) {
5482
var numberString;
5583

lib/protocol/sequences/Query.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,17 @@ Query.prototype._handleFinalResultPacket = function(packet) {
140140
};
141141

142142
Query.prototype['RowDataPacket'] = function(packet, parser, connection) {
143-
packet.parse(parser, this._resultSet.fieldPackets, this.typeCast, this.nestTables, connection);
144-
143+
var row;
144+
if (this.arrayRow) {
145+
row = packet.parseAsArray(parser, this._resultSet.fieldPackets, this.typeCast, connection);
146+
} else {
147+
packet.parse(parser, this._resultSet.fieldPackets, this.typeCast, this.nestTables, connection);
148+
row = packet;
149+
}
145150
if (this._callback) {
146-
this._resultSet.rows.push(packet);
151+
this._resultSet.rows.push(row);
147152
} else {
148-
this.emit('result', packet, this._index);
153+
this.emit('result', row, this._index);
149154
}
150155
};
151156

0 commit comments

Comments
 (0)