use of org.sql2o.data.Table in project sql2o by aaberg.
the class Sql2oTest method testFetchTable.
@Test
public void testFetchTable() {
sql2o.createQuery("create table tabletest(id integer identity primary key, value varchar(20), value2 decimal(5,1))").executeUpdate();
sql2o.createQuery("insert into tabletest(value,value2) values (:value, :value2)").addParameter("value", "something").addParameter("value2", new BigDecimal("3.4")).addToBatch().addParameter("value", "bla").addParameter("value2", new BigDecimal("5.5")).addToBatch().executeBatch();
Table table = sql2o.createQuery("select * from tabletest order by id").executeAndFetchTable();
assertEquals(3, table.columns().size());
assertEquals("ID", table.columns().get(0).getName());
assertEquals("VALUE", table.columns().get(1).getName());
assertEquals("VALUE2", table.columns().get(2).getName());
assertEquals(2, table.rows().size());
Row row0 = table.rows().get(0);
Row row1 = table.rows().get(1);
assertTrue(0 <= row0.getInteger("ID"));
assertEquals("something", row0.getString(1));
assertEquals(new BigDecimal("3.4"), row0.getBigDecimal("VALUE2"));
assertTrue(1 <= row1.getInteger(0));
assertEquals("bla", row1.getString("VALUE"));
assertEquals(5.5D, row1.getDouble(2), 0.00001);
}
use of org.sql2o.data.Table in project sql2o by aaberg.
the class H2Tests method testUUID.
/**
* Ref issue #73
*/
@Test
public void testUUID() {
try (Connection connection = new Sql2o(ds).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.data.Table in project sql2o by aaberg.
the class IssuesTest method testForNpeInRowGet.
/**
* Tests for issue #4 https://github.com/aaberg/sql2o/issues/4
*
* NPE when typing wrong column name in row.get(...)
* Also, column name should not be case sensitive, if sql2o not is in casesensitive property is false.
*/
@Test
public void testForNpeInRowGet() {
sql2o.createQuery("create table issue4table(id integer identity primary key, val varchar(20))").executeUpdate();
sql2o.createQuery("insert into issue4table (val) values (:val)").addParameter("val", "something").addToBatch().addParameter("val", "something else").addToBatch().addParameter("val", "hello").addToBatch().executeBatch();
Table table = sql2o.createQuery("select * from issue4table").executeAndFetchTable();
Row row0 = table.rows().get(0);
String row0Val = row0.getString("vAl");
assertEquals("something", row0Val);
Row row1 = table.rows().get(1);
boolean failed = false;
try {
// Should fail with an sql2o exception
String row1Value = row1.getString("ahsHashah");
} catch (Sql2oException ex) {
failed = true;
assertTrue(ex.getMessage().startsWith("Column with name 'ahsHashah' does not exist"));
}
assertTrue("assert that exception occurred", failed);
}
use of org.sql2o.data.Table 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();
}
}
Aggregations