database #3
@@ -65,7 +65,7 @@ public class SimpleDatabaseManager<E> implements DatabaseManager<E> {
|
|||||||
String query = getQuery(QueryMode.INSERT);
|
String query = getQuery(QueryMode.INSERT);
|
||||||
|
|
||||||
if (object instanceof Task task) {
|
if (object instanceof Task task) {
|
||||||
try (PreparedStatement ps = connection.prepareStatement(query)) {
|
PreparedStatement ps = connection.prepareStatement(query);
|
||||||
ps.setInt(1, task.getTaskId());
|
ps.setInt(1, task.getTaskId());
|
||||||
ps.setString(2, task.getName());
|
ps.setString(2, task.getName());
|
||||||
ps.setString(3, task.getDescription());
|
ps.setString(3, task.getDescription());
|
||||||
@@ -75,7 +75,6 @@ public class SimpleDatabaseManager<E> implements DatabaseManager<E> {
|
|||||||
ps.executeUpdate();
|
ps.executeUpdate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public E getObject(int taskId) throws SQLException {
|
public E getObject(int taskId) throws SQLException {
|
||||||
@@ -113,6 +112,29 @@ public class SimpleDatabaseManager<E> implements DatabaseManager<E> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public E updateObject(int taskId, E object) throws SQLException {
|
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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
CREATE TABLE Task (
|
CREATE TABLE Task (
|
||||||
taskid INT AUTO_INCREMENT PRIMARY KEY,
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
taskid INT,
|
||||||
name VARCHAR(255) NOT NULL,
|
name VARCHAR(255) NOT NULL,
|
||||||
description TEXT,
|
description TEXT,
|
||||||
workerid INT,
|
workerid INT,
|
||||||
|
|||||||
@@ -43,6 +43,6 @@ public class DatabaseBadCasesTest {
|
|||||||
@Test
|
@Test
|
||||||
@DisplayName("Updates an object that does not exist")
|
@DisplayName("Updates an object that does not exist")
|
||||||
public void assertUpdateNonExistentObject() throws SQLException {
|
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