added DTOs and models
This commit is contained in:
parent
cf3fa3f566
commit
0213bc4606
8 changed files with 46 additions and 51 deletions
BIN
.DS_Store
vendored
BIN
.DS_Store
vendored
Binary file not shown.
|
@ -1,9 +1,8 @@
|
|||
import data.enums.CustomReadPreparedStatements;
|
||||
import data.enums.CustomPreparedStatementsRead;
|
||||
import data.models.QueryResultsModel;
|
||||
import services.AppConfigService;
|
||||
import services.DatabaseService;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
final App app = new App();
|
||||
|
@ -21,7 +20,7 @@ class App {
|
|||
final AppConfigService appConfigService;
|
||||
|
||||
public void run() {
|
||||
//appConfigService.promptUserArguments();
|
||||
final ResultSet[] resultSetsQuery1 = databaseService.executeReadReportsEndpoint(CustomReadPreparedStatements.HighestAndLowest10Vaccination);
|
||||
appConfigService.promptUserArguments();
|
||||
final QueryResultsModel resultSetsQuery1 = databaseService.executeReadReportsEndpoint(CustomPreparedStatementsRead.HighestAndLowest10Vaccination);
|
||||
}
|
||||
}
|
|
@ -5,9 +5,9 @@ import data.dtos.QueryDTO;
|
|||
import java.sql.*;
|
||||
|
||||
public class DatabaseApi {
|
||||
public PreparedStatement performQuery(QueryDTO queryDTO, Connection connection) {
|
||||
public PreparedStatement performReadQuery(QueryDTO queryDTO, Connection connection) {
|
||||
try {
|
||||
PreparedStatement statement = connection.prepareStatement(queryDTO.queryTemplate().statementData);
|
||||
PreparedStatement statement = connection.prepareStatement(queryDTO.queryTemplate().statementTemplate);
|
||||
statement.execute();
|
||||
return statement;
|
||||
} catch (SQLException e) {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package data.dtos;
|
||||
|
||||
import data.enums.CustomReadPreparedStatements;
|
||||
import data.enums.CustomPreparedStatementsRead;
|
||||
|
||||
public record QueryDTO(CustomReadPreparedStatements queryTemplate, Object[] placeholderReplacements) {
|
||||
public record QueryDTO(CustomPreparedStatementsRead queryTemplate, Object[] placeholderReplacements) {
|
||||
|
||||
}
|
||||
|
|
19
src/data/enums/CustomPreparedStatementsRead.java
Normal file
19
src/data/enums/CustomPreparedStatementsRead.java
Normal file
|
@ -0,0 +1,19 @@
|
|||
package data.enums;
|
||||
|
||||
public enum CustomPreparedStatementsRead {
|
||||
HighestAndLowest10Vaccination("""
|
||||
|
||||
"""
|
||||
),
|
||||
|
||||
HighestInfectionsPer100K("""
|
||||
|
||||
"""
|
||||
);
|
||||
|
||||
public final String statementTemplate;
|
||||
|
||||
private CustomPreparedStatementsRead(String statementTemplate) {
|
||||
this.statementTemplate = statementTemplate;
|
||||
}
|
||||
}
|
|
@ -1,34 +0,0 @@
|
|||
package data.enums;
|
||||
|
||||
public enum CustomReadPreparedStatements {
|
||||
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;
|
||||
"""
|
||||
);
|
||||
|
||||
public final String statementData;
|
||||
|
||||
private CustomReadPreparedStatements(String statementData) {
|
||||
this.statementData = statementData;
|
||||
}
|
||||
}
|
11
src/data/models/QueryResultsModel.java
Normal file
11
src/data/models/QueryResultsModel.java
Normal file
|
@ -0,0 +1,11 @@
|
|||
package data.models;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
|
||||
/**
|
||||
* Stores the results obtained from executing an SQL query.
|
||||
* Essentially a type-def, which is not natively supported in Java.
|
||||
* @param resultSets Result of the query
|
||||
*/
|
||||
public record QueryResultsModel(java.util.ArrayList<ResultSet> resultSets) {
|
||||
}
|
|
@ -2,9 +2,9 @@ package services;
|
|||
|
||||
import apis.DatabaseApi;
|
||||
import data.dtos.QueryDTO;
|
||||
import data.enums.CustomReadPreparedStatements;
|
||||
import data.enums.CustomPreparedStatementsRead;
|
||||
import data.models.QueryResultsModel;
|
||||
|
||||
import javax.xml.transform.Result;
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
@ -12,9 +12,9 @@ import java.util.ArrayList;
|
|||
* Stores the active connection between the app and the local SQL Server.
|
||||
*/
|
||||
public class DatabaseService {
|
||||
private final String url = "jdbc:postgresql://localhost:5432/lunatech_covid";
|
||||
private final String user = "postgres";
|
||||
private final String password = "postgres";
|
||||
private static final String url = "jdbc:postgresql://localhost:5432/lunatech_covid";
|
||||
private static final String user = "postgres";
|
||||
private static final String password = "postgres";
|
||||
|
||||
private final DatabaseApi databaseApi;
|
||||
private final Connection connection;
|
||||
|
@ -24,9 +24,9 @@ public class DatabaseService {
|
|||
databaseApi = new DatabaseApi();
|
||||
}
|
||||
|
||||
public ResultSet[] executeReadReportsEndpoint(CustomReadPreparedStatements customPreparedStatement) {
|
||||
public QueryResultsModel executeReadReportsEndpoint(CustomPreparedStatementsRead customPreparedStatement) {
|
||||
final QueryDTO queryDTO = new QueryDTO(customPreparedStatement, new Object[]{});
|
||||
final PreparedStatement resultStatement = databaseApi.performQuery(queryDTO, connection);
|
||||
final PreparedStatement resultStatement = databaseApi.performReadQuery(queryDTO, connection);
|
||||
try {
|
||||
final ArrayList<ResultSet> resultSets = new ArrayList<>();
|
||||
do {
|
||||
|
@ -34,7 +34,7 @@ public class DatabaseService {
|
|||
}
|
||||
while (resultStatement.getMoreResults());
|
||||
|
||||
return resultSets.toArray(new ResultSet[0]);
|
||||
return new QueryResultsModel(resultSets);
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue