Search in sources :

Example 6 with Row

use of org.sql2o.data.Row 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);
}
Also used : Table(org.sql2o.data.Table) LazyTable(org.sql2o.data.LazyTable) Row(org.sql2o.data.Row) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 7 with Row

use of org.sql2o.data.Row 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);
}
Also used : Sql2oException(org.sql2o.Sql2oException) Table(org.sql2o.data.Table) Row(org.sql2o.data.Row) Test(org.junit.Test)

Aggregations

Row (org.sql2o.data.Row)7 Test (org.junit.Test)6 Table (org.sql2o.data.Table)5 LazyTable (org.sql2o.data.LazyTable)4 Connection (org.sql2o.Connection)2 BigDecimal (java.math.BigDecimal)1 Iterator (java.util.Iterator)1 Query (org.sql2o.Query)1 Sql2oException (org.sql2o.Sql2oException)1 TableResultSetIterator (org.sql2o.data.TableResultSetIterator)1