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;
}
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();
}
}
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();
}
}
}
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));
}
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)));
}
Aggregations