Created
April 28, 2016 16:36
-
-
Save johnou/d58f18a5687f606d54c74ad19a9cc2af to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.springframework.dao.EmptyResultDataAccessException; | |
import org.springframework.jdbc.core.support.JdbcDaoSupport; | |
import org.springframework.stereotype.Repository; | |
/** | |
* @author Johno Crawford (johno@sulake.com) | |
*/ | |
@Repository | |
public class JdbcOrbitStateDao extends JdbcDaoSupport implements OrbitStateDao { | |
private static final String SELECT_STATE = "SELECT state_value FROM actor_state WHERE state_key = ?"; | |
private static final String DELETE_STATE = "DELETE FROM actor_state WHERE state_key = ?"; | |
private static final String UPDATE_STATE = "UPDATE actor_state SET state_value = ? WHERE state_key = ?"; | |
private static final String INSERT_STATE = "INSERT IGNORE INTO actor_state (state_key, state_value) VALUES (?, ?)"; | |
@Override | |
public String get(String key) { | |
try { | |
return getJdbcTemplate().queryForObject(SELECT_STATE, String.class, key); | |
} catch (EmptyResultDataAccessException ignore) { | |
return null; | |
} | |
} | |
@Override | |
public void delete(String key) { | |
getJdbcTemplate().update(DELETE_STATE, key); | |
} | |
@Override | |
public void update(String key, String value) { | |
int result = getJdbcTemplate().update(UPDATE_STATE, value, key); | |
if (result < 1) { | |
getJdbcTemplate().update(INSERT_STATE, key, value); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment