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

104 lines
1.8 KiB
JavaScript
Raw Normal View History

2016-09-26 09:28:47 +00:00
2022-05-05 13:56:17 +00:00
var Result = require('./result');
2016-09-26 09:28:47 +00:00
/**
* This class stores the database results.
**/
2022-05-05 13:56:17 +00:00
module.exports = new Class({
results: null
,error: null
/**
* Initilizes the resultset object.
**/
2022-05-05 13:56:17 +00:00
,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
**/
2022-05-05 13:56:17 +00:00
,getError: function() {
return this.error;
}
2022-05-05 13:56:17 +00:00
,fetch: function() {
2016-10-11 14:45:10 +00:00
if (this.error)
throw this.error;
if (this.results !== null
&& this.results.length > 0)
2022-05-05 13:56:17 +00:00
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
**/
2022-05-05 13:56:17 +00:00
,fetchResult: function() {
var result = this.fetch();
2022-05-05 13:56:17 +00:00
if (result !== null) {
if (result.data instanceof Array)
2022-05-05 13:56:17 +00:00
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
**/
2022-05-05 13:56:17 +00:00
,fetchRow: function() {
var result = this.fetch();
if (result !== null
&& result.data instanceof Array
&& result.data.length > 0)
return result.data[0];
return null;
}
2022-05-05 13:56:17 +00:00
,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
**/
2022-05-05 13:56:17 +00:00
,fetchValue: function() {
var row = this.fetchRow();
if (row instanceof Array && row.length > 0)
return row[0];
return null;
}
});