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.DatabaseService;
import java.sql.ResultSet;
public class Main {
public static void main(String[] args) {
final App app = new App();
@ -18,6 +21,7 @@ class App {
final AppConfigService appConfigService;
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;

View file

@ -1,4 +1,4 @@
package constants;
package data.constants;
public class ConstValues {
//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;
import constants.ConstFormatters;
import constants.ConstValues;
import data.constants.ConstFormatters;
import data.constants.ConstValues;
import java.text.ParseException;
@ -38,7 +38,7 @@ public class AppConfigService {
System.out.printf("Unable to parse date. Should be in format '%s'.\nTry again:%n", ConstFormatters.APPLICATION_DATE_FORMATTER.toPattern());
}
}
scanner.close();
}
}

View file

@ -1,8 +1,12 @@
package services;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import apis.DatabaseApi;
import data.dtos.QueryDTO;
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.
@ -12,13 +16,31 @@ public class DatabaseService {
private final String user = "postgres";
private final String password = "postgres";
private final DatabaseApi databaseApi;
private final Connection connection;
public DatabaseService() {
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);
}
}
/**
* Connect to the PostgreSQL database
*
* @return a Connection object
@ -34,4 +56,4 @@ public class DatabaseService {
return connection;
}
}
}