From 7465fdb9aa847d29dacc56adbe473f1c1ceb298e Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Tue, 4 Jun 2013 20:54:56 +0200 Subject: o Creating a QueueService on top of the DAOs. --- src/main/java/io/trygvis/queue/TaskDao.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'src/main/java/io/trygvis/queue/TaskDao.java') diff --git a/src/main/java/io/trygvis/queue/TaskDao.java b/src/main/java/io/trygvis/queue/TaskDao.java index 5459933..3aa2ac2 100644 --- a/src/main/java/io/trygvis/queue/TaskDao.java +++ b/src/main/java/io/trygvis/queue/TaskDao.java @@ -11,7 +11,8 @@ import java.util.Collections; import java.util.Date; import java.util.List; -import static java.util.Arrays.asList; +import static io.trygvis.queue.Task.argumentsToString; +import static io.trygvis.queue.Task.stringToArguments; public class TaskDao { @@ -23,11 +24,11 @@ public class TaskDao { this.connection = connection; } - public long insert(String queue, Date scheduled, String arguments) throws SQLException { + public long insert(String queue, Date scheduled, List arguments) throws SQLException { return insert(null, queue, scheduled, arguments); } - public long insert(Long parent, String queue, Date scheduled, String arguments) throws SQLException { + public long insert(Long parent, String queue, Date scheduled, List arguments) throws SQLException { String sql = "INSERT INTO task(id, parent, run_count, queue, scheduled, arguments) " + "VALUES(nextval('task_seq'), ?, 0, ?, ?, ?)"; try (PreparedStatement stmt = connection.prepareStatement(sql)) { @@ -39,7 +40,7 @@ public class TaskDao { } stmt.setString(i++, queue); stmt.setTimestamp(i++, new Timestamp(scheduled.getTime())); - stmt.setString(i, arguments); + stmt.setString(i, argumentsToString(arguments)); stmt.executeUpdate(); } try (PreparedStatement stmt = connection.prepareStatement("SELECT currval('task_seq')")) { @@ -51,6 +52,7 @@ public class TaskDao { public Task findById(long id) throws SQLException { try (PreparedStatement stmt = connection.prepareStatement("SELECT " + fields + " FROM task WHERE id=?")) { + stmt.setLong(1, id); ResultSet rs = stmt.executeQuery(); return rs.next() ? mapRow(rs) : null; } @@ -76,7 +78,7 @@ public class TaskDao { setTimestamp(stmt, i++, task.lastRun); stmt.setInt(i++, task.runCount); setTimestamp(stmt, i++, task.completed); - stmt.setLong(i, task.id); + stmt.setLong(i, task.id()); stmt.executeUpdate(); } } @@ -99,6 +101,6 @@ public class TaskDao { rs.getTimestamp(5), rs.getInt(6), rs.getTimestamp(7), - arguments != null ? asList(arguments.split(" ")) : Collections.emptyList()); + arguments != null ? stringToArguments(arguments) : Collections.emptyList()); } } -- cgit v1.2.3