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

52 lines
2.0 KiB
Java

package data.repos;
import data.models.PersistentResultSetModel;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* Provides a way to store and persist the results of read queries.
* It contains a list of `PersistentResultSetModel` instances 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 {
/**
* Store for the results
*/
private final List<PersistentResultSetModel> persistentResults = new ArrayList<>();
/**
* Represents a description of the query that was executed.
*/
private final String queryDescription;
public QueryResultsRepository(String queryDescription) {
this.queryDescription = queryDescription;
}
/**
* Creates a PersistentResultSetModel instance from given ResultSet, and adds it to the persistentResults list
*
* @param resultSet the resultSet to be added to the persistentResults list
* @throws SQLException if an error occurs while creating the PersistentResultSetModel instance
*/
public void addResult(ResultSet resultSet) throws SQLException {
persistentResults.add(new PersistentResultSetModel(resultSet));
}
@Override
public String toString() {
StringBuilder templateBuilder = new StringBuilder("\n\nQUERY DESCRIPTION: ").append(queryDescription);
for (final PersistentResultSetModel result : persistentResults) {
templateBuilder.append("\n").append(result).append("\n>");
}
return templateBuilder.deleteCharAt(templateBuilder.length() - 1).append("\n\n").toString();
}
}