use of org.sql2o.Connection in project sql2o by aaberg.
the class H2Tests method testIssue155.
@Test
public void testIssue155() {
Sql2o sql2o = new Sql2o(ds, new NoQuirks(new HashMap<Class, Converter>() {
{
put(DateTime.class, new DateTimeConverter(DateTimeZone.getDefault()));
}
}));
assertThat(sql2o.getQuirks(), is(instanceOf(NoQuirks.class)));
try (Connection connection = sql2o.open()) {
int val = connection.createQuery("select 42").executeScalar(Integer.class);
assertThat(val, is(equalTo(42)));
}
}
use of org.sql2o.Connection in project sql2o by aaberg.
the class IssuesTest method testIssue166OneCharacterParameterFail.
@Test
public void testIssue166OneCharacterParameterFail() {
try (Connection connection = sql2o.open()) {
connection.createQuery("create table testIssue166OneCharacterParameterFail(id integer, val varchar(10))").executeUpdate();
// This because of the :v parameter.
connection.createQuery("insert into testIssue166OneCharacterParameterFail(id, val) values(:id, :v)").addParameter("id", 1).addParameter("v", "foobar").executeUpdate();
int cnt = connection.createQuery("select count(*) from testIssue166OneCharacterParameterFail where id = :p").addParameter("p", 1).executeScalar(Integer.class);
assertEquals(1, cnt);
}
}
use of org.sql2o.Connection 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.Connection in project sql2o by aaberg.
the class IssuesTest method testIssue149NullPointerWhenUsingWrongParameterName.
@Test
public void testIssue149NullPointerWhenUsingWrongParameterName() {
try (Connection connection = sql2o.open()) {
connection.createQuery("create table issue149 (id integer primary key, val varchar(20))").executeUpdate();
connection.createQuery("insert into issue149(id, val) values (:id, :val)").addParameter("id", 1).addParameter("asdsa", // spell-error in parameter name
"something").executeUpdate();
fail("Expected exception!!");
} catch (Sql2oException ex) {
// awesome!
} catch (Throwable t) {
fail("A " + t.getClass().getName() + " was thrown, but An " + Sql2oException.class.getName() + " was expected");
}
}
use of org.sql2o.Connection in project sql2o by aaberg.
the class OracleTest method testUUiID.
@Test
@Ignore
public void testUUiID() {
UUID uuid1 = UUID.randomUUID();
UUID uuid2 = UUID.randomUUID();
final String ddl = "create table testUUID(id integer primary key, uuidval raw(16))";
final String insertSql = "insert into testUUID(id, uuidval) values (:id, :val)";
final String selectSql = "select uuidval from testUUID where id = :id";
try {
try (Connection connection = sql2o.open()) {
connection.createQuery(ddl).executeUpdate();
Query insertQuery = connection.createQuery(insertSql);
insertQuery.addParameter("id", 1).addParameter("val", uuid1).executeUpdate();
insertQuery.addParameter("id", 2).addParameter("val", uuid2).executeUpdate();
Query selectQuery = connection.createQuery(selectSql);
UUID uuid1FromDb = selectQuery.addParameter("id", 1).executeScalar(UUID.class);
UUID uuid2FromDb = selectQuery.addParameter("id", 2).executeScalar(UUID.class);
assertEquals(uuid1, uuid1FromDb);
assertEquals(uuid2, uuid2FromDb);
}
} catch (Exception e) {
e.printStackTrace();
fail("test failed. Exception");
} finally {
try (Connection con = sql2o.open()) {
con.createQuery("drop table testUUID").executeUpdate();
}
}
}
Aggregations