@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); }
@Test public void testRowGetObjectWithConverters() { String sql = "select 1 col1, '23' col2 from (values(0))"; Table t = sql2o.createQuery(sql).executeAndFetchTable(); Row r = t.rows().get(0); String col1AsString = r.getObject("col1", String.class); Integer col1AsInteger = r.getObject("col1", Integer.class); Long col1AsLong = r.getObject("col1", Long.class); assertThat(col1AsString, is(equalTo("1"))); assertThat(col1AsInteger, is(equalTo(1))); assertThat(col1AsLong, is(equalTo(1L))); String col2AsString = r.getObject("col2", String.class); Integer col2AsInteger = r.getObject("col2", Integer.class); Long col2AsLong = r.getObject("col2", Long.class); assertThat(col2AsString, is(equalTo("23"))); assertThat(col2AsInteger, is(equalTo(23))); assertThat(col2AsLong, is(equalTo(23L))); }