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

129 lines
2.7 KiB
JavaScript

import { Result } from './result';
/**
* This class stores the database results.
*/
export class ResultSet {
results = null;
error = null;
/**
* Initilizes the resultset object.
*/
constructor(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() {
const 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() {
const 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() {
const 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() {
const 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() {
const result = this.fetch();
if (
result !== null &&
result.data instanceof Array &&
result.data.length > 0
) {
const object = result.data[0];
const row = new Array(result.columns.length);
for (let i = 0; i < row.length; i++) {
row[i] = object[result.columns[i].name];
}
return row;
}
return null;
}
}