2015-01-23 13:09:30 +00:00
|
|
|
/**
|
|
|
|
* This class stores a database result.
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2016-09-26 09:28:47 +00:00
|
|
|
module.exports = new Class
|
2015-01-23 13:09:30 +00:00
|
|
|
({
|
|
|
|
/**
|
|
|
|
* Initilizes the result object.
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-03-19 19:36:11 +00:00
|
|
|
initialize: function (result)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2017-04-08 11:42:27 +00:00
|
|
|
Object.assign (this, {
|
|
|
|
data: result.data,
|
|
|
|
tables: result.tables,
|
|
|
|
columns: result.columns,
|
|
|
|
row: -1
|
|
|
|
});
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2015-03-19 19:36:11 +00:00
|
|
|
if (this.columns)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
|
|
|
this.columnMap = {};
|
|
|
|
|
2015-03-19 19:36:11 +00:00
|
|
|
for (var i = 0; i < this.columns.length; i++)
|
|
|
|
this.columnMap[this.columns[i].name] = i;
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
2015-03-19 19:36:11 +00:00
|
|
|
else
|
|
|
|
this.columnMap = null;
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a value from de result.
|
|
|
|
*
|
|
|
|
* @param {String} columnName The column name
|
|
|
|
* @return {Object} The cell value
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-01-23 13:09:30 +00:00
|
|
|
,get: function (columnName)
|
|
|
|
{
|
|
|
|
var columnIndex = this.columnMap[columnName];
|
|
|
|
return this.data[this.row][columnIndex];
|
|
|
|
}
|
2017-04-08 11:42:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the current row as an object.
|
|
|
|
*
|
|
|
|
* @return {Object} The row or %null if there are no more rows
|
|
|
|
*/
|
|
|
|
,getObject: function ()
|
|
|
|
{
|
|
|
|
var row = this.data[this.row];
|
|
|
|
var cols = this.columns;
|
|
|
|
var object = {};
|
|
|
|
|
|
|
|
for (var i = 0; i < cols.length; i++)
|
|
|
|
object[cols[i].name] = row[i];
|
|
|
|
|
|
|
|
return object;
|
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets the result iterator.
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-01-23 13:09:30 +00:00
|
|
|
,reset: function ()
|
|
|
|
{
|
|
|
|
this.row = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Moves the internal iterator to the next row.
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-01-23 13:09:30 +00:00
|
|
|
,next: function ()
|
|
|
|
{
|
|
|
|
this.row++;
|
|
|
|
|
|
|
|
if (this.row >= this.data.length)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|