covid_10012023_covid-reports/src/data/models/PersistentResultSetModel.java

96 lines
3.8 KiB
Java

package data.models;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* The class `PersistentResultSetModel` adapts a given `ResultSet` to a
* PersistentResultModel. The class provides two main fields, an array of `ResultRowModel` called
* `resultRowEntries` and an array of `String` called `columnNames`.
* The factory constructor takes a `ResultSet` as a parameter, and adapts it to a `PersistentResultModel`.
* The class also provides a toString method that returns the string representation of the result set.
*/
public class PersistentResultSetModel {
/**
* Factory constructor, that adapts a given ResultSet to PersistentResultModel.
* @param resultSet
* @throws SQLException
*/
public PersistentResultSetModel(ResultSet resultSet) throws SQLException {
final ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
final int columnCount = resultSetMetaData.getColumnCount();
// Retrieving column names
columnNames = new String[columnCount];
for (int columnIndex = 0; columnIndex < columnCount; columnIndex++)
columnNames[columnIndex] = resultSetMetaData.getColumnName(columnIndex+1);
// Retrieving rest of the results
final List<ResultRowModel> rows = new ArrayList<>();
final Object[] rowElements = new Object[columnCount];
while (resultSet.next()) {
for (int columnIndex = 0; columnIndex < rowElements.length; columnIndex++)
rowElements[columnIndex] = resultSet.getObject(columnIndex+1);
rows.add(new ResultRowModel(rowElements.clone()));
}
resultRowEntries = new ResultRowModel[rows.size()];
rows.toArray(resultRowEntries);
}
/**
* Represents all the rows of the result set.
*/
final public ResultRowModel[] resultRowEntries;
/**
* Represents column names that appear in the result set.
*/
/**
* It returns the string representation of the result set in tabular format, showing the column names and rowElements.
*/
final public String[] columnNames;
/**
* The `ResultRowModel` class represents a single row in the result set. It contains an array of
* `Object` called `rowElements`. The class provides an implementation of the `toString()` method
* that returns the string representation of the row.
* @param rowElements The elements of the row
*/
public record ResultRowModel(Object[] rowElements){
@Override
public String toString() {
StringBuilder templateBuilder = new StringBuilder("|");
for (final Object rowElement : rowElements) {
templateBuilder.append("\t\t\t").append(rowElement).append("\t\t\t").append('|');
}
return templateBuilder.toString();
}
}
@Override
public String toString() {
if (resultRowEntries.length == 0) return "--------No results--------";
final int rowLengthEstimate = resultRowEntries[0].toString().replaceAll("\t", " ").length();
final String outerHorizontalLine = new String(new char[rowLengthEstimate]).replace("\0", "_");
final String innerHorizontalLine = new String(new char[rowLengthEstimate]).replace("\0", "-");
StringBuilder templateBuilder = new StringBuilder(outerHorizontalLine).append('\n');
for (final String columnName : columnNames)
templateBuilder.append("|\t\t\t").append(columnName.toUpperCase()).append("\t\t\t");
templateBuilder.append("|\n").append(outerHorizontalLine);
for (final ResultRowModel row: resultRowEntries)
templateBuilder.append('\n').append(row).append('\n').append(innerHorizontalLine);
return templateBuilder + "\n" + outerHorizontalLine;
}
}