-
-
Notifications
You must be signed in to change notification settings - Fork 212
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 8 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
85 changes: 85 additions & 0 deletions
85
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,85 @@ | ||
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.LocalDate class in the database as Date object. | ||
* | ||
* @author graynk | ||
*/ | ||
public class InstantType extends BaseDataType { | ||
|
||
private static InstantType singleton; | ||
public static InstantType getSingleton() { | ||
if (singleton == null) { | ||
try { | ||
Class.forName("java.time.Instant", false, null); | ||
singleton = new InstantType(); | ||
} catch (ClassNotFoundException e) { | ||
return null; // No java.time on classpath; | ||
} | ||
} | ||
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 LocalDateTime value: " + defaultStr, e); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should say "Instant". |
||
} | ||
} | ||
|
||
@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 boolean isValidForVersion() { | ||
return true; | ||
} | ||
|
||
@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); | ||
} | ||
|
||
@Override | ||
public boolean isArgumentHolderRequired() { | ||
return true; | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
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,83 @@ | ||
package com.j256.ormlite.field.types; | ||
|
||
import java.lang.reflect.Field; | ||
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. | ||
* | ||
* @author graynk | ||
*/ | ||
public class LocalDateSqlType extends BaseDataType { | ||
|
||
private static LocalDateSqlType singleton; | ||
public static LocalDateSqlType getSingleton() { | ||
if (singleton == null) { | ||
try { | ||
Class.forName("java.time.LocalDate", false, null); | ||
singleton = new LocalDateSqlType(); | ||
} catch (ClassNotFoundException e) { | ||
return null; // No java.time on classpath; | ||
} | ||
} | ||
return singleton; | ||
} | ||
private LocalDateSqlType() { super(SqlType.LOCAL_DATE, new Class<?>[] { LocalDate.class }); } | ||
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(); | ||
} | ||
|
||
@Override | ||
public boolean isValidForVersion() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public Object moveToNextValue(Object currentValue) { | ||
LocalDate date = (LocalDate) currentValue; | ||
return date.plusDays(1); | ||
} | ||
|
||
@Override | ||
public boolean isArgumentHolderRequired() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isValidForField(Field field) { | ||
return (field.getType() == LocalDate.class); | ||
} | ||
} |
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.
I think the name for this is a little confusing since we are not storing this as a
java.sql
type.Also, I think the Javadocs for the
*_SQL
types could be expanded to actually describe what's different about them.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.
I agree, this is true for all
*_SQL
types, but I don't know how to name them differently.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.
Well the only one called that right now is
SQL_DATE
and for good reason.Types more like this situation is
DATE_LONG
,DATE_INTEGER
,BOOLEAN_INTEGER
, etc. I'm not sure what the best name is.OFFSET_TIME_TIMESTAMP
? Does that sound clumsy?I'd still modify the Javadoc for these enums to explain their purpose either way.