database #3

Merged
Ferryry merged 11 commits from database into main 2025-12-08 18:10:08 +01:00
3 changed files with 33 additions and 10 deletions
Showing only changes of commit 292d6c74c3 - Show all commits

View File

@@ -65,7 +65,7 @@ public class SimpleDatabaseManager<E> implements DatabaseManager<E> {
String query = getQuery(QueryMode.INSERT);
if (object instanceof Task task) {
try (PreparedStatement ps = connection.prepareStatement(query)) {
PreparedStatement ps = connection.prepareStatement(query);
ps.setInt(1, task.getTaskId());
ps.setString(2, task.getName());
ps.setString(3, task.getDescription());
@@ -75,7 +75,6 @@ public class SimpleDatabaseManager<E> implements DatabaseManager<E> {
ps.executeUpdate();
}
}
}
@Override
public E getObject(int taskId) throws SQLException {
@@ -113,6 +112,29 @@ public class SimpleDatabaseManager<E> implements DatabaseManager<E> {
@Override
public E updateObject(int taskId, E object) throws SQLException {
String query = getQuery(QueryMode.UPDATE);
if (object instanceof Task task) {
if (taskId != task.getTaskId()) {
throw new IllegalArgumentException("Task ID does not equals Task ID from Task!");
}
PreparedStatement ps = connection.prepareStatement(query);
ps.setString(1, task.getName());
ps.setString(2, task.getDescription());
ps.setInt(3, taskStateToInt(task.getTaskState()));
ps.setInt(4, task.getTaskId());
int updated = ps.executeUpdate();
if (updated == 0) {
return null;
}
return (E) task;
}
return null;
}

View File

@@ -1,5 +1,6 @@
CREATE TABLE Task (
taskid INT AUTO_INCREMENT PRIMARY KEY,
id INT AUTO_INCREMENT PRIMARY KEY,
taskid INT,
name VARCHAR(255) NOT NULL,
description TEXT,
workerid INT,

View File

@@ -43,6 +43,6 @@ public class DatabaseBadCasesTest {
@Test
@DisplayName("Updates an object that does not exist")
public void assertUpdateNonExistentObject() throws SQLException {
assertNull(databaseManager.updateObject(123, new Task(1, 2, "", "")));
assertNull(databaseManager.updateObject(123, new Task(123, 2, "", "")));
}
}