Heavily refactored DatabaseManager.java and SimpleDatabaseManager.java. Fully refactored DatabaseGoodCasesTest.java and DatabaseBadCasesTest.java.
This commit is contained in:
@@ -4,8 +4,26 @@ import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* A database interface with basic database connectivity
|
||||
* @author Riley Schneider
|
||||
*/
|
||||
public interface Database {
|
||||
public void connect() throws SQLException;
|
||||
public void close() throws SQLException;
|
||||
public void clearDatabase() throws SQLException;
|
||||
/**
|
||||
* Creates a connection to the database
|
||||
* @throws SQLException Thrown if database error occurs
|
||||
*/
|
||||
void connect() throws SQLException;
|
||||
|
||||
/**
|
||||
* Closes a connection to the database
|
||||
* @throws SQLException Thrown if database error occurs
|
||||
*/
|
||||
void close() throws SQLException;
|
||||
|
||||
/**
|
||||
* Clears the entire tables in the database and reset primary key counter to each table
|
||||
* @throws SQLException Thrown if database error occurs
|
||||
*/
|
||||
void clearDatabase() throws SQLException;
|
||||
}
|
||||
|
||||
@@ -1,16 +1,115 @@
|
||||
package hhn.temp.project.provider;
|
||||
|
||||
import jdk.jshell.spi.ExecutionControl;
|
||||
import hhn.temp.project.Task;
|
||||
import hhn.temp.project.Worker;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Collection;
|
||||
|
||||
public interface DatabaseManager<E> extends Database {
|
||||
public void saveObjects(Collection<E> objects) throws SQLException;
|
||||
public Collection<E> getObjects() throws SQLException;
|
||||
public void saveObject(E object) throws SQLException;
|
||||
public E getObject(int taskId) throws SQLException;
|
||||
public void deleteObject(int taskId) throws SQLException;
|
||||
public E updateObject(int taskId, E object) throws SQLException;
|
||||
/**
|
||||
* Simple Database Manager to handle simple command such as insertion, deletion, updating of Task and Worker objects.
|
||||
* @author Riley Schneider
|
||||
*/
|
||||
public interface DatabaseManager extends Database {
|
||||
/**
|
||||
* Saves a Task object into the database
|
||||
* @param task Task to be saved into the database
|
||||
* @throws SQLException Thrown if database error occurs
|
||||
*/
|
||||
void saveTask(Task task) throws SQLException;
|
||||
|
||||
/**
|
||||
* Saves a collection of Task objects into the database
|
||||
* @param tasks Tasks to be saved into the database
|
||||
* @throws SQLException Thrown if database error occurs
|
||||
*/
|
||||
void saveTasks(Collection<Task> tasks) throws SQLException;
|
||||
|
||||
/**
|
||||
* Saves a Worker object into the database
|
||||
* @param worker Worker to be saved into the database
|
||||
* @throws SQLException Thrown if database error occurs
|
||||
*/
|
||||
void saveWorker(Worker worker) throws SQLException;
|
||||
|
||||
/**
|
||||
* Saves a collection Worker objects into the database
|
||||
* @param workers Workers to be saved into the database
|
||||
* @throws SQLException Thrown if database error occurs
|
||||
*/
|
||||
void saveWorkers(Collection<Worker> workers) throws SQLException;
|
||||
|
||||
/**
|
||||
* Updates a Task object that already exists in the database
|
||||
* @param taskId The ID of the given task
|
||||
* @param newTaskObject The new Task object that will overwrite the existent task on database
|
||||
* @throws SQLException Thrown if database error occurs
|
||||
*/
|
||||
void updateTask(int taskId, Task newTaskObject) throws SQLException;
|
||||
|
||||
/**
|
||||
* Updates a Worker object that already exists in the database
|
||||
* @param workerId The ID of the given worker
|
||||
* @param newWorkerObject The new Worker object that will overwrite the existent task on database
|
||||
* @throws SQLException Thrown if database error occurs
|
||||
*/
|
||||
void updateWorker(int workerId, Worker newWorkerObject) throws SQLException;
|
||||
|
||||
/**
|
||||
* Deletes a task from the database
|
||||
* @param taskId The ID of the given task that needs to be deleted
|
||||
* @throws SQLException Thrown if database error occurs
|
||||
*/
|
||||
void deleteTask(int taskId) throws SQLException;
|
||||
|
||||
/**
|
||||
* Deletes a worker from the database
|
||||
* @param workerId The ID of the given worker that needs to be deleted
|
||||
* @throws SQLException Thrown if database error occurs
|
||||
*/
|
||||
void deleteWorker(int workerId) throws SQLException;
|
||||
|
||||
/**
|
||||
* Gets a collection of all tasks available in the database
|
||||
* @return A collection of tasks (empty if no task available)
|
||||
* @throws SQLException Thrown if database error occurs
|
||||
*/
|
||||
Collection<Task> getTasks() throws SQLException;
|
||||
|
||||
/**
|
||||
* Gets a collection of all workers available in the database
|
||||
* @return A collection of workers (empty if no task available)
|
||||
* @throws SQLException Thrown if database error occurs
|
||||
*/
|
||||
Collection<Worker> getWorkers() throws SQLException;
|
||||
|
||||
/**
|
||||
* Gets a task given by its ID from the database
|
||||
* @param taskId The task ID
|
||||
* @return A task from the database (NULL if no task found)
|
||||
* @throws SQLException Thrown if database error occurs
|
||||
*/
|
||||
Task getTaskByTaskId(int taskId) throws SQLException;
|
||||
|
||||
/**
|
||||
* Gets a worker given by its ID from the database
|
||||
* @param workerId The worker ID
|
||||
* @return A worker from the database (NULL if no worker found)
|
||||
* @throws SQLException Thrown if database error occurs
|
||||
*/
|
||||
Worker getWorkerByWorkerId(int workerId) throws SQLException;
|
||||
|
||||
/**
|
||||
* Gets the total number of available tasks in the database.
|
||||
* @return The total number of tasks (0 = zero entries; -1 = error)
|
||||
* @throws SQLException Thrown if database error occurs
|
||||
*/
|
||||
int getTotalNumberOfTasks() throws SQLException;
|
||||
|
||||
/**
|
||||
* Gets the total number of available workers in the database.
|
||||
* @return The total number of workers (0 = zero entries; -1 = error)
|
||||
* @throws SQLException Thrown if database error occurs
|
||||
*/
|
||||
int getTotalNumberOfWorkers() throws SQLException;
|
||||
}
|
||||
@@ -1,152 +1,94 @@
|
||||
package hhn.temp.project.provider;
|
||||
|
||||
import hhn.temp.project.Task;
|
||||
import hhn.temp.project.TaskState;
|
||||
import hhn.temp.project.Worker;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
public class SimpleDatabaseManager<E> implements DatabaseManager<E> {
|
||||
public class SimpleDatabaseManager implements DatabaseManager {
|
||||
|
||||
private Connection connection;
|
||||
|
||||
private enum QueryMode {
|
||||
SELECT, INSERT, DELETE, UPDATE
|
||||
@Override
|
||||
public void saveTask(Task task) throws SQLException {
|
||||
throw new SQLException();
|
||||
}
|
||||
|
||||
private int taskStateToInt(TaskState taskState) {
|
||||
return (taskState == TaskState.FINISHED) ? 1 : 0;
|
||||
@Override
|
||||
public void saveTasks(Collection<Task> tasks) throws SQLException {
|
||||
throw new SQLException();
|
||||
}
|
||||
|
||||
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;
|
||||
@Override
|
||||
public void saveWorker(Worker worker) throws SQLException {
|
||||
throw new SQLException();
|
||||
}
|
||||
|
||||
private String getQuery(QueryMode query) {
|
||||
String queryString = null;
|
||||
@Override
|
||||
public void saveWorkers(Collection<Worker> workers) throws SQLException {
|
||||
throw new SQLException();
|
||||
}
|
||||
|
||||
switch (query) {
|
||||
case INSERT -> queryString = "INSERT INTO Task (taskid, 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 = ?";
|
||||
}
|
||||
@Override
|
||||
public void updateTask(int taskId, Task newTaskObject) throws SQLException {
|
||||
throw new SQLException();
|
||||
}
|
||||
|
||||
return queryString;
|
||||
@Override
|
||||
public void updateWorker(int workerId, Worker newWorkerObject) throws SQLException {
|
||||
throw new SQLException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteTask(int taskId) throws SQLException {
|
||||
throw new SQLException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteWorker(int workerId) throws SQLException {
|
||||
throw new SQLException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Task> getTasks() throws SQLException {
|
||||
throw new SQLException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Worker> getWorkers() throws SQLException {
|
||||
throw new SQLException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Task getTaskByTaskId(int taskId) throws SQLException {
|
||||
return new Task(0, 0, null, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Worker getWorkerByWorkerId(int workerId) throws SQLException {
|
||||
return new Worker(null, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTotalNumberOfTasks() throws SQLException {
|
||||
return -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTotalNumberOfWorkers() throws SQLException {
|
||||
return -1;
|
||||
}
|
||||
|
||||
public void clearDatabase() throws SQLException {
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
statement.executeUpdate("TRUNCATE TABLE Task;");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveObjects(Collection<E> objects) throws SQLException {
|
||||
for (E obj : objects) {
|
||||
saveObject(obj);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Collection<E> getObjects() throws SQLException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveObject(E object) throws SQLException {
|
||||
String query = getQuery(QueryMode.INSERT);
|
||||
|
||||
if (object instanceof Task task) {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public E getObject(int taskId) throws SQLException {
|
||||
String query = getQuery(QueryMode.SELECT);
|
||||
Task task = null;
|
||||
|
||||
PreparedStatement ps = connection.prepareStatement(query);
|
||||
ps.setInt(1, taskId);
|
||||
|
||||
ResultSet resultSet = ps.executeQuery();
|
||||
|
||||
if (resultSet.next()) {
|
||||
|
||||
task = new Task(
|
||||
resultSet.getInt("taskid"),
|
||||
resultSet.getInt("workerid"),
|
||||
resultSet.getString("name"),
|
||||
resultSet.getString("description")
|
||||
);
|
||||
task.setTaskState(intToTaskState(resultSet.getInt("taskstate")));
|
||||
}
|
||||
|
||||
return (E) task;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteObject(int taskId) throws SQLException {
|
||||
String query = getQuery(QueryMode.DELETE);
|
||||
|
||||
try (PreparedStatement ps = connection.prepareStatement(query)) {
|
||||
ps.setInt(1, taskId);
|
||||
ps.executeUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
@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;
|
||||
throw new SQLException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connect() throws SQLException {
|
||||
connection = DriverManager.getConnection("jdbc:mysql://sql7.freesqldatabase.com/sql7810540?user=sql7810540&password=mXdJCFtDZz");
|
||||
throw new SQLException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws SQLException {
|
||||
if (connection != null) {
|
||||
connection.close();
|
||||
}
|
||||
throw new SQLException();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user