Search in sources :

Example 21 with Connection

use of org.sql2o.Connection in project sql2o by aaberg.

the class IssuesTest method testIssue134ThrowOnMappingErrorProperty.

/**
 * Testing for github issue #134.
 * Add option to ignore mapping errors
 */
@Test
public void testIssue134ThrowOnMappingErrorProperty() {
    String sql = "select 1 id, 'foo' val1, 'bar' val2 from (values(0))";
    class Pojo {

        public int id;

        public String val1;
    }
    try (Connection connection = sql2o.open()) {
        try {
            Pojo pojo = connection.createQuery(sql).executeAndFetchFirst(Pojo.class);
            fail("Expeced an exception to be thrown");
        } catch (Sql2oException e) {
            assertEquals("Could not map VAL2 to any property.", e.getMessage());
        }
        Pojo pojo = connection.createQuery(sql).throwOnMappingFailure(false).executeAndFetchFirst(Pojo.class);
        assertEquals(1, pojo.id);
        assertEquals("foo", pojo.val1);
    }
}
Also used : Sql2oException(org.sql2o.Sql2oException) Issue1Pojo(org.sql2o.issues.pojos.Issue1Pojo) Connection(org.sql2o.Connection) Test(org.junit.Test)

Example 22 with Connection

use of org.sql2o.Connection in project sql2o by aaberg.

the class PostgresTest method testUUID.

@Test
public void testUUID() {
    Connection connection = null;
    try {
        connection = sql2o.beginTransaction();
        String createSql = "create table uuidtable(id serial primary key, data uuid)";
        connection.createQuery(createSql).executeUpdate();
        UUID uuid = UUID.randomUUID();
        String insertSql = "insert into uuidtable(data) values (:data)";
        connection.createQuery(insertSql).addParameter("data", uuid).executeUpdate();
        String selectSql = "select data from uuidtable";
        UUID fetchedUuid = connection.createQuery(selectSql).executeScalar(UUID.class);
        assertThat(fetchedUuid, is(equalTo(uuid)));
    } finally {
        if (connection != null) {
            connection.rollback();
        }
    }
}
Also used : Connection(org.sql2o.Connection) UUID(java.util.UUID) Test(org.junit.Test)

Example 23 with Connection

use of org.sql2o.Connection in project sql2o by aaberg.

the class UUIDTest method testUUID.

@Test
public void testUUID() throws Exception {
    try (Connection connection = sql2o.beginTransaction()) {
        connection.createQuery("create table uuidtest(id uuid primary key, val uuid null)").executeUpdate();
        UUID uuid1 = UUID.randomUUID();
        UUID uuid2 = UUID.randomUUID();
        UUID uuid3 = UUID.randomUUID();
        UUID uuid4 = null;
        Query insQuery = connection.createQuery("insert into uuidtest(id, val) values (:id, :val)");
        insQuery.addParameter("id", uuid1).addParameter("val", uuid2).executeUpdate();
        insQuery.addParameter("id", uuid3).addParameter("val", uuid4).executeUpdate();
        Table table = connection.createQuery("select * from uuidtest").executeAndFetchTable();
        assertThat((UUID) table.rows().get(0).getObject("id"), is(equalTo(uuid1)));
        assertThat((UUID) table.rows().get(0).getObject("val"), is(equalTo(uuid2)));
        assertThat((UUID) table.rows().get(1).getObject("id"), is(equalTo(uuid3)));
        assertThat(table.rows().get(1).getObject("val"), is(nullValue()));
        connection.rollback();
    }
}
Also used : Table(org.sql2o.data.Table) Query(org.sql2o.Query) Connection(org.sql2o.Connection) UUID(java.util.UUID) Test(org.junit.Test)

Example 24 with Connection

use of org.sql2o.Connection in project runelite by runelite.

the class ItemService method fetchItem.

public ItemEntry fetchItem(int itemId) {
    try {
        RSItem rsItem = fetchRSItem(itemId);
        byte[] icon = null, iconLarge = null;
        try {
            icon = fetchImage(rsItem.getIcon());
        } catch (IOException ex) {
            log.warn("error fetching image", ex);
        }
        try {
            iconLarge = fetchImage(rsItem.getIcon_large());
        } catch (IOException ex) {
            log.warn("error fetching image", ex);
        }
        try (Connection con = sql2o.open()) {
            con.createQuery("insert into items (id, name, description, type, icon, icon_large) values (:id," + " :name, :description, :type, :icon, :icon_large) ON DUPLICATE KEY UPDATE name = :name," + " description = :description, type = :type, icon = :icon, icon_large = :icon_large").addParameter("id", rsItem.getId()).addParameter("name", rsItem.getName()).addParameter("description", rsItem.getDescription()).addParameter("type", rsItem.getType()).addParameter("icon", icon).addParameter("icon_large", iconLarge).executeUpdate();
        }
        ItemEntry item = new ItemEntry();
        item.setId(itemId);
        item.setName(rsItem.getName());
        item.setDescription(rsItem.getDescription());
        item.setType(ItemType.of(rsItem.getType()));
        item.setIcon(icon);
        item.setIcon_large(iconLarge);
        return item;
    } catch (IOException ex) {
        log.warn("unable to fetch item {}", itemId, ex);
        return null;
    }
}
Also used : Connection(org.sql2o.Connection) IOException(java.io.IOException)

Example 25 with Connection

use of org.sql2o.Connection in project runelite by runelite.

the class ItemService method fetchPrice.

public List<PriceEntry> fetchPrice(int itemId) {
    try (Connection con = sql2o.beginTransaction()) {
        RSPrices rsprice = fetchRSPrices(itemId);
        List<PriceEntry> entries = new ArrayList<>();
        Instant now = Instant.now();
        Query query = con.createQuery("insert into prices (item, price, time, fetched_time) values (:item, :price, :time, :fetched_time) " + "ON DUPLICATE KEY UPDATE price = VALUES(price), fetched_time = VALUES(fetched_time)");
        for (Map.Entry<Long, Integer> entry : rsprice.getDaily().entrySet()) {
            // ms since epoch
            long ts = entry.getKey();
            // gp
            int price = entry.getValue();
            Instant time = Instant.ofEpochMilli(ts);
            PriceEntry priceEntry = new PriceEntry();
            priceEntry.setItem(itemId);
            priceEntry.setPrice(price);
            priceEntry.setTime(time);
            priceEntry.setFetched_time(now);
            entries.add(priceEntry);
            query.addParameter("item", itemId).addParameter("price", price).addParameter("time", time).addParameter("fetched_time", now).addToBatch();
        }
        query.executeBatch();
        con.commit();
        return entries;
    } catch (IOException ex) {
        log.warn("unable to fetch price for item {}", itemId, ex);
        return null;
    }
}
Also used : Query(org.sql2o.Query) Instant(java.time.Instant) Connection(org.sql2o.Connection) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Map(java.util.Map)

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