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

124 lines
2.1 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-24 10:18:44 +00:00
*/
2022-05-05 13:56:17 +00:00
module.exports = new Class({
results: null
,error: null
/**
* Initilizes the resultset object.
2022-05-24 10:18:44 +00:00
*/
2022-11-16 01:46:44 +00:00
,initialize(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-24 10:18:44 +00:00
*/
2022-11-16 01:46:44 +00:00
,getError() {
return this.error;
}
2022-11-16 01:46:44 +00:00
,fetch() {
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-24 10:18:44 +00:00
*/
2022-11-16 01:46:44 +00:00
,fetchResult() {
2022-05-05 13:56:17 +00:00
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;
}
2022-05-24 10:18:44 +00:00
/**
2022-05-28 15:49:46 +00:00
* Fetchs the first row object from the next resultset.
*
* @return {Array} the row if success, %null otherwise
2022-05-24 10:18:44 +00:00
*/
2022-11-16 01:46:44 +00:00
,fetchObject() {
2022-05-05 13:56:17 +00:00
var result = this.fetch();
if (result !== null
&& result.data instanceof Array
&& result.data.length > 0)
return result.data[0];
2022-05-28 15:49:46 +00:00
return null;
}
2022-05-24 10:18:44 +00:00
/**
2022-05-28 15:49:46 +00:00
* Fetchs data from the next resultset.
2022-05-24 10:18:44 +00:00
*
2022-05-28 15:49:46 +00:00
* @return {Array} the data
2022-05-24 10:18:44 +00:00
*/
2022-11-16 01:46:44 +00:00
,fetchData() {
2022-05-05 13:56:17 +00:00
var result = this.fetch();
if (result !== null
2022-05-28 15:49:46 +00:00
&& result.data instanceof Array)
return result.data;
2022-05-05 13:56:17 +00:00
return null;
}
/**
* Fetchs the first row and column value from the next resultset.
*
* @return {Object} the value if success, %null otherwise
2022-05-24 10:18:44 +00:00
*/
2022-11-16 01:46:44 +00:00
,fetchValue() {
2022-05-05 13:56:17 +00:00
var row = this.fetchRow();
if (row instanceof Array && row.length > 0)
return row[0];
return null;
}
2022-05-28 15:49:46 +00:00
/**
* Fetchs the first row from the next resultset.
*
* @return {Array} the row if success, %null otherwise
*/
2022-11-16 01:46:44 +00:00
,fetchRow() {
2022-05-28 15:49:46 +00:00
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;
}
});