All files / lib Result.js

58.33% Statements 7/12
85.71% Branches 6/7
42.86% Functions 3/7
58.33% Lines 7/12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 945x                                 218x 218x       218x                                                                                                                 140x                 140x       5x  
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
	 */
	constructor (rows = [], columns = []) {
		this._rows = rows;
		this._columns = columns;
 
		// If columns aren't explicitly given,
		// get the list from the first row's keys
		Iif (
			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
	 */
	get rows () {
		return this._rows;
	}
 
	/**
	 * Set the result rows for the result object
	 *
	 * @private
	 * @param {Array} rows - the data rows of the result
	 * @return {void}
	 */
	set rows (rows) {
		this._rows = rows;
	}
 
	/**
	 * Return the result columns
	 *
	 * @private
	 * @return {Array} - the column names in the result
	 */
	get columns () {
		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}
	 */
	set columns (cols) {
		this._columns = cols;
	}
 
	/**
	 * Get the number of rows returned by the query
	 *
	 * @return {Number} - the number of rows in the result
	 */
	rowCount () {
		return this._rows.length;
	}
 
	/**
	 * Get the number of columns returned by the query
	 *
	 * @return {Number} - the number of columns in the result
	 */
	columnCount () {
		return this._columns.length;
	}
}
 
module.exports = Result;