printing
This commit is contained in:
parent
c7e9824208
commit
ffd4b3fa5b
8 changed files with 127 additions and 81 deletions
|
@ -6,7 +6,6 @@ import services.DatabaseService;
|
||||||
import services.ReportGenerationService;
|
import services.ReportGenerationService;
|
||||||
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
@ -29,7 +28,7 @@ class App {
|
||||||
private static final CustomPreparedStatementsRead[] reportEndpoints = {
|
private static final CustomPreparedStatementsRead[] reportEndpoints = {
|
||||||
CustomPreparedStatementsRead.HighestAndLowest10Vaccination,
|
CustomPreparedStatementsRead.HighestAndLowest10Vaccination,
|
||||||
CustomPreparedStatementsRead.HighestInfectionsPer100K,
|
CustomPreparedStatementsRead.HighestInfectionsPer100K,
|
||||||
//CustomPreparedStatementsRead.CountriesLaggingBehind,
|
CustomPreparedStatementsRead.CountriesLaggingBehind,
|
||||||
};
|
};
|
||||||
|
|
||||||
private static final CustomPreparedStatementsRead[] queryEndpoints = {
|
private static final CustomPreparedStatementsRead[] queryEndpoints = {
|
||||||
|
@ -37,13 +36,20 @@ class App {
|
||||||
};
|
};
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
//appConfigService.promptUserArguments();
|
|
||||||
try {
|
try {
|
||||||
QueryResultsRepository resultsRepository;
|
QueryResultsRepository resultsRepository;
|
||||||
for (final CustomPreparedStatementsRead query : reportEndpoints) {
|
for (final CustomPreparedStatementsRead query : reportEndpoints) {
|
||||||
resultsRepository = databaseService.executeReadReportsEndpoint(new QueryDTO(query));
|
resultsRepository = databaseService.executeReadReportsEndpoint(new QueryDTO(query));
|
||||||
reportGenerationService.reportResults(resultsRepository);
|
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) {
|
} catch (SQLException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,9 +15,8 @@ public enum CustomPreparedStatementsRead {
|
||||||
FROM cases
|
FROM cases
|
||||||
WHERE iso_country IN (SELECT code FROM matched_countries) AND recorded_date <= ?
|
WHERE iso_country IN (SELECT code FROM matched_countries) AND recorded_date <= ?
|
||||||
ORDER BY 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("""
|
HighestAndLowest10Vaccination("""
|
||||||
|
@ -43,32 +42,30 @@ public enum CustomPreparedStatementsRead {
|
||||||
ON countries.code = vacc_count_per_country.iso_country
|
ON countries.code = vacc_count_per_country.iso_country
|
||||||
ORDER BY total_vaccinations ASC
|
ORDER BY total_vaccinations ASC
|
||||||
LIMIT 10
|
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)"
|
"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("""
|
CountriesLaggingBehind("""
|
||||||
SELECT c.name
|
SELECT c.name
|
||||||
FROM countries c
|
FROM countries c INNER JOIN (
|
||||||
LEFT JOIN vaccinations v ON c.code = v.iso_country
|
SELECT iso_country, SUM(daily_vaccinations) as total_vaccinations
|
||||||
GROUP BY c.name
|
FROM vaccinations
|
||||||
HAVING SUM(v.daily_vaccinations) < c.population
|
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;
|
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){}
|
|
||||||
}
|
|
69
src/data/models/PersistentResultSetModel.java
Normal file
69
src/data/models/PersistentResultSetModel.java
Normal file
|
@ -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;
|
package data.repos;
|
||||||
|
|
||||||
|
|
||||||
import data.models.PersistentResultModel;
|
import data.models.PersistentResultSetModel;
|
||||||
|
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
@ -11,11 +11,16 @@ import java.util.ArrayList;
|
||||||
* Stores the results obtained from executing an SQL query.
|
* Stores the results obtained from executing an SQL query.
|
||||||
*/
|
*/
|
||||||
public class QueryResultsRepository {
|
public class QueryResultsRepository {
|
||||||
|
public QueryResultsRepository(String queryDescription) {
|
||||||
|
this.queryDescription = queryDescription;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Store for the results
|
* Store for the results
|
||||||
*/
|
*/
|
||||||
final private ArrayList<PersistentResultModel> persistentResults = new ArrayList<>();
|
final private ArrayList<PersistentResultSetModel> persistentResults = new ArrayList<>();
|
||||||
public PersistentResultModel[] getResults () {return persistentResults.toArray(new PersistentResultModel[]{});}
|
|
||||||
|
final private String queryDescription;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a ResultSet to the store to persist.
|
* Adds a ResultSet to the store to persist.
|
||||||
|
@ -23,6 +28,16 @@ public class QueryResultsRepository {
|
||||||
* @throws SQLException
|
* @throws SQLException
|
||||||
*/
|
*/
|
||||||
public void addResult(ResultSet resultSet) 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 String countryName;
|
||||||
public Date date;
|
public Date date;
|
||||||
|
|
||||||
public void promptUserArguments() {
|
public void promptAndSetUserArguments() {
|
||||||
final Scanner scanner = new Scanner(System.in);
|
final Scanner scanner = new Scanner(System.in);
|
||||||
System.out.println(ConstValues.inputCountryName);
|
System.out.println(ConstValues.inputCountryName);
|
||||||
countryName = scanner.nextLine();
|
countryName = scanner.nextLine();
|
||||||
|
|
|
@ -31,7 +31,7 @@ public class DatabaseService {
|
||||||
public QueryResultsRepository executeReadReportsEndpoint(QueryDTO queryDTO) throws SQLException {
|
public QueryResultsRepository executeReadReportsEndpoint(QueryDTO queryDTO) throws SQLException {
|
||||||
final PreparedStatement resultStatement = databaseApi.performReadQuery(queryDTO, connection);
|
final PreparedStatement resultStatement = databaseApi.performReadQuery(queryDTO, connection);
|
||||||
|
|
||||||
final QueryResultsRepository resultSets = new QueryResultsRepository();
|
final QueryResultsRepository resultSets = new QueryResultsRepository(queryDTO.statement().description);
|
||||||
do {
|
do {
|
||||||
final ResultSet resultSet = resultStatement.getResultSet();
|
final ResultSet resultSet = resultStatement.getResultSet();
|
||||||
resultSets.addResult(resultSet);
|
resultSets.addResult(resultSet);
|
||||||
|
|
|
@ -4,6 +4,6 @@ import data.repos.QueryResultsRepository;
|
||||||
|
|
||||||
public class ReportGenerationService {
|
public class ReportGenerationService {
|
||||||
public void reportResults(QueryResultsRepository queryResultsRepository) {
|
public void reportResults(QueryResultsRepository queryResultsRepository) {
|
||||||
//queryResultsModel.
|
System.out.println(queryResultsRepository);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue