Skip to content

Added array row support. #2106

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
- [Error handling](#error-handling)
- [Exception Safety](#exception-safety)
- [Type casting](#type-casting)
- [Array row](#array-row)
- [Connection Flags](#connection-flags)
- [Debugging and reporting problems](#debugging-and-reporting-problems)
- [Security issues](#security-issues)
Expand Down Expand Up @@ -212,6 +213,7 @@ issue [#501](https://github.com/mysqljs/mysql/issues/501). (Default: `false`)
(insecure) authentication method. (Default: `false`)
* `typeCast`: Determines if column values should be converted to native
JavaScript types. (Default: `true`)
* `arrayRow`: Determines if a row should be an array or an object. (Default: `false`)
* `queryFormat`: A custom query format function. See [Custom format](#custom-format).
* `supportBigNumbers`: When dealing with big numbers (BIGINT and DECIMAL columns) in the database,
you should enable this option (Default: `false`).
Expand Down Expand Up @@ -1335,6 +1337,22 @@ parser.parseGeometryValue()
__You can find which field function you need to use by looking at: [RowDataPacket.prototype._typeCast](https://github.com/mysqljs/mysql/blob/master/lib/protocol/packets/RowDataPacket.js#L41)__


## Array row

The rows of a table can be returned as an array or an object on query or on connection level (including pooled connections):

```js
var connection = require('mysql').createConnection({arrayRow: true});
```

```js
connection.query({sql: '...', arrayRow: true}, function (err, result, fields) {
if (error) throw error;
// ...
});
```


## Connection Flags

If, for any reason, you would like to change the default connection flags, you
Expand Down
10 changes: 9 additions & 1 deletion lib/Connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,16 @@ Connection.prototype.query = function query(sql, values, cb) {
var query = Connection.createQuery(sql, values, cb);
query._connection = this;

if (!(typeof sql === 'object' && 'typeCast' in sql)) {
if (typeof sql === 'object') {
if (!('typecast' in sql)) {
query.typeCast = this.config.typeCast;
}
if (!('arrayRow' in sql)) {
query.arrayRow = this.config.arrayRow;
}
} else {
query.typeCast = this.config.typeCast;
query.arrayRow = this.config.arrayRow;
}

if (query.sql) {
Expand Down
1 change: 1 addition & 0 deletions lib/ConnectionConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ function ConnectionConfig(options) {
this.typeCast = (options.typeCast === undefined)
? true
: options.typeCast;
this.arrayRow = options.arrayRow || false;

if (this.timezone[0] === ' ') {
// "+" is a url encoded char for space so it
Expand Down
10 changes: 9 additions & 1 deletion lib/Pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,16 @@ Pool.prototype.end = function (cb) {
Pool.prototype.query = function (sql, values, cb) {
var query = Connection.createQuery(sql, values, cb);

if (!(typeof sql === 'object' && 'typeCast' in sql)) {
if (typeof sql === 'object') {
if (!('typecast' in sql)) {
query.typeCast = this.config.connectionConfig.typeCast;
}
if (!('arrayRow' in sql)) {
query.arrayRow = this.config.connectionConfig.arrayRow;
}
} else {
query.typeCast = this.config.connectionConfig.typeCast;
query.arrayRow = this.config.connectionConfig.arrayRow;
}

if (this.config.connectionConfig.trace) {
Expand Down
30 changes: 30 additions & 0 deletions lib/protocol/packets/RowDataPacket.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ Object.defineProperty(RowDataPacket.prototype, 'parse', {
value : parse
});

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

Object.defineProperty(RowDataPacket.prototype, '_typeCast', {
configurable : true,
enumerable : false,
Expand Down Expand Up @@ -50,6 +56,30 @@ function parse(parser, fieldPackets, typeCast, nestTables, connection) {
}
}

function parseAsArray(parser, fieldPackets, typeCast, connection) {
var self = this;
var next = function () {
return self._typeCast(fieldPacket, parser, connection.config.timezone, connection.config.supportBigNumbers, connection.config.bigNumberStrings, connection.config.dateStrings);
};
var row = new Array(fieldPackets.length);

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

if (typeof typeCast === 'function') {
row[i] = typeCast.apply(connection, [ new Field({ packet: fieldPacket, parser: parser }), next ]);
} else {
row[i] = (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() );
}
}

return row;
}

function typeCast(field, parser, timeZone, supportBigNumbers, bigNumberStrings, dateStrings) {
var numberString;

Expand Down
15 changes: 11 additions & 4 deletions lib/protocol/sequences/Query.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,19 @@ 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;

if (this.arrayRow) {
row = packet.parseAsArray(parser, this._resultSet.fieldPackets, this.typeCast, connection);
} else {
packet.parse(parser, this._resultSet.fieldPackets, this.typeCast, this.nestTables, connection);
row = packet;
}

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