Merge pull request 'Implemented database functionality' (#4) from database into main
Reviewed-on: #4 Tried with a few extra steps. Needs a ReadME, but works otherwise.
This commit is contained in:
1
.idea/modules.xml
generated
1
.idea/modules.xml
generated
@@ -2,6 +2,7 @@
|
|||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ProjectModuleManager">
|
<component name="ProjectModuleManager">
|
||||||
<modules>
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/modules/Template.iml" filepath="$PROJECT_DIR$/.idea/modules/Template.iml" />
|
||||||
<module fileurl="file://$PROJECT_DIR$/.idea/modules/Template.test.iml" filepath="$PROJECT_DIR$/.idea/modules/Template.test.iml" />
|
<module fileurl="file://$PROJECT_DIR$/.idea/modules/Template.test.iml" filepath="$PROJECT_DIR$/.idea/modules/Template.test.iml" />
|
||||||
</modules>
|
</modules>
|
||||||
</component>
|
</component>
|
||||||
|
|||||||
19
docker/compose.yml
Normal file
19
docker/compose.yml
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
version: "3.9"
|
||||||
|
|
||||||
|
services:
|
||||||
|
mysql:
|
||||||
|
image: mysql:8.0
|
||||||
|
container_name: mysql-db
|
||||||
|
restart: unless-stopped
|
||||||
|
environment:
|
||||||
|
MYSQL_ROOT_PASSWORD: root
|
||||||
|
MYSQL_DATABASE: sql7810540
|
||||||
|
MYSQL_USER: sql7810540
|
||||||
|
MYSQL_PASSWORD: mXdJCFtDZz
|
||||||
|
ports:
|
||||||
|
- "3306:3306"
|
||||||
|
volumes:
|
||||||
|
- mysql_data:/var/lib/mysql
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
mysql_data:
|
||||||
1
resources/sql/CountAllFieldsTask.sql
Normal file
1
resources/sql/CountAllFieldsTask.sql
Normal file
@@ -0,0 +1 @@
|
|||||||
|
SELECT COUNT(*) AS total FROM Task
|
||||||
1
resources/sql/CountAllFieldsWorker.sql
Normal file
1
resources/sql/CountAllFieldsWorker.sql
Normal file
@@ -0,0 +1 @@
|
|||||||
|
SELECT COUNT(*) AS total FROM Worker
|
||||||
1
resources/sql/SelectAllTask.sql
Normal file
1
resources/sql/SelectAllTask.sql
Normal file
@@ -0,0 +1 @@
|
|||||||
|
SELECT * FROM Task
|
||||||
1
resources/sql/SelectAllWorker.sql
Normal file
1
resources/sql/SelectAllWorker.sql
Normal file
@@ -0,0 +1 @@
|
|||||||
|
SELECT * FROM Worker
|
||||||
1
resources/sql/SelectTaskById.sql
Normal file
1
resources/sql/SelectTaskById.sql
Normal file
@@ -0,0 +1 @@
|
|||||||
|
SELECT * FROM Task WHERE taskid = ? LIMIT 1
|
||||||
1
resources/sql/SelectWorkerById.sql
Normal file
1
resources/sql/SelectWorkerById.sql
Normal file
@@ -0,0 +1 @@
|
|||||||
|
SELECT * FROM Worker WHERE workerid = ? LIMIT 1
|
||||||
1
resources/sql/UpdateWorkerTable.sql
Normal file
1
resources/sql/UpdateWorkerTable.sql
Normal file
@@ -0,0 +1 @@
|
|||||||
|
UPDATE Worker SET workerid = ?, name = ? WHERE workerid = ?
|
||||||
@@ -4,5 +4,5 @@ CREATE TABLE Task (
|
|||||||
name VARCHAR(255) NOT NULL,
|
name VARCHAR(255) NOT NULL,
|
||||||
description TEXT,
|
description TEXT,
|
||||||
workerid INT,
|
workerid INT,
|
||||||
taskstate INT
|
taskstate BIT
|
||||||
);
|
);
|
||||||
5
resources/sql/createWorkerTable.sql
Normal file
5
resources/sql/createWorkerTable.sql
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
CREATE TABLE Worker (
|
||||||
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
workerid INT,
|
||||||
|
name VARCHAR(255) NOT NULL
|
||||||
|
);
|
||||||
1
resources/sql/insertWorkerTable.sql
Normal file
1
resources/sql/insertWorkerTable.sql
Normal file
@@ -0,0 +1 @@
|
|||||||
|
INSERT INTO Worker (workerid, name) VALUES (?, ?)
|
||||||
@@ -1,193 +1,403 @@
|
|||||||
package hhn.temp.project.provider;
|
package hhn.temp.project.provider;
|
||||||
|
|
||||||
import hhn.temp.project.Task;
|
import hhn.temp.project.Task;
|
||||||
|
import hhn.temp.project.TaskState;
|
||||||
import hhn.temp.project.Worker;
|
import hhn.temp.project.Worker;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
import java.sql.*;
|
import java.sql.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class SimpleDatabaseManager implements DatabaseManager {
|
public class SimpleDatabaseManager implements DatabaseManager {
|
||||||
|
|
||||||
private boolean connected = false;
|
private Connection connection;
|
||||||
private Map<Integer, Task> temporaryTaskList = new HashMap();
|
|
||||||
private Map<Integer, Worker> temporaryWorkerList = new HashMap<>();
|
|
||||||
|
|
||||||
@Override
|
private final static Path INSERT_TASK = Path.of("resources/sql/InsertTaskTable.sql");
|
||||||
public void saveTask(Task task) throws SQLException {
|
private final static Path DELETE_TASK = Path.of("resources/sql/DeleteTaskTable.sql");
|
||||||
if (!connected) {
|
private final static Path SELECT_TASK = Path.of("resources/sql/SelectTaskTable.sql");
|
||||||
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
|
private final static Path UPDATE_TASK = Path.of("resources/sql/UpdateTaskTable.sql");
|
||||||
|
private final static Path COUNT_ALL_TASK = Path.of("resources/sql/CountAllFieldsTask.sql");
|
||||||
|
private final static Path SELECT_ALL_TASK = Path.of("resources/sql/SelectAllFieldsTask.sql");
|
||||||
|
private final static Path SELECT_TASK_BY_ID = Path.of("resources/sql/SelectTaskById.sql");
|
||||||
|
private final static Path INSERT_WORKER = Path.of("resources/sql/InsertWorkerTable.sql");
|
||||||
|
private final static Path DELETE_WORKER = Path.of("resources/sql/DeleteWorkerTable.sql");
|
||||||
|
private final static Path SELECT_WORKER = Path.of("resources/sql/SelectWorkerTable.sql");
|
||||||
|
private final static Path UPDATE_WORKER = Path.of("resources/sql/UpdateWorkerTable.sql");
|
||||||
|
private final static Path COUNT_ALL_WORKER = Path.of("resources/sql/CountAllFieldsWorker.sql");
|
||||||
|
private final static Path SELECT_ALL_WORKER = Path.of("resources/sql/SelectAllFieldsWorker.sql");
|
||||||
|
private final static Path SELECT_WORKER_BY_ID = Path.of("resources/sql/SelectWorkerById.sql");
|
||||||
|
|
||||||
|
public enum QueryMode {
|
||||||
|
INSERT_TASK,
|
||||||
|
SELECT_TASK,
|
||||||
|
UPDATE_TASK,
|
||||||
|
DELETE_TASK,
|
||||||
|
SELECT_ALL_TASK,
|
||||||
|
SELECT_TASK_BY_ID,
|
||||||
|
COUNT_ALL_TASK,
|
||||||
|
INSERT_WORKER,
|
||||||
|
SELECT_WORKER,
|
||||||
|
UPDATE_WORKER,
|
||||||
|
DELETE_WORKER,
|
||||||
|
COUNT_ALL_WORKER,
|
||||||
|
SELECT_ALL_WORKER,
|
||||||
|
SELECT_WORKER_BY_ID,
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!temporaryTaskList.containsKey(task.getTaskId())) {
|
public String loadFile(QueryMode queryMode) throws IOException {
|
||||||
temporaryTaskList.put(task.getTaskId(), task);
|
switch (queryMode) {
|
||||||
} else {
|
case INSERT_TASK -> {
|
||||||
throw new SQLException("[INSERTION FAILED] Task with the same Task ID already exists.");
|
return Files.readString(INSERT_TASK);
|
||||||
|
}
|
||||||
|
case SELECT_TASK -> {
|
||||||
|
return Files.readString(SELECT_TASK);
|
||||||
|
}
|
||||||
|
case DELETE_TASK -> {
|
||||||
|
return Files.readString(DELETE_TASK);
|
||||||
|
}
|
||||||
|
case UPDATE_TASK -> {
|
||||||
|
return Files.readString(UPDATE_TASK);
|
||||||
|
}
|
||||||
|
case COUNT_ALL_TASK -> {
|
||||||
|
return Files.readString(COUNT_ALL_TASK);
|
||||||
|
}
|
||||||
|
case SELECT_ALL_TASK -> {
|
||||||
|
return Files.readString(SELECT_ALL_TASK);
|
||||||
|
}
|
||||||
|
case SELECT_TASK_BY_ID -> {
|
||||||
|
return Files.readString(SELECT_TASK_BY_ID);
|
||||||
|
}
|
||||||
|
case INSERT_WORKER -> {
|
||||||
|
return Files.readString(INSERT_WORKER);
|
||||||
|
}
|
||||||
|
case SELECT_WORKER -> {
|
||||||
|
return Files.readString(SELECT_WORKER);
|
||||||
|
}
|
||||||
|
case DELETE_WORKER -> {
|
||||||
|
return Files.readString(DELETE_WORKER);
|
||||||
|
}
|
||||||
|
case UPDATE_WORKER -> {
|
||||||
|
return Files.readString(UPDATE_WORKER);
|
||||||
|
}
|
||||||
|
case COUNT_ALL_WORKER -> {
|
||||||
|
return Files.readString(COUNT_ALL_WORKER);
|
||||||
|
}
|
||||||
|
case SELECT_ALL_WORKER -> {
|
||||||
|
return Files.readString(SELECT_ALL_WORKER);
|
||||||
|
}
|
||||||
|
case SELECT_WORKER_BY_ID -> {
|
||||||
|
return Files.readString(SELECT_WORKER_BY_ID);
|
||||||
|
}
|
||||||
|
default -> {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private TaskState integerToTaskState(final int in) {
|
||||||
|
return in == 1 ? TaskState.FINISHED : TaskState.IN_PROGRESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int taskStateToInteger(final TaskState taskState) {
|
||||||
|
return taskState == TaskState.FINISHED ? 1 : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void saveTask(final Task task) throws SQLException {
|
||||||
|
String query;
|
||||||
|
try {
|
||||||
|
query = loadFile(QueryMode.INSERT_TASK);
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println(e.getStackTrace());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try (PreparedStatement preparedStatement = connection.prepareStatement(query);) {
|
||||||
|
preparedStatement.setInt(1, task.getTaskId());
|
||||||
|
preparedStatement.setString(2, task.getName());
|
||||||
|
preparedStatement.setString(3, task.getDescription());
|
||||||
|
preparedStatement.setInt(4, task.getWorkerId());
|
||||||
|
preparedStatement.setInt(5, taskStateToInteger(task.getTaskState()));
|
||||||
|
preparedStatement.execute();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void saveTasks(Collection<Task> tasks) throws SQLException {
|
public void saveTasks(final Collection<Task> tasks) throws SQLException {
|
||||||
for (Task task : tasks) {
|
for (Task task : tasks) {
|
||||||
saveTask(task);
|
saveTask(task);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void saveWorker(Worker worker) throws SQLException {
|
public void saveWorker(final Worker worker) throws SQLException {
|
||||||
if (!connected) {
|
String query;
|
||||||
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
|
try {
|
||||||
|
query = loadFile(QueryMode.INSERT_WORKER);
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println(e.getStackTrace());
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
try (PreparedStatement preparedStatement = connection.prepareStatement(query);) {
|
||||||
if (!temporaryWorkerList.containsKey(worker.getId())) {
|
preparedStatement.setInt(1, worker.getId());
|
||||||
temporaryWorkerList.put(worker.getId(), worker);
|
preparedStatement.setString(2, worker.getName());
|
||||||
} else {
|
preparedStatement.execute();
|
||||||
throw new SQLException("[INSERTION FAILED] Task with the same Task ID already exists.");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void saveWorkers(Collection<Worker> workers) throws SQLException {
|
public void saveWorkers(final Collection<Worker> workers) throws SQLException {
|
||||||
for (Worker worker : workers) {
|
for (Worker worker : workers) {
|
||||||
saveWorker(worker);
|
saveWorker(worker);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void updateTask(int taskId, Task newTaskObject) throws SQLException {
|
public void updateTask(final int taskId, final Task newTaskObject) throws SQLException {
|
||||||
if (!connected) {
|
String query;
|
||||||
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
|
try {
|
||||||
}
|
query = loadFile(QueryMode.UPDATE_TASK);
|
||||||
|
} catch (IOException e) {
|
||||||
if (temporaryTaskList.containsKey(taskId) && newTaskObject.getTaskId() == taskId) {
|
System.err.println(e.getStackTrace());
|
||||||
temporaryTaskList.remove(taskId);
|
|
||||||
temporaryTaskList.put(taskId, newTaskObject);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
try (PreparedStatement preparedStatement = connection.prepareStatement(query);) {
|
||||||
throw new SQLException("[UPDATE FAILED] Task ID not found OR Task ID does not equal New Task ID.");
|
preparedStatement.setString(1, newTaskObject.getName());
|
||||||
|
preparedStatement.setString(2, newTaskObject.getDescription());
|
||||||
|
preparedStatement.setInt(3, taskStateToInteger(newTaskObject.getTaskState()));
|
||||||
|
preparedStatement.setInt(4, taskId);
|
||||||
|
preparedStatement.execute();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void updateWorker(int workerId, Worker newWorkerObject) throws SQLException {
|
public void updateWorker(final int workerId, final Worker newWorkerObject) throws SQLException {
|
||||||
if (!connected) {
|
String query;
|
||||||
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
|
try {
|
||||||
}
|
query = loadFile(QueryMode.UPDATE_WORKER);
|
||||||
|
} catch (IOException e) {
|
||||||
if (temporaryWorkerList.containsKey(workerId) && newWorkerObject.getId() == workerId) {
|
System.err.println(e.getStackTrace());
|
||||||
temporaryWorkerList.remove(workerId);
|
|
||||||
temporaryWorkerList.put(workerId, newWorkerObject);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
try (PreparedStatement preparedStatement = connection.prepareStatement(query);) {
|
||||||
throw new SQLException("[UPDATE FAILED] Worker ID not found OR Worker ID does not equal New Worker ID.");
|
preparedStatement.setInt(1, newWorkerObject.getId());
|
||||||
|
preparedStatement.setString(2, newWorkerObject.getName());
|
||||||
|
preparedStatement.setInt(3, workerId);
|
||||||
|
preparedStatement.execute();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void deleteTask(int taskId) throws SQLException {
|
public void deleteTask(final int taskId) throws SQLException {
|
||||||
if (!connected) {
|
String query;
|
||||||
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
|
try {
|
||||||
}
|
query = loadFile(QueryMode.DELETE_TASK);
|
||||||
|
} catch (IOException e) {
|
||||||
if (temporaryTaskList.containsKey(taskId)) {
|
System.err.println(e.getStackTrace());
|
||||||
temporaryTaskList.remove(taskId);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
try (PreparedStatement preparedStatement = connection.prepareStatement(query);) {
|
||||||
throw new SQLException("[DELETION FAILED] Could not find Task ID. Nothing to delete.");
|
preparedStatement.setInt(1, taskId);
|
||||||
|
preparedStatement.execute();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void deleteWorker(int workerId) throws SQLException {
|
public void deleteWorker(final int workerId) throws SQLException {
|
||||||
if (!connected) {
|
String query;
|
||||||
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
|
try {
|
||||||
}
|
query = loadFile(QueryMode.DELETE_WORKER);
|
||||||
|
} catch (IOException e) {
|
||||||
if (temporaryWorkerList.containsKey(workerId)) {
|
System.err.println(e.getStackTrace());
|
||||||
temporaryWorkerList.remove(workerId);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
try (PreparedStatement preparedStatement = connection.prepareStatement(query);) {
|
||||||
throw new SQLException("[DELETION FAILED] Could not find Worker ID. Nothing to delete.");
|
preparedStatement.setInt(1, workerId);
|
||||||
|
preparedStatement.execute();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<Task> getTasks() throws SQLException {
|
public Collection<Task> getTasks() throws SQLException {
|
||||||
if (!connected) {
|
String query;
|
||||||
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
|
try {
|
||||||
|
query = loadFile(QueryMode.SELECT_ALL_TASK);
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println(e.getStackTrace());
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return temporaryTaskList.values();
|
Collection<Task> tasks = new ArrayList<>();
|
||||||
|
|
||||||
|
try (Statement statement = connection.createStatement();
|
||||||
|
ResultSet rs = statement.executeQuery(query);) {
|
||||||
|
|
||||||
|
while (rs.next()) {
|
||||||
|
int taskId = rs.getInt("taskid");
|
||||||
|
String name = rs.getString("name");
|
||||||
|
String description = rs.getString("description");
|
||||||
|
int workerId = rs.getInt("workerid");
|
||||||
|
TaskState taskState = integerToTaskState(rs.getInt("taskstate"));
|
||||||
|
|
||||||
|
Task task = new Task(taskId, workerId, name, description);
|
||||||
|
task.setTaskState(taskState);
|
||||||
|
|
||||||
|
tasks.add(task);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return tasks;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<Worker> getWorkers() throws SQLException {
|
public Collection<Worker> getWorkers() throws SQLException {
|
||||||
if (!connected) {
|
String query;
|
||||||
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
|
try {
|
||||||
|
query = loadFile(QueryMode.SELECT_ALL_WORKER);
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println(e.getStackTrace());
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return temporaryWorkerList.values();
|
Collection<Worker> workers = new ArrayList<>();
|
||||||
|
|
||||||
|
try (Statement statement = connection.createStatement();
|
||||||
|
ResultSet rs = statement.executeQuery(query);) {
|
||||||
|
|
||||||
|
while (rs.next()) {
|
||||||
|
int workerId = rs.getInt("workerid");
|
||||||
|
String name = rs.getString("name");
|
||||||
|
|
||||||
|
Worker worker = new Worker(name, workerId);
|
||||||
|
|
||||||
|
workers.add(worker);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return workers;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Task getTaskByTaskId(int taskId) throws SQLException {
|
public Task getTaskByTaskId(final int taskId) throws SQLException {
|
||||||
if (!connected) {
|
String query;
|
||||||
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
|
try {
|
||||||
|
query = loadFile(QueryMode.SELECT_TASK_BY_ID);
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println(e.getStackTrace());
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (temporaryTaskList.containsKey(taskId)) {
|
try (PreparedStatement statement = connection.prepareStatement(query);) {
|
||||||
return temporaryTaskList.get(taskId);
|
|
||||||
|
statement.setInt(1, taskId);
|
||||||
|
|
||||||
|
ResultSet rs = statement.executeQuery();
|
||||||
|
|
||||||
|
while (rs.next()) {
|
||||||
|
int taskId1 = rs.getInt("taskid");
|
||||||
|
String name = rs.getString("name");
|
||||||
|
String description = rs.getString("description");
|
||||||
|
int workerId = rs.getInt("workerid");
|
||||||
|
TaskState taskState = integerToTaskState(rs.getInt("taskstate"));
|
||||||
|
|
||||||
|
Task task = new Task(taskId1, workerId, name, description);
|
||||||
|
task.setTaskState(taskState);
|
||||||
|
|
||||||
|
return task;
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new SQLException("[SELECTION FAILED] Could not find Task ID. Nothing to get.");
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Worker getWorkerByWorkerId(int workerId) throws SQLException {
|
public Worker getWorkerByWorkerId(final int workerId) throws SQLException {
|
||||||
if (!connected) {
|
String query;
|
||||||
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
|
try {
|
||||||
|
query = loadFile(QueryMode.SELECT_WORKER_BY_ID);
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println(e.getStackTrace());
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (temporaryWorkerList.containsKey(workerId)) {
|
try (PreparedStatement statement = connection.prepareStatement(query);) {
|
||||||
return temporaryWorkerList.get(workerId);
|
|
||||||
|
statement.setInt(1, workerId);
|
||||||
|
|
||||||
|
ResultSet rs = statement.executeQuery();
|
||||||
|
|
||||||
|
while (rs.next()) {
|
||||||
|
int workerId1 = rs.getInt("workerid");
|
||||||
|
String name = rs.getString("name");
|
||||||
|
|
||||||
|
Worker worker = new Worker(name, workerId1);
|
||||||
|
|
||||||
|
return worker;
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new SQLException("[SELECTION FAILED] Could not find Worker ID. Nothing to get.");
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getTotalNumberOfTasks() throws SQLException {
|
public int getTotalNumberOfTasks() throws SQLException {
|
||||||
if (!connected) {
|
String query = "";
|
||||||
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
|
try {
|
||||||
|
query = loadFile(QueryMode.COUNT_ALL_TASK);
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println(e.getStackTrace());
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
try (Statement statement = connection.createStatement();
|
||||||
|
ResultSet rs = statement.executeQuery(query);) {
|
||||||
|
|
||||||
|
if (rs.next()) {
|
||||||
|
return rs.getInt("total");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return temporaryTaskList.size();
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getTotalNumberOfWorkers() throws SQLException {
|
public int getTotalNumberOfWorkers() throws SQLException {
|
||||||
if (!connected) {
|
String query = "";
|
||||||
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
|
try {
|
||||||
|
query = loadFile(QueryMode.COUNT_ALL_WORKER);
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println(e.getStackTrace());
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
try (Statement statement = connection.createStatement();
|
||||||
|
ResultSet rs = statement.executeQuery(query);) {
|
||||||
|
|
||||||
|
if (rs.next()) {
|
||||||
|
return rs.getInt("total");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return temporaryWorkerList.size();
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void clearDatabase() throws SQLException {
|
public void clearDatabase() throws SQLException {
|
||||||
if (!connected) {
|
try (Statement statement = connection.createStatement()) {
|
||||||
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
|
statement.execute("TRUNCATE TABLE Task");
|
||||||
|
statement.execute("TRUNCATE TABLE Worker");
|
||||||
}
|
}
|
||||||
|
|
||||||
temporaryTaskList.clear();
|
|
||||||
temporaryWorkerList.clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void connect() throws SQLException {
|
public void connect() throws SQLException {
|
||||||
connected = true;
|
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/sql7810540?user=sql7810540&password=mXdJCFtDZz");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close() throws SQLException {
|
public void close() throws SQLException {
|
||||||
connected = false;
|
connection.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
package hhn.temp.project;
|
package hhn.temp.project;
|
||||||
|
|
||||||
import hhn.temp.project.provider.DatabaseManager;
|
|
||||||
import hhn.temp.project.provider.SimpleDatabaseManager;
|
import hhn.temp.project.provider.SimpleDatabaseManager;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.DisplayName;
|
import org.junit.jupiter.api.DisplayName;
|
||||||
@@ -9,14 +8,14 @@ import org.junit.jupiter.api.Test;
|
|||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class DatabaseGoodCasesTest {
|
public class DatabaseGoodCasesTest {
|
||||||
|
|
||||||
private DatabaseManager databaseManager;
|
private SimpleDatabaseManager databaseManager;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
public void setup() throws SQLException {
|
public void setup() throws SQLException {
|
||||||
@@ -83,4 +82,9 @@ public class DatabaseGoodCasesTest {
|
|||||||
assertEquals(0, databaseManager.getTotalNumberOfTasks());
|
assertEquals(0, databaseManager.getTotalNumberOfTasks());
|
||||||
assertEquals(0, databaseManager.getTotalNumberOfWorkers());
|
assertEquals(0, databaseManager.getTotalNumberOfWorkers());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRandom() throws IOException, URISyntaxException {
|
||||||
|
System.out.println(databaseManager.loadFile(SimpleDatabaseManager.QueryMode.INSERT_TASK));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user