-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #367 from aaberg/java-time-support
Java time support
- Loading branch information
Showing
25 changed files
with
798 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
core/src/main/java/org/sql2o/converters/InstantConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
26
core/src/main/java/org/sql2o/converters/LocalDateConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
core/src/main/java/org/sql2o/converters/LocalDateTimeConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
24
core/src/main/java/org/sql2o/converters/LocalTimeConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 0 additions & 20 deletions
20
core/src/main/java/org/sql2o/converters/OffsetTimeConverter.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
core/src/test/java/org/sql2o/H2ArgumentsSourceProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", "") | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
core/src/test/java/org/sql2o/TestDatabasesArgumentSourceProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", "") | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 0 additions & 27 deletions
27
core/src/test/java/org/sql2o/converters/CustomUUIDConverter.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.