package io.trygvis.queue; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Component; import java.sql.ResultSet; import java.sql.SQLException; import static org.springframework.dao.support.DataAccessUtils.singleResult; @Component public class QueueDao { @Autowired private 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)); } } }