printing
This commit is contained in:
parent
c7e9824208
commit
ffd4b3fa5b
|
@ -6,7 +6,6 @@ import services.DatabaseService;
|
|||
import services.ReportGenerationService;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
|
@ -29,7 +28,7 @@ class App {
|
|||
private static final CustomPreparedStatementsRead[] reportEndpoints = {
|
||||
CustomPreparedStatementsRead.HighestAndLowest10Vaccination,
|
||||
CustomPreparedStatementsRead.HighestInfectionsPer100K,
|
||||
//CustomPreparedStatementsRead.CountriesLaggingBehind,
|
||||
CustomPreparedStatementsRead.CountriesLaggingBehind,
|
||||
};
|
||||
|
||||
private static final CustomPreparedStatementsRead[] queryEndpoints = {
|
||||
|
@ -37,13 +36,20 @@ class App {
|
|||
};
|
||||
|
||||
public void run() {
|
||||
//appConfigService.promptUserArguments();
|
||||
try {
|
||||
QueryResultsRepository resultsRepository;
|
||||
for (final CustomPreparedStatementsRead query : reportEndpoints) {
|
||||
resultsRepository = databaseService.executeReadReportsEndpoint(new QueryDTO(query));
|
||||
reportGenerationService.reportResults(resultsRepository);
|
||||
}
|
||||
|
||||
for(final CustomPreparedStatementsRead query: queryEndpoints) {
|
||||
appConfigService.promptAndSetUserArguments();
|
||||
resultsRepository = databaseService.executeReadReportsEndpoint(new QueryDTO(query, new Object[]{appConfigService.countryName, appConfigService.date,}));
|
||||
reportGenerationService.reportResults(resultsRepository);
|
||||
}
|
||||
|
||||
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
|
|
@ -7,24 +7,23 @@ package data.enums;
|
|||
public enum CustomPreparedStatementsRead {
|
||||
DailyInfectionsAndDeathAggregate("""
|
||||
WITH matched_countries AS (
|
||||
SELECT c.name, c.code
|
||||
FROM countries c
|
||||
WHERE c.name LIKE CONCAT('%',?, '%')
|
||||
SELECT c.name, c.code
|
||||
FROM countries c
|
||||
WHERE c.name LIKE CONCAT('%',?, '%')
|
||||
)
|
||||
SELECT recorded_date, infections, deaths
|
||||
FROM cases
|
||||
WHERE iso_country IN (SELECT code FROM matched_countries) AND recorded_date <= ?
|
||||
ORDER BY recorded_date
|
||||
;
|
||||
""",
|
||||
"10 countries with the highest vaccinations (with count), and countries with lowest number of vaccinations."
|
||||
"Daily infections and deaths up until, inside a range (incl. fuzzy search)"
|
||||
),
|
||||
|
||||
HighestAndLowest10Vaccination("""
|
||||
WITH vacc_count_per_country AS (
|
||||
SELECT iso_country, SUM(daily_vaccinations) as total_vaccinations
|
||||
FROM vaccinations
|
||||
GROUP BY iso_country
|
||||
SELECT iso_country, SUM(daily_vaccinations) as total_vaccinations
|
||||
FROM vaccinations
|
||||
GROUP BY iso_country
|
||||
)
|
||||
SELECT name, total_vaccinations
|
||||
FROM countries JOIN vacc_count_per_country
|
||||
|
@ -34,41 +33,39 @@ public enum CustomPreparedStatementsRead {
|
|||
;
|
||||
|
||||
WITH vacc_count_per_country AS (
|
||||
SELECT iso_country, SUM(daily_vaccinations) as total_vaccinations
|
||||
FROM vaccinations
|
||||
GROUP BY iso_country
|
||||
SELECT iso_country, SUM(daily_vaccinations) as total_vaccinations
|
||||
FROM vaccinations
|
||||
GROUP BY iso_country
|
||||
)
|
||||
SELECT name, total_vaccinations
|
||||
FROM countries JOIN vacc_count_per_country
|
||||
ON countries.code = vacc_count_per_country.iso_country
|
||||
ORDER BY total_vaccinations ASC
|
||||
LIMIT 10
|
||||
;
|
||||
""",
|
||||
"10 countries with the highest vaccinations (with count), and countries with lowest number of vaccinations."
|
||||
),
|
||||
|
||||
HighestInfectionsPer100K("""
|
||||
SELECT c.name, ((SUM(cases.infections) * 100000) / c.population) as infections_per_100k, c.population, SUM(cases.infections) as total_infections
|
||||
FROM cases INNER JOIN countries c ON cases.iso_country = c.code
|
||||
GROUP BY c.name, c.population
|
||||
ORDER BY infections_per_100k DESC
|
||||
LIMIT 10
|
||||
""",
|
||||
"10 countries with the highest infections per 100k inhabitants (with count)"
|
||||
),
|
||||
|
||||
HighestInfectionsPer100K("""
|
||||
SELECT c.name, c.population, SUM(cases.infections) as total_infections, (SUM(cases.infections) * 100000) / c.population as infections_per_100k
|
||||
FROM cases
|
||||
INNER JOIN countries c ON cases.iso_country = c.code
|
||||
GROUP BY c.name, c.population
|
||||
ORDER BY infections_per_100k DESC
|
||||
LIMIT 10
|
||||
;
|
||||
""",
|
||||
""
|
||||
),
|
||||
|
||||
CountriesLaggingBehind("""
|
||||
SELECT c.name
|
||||
FROM countries c
|
||||
LEFT JOIN vaccinations v ON c.code = v.iso_country
|
||||
GROUP BY c.name
|
||||
HAVING SUM(v.daily_vaccinations) < c.population
|
||||
;
|
||||
FROM countries c INNER JOIN (
|
||||
SELECT iso_country, SUM(daily_vaccinations) as total_vaccinations
|
||||
FROM vaccinations
|
||||
GROUP BY iso_country
|
||||
) v ON c.code = v.iso_country
|
||||
WHERE v.total_vaccinations < c.population
|
||||
""",
|
||||
""
|
||||
"Countries that haven't vaccinated their whole population with at least one shot or jab."
|
||||
);
|
||||
|
||||
public final String statementTemplate;
|
||||
|
|
|
@ -1,41 +0,0 @@
|
|||
package data.models;
|
||||
|
||||
import java.sql.Array;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.ResultSetMetaData;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class PersistentResultModel {
|
||||
/**
|
||||
* Factory constructor, that adapts a given ResultSet to PersistentResultModel.
|
||||
* @param resultSet
|
||||
* @throws SQLException
|
||||
*/
|
||||
public PersistentResultModel (ResultSet resultSet) throws SQLException {
|
||||
final ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
|
||||
final int columnCount = resultSetMetaData.getColumnCount();
|
||||
|
||||
final ArrayList<Object[]> grwingResultsGrid = new ArrayList<>();
|
||||
while (resultSet.next()) {
|
||||
final Object[] currentRow = new Object[columnCount];
|
||||
for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) {
|
||||
currentRow[columnIndex] = resultSet.getObject(columnIndex+1);
|
||||
}
|
||||
grwingResultsGrid.add(currentRow);
|
||||
}
|
||||
|
||||
resultColumns = new ResultColumnModel[columnCount];
|
||||
for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) {
|
||||
int finalColumnIndex = columnIndex;
|
||||
final var entries = grwingResultsGrid.stream().map(row -> row[finalColumnIndex]).toList().toArray();
|
||||
resultColumns[columnIndex] = new ResultColumnModel(resultSetMetaData.getColumnName(columnIndex+1), entries);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
final public ResultColumnModel[] resultColumns;
|
||||
public record ResultColumnModel(String columnName, Object[] entries){}
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
package data.models;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.ResultSetMetaData;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
final public ResultRowModel[] resultRowEntries;
|
||||
final public String[] columnNames;
|
||||
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() {
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
package data.repos;
|
||||
|
||||
|
||||
import data.models.PersistentResultModel;
|
||||
import data.models.PersistentResultSetModel;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
@ -11,11 +11,16 @@ import java.util.ArrayList;
|
|||
* Stores the results obtained from executing an SQL query.
|
||||
*/
|
||||
public class QueryResultsRepository {
|
||||
public QueryResultsRepository(String queryDescription) {
|
||||
this.queryDescription = queryDescription;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store for the results
|
||||
*/
|
||||
final private ArrayList<PersistentResultModel> persistentResults = new ArrayList<>();
|
||||
public PersistentResultModel[] getResults () {return persistentResults.toArray(new PersistentResultModel[]{});}
|
||||
final private ArrayList<PersistentResultSetModel> persistentResults = new ArrayList<>();
|
||||
|
||||
final private String queryDescription;
|
||||
|
||||
/**
|
||||
* Adds a ResultSet to the store to persist.
|
||||
|
@ -23,6 +28,16 @@ public class QueryResultsRepository {
|
|||
* @throws SQLException
|
||||
*/
|
||||
public void addResult(ResultSet resultSet) throws SQLException {
|
||||
persistentResults.add(new PersistentResultModel(resultSet));
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@ public class AppConfigService {
|
|||
public String countryName;
|
||||
public Date date;
|
||||
|
||||
public void promptUserArguments() {
|
||||
public void promptAndSetUserArguments() {
|
||||
final Scanner scanner = new Scanner(System.in);
|
||||
System.out.println(ConstValues.inputCountryName);
|
||||
countryName = scanner.nextLine();
|
||||
|
|
|
@ -31,7 +31,7 @@ public class DatabaseService {
|
|||
public QueryResultsRepository executeReadReportsEndpoint(QueryDTO queryDTO) throws SQLException {
|
||||
final PreparedStatement resultStatement = databaseApi.performReadQuery(queryDTO, connection);
|
||||
|
||||
final QueryResultsRepository resultSets = new QueryResultsRepository();
|
||||
final QueryResultsRepository resultSets = new QueryResultsRepository(queryDTO.statement().description);
|
||||
do {
|
||||
final ResultSet resultSet = resultStatement.getResultSet();
|
||||
resultSets.addResult(resultSet);
|
||||
|
|
|
@ -4,6 +4,6 @@ import data.repos.QueryResultsRepository;
|
|||
|
||||
public class ReportGenerationService {
|
||||
public void reportResults(QueryResultsRepository queryResultsRepository) {
|
||||
//queryResultsModel.
|
||||
System.out.println(queryResultsRepository);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue