Search in sources :

Example 1 with NoQuirks

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();
}
Also used : Connection(java.sql.Connection) NoQuirks(org.sql2o.quirks.NoQuirks) DataSource(javax.sql.DataSource)

Example 2 with NoQuirks

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)));
    }
}
Also used : HashMap(java.util.HashMap) Connection(org.sql2o.Connection) DateTimeConverter(org.sql2o.converters.joda.DateTimeConverter) NoQuirks(org.sql2o.quirks.NoQuirks) Sql2o(org.sql2o.Sql2o) Test(org.junit.Test)

Example 3 with NoQuirks

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));
}
Also used : HashMap(java.util.HashMap) InstantConverter(net.runelite.http.service.util.InstantConverter) Converter(org.sql2o.converters.Converter) InstantConverter(net.runelite.http.service.util.InstantConverter) NoQuirks(org.sql2o.quirks.NoQuirks) Sql2o(org.sql2o.Sql2o) DataSource(javax.sql.DataSource) Bean(org.springframework.context.annotation.Bean)

Example 4 with NoQuirks

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));
}
Also used : HashMap(java.util.HashMap) InstantConverter(net.runelite.http.service.util.InstantConverter) Converter(org.sql2o.converters.Converter) InstantConverter(net.runelite.http.service.util.InstantConverter) NoQuirks(org.sql2o.quirks.NoQuirks) Sql2o(org.sql2o.Sql2o) DataSource(javax.sql.DataSource) Bean(org.springframework.context.annotation.Bean)

Example 5 with NoQuirks

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();
}
Also used : Connection(java.sql.Connection) NoQuirks(org.sql2o.quirks.NoQuirks) DataSource(javax.sql.DataSource)

Aggregations

NoQuirks (org.sql2o.quirks.NoQuirks)12 HashMap (java.util.HashMap)8 Sql2o (org.sql2o.Sql2o)8 Bean (org.springframework.context.annotation.Bean)7 Converter (org.sql2o.converters.Converter)7 DataSource (javax.sql.DataSource)6 InstantConverter (net.runelite.http.service.util.InstantConverter)6 Connection (java.sql.Connection)3 Test (org.junit.Test)2 Before (org.junit.Before)1 Qualifier (org.springframework.beans.factory.annotation.Qualifier)1 Connection (org.sql2o.Connection)1 DateTimeConverter (org.sql2o.converters.joda.DateTimeConverter)1 Quirks (org.sql2o.quirks.Quirks)1