database #3
@@ -65,15 +65,14 @@ public class SimpleDatabaseManager<E> implements DatabaseManager<E> {
|
||||
String query = getQuery(QueryMode.INSERT);
|
||||
|
||||
if (object instanceof Task task) {
|
||||
try (PreparedStatement ps = connection.prepareStatement(query)) {
|
||||
ps.setInt(1, task.getTaskId());
|
||||
ps.setString(2, task.getName());
|
||||
ps.setString(3, task.getDescription());
|
||||
ps.setInt(4, task.getWorkerId());
|
||||
ps.setInt(5, taskStateToInt(task.getTaskState()));
|
||||
PreparedStatement ps = connection.prepareStatement(query);
|
||||
ps.setInt(1, task.getTaskId());
|
||||
ps.setString(2, task.getName());
|
||||
ps.setString(3, task.getDescription());
|
||||
ps.setInt(4, task.getWorkerId());
|
||||
ps.setInt(5, taskStateToInt(task.getTaskState()));
|
||||
|
||||
ps.executeUpdate();
|
||||
}
|
||||
ps.executeUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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, "", "")));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user