Skip to content
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

Add an option to return resultset as array #2092

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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();
});

});