Search in sources :

Example 1 with DateTimeConverter

use of org.sql2o.converters.joda.DateTimeConverter in project sql2o by aaberg.

the class Convert method fillDefaults.

private static void fillDefaults(Map<Class<?>, Converter<?>> mapToFill) {
    mapToFill.put(Integer.class, new IntegerConverter(false));
    mapToFill.put(int.class, new IntegerConverter(true));
    mapToFill.put(Double.class, new DoubleConverter(false));
    mapToFill.put(double.class, new DoubleConverter(true));
    mapToFill.put(Float.class, new FloatConverter(false));
    mapToFill.put(float.class, new FloatConverter(true));
    mapToFill.put(Long.class, new LongConverter(false));
    mapToFill.put(long.class, new LongConverter(true));
    mapToFill.put(Short.class, new ShortConverter(false));
    mapToFill.put(short.class, new ShortConverter(true));
    mapToFill.put(Byte.class, new ByteConverter(false));
    mapToFill.put(byte.class, new ByteConverter(true));
    mapToFill.put(BigDecimal.class, new BigDecimalConverter());
    mapToFill.put(String.class, new StringConverter());
    mapToFill.put(java.util.Date.class, DateConverter.instance);
    mapToFill.put(java.sql.Date.class, new AbstractDateConverter<java.sql.Date>(java.sql.Date.class) {

        @Override
        protected java.sql.Date fromMilliseconds(long millisecond) {
            return new java.sql.Date(millisecond);
        }
    });
    mapToFill.put(java.sql.Time.class, new AbstractDateConverter<java.sql.Time>(java.sql.Time.class) {

        @Override
        protected java.sql.Time fromMilliseconds(long millisecond) {
            return new java.sql.Time(millisecond);
        }
    });
    mapToFill.put(java.sql.Timestamp.class, new AbstractDateConverter<java.sql.Timestamp>(java.sql.Timestamp.class) {

        @Override
        protected java.sql.Timestamp fromMilliseconds(long millisecond) {
            return new java.sql.Timestamp(millisecond);
        }
    });
    BooleanConverter booleanConverter = new BooleanConverter();
    mapToFill.put(Boolean.class, booleanConverter);
    mapToFill.put(boolean.class, booleanConverter);
    ByteArrayConverter byteArrayConverter = new ByteArrayConverter();
    // it's impossible to cast Byte[].class <-> byte[].class
    // and I'm too lazy to implement converter for Byte[].class
    // since it's really doesn't wide-used
    // otherwise someone already detect this error
    // mapToFill.put(Byte[].class, byteArrayConverter);
    mapToFill.put(byte[].class, byteArrayConverter);
    InputStreamConverter inputStreamConverter = new InputStreamConverter();
    mapToFill.put(InputStream.class, inputStreamConverter);
    mapToFill.put(ByteArrayInputStream.class, inputStreamConverter);
    mapToFill.put(UUID.class, new UUIDConverter());
    if (FeatureDetector.isJodaTimeAvailable()) {
        mapToFill.put(DateTime.class, new DateTimeConverter());
        mapToFill.put(LocalTime.class, new LocalTimeConverter());
        mapToFill.put(LocalDate.class, new LocalDateConverter());
    }
}
Also used : DateTime(org.joda.time.DateTime) LocalTime(org.joda.time.LocalTime) LocalDateConverter(org.sql2o.converters.joda.LocalDateConverter) LocalTimeConverter(org.sql2o.converters.joda.LocalTimeConverter) LocalDate(org.joda.time.LocalDate) DateTimeConverter(org.sql2o.converters.joda.DateTimeConverter)

Example 2 with DateTimeConverter

use of org.sql2o.converters.joda.DateTimeConverter 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)

Aggregations

DateTimeConverter (org.sql2o.converters.joda.DateTimeConverter)2 HashMap (java.util.HashMap)1 DateTime (org.joda.time.DateTime)1 LocalDate (org.joda.time.LocalDate)1 LocalTime (org.joda.time.LocalTime)1 Test (org.junit.Test)1 Connection (org.sql2o.Connection)1 Sql2o (org.sql2o.Sql2o)1 LocalDateConverter (org.sql2o.converters.joda.LocalDateConverter)1 LocalTimeConverter (org.sql2o.converters.joda.LocalTimeConverter)1 NoQuirks (org.sql2o.quirks.NoQuirks)1