package io.trygvis.queue; import org.springframework.beans.factory.annotation.*; import org.springframework.jdbc.core.*; import org.springframework.stereotype.*; import java.sql.*; import static org.springframework.dao.support.DataAccessUtils.*; public class QueueDao { private final JdbcTemplate jdbcTemplate; public QueueDao(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public Queue findByName(String name) { return singleResult(jdbcTemplate.query("SELECT name, interval FROM queue WHERE name=?", new QueueRowMapper(), name)); } public void insert(Queue q) { jdbcTemplate.update("INSERT INTO queue(name, interval) VALUES(?, ?)", q.name, q.interval); } public void update(Queue q) { jdbcTemplate.update("UPDATE queue SET interval=? WHERE name=?", q.interval, q.name); } private class QueueRowMapper implements RowMapper { public Queue mapRow(ResultSet rs, int rowNum) throws SQLException { return new Queue(rs.getString(1), rs.getLong(2)); } } }