use of org.sql2o.Connection in project sql2o by aaberg.
the class PostgresTest method testGetKeyOnSequence.
@Test
public void testGetKeyOnSequence() {
Connection connection = null;
try {
connection = sql2o.beginTransaction();
String createSequenceSql = "create sequence testseq";
connection.createQuery(createSequenceSql).executeUpdate();
String createTableSql = "create table test_seq_table (id integer primary key, val varchar(20))";
connection.createQuery(createTableSql).executeUpdate();
String insertSql = "insert into test_seq_table(id, val) values (nextval('testseq'), 'something')";
Long key = connection.createQuery(insertSql, true).executeUpdate().getKey(Long.class);
assertThat(key, equalTo(1L));
key = connection.createQuery(insertSql, true).executeUpdate().getKey(Long.class);
assertThat(key, equalTo(2L));
} finally {
if (connection != null) {
connection.rollback();
}
}
}
use of org.sql2o.Connection in project sql2o by aaberg.
the class AutoClosableTest method testAutoClosable.
@Test
public void testAutoClosable() {
final String createSql = "create table testtable(id int identity primary key, val varchar(50))";
final String insertSql = "insert into testtable(val) values (:val)";
final String selectSql = "select * fom testtable";
final String selectCount = "select count(*) cnt from testtable";
try (Connection con = sql2o.beginTransaction()) {
// create table and insert something in a transaction
con.createQuery(createSql).executeUpdate();
con.createQuery(insertSql).addParameter("val", "foo").executeUpdate();
con.createQuery(insertSql).addParameter("val", "bar").executeUpdate();
con.commit();
}
Long cnt = (Long) sql2o.createQuery(selectCount).executeScalar();
assertThat(cnt, is(equalTo(2l)));
try (Connection con = sql2o.beginTransaction()) {
con.createQuery(insertSql).addParameter("val", "something").executeUpdate();
con.createQuery(insertSql).addParameter("val", "We want to").executeUpdate();
con.createQuery(insertSql).addParameter("val", "rollback").executeUpdate();
// don't commit, and transaction will be rolled back.
}
cnt = (Long) sql2o.createQuery(selectCount).executeScalar();
assertThat(cnt, is(equalTo(2l)));
}
use of org.sql2o.Connection in project runelite by runelite.
the class CacheUpdater method update.
public void update() throws IOException, InvalidEndpointException, InvalidPortException, InterruptedException {
int rsVersion = RuneLiteAPI.getRsVersion();
try (Connection con = sql2o.beginTransaction()) {
CacheDAO cacheDao = new CacheDAO();
CacheEntry cache = cacheDao.findMostRecent(con);
boolean created = false;
if (cache == null) {
created = true;
cache = cacheDao.createCache(con, rsVersion, Instant.now());
}
CacheStorage storage = new CacheStorage(cache, cacheDao, con);
Store store = new Store(storage);
store.load();
ExecutorService executor = Executors.newSingleThreadExecutor();
CacheClient client = new CacheClient(store, rsVersion, (Archive archive, byte[] data) -> executor.submit(new CacheUploader(minioClient, minioBucket, archive, data)));
client.connect();
HandshakeResponseType result = client.handshake().join();
if (result != HandshakeResponseType.RESPONSE_OK) {
logger.warn("Out of date!");
return;
}
List<IndexInfo> indexes = client.requestIndexes();
List<IndexEntry> entries = cacheDao.findIndexesForCache(con, cache);
if (!checkOutOfDate(indexes, entries)) {
logger.info("All up to date.");
return;
}
client.download();
CacheEntry newCache = created ? cache : cacheDao.createCache(con, rsVersion, Instant.now());
storage.setCacheEntry(newCache);
store.save();
// ensure objects are added to the store before they become
// visible in the database
executor.shutdown();
while (!executor.awaitTermination(1, TimeUnit.SECONDS)) {
logger.debug("Waiting for termination of executor...");
}
// commit database
con.commit();
}
}
use of org.sql2o.Connection in project runelite by runelite.
the class AuthFilter method handle.
public SessionEntry handle(HttpServletRequest request, HttpServletResponse response) throws IOException {
String runeliteAuth = request.getHeader(RuneLiteAPI.RUNELITE_AUTH);
if (runeliteAuth == null) {
response.sendError(401, "Access denied");
return null;
}
UUID uuid = UUID.fromString(runeliteAuth);
try (Connection con = sql2o.open()) {
SessionEntry sessionEntry = con.createQuery("select user, uuid, created from sessions where uuid = :uuid").addParameter("uuid", uuid.toString()).executeAndFetchFirst(SessionEntry.class);
if (sessionEntry == null) {
response.sendError(401, "Access denied");
return null;
}
Instant now = Instant.now();
con.createQuery("update sessions set last_used = :last_used where uuid = :uuid").addParameter("last_used", Timestamp.from(now)).addParameter("uuid", uuid.toString()).executeUpdate();
sessionEntry.setLastUsed(now);
return sessionEntry;
}
}
use of org.sql2o.Connection in project runelite by runelite.
the class SessionService method updateLast.
public void updateLast(UUID session) {
try (Connection con = sql2o.open()) {
Instant last = Instant.now();
con.createQuery("update session set last = :last where uuid = :uuid").addParameter("last", last).addParameter("uuid", session.toString()).executeUpdate();
}
}
Aggregations