Search in sources :

Example 6 with Table

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);
}
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 Table

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();
    }
}
Also used : Table(org.sql2o.data.Table) Query(org.sql2o.Query) Connection(org.sql2o.Connection) UUID(java.util.UUID) Sql2o(org.sql2o.Sql2o) Test(org.junit.Test)

Example 8 with Table

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

Example 9 with Table

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();
    }
}
Also used : Table(org.sql2o.data.Table) Query(org.sql2o.Query) Connection(org.sql2o.Connection) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)9 Table (org.sql2o.data.Table)9 Connection (org.sql2o.Connection)5 Row (org.sql2o.data.Row)5 Query (org.sql2o.Query)3 LazyTable (org.sql2o.data.LazyTable)3 UUID (java.util.UUID)2 BigDecimal (java.math.BigDecimal)1 Sql2o (org.sql2o.Sql2o)1 Sql2oException (org.sql2o.Sql2oException)1