covid_10012023_covid-reports/src/data/repos/QueryResultsRepository.java

51 lines
1.8 KiB
Java

package data.repos;
import data.models.PersistentResultSetModel;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
/**
* Provides a way to store and persist the results of read queries.
* It contains an `ArrayList` of `PersistentResultSetModel` called `persistentResults` and a `String` called `queryDescription`.
* The class provides a constructor that takes a `queryDescription` as a parameter and an `addResult()` method
* that takes a `ResultSet` as a parameter and adds it to the `persistentResults` list.
* The class also provides a `toString()` method that returns the string representation of the stored results and their description.
*/
public class QueryResultsRepository {
public QueryResultsRepository(String queryDescription) {
this.queryDescription = queryDescription;
}
/**
* Store for the results
*/
final private ArrayList<PersistentResultSetModel> persistentResults = new ArrayList<>();
/**
* Represents a description of the query that was executed.
*/
final private String queryDescription;
/**
* Creates a PersistentResultSetModel instance from given ResultSet, and adds it to the persistentResults list
* @param resultSet
* @throws SQLException
*/
public void addResult(ResultSet resultSet) throws SQLException {
persistentResults.add(new PersistentResultSetModel(resultSet));
}
@Override
public String toString() {
StringBuilder templateBuilder = new StringBuilder("\n\nDESCRIPTION: ").append(queryDescription);
for (final PersistentResultSetModel result : persistentResults) {
templateBuilder.append("\n").append(result).append("\n>");
}
return templateBuilder.deleteCharAt(templateBuilder.length()-1).append("\n\n").toString();
}
}