querying backbone

This commit is contained in:
Mehul Ahal 2023-01-10 23:30:10 +01:00 committed by LahaLuhem
parent af8c80f216
commit cf3fa3f566
8 changed files with 95 additions and 11 deletions

View file

@ -1,6 +1,9 @@
import data.enums.CustomReadPreparedStatements;
import services.AppConfigService; import services.AppConfigService;
import services.DatabaseService; import services.DatabaseService;
import java.sql.ResultSet;
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();
@ -18,6 +21,7 @@ class App {
final AppConfigService appConfigService; final AppConfigService appConfigService;
public void run() { public void run() {
appConfigService.promptUserArguments(); //appConfigService.promptUserArguments();
final ResultSet[] resultSetsQuery1 = databaseService.executeReadReportsEndpoint(CustomReadPreparedStatements.HighestAndLowest10Vaccination);
} }
} }

17
src/apis/DatabaseApi.java Normal file
View file

@ -0,0 +1,17 @@
package apis;
import data.dtos.QueryDTO;
import java.sql.*;
public class DatabaseApi {
public PreparedStatement performQuery(QueryDTO queryDTO, Connection connection) {
try {
PreparedStatement statement = connection.prepareStatement(queryDTO.queryTemplate().statementData);
statement.execute();
return statement;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}

View file

@ -1,4 +1,4 @@
package constants; package data.constants;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;

View file

@ -1,4 +1,4 @@
package constants; package data.constants;
public class ConstValues { public class ConstValues {
//I //I

View file

@ -0,0 +1,7 @@
package data.dtos;
import data.enums.CustomReadPreparedStatements;
public record QueryDTO(CustomReadPreparedStatements queryTemplate, Object[] placeholderReplacements) {
}

View file

@ -0,0 +1,34 @@
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;
}
}

View file

@ -1,7 +1,7 @@
package services; package services;
import constants.ConstFormatters; import data.constants.ConstFormatters;
import constants.ConstValues; import data.constants.ConstValues;
import java.text.ParseException; import java.text.ParseException;

View file

@ -1,8 +1,12 @@
package services; package services;
import java.sql.Connection; import apis.DatabaseApi;
import java.sql.DriverManager; import data.dtos.QueryDTO;
import java.sql.SQLException; import data.enums.CustomReadPreparedStatements;
import javax.xml.transform.Result;
import java.sql.*;
import java.util.ArrayList;
/** /**
* Stores the active connection between the app and the local SQL Server. * Stores the active connection between the app and the local SQL Server.
@ -12,10 +16,28 @@ public class DatabaseService {
private final String user = "postgres"; private final String user = "postgres";
private final String password = "postgres"; private final String password = "postgres";
private final DatabaseApi databaseApi;
private final Connection connection; private final Connection connection;
public DatabaseService() { public DatabaseService() {
connection = connect(); connection = connect();
databaseApi = new DatabaseApi();
}
public ResultSet[] executeReadReportsEndpoint(CustomReadPreparedStatements customPreparedStatement) {
final QueryDTO queryDTO = new QueryDTO(customPreparedStatement, new Object[]{});
final PreparedStatement resultStatement = databaseApi.performQuery(queryDTO, connection);
try {
final ArrayList<ResultSet> resultSets = new ArrayList<>();
do {
resultSets.add(resultStatement.getResultSet());
}
while (resultStatement.getMoreResults());
return resultSets.toArray(new ResultSet[0]);
} catch (SQLException e) {
throw new RuntimeException(e);
}
} }
/** /**