/**
 * This class stores a database result.
 */
module.exports = new Class
({
	/**
	 * Initilizes the result object.
	 */
	initialize: function (result)
	{
		Object.assign (this, {
			data: result.data,
			tables: result.tables,
			columns: result.columns,
			row: -1
		});
		
		if (this.columns)
		{
			this.columnMap = {};

			for (var i = 0; i < this.columns.length; i++)
				this.columnMap[this.columns[i].name] = i;
		}
		else
			this.columnMap = null;
	}
	
	/**
	 * Gets a value from de result.
	 *
	 * @param {string} columnName The column name
	 * @return {Object} The cell value
	 */
	,get: function (columnName)
	{
		var columnIndex = this.columnMap[columnName];
		return this.data[this.row][columnIndex];
	}

	/**
	 * Moves the pointer to the next row and returns it as an object.
	 *
	 * @return {Object} The row or %null if there are no more rows
	 */
	,fetchObject: function ()
	{
		if (!this.next ())
			return null;

		return this.getObject (this.row);
	}

	/**
	 * Returns the current row as an object.
	 *
	 * @param {number} rowIndex The row index
	 * @return {Object} The row or %null if there are no more rows
	 */
	,getObject: function (rowIndex)
	{
		var row = this.data[rowIndex];
		var cols = this.columns;
		var object = {};

		for (var i = 0; i < cols.length; i++)
			object[cols[i].name] = row[i];

		return object;
	}
	
	/**
	 * Resets the result iterator.
	 */
	,reset: function ()
	{
		this.row = -1;
	}
	
	/**
	 * Moves the internal iterator to the next row.
	 */
	,next: function ()
	{
		this.row++;
		
		if (this.row >= this.data.length)
			return false;
			
		return true;
	}
});

