From 3344ba67ccbc38cc6a5d6f025d8d649c3191f7df Mon Sep 17 00:00:00 2001 From: Riley Schneider <88947587+Ferryry@users.noreply.github.com> Date: Sun, 14 Dec 2025 17:53:04 +0100 Subject: [PATCH 1/2] Updated files and created compose.yml for internal database usage --- docker/compose.yml | 19 ++++++++++ .../sql/createTaskTable.sql | 0 resources/sql/createWorkerTable.sql | 5 +++ .../sql/deleteTaskTable.sql | 0 .../sql/insertTaskTable.sql | 0 resources/sql/insertWorkerTable.sql | 1 + .../sql/selectTaskTable.sql | 0 .../sql/updateTaskTable.sql | 0 .../provider/SimpleDatabaseManager.java | 35 +++++++++++++++++++ .../temp/project/DatabaseGoodCasesTest.java | 8 ++++- 10 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 docker/compose.yml rename {src/main/java/hhn/temp/project/provider => resources}/sql/createTaskTable.sql (100%) create mode 100644 resources/sql/createWorkerTable.sql rename {src/main/java/hhn/temp/project/provider => resources}/sql/deleteTaskTable.sql (100%) rename {src/main/java/hhn/temp/project/provider => resources}/sql/insertTaskTable.sql (100%) create mode 100644 resources/sql/insertWorkerTable.sql rename {src/main/java/hhn/temp/project/provider => resources}/sql/selectTaskTable.sql (100%) rename {src/main/java/hhn/temp/project/provider => resources}/sql/updateTaskTable.sql (100%) diff --git a/docker/compose.yml b/docker/compose.yml new file mode 100644 index 00000000..60f5f3a9 --- /dev/null +++ b/docker/compose.yml @@ -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: \ No newline at end of file diff --git a/src/main/java/hhn/temp/project/provider/sql/createTaskTable.sql b/resources/sql/createTaskTable.sql similarity index 100% rename from src/main/java/hhn/temp/project/provider/sql/createTaskTable.sql rename to resources/sql/createTaskTable.sql diff --git a/resources/sql/createWorkerTable.sql b/resources/sql/createWorkerTable.sql new file mode 100644 index 00000000..159d6dde --- /dev/null +++ b/resources/sql/createWorkerTable.sql @@ -0,0 +1,5 @@ +CREATE TABLE Worker ( + id INT AUTO_INCREMENT PRIMARY KEY, + workerid INT, + name VARCHAR(255) NOT NULL +); \ No newline at end of file diff --git a/src/main/java/hhn/temp/project/provider/sql/deleteTaskTable.sql b/resources/sql/deleteTaskTable.sql similarity index 100% rename from src/main/java/hhn/temp/project/provider/sql/deleteTaskTable.sql rename to resources/sql/deleteTaskTable.sql diff --git a/src/main/java/hhn/temp/project/provider/sql/insertTaskTable.sql b/resources/sql/insertTaskTable.sql similarity index 100% rename from src/main/java/hhn/temp/project/provider/sql/insertTaskTable.sql rename to resources/sql/insertTaskTable.sql diff --git a/resources/sql/insertWorkerTable.sql b/resources/sql/insertWorkerTable.sql new file mode 100644 index 00000000..d99a2d5e --- /dev/null +++ b/resources/sql/insertWorkerTable.sql @@ -0,0 +1 @@ +INSERT INTO Worker (workerid, name) VALUES (?, ?) \ No newline at end of file diff --git a/src/main/java/hhn/temp/project/provider/sql/selectTaskTable.sql b/resources/sql/selectTaskTable.sql similarity index 100% rename from src/main/java/hhn/temp/project/provider/sql/selectTaskTable.sql rename to resources/sql/selectTaskTable.sql diff --git a/src/main/java/hhn/temp/project/provider/sql/updateTaskTable.sql b/resources/sql/updateTaskTable.sql similarity index 100% rename from src/main/java/hhn/temp/project/provider/sql/updateTaskTable.sql rename to resources/sql/updateTaskTable.sql diff --git a/src/main/java/hhn/temp/project/provider/SimpleDatabaseManager.java b/src/main/java/hhn/temp/project/provider/SimpleDatabaseManager.java index e53ff173..ca958401 100644 --- a/src/main/java/hhn/temp/project/provider/SimpleDatabaseManager.java +++ b/src/main/java/hhn/temp/project/provider/SimpleDatabaseManager.java @@ -3,15 +3,50 @@ package hhn.temp.project.provider; import hhn.temp.project.Task; import hhn.temp.project.Worker; +import java.io.File; +import java.io.IOException; +import java.net.URISyntaxException; +import java.nio.file.Files; +import java.nio.file.Path; import java.sql.*; import java.util.*; public class SimpleDatabaseManager implements DatabaseManager { + private final static Path INSERT_TASK = Path.of("./src/main/resources/sql/InsertTaskTable.sql"); + private final static Path DELETE_TASK = Path.of("./src/main/resources/sql/DeleteTaskTable.sql"); + private final static Path SELECT_TASK = Path.of("./src/main/resources/sql/SelectTaskTable.sql"); + private final static Path UPDATE_TASK = Path.of("./src/main/resources/sql/UpdateTaskTable.sql"); + private boolean connected = false; private Map temporaryTaskList = new HashMap(); private Map temporaryWorkerList = new HashMap<>(); + public enum QueryMode { + INSERT, + SELECT, + UPDATE, + DELETE + } + + public String loadFile(QueryMode queryMode) throws IOException, URISyntaxException { + switch (queryMode) { + case INSERT -> { + return Files.readString(INSERT_TASK); + } + case SELECT -> { + + } + case DELETE -> { + + } + case UPDATE -> { + + } + } + return ""; + } + @Override public void saveTask(Task task) throws SQLException { if (!connected) { diff --git a/test/hhn/temp/project/DatabaseGoodCasesTest.java b/test/hhn/temp/project/DatabaseGoodCasesTest.java index b72b00be..e34bd28f 100644 --- a/test/hhn/temp/project/DatabaseGoodCasesTest.java +++ b/test/hhn/temp/project/DatabaseGoodCasesTest.java @@ -9,6 +9,7 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; import java.io.IOException; +import java.net.URISyntaxException; import java.sql.SQLException; import java.util.ArrayList; import java.util.Collection; @@ -16,7 +17,7 @@ import java.util.List; public class DatabaseGoodCasesTest { - private DatabaseManager databaseManager; + private SimpleDatabaseManager databaseManager; @BeforeEach public void setup() throws SQLException { @@ -83,4 +84,9 @@ public class DatabaseGoodCasesTest { assertEquals(0, databaseManager.getTotalNumberOfTasks()); assertEquals(0, databaseManager.getTotalNumberOfWorkers()); } + + @Test + public void testRandom() throws IOException, URISyntaxException { + System.out.println(databaseManager.loadFile(SimpleDatabaseManager.QueryMode.INSERT)); + } } \ No newline at end of file -- 2.49.1 From 35e22bba99720f99165c947e38497bc078aea812 Mon Sep 17 00:00:00 2001 From: Riley Schneider <88947587+Ferryry@users.noreply.github.com> Date: Thu, 18 Dec 2025 10:01:58 +0100 Subject: [PATCH 2/2] Added MySQL functionality --- .idea/modules.xml | 1 + docker/compose.yml | 4 +- resources/sql/CountAllFieldsTask.sql | 1 + resources/sql/CountAllFieldsWorker.sql | 1 + resources/sql/SelectAllTask.sql | 1 + resources/sql/SelectAllWorker.sql | 1 + resources/sql/SelectTaskById.sql | 1 + resources/sql/SelectWorkerById.sql | 1 + resources/sql/UpdateWorkerTable.sql | 1 + resources/sql/createTaskTable.sql | 2 +- .../provider/SimpleDatabaseManager.java | 395 +++++++++++++----- .../temp/project/DatabaseGoodCasesTest.java | 4 +- 12 files changed, 297 insertions(+), 116 deletions(-) create mode 100644 resources/sql/CountAllFieldsTask.sql create mode 100644 resources/sql/CountAllFieldsWorker.sql create mode 100644 resources/sql/SelectAllTask.sql create mode 100644 resources/sql/SelectAllWorker.sql create mode 100644 resources/sql/SelectTaskById.sql create mode 100644 resources/sql/SelectWorkerById.sql create mode 100644 resources/sql/UpdateWorkerTable.sql diff --git a/.idea/modules.xml b/.idea/modules.xml index ffe7c8fc..41600e97 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,6 +2,7 @@ + diff --git a/docker/compose.yml b/docker/compose.yml index 60f5f3a9..9f70a5e7 100644 --- a/docker/compose.yml +++ b/docker/compose.yml @@ -7,8 +7,8 @@ services: restart: unless-stopped environment: MYSQL_ROOT_PASSWORD: root - MYSQL_DATABASE: sql7810540& - MYSQL_USER: sql7810540& + MYSQL_DATABASE: sql7810540 + MYSQL_USER: sql7810540 MYSQL_PASSWORD: mXdJCFtDZz ports: - "3306:3306" diff --git a/resources/sql/CountAllFieldsTask.sql b/resources/sql/CountAllFieldsTask.sql new file mode 100644 index 00000000..8cbf36b5 --- /dev/null +++ b/resources/sql/CountAllFieldsTask.sql @@ -0,0 +1 @@ +SELECT COUNT(*) AS total FROM Task \ No newline at end of file diff --git a/resources/sql/CountAllFieldsWorker.sql b/resources/sql/CountAllFieldsWorker.sql new file mode 100644 index 00000000..8534b834 --- /dev/null +++ b/resources/sql/CountAllFieldsWorker.sql @@ -0,0 +1 @@ +SELECT COUNT(*) AS total FROM Worker \ No newline at end of file diff --git a/resources/sql/SelectAllTask.sql b/resources/sql/SelectAllTask.sql new file mode 100644 index 00000000..2de1e62b --- /dev/null +++ b/resources/sql/SelectAllTask.sql @@ -0,0 +1 @@ +SELECT * FROM Task \ No newline at end of file diff --git a/resources/sql/SelectAllWorker.sql b/resources/sql/SelectAllWorker.sql new file mode 100644 index 00000000..06c9037b --- /dev/null +++ b/resources/sql/SelectAllWorker.sql @@ -0,0 +1 @@ +SELECT * FROM Worker \ No newline at end of file diff --git a/resources/sql/SelectTaskById.sql b/resources/sql/SelectTaskById.sql new file mode 100644 index 00000000..ec4fcfb0 --- /dev/null +++ b/resources/sql/SelectTaskById.sql @@ -0,0 +1 @@ +SELECT * FROM Task WHERE taskid = ? LIMIT 1 \ No newline at end of file diff --git a/resources/sql/SelectWorkerById.sql b/resources/sql/SelectWorkerById.sql new file mode 100644 index 00000000..11b4b8fe --- /dev/null +++ b/resources/sql/SelectWorkerById.sql @@ -0,0 +1 @@ +SELECT * FROM Worker WHERE workerid = ? LIMIT 1 \ No newline at end of file diff --git a/resources/sql/UpdateWorkerTable.sql b/resources/sql/UpdateWorkerTable.sql new file mode 100644 index 00000000..c69999a9 --- /dev/null +++ b/resources/sql/UpdateWorkerTable.sql @@ -0,0 +1 @@ +UPDATE Worker SET workerid = ?, name = ? WHERE workerid = ? \ No newline at end of file diff --git a/resources/sql/createTaskTable.sql b/resources/sql/createTaskTable.sql index bc1ab8f5..0098750e 100644 --- a/resources/sql/createTaskTable.sql +++ b/resources/sql/createTaskTable.sql @@ -4,5 +4,5 @@ CREATE TABLE Task ( name VARCHAR(255) NOT NULL, description TEXT, workerid INT, - taskstate INT + taskstate BIT ); \ No newline at end of file diff --git a/src/main/java/hhn/temp/project/provider/SimpleDatabaseManager.java b/src/main/java/hhn/temp/project/provider/SimpleDatabaseManager.java index ca958401..4b364c49 100644 --- a/src/main/java/hhn/temp/project/provider/SimpleDatabaseManager.java +++ b/src/main/java/hhn/temp/project/provider/SimpleDatabaseManager.java @@ -1,11 +1,10 @@ package hhn.temp.project.provider; import hhn.temp.project.Task; +import hhn.temp.project.TaskState; import hhn.temp.project.Worker; -import java.io.File; import java.io.IOException; -import java.net.URISyntaxException; import java.nio.file.Files; import java.nio.file.Path; import java.sql.*; @@ -13,216 +12,392 @@ import java.util.*; public class SimpleDatabaseManager implements DatabaseManager { - private final static Path INSERT_TASK = Path.of("./src/main/resources/sql/InsertTaskTable.sql"); - private final static Path DELETE_TASK = Path.of("./src/main/resources/sql/DeleteTaskTable.sql"); - private final static Path SELECT_TASK = Path.of("./src/main/resources/sql/SelectTaskTable.sql"); - private final static Path UPDATE_TASK = Path.of("./src/main/resources/sql/UpdateTaskTable.sql"); + private Connection connection; - private boolean connected = false; - private Map temporaryTaskList = new HashMap(); - private Map temporaryWorkerList = new HashMap<>(); + private final static Path INSERT_TASK = Path.of("resources/sql/InsertTaskTable.sql"); + private final static Path DELETE_TASK = Path.of("resources/sql/DeleteTaskTable.sql"); + private final static Path SELECT_TASK = Path.of("resources/sql/SelectTaskTable.sql"); + 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, - SELECT, - UPDATE, - DELETE + 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, } - public String loadFile(QueryMode queryMode) throws IOException, URISyntaxException { + public String loadFile(QueryMode queryMode) throws IOException { switch (queryMode) { - case INSERT -> { + case INSERT_TASK -> { return Files.readString(INSERT_TASK); } - case SELECT -> { - + case SELECT_TASK -> { + return Files.readString(SELECT_TASK); } - case DELETE -> { - + case DELETE_TASK -> { + return Files.readString(DELETE_TASK); } - case UPDATE -> { - + 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 ""; } } - 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(Task task) throws SQLException { - if (!connected) { - throw new SQLException("[CONNECTION FAILED] Instance is not connected to database."); + 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; } - - if (!temporaryTaskList.containsKey(task.getTaskId())) { - temporaryTaskList.put(task.getTaskId(), task); - } else { - throw new SQLException("[INSERTION FAILED] Task with the same Task ID already exists."); + 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 - public void saveTasks(Collection tasks) throws SQLException { + public void saveTasks(final Collection tasks) throws SQLException { for (Task task : tasks) { saveTask(task); } } @Override - public void saveWorker(Worker worker) throws SQLException { - if (!connected) { - throw new SQLException("[CONNECTION FAILED] Instance is not connected to database."); + public void saveWorker(final Worker worker) throws SQLException { + String query; + try { + query = loadFile(QueryMode.INSERT_WORKER); + } catch (IOException e) { + System.err.println(e.getStackTrace()); + return; } - - if (!temporaryWorkerList.containsKey(worker.getId())) { - temporaryWorkerList.put(worker.getId(), worker); - } else { - throw new SQLException("[INSERTION FAILED] Task with the same Task ID already exists."); + try (PreparedStatement preparedStatement = connection.prepareStatement(query);) { + preparedStatement.setInt(1, worker.getId()); + preparedStatement.setString(2, worker.getName()); + preparedStatement.execute(); } } @Override - public void saveWorkers(Collection workers) throws SQLException { + public void saveWorkers(final Collection workers) throws SQLException { for (Worker worker : workers) { saveWorker(worker); } } @Override - public void updateTask(int taskId, Task newTaskObject) throws SQLException { - if (!connected) { - throw new SQLException("[CONNECTION FAILED] Instance is not connected to database."); - } - - if (temporaryTaskList.containsKey(taskId) && newTaskObject.getTaskId() == taskId) { - temporaryTaskList.remove(taskId); - temporaryTaskList.put(taskId, newTaskObject); + public void updateTask(final int taskId, final Task newTaskObject) throws SQLException { + String query; + try { + query = loadFile(QueryMode.UPDATE_TASK); + } catch (IOException e) { + System.err.println(e.getStackTrace()); return; } - - throw new SQLException("[UPDATE FAILED] Task ID not found OR Task ID does not equal New Task ID."); + try (PreparedStatement preparedStatement = connection.prepareStatement(query);) { + preparedStatement.setString(1, newTaskObject.getName()); + preparedStatement.setString(2, newTaskObject.getDescription()); + preparedStatement.setInt(3, taskStateToInteger(newTaskObject.getTaskState())); + preparedStatement.setInt(4, taskId); + preparedStatement.execute(); + } } @Override - public void updateWorker(int workerId, Worker newWorkerObject) throws SQLException { - if (!connected) { - throw new SQLException("[CONNECTION FAILED] Instance is not connected to database."); - } - - if (temporaryWorkerList.containsKey(workerId) && newWorkerObject.getId() == workerId) { - temporaryWorkerList.remove(workerId); - temporaryWorkerList.put(workerId, newWorkerObject); + public void updateWorker(final int workerId, final Worker newWorkerObject) throws SQLException { + String query; + try { + query = loadFile(QueryMode.UPDATE_WORKER); + } catch (IOException e) { + System.err.println(e.getStackTrace()); return; } - - throw new SQLException("[UPDATE FAILED] Worker ID not found OR Worker ID does not equal New Worker ID."); + try (PreparedStatement preparedStatement = connection.prepareStatement(query);) { + preparedStatement.setInt(1, newWorkerObject.getId()); + preparedStatement.setString(2, newWorkerObject.getName()); + preparedStatement.setInt(3, workerId); + preparedStatement.execute(); + } } @Override - public void deleteTask(int taskId) throws SQLException { - if (!connected) { - throw new SQLException("[CONNECTION FAILED] Instance is not connected to database."); - } - - if (temporaryTaskList.containsKey(taskId)) { - temporaryTaskList.remove(taskId); + public void deleteTask(final int taskId) throws SQLException { + String query; + try { + query = loadFile(QueryMode.DELETE_TASK); + } catch (IOException e) { + System.err.println(e.getStackTrace()); return; } - - throw new SQLException("[DELETION FAILED] Could not find Task ID. Nothing to delete."); + try (PreparedStatement preparedStatement = connection.prepareStatement(query);) { + preparedStatement.setInt(1, taskId); + preparedStatement.execute(); + } } @Override - public void deleteWorker(int workerId) throws SQLException { - if (!connected) { - throw new SQLException("[CONNECTION FAILED] Instance is not connected to database."); - } - - if (temporaryWorkerList.containsKey(workerId)) { - temporaryWorkerList.remove(workerId); + public void deleteWorker(final int workerId) throws SQLException { + String query; + try { + query = loadFile(QueryMode.DELETE_WORKER); + } catch (IOException e) { + System.err.println(e.getStackTrace()); return; } - - throw new SQLException("[DELETION FAILED] Could not find Worker ID. Nothing to delete."); + try (PreparedStatement preparedStatement = connection.prepareStatement(query);) { + preparedStatement.setInt(1, workerId); + preparedStatement.execute(); + } } @Override public Collection getTasks() throws SQLException { - if (!connected) { - throw new SQLException("[CONNECTION FAILED] Instance is not connected to database."); + String query; + try { + query = loadFile(QueryMode.SELECT_ALL_TASK); + } catch (IOException e) { + System.err.println(e.getStackTrace()); + return null; } - return temporaryTaskList.values(); + Collection 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 public Collection getWorkers() throws SQLException { - if (!connected) { - throw new SQLException("[CONNECTION FAILED] Instance is not connected to database."); + String query; + try { + query = loadFile(QueryMode.SELECT_ALL_WORKER); + } catch (IOException e) { + System.err.println(e.getStackTrace()); + return null; } - return temporaryWorkerList.values(); + Collection 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 - public Task getTaskByTaskId(int taskId) throws SQLException { - if (!connected) { - throw new SQLException("[CONNECTION FAILED] Instance is not connected to database."); + public Task getTaskByTaskId(final int taskId) throws SQLException { + String query; + try { + query = loadFile(QueryMode.SELECT_TASK_BY_ID); + } catch (IOException e) { + System.err.println(e.getStackTrace()); + return null; } - if (temporaryTaskList.containsKey(taskId)) { - return temporaryTaskList.get(taskId); + try (PreparedStatement statement = connection.prepareStatement(query);) { + + 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 - public Worker getWorkerByWorkerId(int workerId) throws SQLException { - if (!connected) { - throw new SQLException("[CONNECTION FAILED] Instance is not connected to database."); + public Worker getWorkerByWorkerId(final int workerId) throws SQLException { + String query; + try { + query = loadFile(QueryMode.SELECT_WORKER_BY_ID); + } catch (IOException e) { + System.err.println(e.getStackTrace()); + return null; } - if (temporaryWorkerList.containsKey(workerId)) { - return temporaryWorkerList.get(workerId); + try (PreparedStatement statement = connection.prepareStatement(query);) { + + 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 public int getTotalNumberOfTasks() throws SQLException { - if (!connected) { - throw new SQLException("[CONNECTION FAILED] Instance is not connected to database."); + String query = ""; + 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 public int getTotalNumberOfWorkers() throws SQLException { - if (!connected) { - throw new SQLException("[CONNECTION FAILED] Instance is not connected to database."); + String query = ""; + 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 { - if (!connected) { - throw new SQLException("[CONNECTION FAILED] Instance is not connected to database."); + try (Statement statement = connection.createStatement()) { + statement.execute("TRUNCATE TABLE Task"); + statement.execute("TRUNCATE TABLE Worker"); } - - temporaryTaskList.clear(); - temporaryWorkerList.clear(); } @Override public void connect() throws SQLException { - connected = true; + connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/sql7810540?user=sql7810540&password=mXdJCFtDZz"); } @Override public void close() throws SQLException { - connected = false; + connection.close(); } -} +} \ No newline at end of file diff --git a/test/hhn/temp/project/DatabaseGoodCasesTest.java b/test/hhn/temp/project/DatabaseGoodCasesTest.java index e34bd28f..88a214fb 100644 --- a/test/hhn/temp/project/DatabaseGoodCasesTest.java +++ b/test/hhn/temp/project/DatabaseGoodCasesTest.java @@ -1,6 +1,5 @@ package hhn.temp.project; -import hhn.temp.project.provider.DatabaseManager; import hhn.temp.project.provider.SimpleDatabaseManager; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; @@ -11,7 +10,6 @@ import static org.junit.jupiter.api.Assertions.*; import java.io.IOException; import java.net.URISyntaxException; import java.sql.SQLException; -import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -87,6 +85,6 @@ public class DatabaseGoodCasesTest { @Test public void testRandom() throws IOException, URISyntaxException { - System.out.println(databaseManager.loadFile(SimpleDatabaseManager.QueryMode.INSERT)); + System.out.println(databaseManager.loadFile(SimpleDatabaseManager.QueryMode.INSERT_TASK)); } } \ No newline at end of file -- 2.49.1