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);
}
}
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();
}
}
}
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();
}
}
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;
}
}
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;
}
}
Aggregations