Skip to content

Commit

Permalink
#770 Add an option to return resultset as array
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianfiorotto committed Oct 24, 2018
1 parent cf5d1e3 commit b33370a
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 4 deletions.
19 changes: 19 additions & 0 deletions Readme.md
Expand Up @@ -657,6 +657,25 @@ connection.query(
);
```

Use the option `arrayRows` to get rows as arrays where the values can be obtained using
the position in the field list instead of the field's name.

```js
connection.query(
{
sql: 'SELECT * FROM `books` WHERE `author` = ?',
arrayRows: true
},
'David',
function (error, results, fields) {
// error will be an Error if one occurred during the query
// results will contain the results of the query as an array of arrays
// fields will contain information about the returned results fields (if any)
}
);
```


## Escaping query values

**Caution** These methods of escaping values only works when the
Expand Down
2 changes: 1 addition & 1 deletion lib/protocol/Protocol.js
Expand Up @@ -244,7 +244,7 @@ Protocol.prototype._parsePacket = function() {
var packetName = Packet.name;

// Special case: Faster dispatch, and parsing done inside sequence
if (Packet === Packets.RowDataPacket) {
if (Packet === Packets.RowDataPacket || Packet === Packets.ArrayRowDataPacket) {
sequence.RowDataPacket(packet, this._parser, this._connection);

if (this._config.debug) {
Expand Down
43 changes: 43 additions & 0 deletions lib/protocol/packets/ArrayRowDataPacket.js
@@ -0,0 +1,43 @@
var Charsets = require('../constants/charsets');
var RowDataPacket = require('./RowDataPacket');
var Field = require('./Field');

module.exports = ArrayRowDataPacket;
function ArrayRowDataPacket() {
}

Object.defineProperty(ArrayRowDataPacket.prototype, 'parse', {
configurable : true,
enumerable : false,
value : parse
});

Object.defineProperty(ArrayRowDataPacket.prototype, '_typeCast', {
configurable : true,
enumerable : false,
value : RowDataPacket.prototype._typeCast
});

function parse(parser, fieldPackets, typeCast, nestTables, connection) {
var self = this;
var next = function () {
return self._typeCast(fieldPacket, parser, connection.config.timezone, connection.config.supportBigNumbers, connection.config.bigNumberStrings, connection.config.dateStrings);
};
this.values = [];

for (var i = 0; i < fieldPackets.length; i++) {
var fieldPacket = fieldPackets[i];
var value;

if (typeof typeCast === 'function') {
value = typeCast.apply(connection, [ new Field({ packet: fieldPacket, parser: parser }), next ]);
} else {
value = (typeCast)
? this._typeCast(fieldPacket, parser, connection.config.timezone, connection.config.supportBigNumbers, connection.config.bigNumberStrings, connection.config.dateStrings)
: ( (fieldPacket.charsetNr === Charsets.BINARY)
? parser.parseLengthCodedBuffer()
: parser.parseLengthCodedString() );
}
this.values[i] = value;
}
}
1 change: 1 addition & 0 deletions lib/protocol/packets/index.js
@@ -1,3 +1,4 @@
exports.ArrayRowDataPacket = require('./ArrayRowDataPacket');
exports.AuthSwitchRequestPacket = require('./AuthSwitchRequestPacket');
exports.AuthSwitchResponsePacket = require('./AuthSwitchResponsePacket');
exports.ClientAuthenticationPacket = require('./ClientAuthenticationPacket');
Expand Down
16 changes: 13 additions & 3 deletions lib/protocol/sequences/Query.js
Expand Up @@ -17,6 +17,7 @@ function Query(options, callback) {
? true
: options.typeCast;
this.nestTables = options.nestTables || false;
this.arrayRows = options.arrayRows || false;

this._resultSet = null;
this._results = [];
Expand Down Expand Up @@ -54,7 +55,12 @@ Query.prototype.determinePacket = function determinePacket(byte, parser) {
return Packets.EofPacket;
}

return Packets.RowDataPacket;
if (this.arrayRows) {
return Packets.ArrayRowDataPacket;
}
else {
return Packets.RowDataPacket;
}
};

Query.prototype['OkPacket'] = function(packet) {
Expand Down Expand Up @@ -141,11 +147,15 @@ Query.prototype._handleFinalResultPacket = function(packet) {

Query.prototype['RowDataPacket'] = function(packet, parser, connection) {
packet.parse(parser, this._resultSet.fieldPackets, this.typeCast, this.nestTables, connection);
var row = packet;
if (this.arrayRows) {
row = row.values;
}

if (this._callback) {
this._resultSet.rows.push(packet);
this._resultSet.rows.push(row);
} else {
this.emit('result', packet, this._index);
this.emit('result', row, this._index);
}
};

Expand Down
21 changes: 21 additions & 0 deletions test/unit/query/test-query-rows-array.js
@@ -0,0 +1,21 @@
var assert = require('assert');
var common = require('../../common');
var connection = common.createConnection({port: common.fakeServerPort, user: 'testuser'});

var server = common.createFakeServer();

server.listen(common.fakeServerPort, function (err) {
assert.ifError(err);

connection.query({
sql : 'SELECT CURRENT_USER()',
arrayRows : true
}, function (err, rows) {
assert.ifError(err);
assert.deepEqual(rows, [['testuser@localhost']]);

connection.destroy();
server.destroy();
});

});

0 comments on commit b33370a

Please sign in to comment.