-
-
Notifications
You must be signed in to change notification settings - Fork 100
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 #37
base: main
Are you sure you want to change the base?
Conversation
…upporting time with timezone
…to JDBC 4.2 Not sure about Db2 and Netezza.
LGTM, and backward-compatible I would say (untested). |
Thanks for this. How is backwards compatibility achieved? Does this force ORMLite to Java 8? |
Java 8 is required on compile, but it can still be compiled to source and target of 1.6. New methods import java.time, but they don't get called (since java.time isn't on the classpath and there are no classes to persist with those methods). The issues (still solvable) arise in the ormlite-core package, I wrote more there: There are also backports of java.time like ThreeTen and ThreeTenABP, for which users in theory would be able to write custom persisters based on new Also in this PR I forgot to commit 2 new overriden methods, |
Thanks. Good stuff. If you don't mind, please minimize incidental line changes. I see changes to imports to use .* but my eclipse settings will expand that again so please fix. |
Sure, not a problem. Unfolded imports back, made changes to new classes to avoid calling non-existent classes due to backwards compatibility, verified that compiled artifacts work on Java 1.6 |
|
||
@Override | ||
public FieldConverter getFieldConverter(DataPersister dataPersister, FieldType fieldType) { | ||
// H2 doesn't support TIME WITH TIME ZONE |
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.
Minor: this should refer to Postgres - not H2.
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.
Fixed, thanks
By the way which databases did you end up being able to test? I can try out a few later today if needed. I set up a few yesterday for testing. Struggled to get the Db2 JDBC driver to connect, Netezza seems unobtainable now (support ends formally in a few months so perhaps support for it on ORMLite's side should be deprecated), and ODBC bridge is dead (#38) but I've got everything else up to Postgres so far. Will do SQLite and SQL Server today. |
Only H2, sorry, I would be very grateful if you checked what you can, I just don't have the time. I used only documentation for each DB to understand what it does or does not support. |
Ok, will do. There's already a few issues with different DBs right now anyway without these changes which I'll be looking into too. Oracle's JDBC driver is a disaster. Yes, the organisation behind Java made a bad JDBC driver. Existing Date/Time classes types don't even work 100% right now. Character.class is unsupported too (#2). |
public FieldConverter getFieldConverter(DataPersister dataPersister, FieldType fieldType) { | ||
// H2 doesn't support TIME WITH TIME ZONE | ||
if (dataPersister.getSqlType() == SqlType.OFFSET_TIME) | ||
return DataType.OFFSET_TIME_SQL.getDataPersister(); |
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.
Following the change in -core, this should now be OFFSET_TIME_COMPAT
. Similarly in Postgres too.
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.
Fixed
Basic tests for these should be added to JdbcBaseDaoImplTest. |
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've started testing things but a couple things popped up in the process so I haven't gotten too far yet.
For the H2 update, all I had to do was change this to use getAbsolutePath()
, merge #39, and remove testQueryKeyHolderNoKeys
, testIdColumnInteger
and testIdColumnChangedFromStringToNumber
(and I renamed testIdColumnInvalid
to testGeneratedIdColumn
).
These tests were testing unspecified JDBC behaviour (or at least not clearly specified enough for JDBC drivers to agree on it). PreparedStatement.getGeneratedKeys()
is only guaranteed to work when you don't explicitly use your own value for the generated ID column. It is only guaranteed to return data that was actually generated. Hence these tests, which broke after the update (and was broke already in some other drivers), were not correct to begin with. ormlite-core uses the correct behaviour and only passes GeneratedKeyHolder
when a generated ID isn't set in the insert operation, so these tests were not testing normal functionality of ORMLite.
I can share a commit of the H2 changes tomorrow if desired. I do have some tests in JdbcBaseDaoImplTest
now too in order to help me test the different drivers but they're not final yet.
return DataType.LOCAL_DATE_TIME_SQL.getDataPersister(); | ||
case OFFSET_TIME: // db2 doesn't seem to support TIME/STAMP WITH TIME ZONE | ||
case OFFSET_DATE_TIME: | ||
return null; |
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 pattern is going to cause problems. Returning null leads to hard-to-debug NullPointerExceptions within ORMLite.
We have two options here I can think of:
-
Throw a friendly exception. It'll be a bit messy to take this into account in the tests though.
-
Make a best effort at converting. We should be able to easily emulate the behaviour of PostgreSQL and H2. They, at database level, just convert the timestamp to UTC and store it as such. On converting back it uses the system timezone. The timezone is never actually stored, so the
WITH TIMEZONE
didn't actually mean much in the first place. We could do the same here using aFieldConverter
toLocalTime
/LocalDateTime
. We want to takeInstant
into account too (which doesn't have its own SQL type because it rightly doesn't need one) so perhaps aFieldConverter
wrapper around the data persister would work better than just creating a newDataPersister
. It gives more flexibility for custom persisters too (say if you wanted to create persisters for ThreeTen for example). See Fixed exceptions in some JDBC drivers when working with chars #40 for an approach I took in another situation. I think this approach will work best. We won't be doing any worse than PostgreSQL and H2's efforts.
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.
Sorry, haven't had a lot of free time on my hands. I'll be taking a vacation somewhere in the end of January/beginning of February and will do my best to catch up on both PRs then.
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.
Sure, no problem. I’ll test everything again when you manage to take a look.
Accompanies j256/ormlite-core#159 in closing j256/ormlite-core#154