Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b33370a

Browse filesBrowse files
#770 Add an option to return resultset as array
1 parent cf5d1e3 commit b33370a
Copy full SHA for b33370a

File tree

Expand file treeCollapse file tree

6 files changed

+98
-4
lines changed
Filter options
Expand file treeCollapse file tree

6 files changed

+98
-4
lines changed

‎Readme.md

Copy file name to clipboardExpand all lines: Readme.md
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,25 @@ connection.query(
657657
);
658658
```
659659

660+
Use the option `arrayRows` to get rows as arrays where the values can be obtained using
661+
the position in the field list instead of the field's name.
662+
663+
```js
664+
connection.query(
665+
{
666+
sql: 'SELECT * FROM `books` WHERE `author` = ?',
667+
arrayRows: true
668+
},
669+
'David',
670+
function (error, results, fields) {
671+
// error will be an Error if one occurred during the query
672+
// results will contain the results of the query as an array of arrays
673+
// fields will contain information about the returned results fields (if any)
674+
}
675+
);
676+
```
677+
678+
660679
## Escaping query values
661680

662681
**Caution** These methods of escaping values only works when the

‎lib/protocol/Protocol.js

Copy file name to clipboardExpand all lines: lib/protocol/Protocol.js
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ Protocol.prototype._parsePacket = function() {
244244
var packetName = Packet.name;
245245

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

250250
if (this._config.debug) {
+43Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
var Charsets = require('../constants/charsets');
2+
var RowDataPacket = require('./RowDataPacket');
3+
var Field = require('./Field');
4+
5+
module.exports = ArrayRowDataPacket;
6+
function ArrayRowDataPacket() {
7+
}
8+
9+
Object.defineProperty(ArrayRowDataPacket.prototype, 'parse', {
10+
configurable : true,
11+
enumerable : false,
12+
value : parse
13+
});
14+
15+
Object.defineProperty(ArrayRowDataPacket.prototype, '_typeCast', {
16+
configurable : true,
17+
enumerable : false,
18+
value : RowDataPacket.prototype._typeCast
19+
});
20+
21+
function parse(parser, fieldPackets, typeCast, nestTables, connection) {
22+
var self = this;
23+
var next = function () {
24+
return self._typeCast(fieldPacket, parser, connection.config.timezone, connection.config.supportBigNumbers, connection.config.bigNumberStrings, connection.config.dateStrings);
25+
};
26+
this.values = [];
27+
28+
for (var i = 0; i < fieldPackets.length; i++) {
29+
var fieldPacket = fieldPackets[i];
30+
var value;
31+
32+
if (typeof typeCast === 'function') {
33+
value = typeCast.apply(connection, [ new Field({ packet: fieldPacket, parser: parser }), next ]);
34+
} else {
35+
value = (typeCast)
36+
? this._typeCast(fieldPacket, parser, connection.config.timezone, connection.config.supportBigNumbers, connection.config.bigNumberStrings, connection.config.dateStrings)
37+
: ( (fieldPacket.charsetNr === Charsets.BINARY)
38+
? parser.parseLengthCodedBuffer()
39+
: parser.parseLengthCodedString() );
40+
}
41+
this.values[i] = value;
42+
}
43+
}

‎lib/protocol/packets/index.js

Copy file name to clipboardExpand all lines: lib/protocol/packets/index.js
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
exports.ArrayRowDataPacket = require('./ArrayRowDataPacket');
12
exports.AuthSwitchRequestPacket = require('./AuthSwitchRequestPacket');
23
exports.AuthSwitchResponsePacket = require('./AuthSwitchResponsePacket');
34
exports.ClientAuthenticationPacket = require('./ClientAuthenticationPacket');

‎lib/protocol/sequences/Query.js

Copy file name to clipboardExpand all lines: lib/protocol/sequences/Query.js
+13-3Lines changed: 13 additions & 3 deletions
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.arrayRows = options.arrayRows || false;
2021

2122
this._resultSet = null;
2223
this._results = [];
@@ -54,7 +55,12 @@ Query.prototype.determinePacket = function determinePacket(byte, parser) {
5455
return Packets.EofPacket;
5556
}
5657

57-
return Packets.RowDataPacket;
58+
if (this.arrayRows) {
59+
return Packets.ArrayRowDataPacket;
60+
}
61+
else {
62+
return Packets.RowDataPacket;
63+
}
5864
};
5965

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

142148
Query.prototype['RowDataPacket'] = function(packet, parser, connection) {
143149
packet.parse(parser, this._resultSet.fieldPackets, this.typeCast, this.nestTables, connection);
150+
var row = packet;
151+
if (this.arrayRows) {
152+
row = row.values;
153+
}
144154

145155
if (this._callback) {
146-
this._resultSet.rows.push(packet);
156+
this._resultSet.rows.push(row);
147157
} else {
148-
this.emit('result', packet, this._index);
158+
this.emit('result', row, this._index);
149159
}
150160
};
151161

+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var assert = require('assert');
2+
var common = require('../../common');
3+
var connection = common.createConnection({port: common.fakeServerPort, user: 'testuser'});
4+
5+
var server = common.createFakeServer();
6+
7+
server.listen(common.fakeServerPort, function (err) {
8+
assert.ifError(err);
9+
10+
connection.query({
11+
sql : 'SELECT CURRENT_USER()',
12+
arrayRows : true
13+
}, function (err, rows) {
14+
assert.ifError(err);
15+
assert.deepEqual(rows, [['testuser@localhost']]);
16+
17+
connection.destroy();
18+
server.destroy();
19+
});
20+
21+
});

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.