Search in sources :

Example 1 with Row

use of org.sql2o.data.Row in project sql2o by aaberg.

the class Query method executeAndFetchTableLazy.

public LazyTable executeAndFetchTableLazy() {
    final LazyTable lt = new LazyTable();
    lt.setRows(new ResultSetIterableBase<Row>() {

        public Iterator<Row> iterator() {
            return new TableResultSetIterator(rs, isCaseSensitive(), getConnection().getSql2o().getQuirks(), lt);
        }
    });
    return lt;
}
Also used : LazyTable(org.sql2o.data.LazyTable) Iterator(java.util.Iterator) TableResultSetIterator(org.sql2o.data.TableResultSetIterator) Row(org.sql2o.data.Row) TableResultSetIterator(org.sql2o.data.TableResultSetIterator)

Example 2 with Row

use of org.sql2o.data.Row in project sql2o by aaberg.

the class PostgresTest method testIssue10_StatementsOnPostgres_withTransaction.

@Test
public void testIssue10_StatementsOnPostgres_withTransaction() {
    try (final Connection connection = sql2o.beginTransaction()) {
        String createTableSql = "create table test_table(id SERIAL, val varchar(20))";
        connection.createQuery(createTableSql).executeUpdate();
        String insertSql = "insert into test_table (val) values(:val)";
        Long key = connection.createQuery(insertSql, true).addParameter("val", "something").executeUpdate().getKey(Long.class);
        assertNotNull(key);
        assertTrue(key > 0);
        String selectSql = "select id, val from test_table";
        Table resultTable = connection.createQuery(selectSql).executeAndFetchTable();
        assertThat(resultTable.rows().size(), is(1));
        Row resultRow = resultTable.rows().get(0);
        assertThat(resultRow.getLong("id"), equalTo(key));
        assertThat(resultRow.getString("val"), is("something"));
        // always rollback, as this is only for tesing purposes.
        connection.rollback();
    }
}
Also used : Table(org.sql2o.data.Table) Connection(org.sql2o.Connection) Row(org.sql2o.data.Row) Test(org.junit.Test)

Example 3 with Row

use of org.sql2o.data.Row in project sql2o by aaberg.

the class PostgresTest method testIssue10StatementsOnPostgres_noTransaction.

@Test
public void testIssue10StatementsOnPostgres_noTransaction() {
    try {
        try (Connection connection = sql2o.open()) {
            connection.createQuery("create table test_table(id SERIAL, val varchar(20))").executeUpdate();
        }
        try (Connection connection = sql2o.open()) {
            Long key = connection.createQuery("insert into test_table (val) values(:val)", true).addParameter("val", "something").executeUpdate().getKey(Long.class);
            assertNotNull(key);
            assertTrue(key > 0);
            String selectSql = "select id, val from test_table";
            Table resultTable = connection.createQuery(selectSql).executeAndFetchTable();
            assertThat(resultTable.rows().size(), is(1));
            Row resultRow = resultTable.rows().get(0);
            assertThat(resultRow.getLong("id"), equalTo(key));
            assertThat(resultRow.getString("val"), is("something"));
        }
    } finally {
        try (final Connection connection = sql2o.open();
            final Query query = connection.createQuery("drop table if exists test_table")) {
            query.executeUpdate();
        }
    }
}
Also used : Table(org.sql2o.data.Table) Query(org.sql2o.Query) Connection(org.sql2o.Connection) Row(org.sql2o.data.Row) Test(org.junit.Test)

Example 4 with Row

use of org.sql2o.data.Row in project sql2o by aaberg.

the class Sql2oTest method testLazyTable.

@Test
public void testLazyTable() throws SQLException {
    createAndFillUserTable();
    Query q = sql2o.createQuery("select * from User");
    LazyTable lt = null;
    try {
        lt = q.executeAndFetchTableLazy();
        for (Row r : lt.rows()) {
            String name = r.getString("name");
            assertThat(name, notNullValue());
        }
        // still in autoClosable scope. Expecting connection to be open.
        assertThat(q.getConnection().getJdbcConnection().isClosed(), is(false));
    } finally {
        // simulate autoClose.
        lt.close();
    }
    // simulated autoClosable scope exited. Expecting connection to be closed.
    assertThat(q.getConnection().getJdbcConnection().isClosed(), is(true));
}
Also used : LazyTable(org.sql2o.data.LazyTable) Row(org.sql2o.data.Row) Test(org.junit.Test)

Example 5 with Row

use of org.sql2o.data.Row in project sql2o by aaberg.

the class Sql2oTest method testRowGetObjectWithConverters.

@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)));
}
Also used : Table(org.sql2o.data.Table) LazyTable(org.sql2o.data.LazyTable) 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