diff --git a/.DS_Store b/.DS_Store index 913f297..f14155e 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/src/Main.java b/src/Main.java index a8dbf6e..f45e1ee 100644 --- a/src/Main.java +++ b/src/Main.java @@ -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); } } \ No newline at end of file diff --git a/src/apis/DatabaseApi.java b/src/apis/DatabaseApi.java index 5753a14..fec0eae 100644 --- a/src/apis/DatabaseApi.java +++ b/src/apis/DatabaseApi.java @@ -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) { diff --git a/src/data/dtos/QueryDTO.java b/src/data/dtos/QueryDTO.java index fea5d58..3d0747e 100644 --- a/src/data/dtos/QueryDTO.java +++ b/src/data/dtos/QueryDTO.java @@ -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) { } diff --git a/src/data/enums/CustomPreparedStatementsRead.java b/src/data/enums/CustomPreparedStatementsRead.java new file mode 100644 index 0000000..3df1874 --- /dev/null +++ b/src/data/enums/CustomPreparedStatementsRead.java @@ -0,0 +1,19 @@ +package data.enums; + +public enum CustomPreparedStatementsRead { + HighestAndLowest10Vaccination(""" + + """ + ), + + HighestInfectionsPer100K(""" + + """ + ); + + public final String statementTemplate; + + private CustomPreparedStatementsRead(String statementTemplate) { + this.statementTemplate = statementTemplate; + } +} diff --git a/src/data/enums/CustomReadPreparedStatements.java b/src/data/enums/CustomReadPreparedStatements.java deleted file mode 100644 index 36d5f0d..0000000 --- a/src/data/enums/CustomReadPreparedStatements.java +++ /dev/null @@ -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; - } -} diff --git a/src/data/models/QueryResultsModel.java b/src/data/models/QueryResultsModel.java new file mode 100644 index 0000000..fdffa81 --- /dev/null +++ b/src/data/models/QueryResultsModel.java @@ -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 resultSets) { +} diff --git a/src/services/DatabaseService.java b/src/services/DatabaseService.java index 23d1a1d..c50c177 100644 --- a/src/services/DatabaseService.java +++ b/src/services/DatabaseService.java @@ -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 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); }