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

104 lines
1.8 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: function(results, error) {
this.results = results;
this.error = error;
}
/**
* Gets the query error.
*
* @return {Db.Err} the error or null if no errors hapened
**/
,getError: function() {
return this.error;
}
,fetch: function() {
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: function() {
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 from the next resultset.
*
* @return {Array} the row if success, %null otherwise
**/
,fetchRow: function() {
var result = this.fetch();
if (result !== null
&& result.data instanceof Array
&& result.data.length > 0)
return result.data[0];
return null;
}
,fetchObject: function() {
var result = this.fetch();
if (result !== null
&& result.data instanceof Array
&& result.data.length > 0) {
var row = result.data[0];
var object = {};
for(var i = 0; i < row.length; i++)
object[result.columns[i].name] = row[i];
return object;
}
return null;
}
/**
* Fetchs the first row and column value from the next resultset.
*
* @return {Object} the value if success, %null otherwise
**/
,fetchValue: function() {
var row = this.fetchRow();
if (row instanceof Array && row.length > 0)
return row[0];
return null;
}
});