-
-
Notifications
You must be signed in to change notification settings - Fork 213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for java.time #159
Open
graynk
wants to merge
14
commits into
j256:main
Choose a base branch
from
graynk:jsr310-persister-pr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
872fa38
basic functionality for java.time. some tests fail, uncompliant dbs n…
graynk f37cd17
Merge remote-tracking branch 'upstream/master' into jsr310-persister-pr
graynk f97cdbc
forgot this
graynk 06dce09
Pushed a lot of unwanted code in the last commit.
graynk 92e2b21
Added SQL conversions for acceptable java.time types. New tests pass …
graynk dea9e30
Added workaround for H2 not supporting TIME WITH TIME ZONE
graynk 51001b3
the way it's done in ormlite-jdbc
graynk 77b25d6
Backwards compatible with Java 6
graynk 3cca49c
Inheritance to reduce code duplication
graynk f80d3e5
Protected constructors for base class
graynk 04900ff
Default persisters are now actually default
graynk ffb7fb9
Switched Class.forName calls to a single check for Java version
graynk fb175ba
Merge branch 'master' into jsr310-persister-pr
graynk 9cabc35
Test fixes by Bo98
graynk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
35 changes: 35 additions & 0 deletions
35
src/main/java/com/j256/ormlite/field/types/BaseLocalDateType.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,35 @@ | ||
package com.j256.ormlite.field.types; | ||
|
||
import com.j256.ormlite.field.SqlType; | ||
|
||
/** | ||
* Base class for all of the java.time class types. | ||
* | ||
* @author graynk | ||
*/ | ||
public abstract class BaseLocalDateType extends BaseDataType { | ||
private static final String specificationVersion = System.getProperty("java.specification.version"); | ||
private static final boolean javaTimeSupported = !(specificationVersion.equals("1.6") || specificationVersion.equals("1.7")); | ||
|
||
protected BaseLocalDateType(SqlType sqlType, Class<?>[] classes) { | ||
super(sqlType, classes); | ||
} | ||
|
||
protected BaseLocalDateType(SqlType sqlType) { | ||
super(sqlType); | ||
} | ||
|
||
protected static boolean isJavaTimeSupported() { | ||
return javaTimeSupported; | ||
} | ||
|
||
@Override | ||
public boolean isValidForVersion() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isArgumentHolderRequired() { | ||
return true; | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
src/main/java/com/j256/ormlite/field/types/InstantType.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,67 @@ | ||
package com.j256.ormlite.field.types; | ||
|
||
import java.lang.reflect.Field; | ||
import java.sql.SQLException; | ||
import java.time.Instant; | ||
import java.time.OffsetDateTime; | ||
import java.time.ZoneId; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
import com.j256.ormlite.field.FieldType; | ||
import com.j256.ormlite.field.SqlType; | ||
import com.j256.ormlite.misc.SqlExceptionUtil; | ||
import com.j256.ormlite.support.DatabaseResults; | ||
|
||
/** | ||
* A custom persister that is able to store the java.time.Instant class in the database as Timestamp With Timezone object. | ||
* This class does not have a SQL backup counter-part, the database should support JDBC 4.2 for it to be used. | ||
* Instant is also not a part of JDBC specification, persister converts Instant to OffsetDateTime with timezone fixed at UTC | ||
* | ||
* @author graynk | ||
*/ | ||
public class InstantType extends BaseLocalDateType { | ||
|
||
private static final InstantType singleton = isJavaTimeSupported() ? new InstantType() : null; | ||
public static InstantType getSingleton() { return singleton; } | ||
private InstantType() { super(SqlType.OFFSET_DATE_TIME, new Class<?>[] { Instant.class }); } | ||
protected InstantType(SqlType sqlType, Class<?>[] classes) { super(sqlType, classes); } | ||
|
||
@Override | ||
public Object parseDefaultString(FieldType fieldType, String defaultStr) throws SQLException { | ||
try { | ||
return OffsetDateTime.parse(defaultStr, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss[.SSS]x")); | ||
} catch (NumberFormatException e) { | ||
throw SqlExceptionUtil.create("Problems with field " + fieldType + | ||
" parsing default Instant value: " + defaultStr, e); | ||
} | ||
} | ||
|
||
@Override | ||
public Object resultToSqlArg(FieldType fieldType, DatabaseResults results, int columnPos) throws SQLException { | ||
return results.getOffsetDateTime(columnPos); | ||
} | ||
|
||
@Override | ||
public Object sqlArgToJava(FieldType fieldType, Object sqlArg, int columnPos) { | ||
OffsetDateTime value = (OffsetDateTime) sqlArg; | ||
return value.toInstant(); | ||
} | ||
|
||
@Override | ||
public Object javaToSqlArg(FieldType fieldType, Object javaObject) { | ||
Instant instant = (Instant) javaObject; | ||
// ZoneOffset.UTC is evaluated at InstantType creation, fails on Java 6. Using ZoneId.of() instead | ||
return OffsetDateTime.ofInstant(instant, ZoneId.of("UTC")); | ||
} | ||
|
||
@Override | ||
public Object moveToNextValue(Object currentValue) { | ||
Instant datetime = (Instant) currentValue; | ||
return datetime.plusNanos(1); | ||
} | ||
|
||
@Override | ||
public boolean isValidForField(Field field) { | ||
return (field.getType() == Instant.class); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/com/j256/ormlite/field/types/LocalDateSqlType.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,53 @@ | ||
package com.j256.ormlite.field.types; | ||
|
||
import java.sql.Date; | ||
import java.sql.SQLException; | ||
import java.time.LocalDate; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
import com.j256.ormlite.field.FieldType; | ||
import com.j256.ormlite.field.SqlType; | ||
import com.j256.ormlite.misc.SqlExceptionUtil; | ||
import com.j256.ormlite.support.DatabaseResults; | ||
|
||
/** | ||
* A custom persister that is able to store the java.time.LocalDate class in the database as Date object. | ||
* This class should be used only when database used does not support JDBC 4.2, since it converts java.time.LocalDate | ||
* to java.sql.Date. | ||
* | ||
* @author graynk | ||
*/ | ||
public class LocalDateSqlType extends LocalDateType { | ||
|
||
private static LocalDateSqlType singleton = isJavaTimeSupported() ? new LocalDateSqlType() : null; | ||
public static LocalDateSqlType getSingleton() { return singleton; } | ||
private LocalDateSqlType() { super(SqlType.LOCAL_DATE); } | ||
protected LocalDateSqlType(SqlType sqlType, Class<?>[] classes) { super(sqlType, classes); } | ||
|
||
@Override | ||
public Object parseDefaultString(FieldType fieldType, String defaultStr) throws SQLException { | ||
try { | ||
return Date.valueOf(LocalDate.parse(defaultStr, DateTimeFormatter.ISO_LOCAL_DATE)); | ||
} catch (NumberFormatException e) { | ||
throw SqlExceptionUtil.create("Problems with field " + fieldType + | ||
" parsing default LocalDate value: " + defaultStr, e); | ||
} | ||
} | ||
|
||
@Override | ||
public Object resultToSqlArg(FieldType fieldType, DatabaseResults results, int columnPos) throws SQLException { | ||
return results.getDate(columnPos); | ||
} | ||
|
||
@Override | ||
public Object javaToSqlArg(FieldType fieldType, Object javaObject) { | ||
LocalDate date = (LocalDate) javaObject; | ||
return Date.valueOf(date); | ||
} | ||
|
||
@Override | ||
public Object sqlArgToJava(FieldType fieldType, Object sqlArg, int columnPos) { | ||
Date date = (Date) sqlArg; | ||
return date.toLocalDate(); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/com/j256/ormlite/field/types/LocalDateTimeSqlType.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,53 @@ | ||
package com.j256.ormlite.field.types; | ||
|
||
import java.sql.SQLException; | ||
import java.sql.Timestamp; | ||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
import com.j256.ormlite.field.FieldType; | ||
import com.j256.ormlite.field.SqlType; | ||
import com.j256.ormlite.misc.SqlExceptionUtil; | ||
import com.j256.ormlite.support.DatabaseResults; | ||
|
||
/** | ||
* A custom persister that is able to store the java.time.LocalDateTime class in the database as Timestamp object. | ||
* This class should be used only when database used does not support JDBC 4.2, since it converts java.time.LocalDateTime | ||
* to java.sql.Timestamp. | ||
* | ||
* @author graynk | ||
*/ | ||
public class LocalDateTimeSqlType extends LocalDateTimeType { | ||
|
||
private static final LocalDateTimeSqlType singleton = isJavaTimeSupported() ? new LocalDateTimeSqlType() : null; | ||
public static LocalDateTimeSqlType getSingleton() { return singleton; } | ||
private LocalDateTimeSqlType() { super(SqlType.LOCAL_DATE_TIME); } | ||
protected LocalDateTimeSqlType(SqlType sqlType, Class<?>[] classes) { super(sqlType, classes); } | ||
|
||
@Override | ||
public Object parseDefaultString(FieldType fieldType, String defaultStr) throws SQLException { | ||
try { | ||
return Timestamp.valueOf(LocalDateTime.parse(defaultStr, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss[.SSS]"))); | ||
} catch (NumberFormatException e) { | ||
throw SqlExceptionUtil.create("Problems with field " + fieldType + | ||
" parsing default LocalDateTime value: " + defaultStr, e); | ||
} | ||
} | ||
|
||
@Override | ||
public Object resultToSqlArg(FieldType fieldType, DatabaseResults results, int columnPos) throws SQLException { | ||
return results.getTimestamp(columnPos); | ||
} | ||
|
||
@Override | ||
public Object sqlArgToJava(FieldType fieldType, Object sqlArg, int columnPos) { | ||
Timestamp value = (Timestamp) sqlArg; | ||
return value.toLocalDateTime(); | ||
} | ||
|
||
@Override | ||
public Object javaToSqlArg(FieldType fieldType, Object javaObject) { | ||
LocalDateTime datetime = (LocalDateTime) javaObject; | ||
return Timestamp.valueOf(datetime); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This caused problems in the tests for me. A better approach here (and ultimately more user friendly) would be variable length second fractions.
An example of this is here: Bo98@00fed82, along with adjustments to the tests to make sure they are consistently truncated (H2 supports milliseconds but the tests were comparing it to microseconds on my machine).