2016-03-15 15:37:24 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const helpers = require('./helpers');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Query result object
|
|
|
|
*
|
|
|
|
* @param {Array} rows - the data rows of the result
|
|
|
|
* @param {Array} columns - the column names in the result
|
|
|
|
*/
|
|
|
|
class Result {
|
|
|
|
/**
|
|
|
|
* Create a result object
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {Array} [rows] - the data rows of the result
|
|
|
|
* @param {Array} [columns] - the column names in the result
|
|
|
|
*/
|
2016-11-21 16:39:56 -05:00
|
|
|
constructor (rows=[], columns=[]) {
|
|
|
|
this._rows = rows;
|
|
|
|
this._columns = columns;
|
2016-03-15 15:37:24 -04:00
|
|
|
|
|
|
|
// If columns aren't explicitly given,
|
|
|
|
// get the list from the first row's keys
|
|
|
|
if (
|
|
|
|
this._columns.length === 0 &&
|
|
|
|
this._rows.length > 0 &&
|
|
|
|
helpers.isObject(rows[0])
|
|
|
|
) {
|
|
|
|
this.columns = Object.keys(rows[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the result rows
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @return {Array} - the data rows of the result
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
get rows () {
|
2016-03-15 15:37:24 -04:00
|
|
|
return this._rows;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the result rows for the result object
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {Array} rows - the data rows of the result
|
|
|
|
* @return {void}
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
set rows (rows) {
|
2016-03-15 15:37:24 -04:00
|
|
|
this._rows = rows;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the result columns
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @return {Array} - the column names in the result
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
get columns () {
|
2016-03-15 15:37:24 -04:00
|
|
|
return this._columns;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the result columns for the result object
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {Array} cols - the array of columns for the current result
|
|
|
|
* @return {void}
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
set columns (cols) {
|
2016-03-15 15:37:24 -04:00
|
|
|
this._columns = cols;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the number of rows returned by the query
|
|
|
|
*
|
|
|
|
* @return {Number} - the number of rows in the result
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
rowCount () {
|
2016-03-15 15:37:24 -04:00
|
|
|
return this._rows.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the number of columns returned by the query
|
|
|
|
*
|
|
|
|
* @return {Number} - the number of columns in the result
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
columnCount () {
|
2016-03-15 15:37:24 -04:00
|
|
|
return this._columns.length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-14 16:50:32 -04:00
|
|
|
module.exports = Result;
|