database #3
@@ -27,7 +27,7 @@ public class AssignmentManager {
|
|||||||
if (!workerMap.containsKey(workerId) || name == null || description == null) {
|
if (!workerMap.containsKey(workerId) || name == null || description == null) {
|
||||||
throw new IllegalArgumentException("WorkerId must exist and name or description can't be null");
|
throw new IllegalArgumentException("WorkerId must exist and name or description can't be null");
|
||||||
}
|
}
|
||||||
Task task = new Task(++taskIdCounter, workerId, name, description, this);
|
Task task = new Task(++taskIdCounter, workerId, name, description);
|
||||||
taskMap.put(taskIdCounter, task);
|
taskMap.put(taskIdCounter, task);
|
||||||
return taskIdCounter;
|
return taskIdCounter;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,11 +2,13 @@ package hhn.temp.project.provider;
|
|||||||
|
|
||||||
import jdk.jshell.spi.ExecutionControl;
|
import jdk.jshell.spi.ExecutionControl;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.sql.SQLException;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
public interface DatabaseManager<E> extends Database {
|
public interface DatabaseManager<E> extends Database {
|
||||||
public void saveObjects(Collection<E> objects);
|
public void saveObjects(Collection<E> objects) throws ExecutionControl.NotImplementedException, IOException, SQLException;
|
||||||
public Collection<E> getObjects() throws ExecutionControl.NotImplementedException;
|
public Collection<E> getObjects() throws ExecutionControl.NotImplementedException;
|
||||||
public void saveObject(E object) throws ExecutionControl.NotImplementedException;
|
public void saveObject(E object) throws ExecutionControl.NotImplementedException, IOException, SQLException;
|
||||||
public E getObject(int id) throws ExecutionControl.NotImplementedException;
|
public E getObject(int id) throws ExecutionControl.NotImplementedException;
|
||||||
}
|
}
|
||||||
@@ -1,34 +1,75 @@
|
|||||||
package hhn.temp.project.provider;
|
package hhn.temp.project.provider;
|
||||||
|
|
||||||
|
import hhn.temp.project.Task;
|
||||||
|
import hhn.temp.project.TaskState;
|
||||||
import jdk.jshell.spi.ExecutionControl;
|
import jdk.jshell.spi.ExecutionControl;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.DriverManager;
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class SimpleDatabaseManager<E> implements DatabaseManager<E> {
|
public class SimpleDatabaseManager<E> implements DatabaseManager<E> {
|
||||||
|
|
||||||
private Connection connection;
|
private Connection connection;
|
||||||
|
|
||||||
@Override
|
private enum QueryMode {
|
||||||
public void saveObjects(Collection<E> objects) {
|
SELECT, INSERT, DELETE, UPDATE
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private int taskStateToInt(TaskState taskState) {
|
||||||
|
return (taskState == TaskState.FINISHED) ? 1 : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private TaskState intToTaskState(int i) {
|
||||||
|
if (i < 0 || i > 1) {
|
||||||
|
throw new IllegalArgumentException("Integer may only bet 0 or 1");
|
||||||
|
}
|
||||||
|
|
||||||
|
return (i == 1) ? TaskState.FINISHED : TaskState.IN_PROGRESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getQuery(QueryMode query) throws IOException {
|
||||||
|
String queryString = null;
|
||||||
|
|
||||||
|
switch (query) {
|
||||||
|
case INSERT -> queryString = "INSERT INTO Task (name, description, workerid, taskstate) VALUES (?, ?, ?, ?)";
|
||||||
|
case UPDATE -> queryString = "UPDATE Task SET name = ?, description = ?, taskstate = ? WHERE taskid = ?";
|
||||||
|
case DELETE -> queryString = "DELETE FROM Task WHERE taskid = ?";
|
||||||
|
case SELECT -> queryString = "SELECT * FROM Task WHERE workerid = ?";
|
||||||
|
}
|
||||||
|
|
||||||
|
return queryString;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void saveObjects(Collection<E> objects) throws IOException, SQLException {
|
||||||
|
for (E obj : objects) {
|
||||||
|
saveObject(obj);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<E> getObjects() throws ExecutionControl.NotImplementedException {
|
public Collection<E> getObjects() throws ExecutionControl.NotImplementedException {
|
||||||
throw new ExecutionControl.NotImplementedException("Not Implemented!");
|
throw new ExecutionControl.NotImplementedException("Not Implemented!");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void saveObject(E object) throws ExecutionControl.NotImplementedException {
|
public void saveObject(E object) throws IOException, SQLException {
|
||||||
throw new ExecutionControl.NotImplementedException("Not Implemented!");
|
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()));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
CREATE TABLE Task (
|
||||||
|
taskid INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
name VARCHAR(255) NOT NULL,
|
||||||
|
description TEXT,
|
||||||
|
workerid INT,
|
||||||
|
taskstate INT
|
||||||
|
);
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
DELETE FROM Task WHERE taskid = ?
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
INSERT INTO Task (name, description, workerid, taskstate) VALUES (?, ?, ?, ?)
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
SELECT * FROM Task WHERE workerid = ?
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
UPDATE Task SET name = ?, description = ?, taskstate = ? WHERE taskid = ?
|
||||||
@@ -27,6 +27,7 @@ public class DatabaseGoodCasesTest {
|
|||||||
|
|
||||||
databaseManager.connect();
|
databaseManager.connect();
|
||||||
Task task = databaseManager.getObject(1);
|
Task task = databaseManager.getObject(1);
|
||||||
|
databaseManager.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -37,11 +38,13 @@ public class DatabaseGoodCasesTest {
|
|||||||
|
|
||||||
databaseManager.connect();
|
databaseManager.connect();
|
||||||
databaseManager.saveObject(testTask);
|
databaseManager.saveObject(testTask);
|
||||||
|
databaseManager.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Assert connecting to database")
|
@DisplayName("Assert connecting to database")
|
||||||
public void assertConnectToDatabase() throws SQLException, IOException {
|
public void assertConnectToDatabase() throws SQLException, IOException {
|
||||||
databaseManager.connect();
|
databaseManager.connect();
|
||||||
|
databaseManager.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user