Skip to content

Commit

Permalink
Merge pull request #367 from aaberg/java-time-support
Browse files Browse the repository at this point in the history
Java time support
  • Loading branch information
aaberg authored Apr 13, 2024
2 parents b69a570 + 979ad19 commit f274552
Show file tree
Hide file tree
Showing 25 changed files with 798 additions and 89 deletions.
6 changes: 6 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@
<version>${embedded-db-junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
Expand Down
21 changes: 13 additions & 8 deletions core/src/main/java/org/sql2o/converters/Convert.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.LocalTime;
import org.sql2o.converters.joda.DateTimeConverter;
import org.sql2o.converters.joda.LocalDateConverter;
import org.sql2o.converters.joda.LocalTimeConverter;
import org.sql2o.converters.joda.JodaDateTimeConverter;
import org.sql2o.converters.joda.JodaLocalDateConverter;
import org.sql2o.converters.joda.JodaLocalTimeConverter;
import org.sql2o.tools.FeatureDetector;

import java.io.ByteArrayInputStream;
Expand Down Expand Up @@ -89,13 +89,18 @@ protected java.sql.Timestamp fromMilliseconds(long millisecond) {

mapToFill.put(UUID.class, new UUIDConverter());

mapToFill.put(OffsetTime.class, new OffsetTimeConverter());
mapToFill.put(OffsetDateTime.class, new OffsetDateTimeConverter());
mapToFill.put(java.time.Instant.class, new InstantConverter());
mapToFill.put(java.time.OffsetDateTime.class, new OffsetDateTimeConverter());

mapToFill.put(java.time.LocalDate.class, new LocalDateConverter());
mapToFill.put(java.time.LocalTime.class, new LocalTimeConverter());
mapToFill.put(java.time.LocalDateTime.class, new LocalDateTimeConverter());


if (FeatureDetector.isJodaTimeAvailable()) {
mapToFill.put(DateTime.class, new DateTimeConverter());
mapToFill.put(LocalTime.class, new LocalTimeConverter());
mapToFill.put(LocalDate.class, new LocalDateConverter());
mapToFill.put(DateTime.class, new JodaDateTimeConverter());
mapToFill.put(LocalTime.class, new JodaLocalTimeConverter());
mapToFill.put(LocalDate.class, new JodaLocalDateConverter());
}
}

Expand Down
27 changes: 27 additions & 0 deletions core/src/main/java/org/sql2o/converters/InstantConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.sql2o.converters;

import java.sql.Timestamp;
import java.time.Instant;
import java.time.format.DateTimeParseException;

public class InstantConverter extends ConverterBase<Instant> {
@Override
public Instant convert(Object val) throws ConverterException {
if (val instanceof Timestamp) {
return ((Timestamp)val).toInstant();
}
if (val instanceof String) {
try {
return Instant.parse((String) val);
}
catch(DateTimeParseException e) {
throw new ConverterException("Can't convert string with value '" + val + "' to java.time.Instant", e);
}
}
if (val instanceof Long) {
return Instant.ofEpochMilli((Long)val);
}

throw new ConverterException("Can't convert type " + val.getClass().getName() + " to Instant");
}
}
26 changes: 26 additions & 0 deletions core/src/main/java/org/sql2o/converters/LocalDateConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.sql2o.converters;

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneOffset;

public class LocalDateConverter extends ConverterBase<LocalDate> {
@Override
public LocalDate convert(Object val) throws ConverterException {
if (val instanceof java.sql.Date) {
return ((java.sql.Date) val).toLocalDate();
}
if (val instanceof Long) {
return Instant.ofEpochMilli((Long) val).atOffset(ZoneOffset.UTC).toLocalDate();
}
if (val instanceof String) {
try {
return LocalDate.parse((String) val);
} catch (Exception e) {
throw new ConverterException("Cannot convert string with value '" + val + " to java.time.LocalDate", e);
}
}

throw new ConverterException("Cannot convert type " + val.getClass().toString() + " to java.time.LocalDate");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.sql2o.converters;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;

public class LocalDateTimeConverter extends ConverterBase<LocalDateTime> {
@Override
public LocalDateTime convert(Object val) throws ConverterException {
if (val instanceof java.sql.Timestamp) {
return ((java.sql.Timestamp) val).toLocalDateTime();
}
if (val instanceof Long) {
return Instant.ofEpochMilli((Long)val).atZone(ZoneOffset.UTC).toLocalDateTime();
}
if (val instanceof String) {
try {
return LocalDateTime.parse((String) val);
} catch (Exception e) {
throw new ConverterException("Can't convert String with value '" + val + "' to LocalDateTime", e);
}
}
throw new ConverterException("Can't convert type " + val.getClass().getName() + " to LocalDateTime");
}
}
24 changes: 24 additions & 0 deletions core/src/main/java/org/sql2o/converters/LocalTimeConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.sql2o.converters;

import java.time.LocalTime;

public class LocalTimeConverter extends ConverterBase<LocalTime> {

@Override
public LocalTime convert(Object val) throws ConverterException {
if (val instanceof java.sql.Time) {
return ((java.sql.Time) val).toLocalTime();
}
if (val instanceof java.sql.Timestamp) {
return ((java.sql.Timestamp) val).toLocalDateTime().toLocalTime();
}
if (val instanceof String) {
try {
return LocalTime.parse((String) val);
} catch (Exception e) {
throw new ConverterException("Can't convert String with value '" + val + "' to LocalTime", e);
}
}
throw new ConverterException("Can't convert type " + val.getClass().getName() + " to LocalTime");
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.sql2o.converters;

import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

public class OffsetDateTimeConverter extends ConverterBase<OffsetDateTime>{
@Override
Expand All @@ -9,16 +11,21 @@ public OffsetDateTime convert(Object val) throws ConverterException {
return (OffsetDateTime) val;
}

if (val instanceof java.util.Date) {
return ((java.util.Date) val).toInstant().atOffset(java.time.ZoneOffset.UTC);
if (val instanceof java.sql.Timestamp) {
return ((java.sql.Timestamp) val).toInstant().atZone(ZoneOffset.systemDefault()).toOffsetDateTime();
}

if (val instanceof Number) {
return java.time.Instant.ofEpochMilli(((Number) val).longValue()).atOffset(java.time.ZoneOffset.UTC);
if (val instanceof Long) {
final var instant = Instant.ofEpochMilli((Long)val);
return instant.atZone(ZoneOffset.systemDefault()).toOffsetDateTime();
}

if (val instanceof String) {
return OffsetDateTime.parse((String) val);
try {
return OffsetDateTime.parse((String) val);
} catch (Exception e) {
throw new ConverterException("Cannot convert String with value '" + val+ "' to java.time.OffsetDateTime", e);
}
}

throw new ConverterException("Cannot convert type " + val.getClass() + " to java.time.OffsetDateTime");
Expand Down
20 changes: 0 additions & 20 deletions core/src/main/java/org/sql2o/converters/OffsetTimeConverter.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@
/**
* Used by sql2o to convert a value from the database into a {@link DateTime} instance.
*/
public class DateTimeConverter implements Converter<DateTime> {
public class JodaDateTimeConverter implements Converter<DateTime> {

private final DateTimeZone timeZone;

// it's possible to create instance for other timezone
// and re-register converter
public DateTimeConverter(DateTimeZone timeZone) {
public JodaDateTimeConverter(DateTimeZone timeZone) {
this.timeZone = timeZone;
}

public DateTimeConverter() {
public JodaDateTimeConverter() {
this(DateTimeZone.getDefault());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
import org.sql2o.converters.Converter;
import org.sql2o.converters.ConverterException;

import java.time.OffsetTime;

/**
* Created by lars on 01.05.14.
*/
public class LocalDateConverter implements Converter<LocalDate> {
public class JodaLocalDateConverter implements Converter<LocalDate> {
@Override
public LocalDate convert(Object val) throws ConverterException {
if (val == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* Created by lars on 12/18/13.
*/
public class LocalTimeConverter implements Converter<LocalTime> {
public class JodaLocalTimeConverter implements Converter<LocalTime> {

public LocalTime convert(Object val) throws ConverterException {
if (val == null) {
Expand Down
16 changes: 16 additions & 0 deletions core/src/test/java/org/sql2o/H2ArgumentsSourceProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.sql2o;

import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;

import java.util.stream.Stream;

public class H2ArgumentsSourceProvider implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext extensionContext) throws Exception {
return Stream.of(
Arguments.of("h2", "jdbc:h2:mem:test;MODE=MSSQLServer;DB_CLOSE_DELAY=-1", "sa", "")
);
}
}
11 changes: 0 additions & 11 deletions core/src/test/java/org/sql2o/Sql2oTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -889,17 +889,6 @@ public void testTimeConverter(){
assertThat(jodaTime.getHourOfDay(), is(equalTo(new LocalTime().getHourOfDay())));
}

@Test
public void testOffsetTimeConverter() {
String sql = "select current_time as col1 from (values(0))";

try (Connection connection = sql2o.open()) {
OffsetTime offsetTime = connection.createQuery(sql).executeScalar(OffsetTime.class);
assertNotNull(offsetTime);
assertEquals(offsetTime.getHour(), OffsetTime.now().getHour());
}
}

public static class BindablePojo{
String data1;
private Timestamp data2;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.sql2o;

import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;

import java.util.stream.Stream;

public class TestDatabasesArgumentSourceProvider implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext extensionContext) throws Exception {
return Stream.of(
Arguments.of("h2", "jdbc:h2:mem:test;MODE=MSSQLServer;DB_CLOSE_DELAY=-1", "sa", ""),
Arguments.of("hsqldb", "jdbc:hsqldb:mem:testmemdb", "SA", "")
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ public class BidirectionalConverterTest {
@Before
public void setUp()
{
Quirks quirks = new NoQuirks(){
{
this.converters.put(UUID.class, new CustomUUIDConverter());
}
};
Quirks quirks = new NoQuirks();


this.sql2o = new Sql2o("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1", "sa", "", quirks);
Expand Down Expand Up @@ -94,4 +90,4 @@ private void deleteTable(){
// if it fails, its because the User table doesn't exists. Just ignore this.
}
}
}
}
27 changes: 0 additions & 27 deletions core/src/test/java/org/sql2o/converters/CustomUUIDConverter.java

This file was deleted.

Loading

0 comments on commit f274552

Please sign in to comment.