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();
}
}
}
use of org.sql2o.Connection 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.Connection in project sql2o by aaberg.
the class PostgresTest method testKeyKeyOnSerial.
@Test
public void testKeyKeyOnSerial() {
Connection connection = null;
try {
connection = sql2o.beginTransaction();
String createTableSql = "create table test_serial_table (id serial primary key, val varchar(20))";
connection.createQuery(createTableSql).executeUpdate();
String insertSql = "insert into test_serial_table(val) values ('something')";
Long key = connection.createQuery(insertSql, true).executeUpdate().getKey(Long.class);
assertThat(key, equalTo(1L));
key = connection.createQuery(insertSql, true).executeUpdate().getKey(Long.class);
assertThat(key, equalTo(2L));
} finally {
if (connection != null) {
connection.rollback();
}
}
}
use of org.sql2o.Connection 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();
}
}
}
Aggregations