added SQL queries
This commit is contained in:
parent
0213bc4606
commit
b35ffac6fa
6 changed files with 91 additions and 17 deletions
|
@ -1,8 +1,12 @@
|
||||||
|
import data.dtos.QueryDTO;
|
||||||
import data.enums.CustomPreparedStatementsRead;
|
import data.enums.CustomPreparedStatementsRead;
|
||||||
import data.models.QueryResultsModel;
|
import data.models.QueryResultsModel;
|
||||||
import services.AppConfigService;
|
import services.AppConfigService;
|
||||||
import services.DatabaseService;
|
import services.DatabaseService;
|
||||||
|
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
final App app = new App();
|
final App app = new App();
|
||||||
|
@ -20,7 +24,7 @@ class App {
|
||||||
final AppConfigService appConfigService;
|
final AppConfigService appConfigService;
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
appConfigService.promptUserArguments();
|
//appConfigService.promptUserArguments();
|
||||||
final QueryResultsModel resultSetsQuery1 = databaseService.executeReadReportsEndpoint(CustomPreparedStatementsRead.HighestAndLowest10Vaccination);
|
final QueryResultsModel resultSetsQuery1 = databaseService.executeReadReportsEndpoint(new QueryDTO(CustomPreparedStatementsRead.HighestAndLowest10Vaccination));
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -5,13 +5,16 @@ import data.dtos.QueryDTO;
|
||||||
import java.sql.*;
|
import java.sql.*;
|
||||||
|
|
||||||
public class DatabaseApi {
|
public class DatabaseApi {
|
||||||
public PreparedStatement performReadQuery(QueryDTO queryDTO, Connection connection) {
|
public PreparedStatement performReadQuery(QueryDTO queryDTO, Connection connection) throws SQLException {
|
||||||
try {
|
// Set template
|
||||||
PreparedStatement statement = connection.prepareStatement(queryDTO.queryTemplate().statementTemplate);
|
PreparedStatement statement = connection.prepareStatement(queryDTO.statement().statementTemplate);
|
||||||
statement.execute();
|
|
||||||
return statement;
|
// Add in arguments
|
||||||
} catch (SQLException e) {
|
for(int argIndex = 0; argIndex < queryDTO.templateArgs().length; argIndex++)
|
||||||
throw new RuntimeException(e);
|
// parameter indexing starts at 1
|
||||||
}
|
statement.setString(argIndex + 1, queryDTO.templateArgs()[argIndex].toString());
|
||||||
|
|
||||||
|
statement.execute();
|
||||||
|
return statement;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,8 @@ package data.dtos;
|
||||||
|
|
||||||
import data.enums.CustomPreparedStatementsRead;
|
import data.enums.CustomPreparedStatementsRead;
|
||||||
|
|
||||||
public record QueryDTO(CustomPreparedStatementsRead queryTemplate, Object[] placeholderReplacements) {
|
public record QueryDTO(CustomPreparedStatementsRead statement, Object[] templateArgs) {
|
||||||
|
public QueryDTO(CustomPreparedStatementsRead customPreparedStatementsRead) {
|
||||||
|
this(customPreparedStatementsRead, new Object[]{});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,68 @@
|
||||||
package data.enums;
|
package data.enums;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Holds prepared data for Read-based SQL queries.
|
||||||
|
*
|
||||||
|
*/
|
||||||
public enum CustomPreparedStatementsRead {
|
public enum CustomPreparedStatementsRead {
|
||||||
HighestAndLowest10Vaccination("""
|
DailyInfectionsAndDeathAggregate("""
|
||||||
|
WITH matched_countries AS (
|
||||||
|
SELECT c.name, c.code\s
|
||||||
|
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;
|
||||||
|
"""
|
||||||
|
),
|
||||||
|
|
||||||
|
HighestAndLowest10Vaccination("""
|
||||||
|
WITH vacc_count_per_country AS (
|
||||||
|
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 DESC
|
||||||
|
LIMIT 10
|
||||||
|
;
|
||||||
|
|
||||||
|
WITH vacc_count_per_country AS (
|
||||||
|
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
|
||||||
|
;
|
||||||
"""
|
"""
|
||||||
),
|
),
|
||||||
|
|
||||||
HighestInfectionsPer100K("""
|
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
|
||||||
|
;
|
||||||
"""
|
"""
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -24,12 +24,13 @@ public class DatabaseService {
|
||||||
databaseApi = new DatabaseApi();
|
databaseApi = new DatabaseApi();
|
||||||
}
|
}
|
||||||
|
|
||||||
public QueryResultsModel executeReadReportsEndpoint(CustomPreparedStatementsRead customPreparedStatement) {
|
public QueryResultsModel executeReadReportsEndpoint(QueryDTO queryDTO) {
|
||||||
final QueryDTO queryDTO = new QueryDTO(customPreparedStatement, new Object[]{});
|
|
||||||
final PreparedStatement resultStatement = databaseApi.performReadQuery(queryDTO, connection);
|
|
||||||
try {
|
try {
|
||||||
|
final PreparedStatement resultStatement = databaseApi.performReadQuery(queryDTO, connection);
|
||||||
|
|
||||||
final ArrayList<ResultSet> resultSets = new ArrayList<>();
|
final ArrayList<ResultSet> resultSets = new ArrayList<>();
|
||||||
do {
|
do {
|
||||||
|
// execute and persist results in a model
|
||||||
resultSets.add(resultStatement.getResultSet());
|
resultSets.add(resultStatement.getResultSet());
|
||||||
}
|
}
|
||||||
while (resultStatement.getMoreResults());
|
while (resultStatement.getMoreResults());
|
||||||
|
@ -40,7 +41,7 @@ public class DatabaseService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Connect to the PostgreSQL database
|
* Connect to the PostgreSQL database
|
||||||
*
|
*
|
||||||
* @return a Connection object
|
* @return a Connection object
|
||||||
|
|
9
src/services/ReportGenerationService.java
Normal file
9
src/services/ReportGenerationService.java
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
package services;
|
||||||
|
|
||||||
|
import data.models.QueryResultsModel;
|
||||||
|
|
||||||
|
public class ReportGenerationService {
|
||||||
|
public static void printResults(QueryResultsModel queryResultsModel) {
|
||||||
|
//queryResultsModel.
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue