use of org.sql2o.data.Table in project sql2o by aaberg.
the class IssuesTest method testIndexOutOfRangeExceptionWithMultipleColumnsWithSameName.
/**
* Test for issue #148 (https://github.com/aaberg/sql2o/issues/148)
* ## IndexOutOfRange exception
* When a resultset has multiple columns with the same name, sql2o 1.5.1 will throw an IndexOutOfRange exception when calling executeAndFetchTable() method.
*/
@Test
public void testIndexOutOfRangeExceptionWithMultipleColumnsWithSameName() {
class ThePojo {
public int id;
public String name;
}
String sql = "select 11 id, 'something' name, 'something else' name from (values(0))";
ThePojo p;
Table t;
try (Connection connection = sql2o.open()) {
p = connection.createQuery(sql).executeAndFetchFirst(ThePojo.class);
t = connection.createQuery(sql).executeAndFetchTable();
}
assertEquals(11, p.id);
assertEquals("something else", p.name);
assertEquals(11, (int) t.rows().get(0).getInteger("id"));
assertEquals("something else", t.rows().get(0).getString("name"));
}
use of org.sql2o.data.Table 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.Table 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.Table in project sql2o by aaberg.
the class Sql2oTest method testTable_asList.
@Test
public void testTable_asList() {
createAndFillUserTable();
List<Map<String, Object>> rows;
try (Connection con = sql2o.open()) {
Table table = con.createQuery("select * from user").executeAndFetchTable();
rows = table.asList();
}
assertEquals(insertIntoUsers, rows.size());
for (Map<String, Object> row : rows) {
assertEquals(4, row.size());
assertTrue(row.containsKey("id"));
assertTrue(row.containsKey("name"));
assertTrue(row.containsKey("email"));
assertTrue(row.containsKey("text"));
}
deleteUserTable();
}
use of org.sql2o.data.Table 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