Search in sources :

Example 11 with Connection

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();
        }
    }
}
Also used : Connection(org.sql2o.Connection) Test(org.junit.Test)

Example 12 with Connection

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)));
}
Also used : Connection(org.sql2o.Connection) Test(org.junit.Test)

Example 13 with Connection

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();
    }
}
Also used : CacheClient(net.runelite.cache.client.CacheClient) Archive(net.runelite.cache.fs.Archive) HandshakeResponseType(net.runelite.protocol.api.login.HandshakeResponseType) Connection(org.sql2o.Connection) Store(net.runelite.cache.fs.Store) IndexEntry(net.runelite.cache.updater.beans.IndexEntry) IndexInfo(net.runelite.cache.client.IndexInfo) CacheEntry(net.runelite.cache.updater.beans.CacheEntry) ExecutorService(java.util.concurrent.ExecutorService)

Example 14 with Connection

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;
    }
}
Also used : Instant(java.time.Instant) Connection(org.sql2o.Connection) UUID(java.util.UUID) SessionEntry(net.runelite.http.service.account.beans.SessionEntry)

Example 15 with Connection

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();
    }
}
Also used : Instant(java.time.Instant) Connection(org.sql2o.Connection)

Aggregations

Connection (org.sql2o.Connection)31 Test (org.junit.Test)16 Query (org.sql2o.Query)7 UUID (java.util.UUID)5 Table (org.sql2o.data.Table)5 Instant (java.time.Instant)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 Sql2oException (org.sql2o.Sql2oException)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 SessionEntry (net.runelite.http.service.account.beans.SessionEntry)2 PlayerEntity (net.runelite.http.service.xp.beans.PlayerEntity)2 Sql2o (org.sql2o.Sql2o)2 Row (org.sql2o.data.Row)2 ServiceBuilder (com.github.scribejava.core.builder.ServiceBuilder)1 OAuth2AccessToken (com.github.scribejava.core.model.OAuth2AccessToken)1 OAuthRequest (com.github.scribejava.core.model.OAuthRequest)1 Response (com.github.scribejava.core.model.Response)1 OAuth20Service (com.github.scribejava.core.oauth.OAuth20Service)1 SQLException (java.sql.SQLException)1