use of org.sql2o.quirks.NoQuirks in project sql2o by aaberg.
the class ConnectionTest method test_createQueryWithParams.
public void test_createQueryWithParams() throws Throwable {
DataSource dataSource = mock(DataSource.class);
Connection jdbcConnection = mock(Connection.class);
when(jdbcConnection.isClosed()).thenReturn(false);
when(dataSource.getConnection()).thenReturn(jdbcConnection);
PreparedStatement ps = mock(PreparedStatement.class);
when(jdbcConnection.prepareStatement(anyString())).thenReturn(ps);
Sql2o sql2o = new Sql2o(dataSource, new NoQuirks() {
@Override
public boolean returnGeneratedKeysByDefault() {
return false;
}
});
org.sql2o.Connection cn = new org.sql2o.Connection(sql2o, false);
cn.createQueryWithParams("select :p1 name, :p2 age", "Dmitry Alexandrov", 35).buildPreparedStatement();
verify(dataSource, times(1)).getConnection();
verify(jdbcConnection).isClosed();
verify(jdbcConnection, times(1)).prepareStatement("select ? name, ? age");
verify(ps, times(1)).setString(1, "Dmitry Alexandrov");
verify(ps, times(1)).setInt(2, 35);
// check statement still alive
verify(ps, never()).close();
}
use of org.sql2o.quirks.NoQuirks 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.quirks.NoQuirks in project runelite by runelite.
the class SpringBootWebApplication method cacheSql2o.
@Bean("Runelite Cache SQL2O")
Sql2o cacheSql2o() throws NamingException {
DataSource dataSource = (DataSource) getContext().lookup("jdbc/runelite-cache2");
Map<Class, Converter> converters = new HashMap<>();
converters.put(Instant.class, new InstantConverter());
return new Sql2o(dataSource, new NoQuirks(converters));
}
use of org.sql2o.quirks.NoQuirks in project runelite by runelite.
the class SpringBootWebApplication method trackerSql2o.
@Bean("Runelite XP Tracker SQL2O")
Sql2o trackerSql2o() throws NamingException {
DataSource dataSource = (DataSource) getContext().lookup("jdbc/runelite-tracker");
Map<Class, Converter> converters = new HashMap<>();
converters.put(Instant.class, new InstantConverter());
return new Sql2o(dataSource, new NoQuirks(converters));
}
use of org.sql2o.quirks.NoQuirks in project sql2o by aaberg.
the class ConnectionTest method test_createQueryWithParamsThrowingException.
public void test_createQueryWithParamsThrowingException() throws Throwable {
DataSource dataSource = mock(DataSource.class);
Connection jdbcConnection = mock(Connection.class);
when(jdbcConnection.isClosed()).thenReturn(false);
when(dataSource.getConnection()).thenReturn(jdbcConnection);
PreparedStatement ps = mock(PreparedStatement.class);
doThrow(MyException.class).when(ps).setInt(anyInt(), anyInt());
when(jdbcConnection.prepareStatement(anyString())).thenReturn(ps);
Sql2o sql2o = new Sql2o(dataSource, new NoQuirks() {
@Override
public boolean returnGeneratedKeysByDefault() {
return false;
}
});
try (org.sql2o.Connection cn = sql2o.open()) {
cn.createQueryWithParams("select :p1 name, :p2 age", "Dmitry Alexandrov", 35).buildPreparedStatement();
fail("exception not thrown");
} catch (MyException ex) {
// as designed
}
verify(dataSource, times(1)).getConnection();
verify(jdbcConnection, atLeastOnce()).isClosed();
verify(jdbcConnection, times(1)).prepareStatement("select ? name, ? age");
verify(ps, times(1)).setInt(2, 35);
// check statement was closed
verify(ps, times(1)).close();
}
Aggregations