hedera-web/js/db/result-set.js

124 lines
2.1 KiB
JavaScript

var Result = require('./result');
/**
* This class stores the database results.
*/
module.exports = new Class({
results: null
,error: null
/**
* Initilizes the resultset object.
*/
,initialize(results, error) {
this.results = results;
this.error = error;
}
/**
* Gets the query error.
*
* @return {Db.Err} the error or null if no errors hapened
*/
,getError() {
return this.error;
}
,fetch() {
if (this.error)
throw this.error;
if (this.results !== null
&& this.results.length > 0)
return this.results.shift();
return null;
}
/**
* Fetchs the next result from the resultset.
*
* @return {Db.Result} the result or %null if error or there are no more results
*/
,fetchResult() {
var result = this.fetch();
if (result !== null) {
if (result.data instanceof Array)
return new Result(result);
else
return true;
}
return null;
}
/**
* Fetchs the first row object from the next resultset.
*
* @return {Array} the row if success, %null otherwise
*/
,fetchObject() {
var result = this.fetch();
if (result !== null
&& result.data instanceof Array
&& result.data.length > 0)
return result.data[0];
return null;
}
/**
* Fetchs data from the next resultset.
*
* @return {Array} the data
*/
,fetchData() {
var result = this.fetch();
if (result !== null
&& result.data instanceof Array)
return result.data;
return null;
}
/**
* Fetchs the first row and column value from the next resultset.
*
* @return {Object} the value if success, %null otherwise
*/
,fetchValue() {
var row = this.fetchRow();
if (row instanceof Array && row.length > 0)
return row[0];
return null;
}
/**
* Fetchs the first row from the next resultset.
*
* @return {Array} the row if success, %null otherwise
*/
,fetchRow() {
var result = this.fetch();
if (result !== null
&& result.data instanceof Array
&& result.data.length > 0) {
var object = result.data[0];
var row = new Array(result.columns.length);
for(var i = 0; i < row.length; i++)
row[i] = object[result.columns[i].name];
return row;
}
return null;
}
});