querying backbone
This commit is contained in:
parent
af8c80f216
commit
cf3fa3f566
8 changed files with 95 additions and 11 deletions
|
@ -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
17
src/apis/DatabaseApi.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package constants;
|
package data.constants;
|
||||||
|
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package constants;
|
package data.constants;
|
||||||
|
|
||||||
public class ConstValues {
|
public class ConstValues {
|
||||||
//I
|
//I
|
7
src/data/dtos/QueryDTO.java
Normal file
7
src/data/dtos/QueryDTO.java
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
package data.dtos;
|
||||||
|
|
||||||
|
import data.enums.CustomReadPreparedStatements;
|
||||||
|
|
||||||
|
public record QueryDTO(CustomReadPreparedStatements queryTemplate, Object[] placeholderReplacements) {
|
||||||
|
|
||||||
|
}
|
34
src/data/enums/CustomReadPreparedStatements.java
Normal file
34
src/data/enums/CustomReadPreparedStatements.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
|
||||||
|
@ -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());
|
System.out.printf("Unable to parse date. Should be in format '%s'.\nTry again:%n", ConstFormatters.APPLICATION_DATE_FORMATTER.toPattern());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
scanner.close();
|
scanner.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,13 +16,31 @@ 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
* Connect to the PostgreSQL database
|
* Connect to the PostgreSQL database
|
||||||
*
|
*
|
||||||
* @return a Connection object
|
* @return a Connection object
|
||||||
|
@ -34,4 +56,4 @@ public class DatabaseService {
|
||||||
|
|
||||||
return connection;
|
return connection;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue