Scala Inactive Slick 3.0 mapping between java8 OffsetDateTime and Timestamp

First, if I want to match the date and time with the time zone for Slick, in which class should I use OffsetDateTime or ZonedDateTime ? As for Yoda, we can only use DateTime .

How can I write an implicit conversion between java8 ZonedDateTime and Sql Timestamp to display a Slick table?

It seems pretty simple to use joda DateTime to include timezone information. However, after switching to Java8, I’m not quite sure whether to use ZonedDateTime or OffsetDateTime , since http://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html suggests using OffsetDateTime.

For my current code, I just use the Java8 LocalDateTime , and I write the following implicit for matching between spots.

 implicit val JavaLocalDateTimeMapper = MappedColumnType.base[LocalDateTime, Timestamp]( l => Timestamp.valueOf(l), t => t.toLocalDateTime ) 

Not quite sure if I can write this using either ZonedDateTime or OffsetDateTime ?

+5
source share
2 answers

Short answer

As with Slick 3.1, the short answer is to use OffsetDateTime , but you need to map it to String , not Timestamp to work with any database.

That is, you will need MappedColumnType.base[OffsetDateTime, String] . You can use toString and OffsetDateTime.parse to convert strings:

 scala> import java.time._ import java.time._ scala> val paris = ZoneId.of("Europe/Paris") paris: java.time.ZoneId = Europe/Paris scala> OffsetDateTime.now(paris) res0: java.time.OffsetDateTime = 2016-01-05T20:38:46.473+01:00 scala> OffsetDateTime.parse(res0.toString) res2: java.time.OffsetDateTime = 2016-01-05T20:38:46.473+01:00 

Longer answer

The difference between OffsetDateTime and ZonedDateTime is covered by the answer to What is the difference between java 8 ZonedDateTime and OffsetDateTime? therefore I will not repeat here.

However, you will want to read this to decide if the short answer is appropriate for your situation.

If you use Postgres, support java.time via slick-pg . I have not had the opportunity to use it myself, but it is certainly worth investigating if you use this database. See, for example, the test suite for the "date2" add-in.

Best answer (or "Future answer!")

The great news is that there is an active pull request to add support for java.time data types in Slick. You can track the progress of the ticket that is currently planned for Slick 3.2.

+9
source

There was a fork that, it seems to me, was not released, but it had Java8 support:

https://github.com/xavier-fernandez/slick

This is pretty stable and I use it with HSQLDB in prod. And the related / pull specification request:

https://github.com/slick/slick/pull/1349

If you are looking for code generation, it is not too complicated using the above fork, this post describes how to do this:

https://github.com/slick/slick/pull/1349#issuecomment-245566307

0
source

Source: https://habr.com/ru/post/1239871/


All Articles