-
Notifications
You must be signed in to change notification settings - Fork 357
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
MySQL Java time instances #1791
Closed
Closed
Changes from all commits
Commits
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
CREATE TABLE IF NOT EXISTS test ( | ||
c_integer INTEGER NOT NULL, | ||
c_varchar VARCHAR(1024) NOT NULL, | ||
c_date DATE NOT NULL, | ||
c_datetime DATETIME(6) NOT NULL, | ||
c_time TIME(6) NOT NULL, | ||
c_timestamp TIMESTAMP(6) NOT NULL | ||
); | ||
INSERT INTO test(c_integer, c_varchar, c_date, c_datetime, c_time, c_timestamp) | ||
VALUES (123, 'str', '2019-02-13', '2019-02-13 22:03:21.051', '22:03:21.051', '2019-02-13 22:03:21.051'); |
File renamed without changes.
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
64 changes: 64 additions & 0 deletions
64
modules/mysql/src/main/scala/doobie/mysql/JavaTimeInstances.scala
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,64 @@ | ||
// Copyright (c) 2013-2020 Rob Norris and Contributors | ||
// This software is licensed under the MIT License (MIT). | ||
// For more information see LICENSE or https://opensource.org/licenses/MIT | ||
|
||
package doobie.mysql | ||
|
||
import doobie.Meta | ||
import doobie.enumerated.{JdbcType => JT} | ||
import doobie.util.meta.MetaConstructors | ||
|
||
import java.time.Instant | ||
import java.time.LocalDate | ||
import java.time.LocalDateTime | ||
import java.time.LocalTime | ||
import java.time.OffsetDateTime | ||
import java.time.OffsetTime | ||
import java.time.ZoneOffset | ||
|
||
/** | ||
* Instances for JSR-310 date time types. | ||
* | ||
* Note that to ensure instants are preserved you may need to use one of the solutions described | ||
* in [[https://docs.oracle.com/cd/E17952_01/connector-j-8.0-en/connector-j-time-instants.html]]. | ||
*/ | ||
trait JavaTimeInstances extends MetaConstructors { | ||
|
||
implicit val JavaTimeOffsetDateTimeMeta: Meta[OffsetDateTime] = | ||
Basic.oneObject( | ||
JT.TimestampWithTimezone, | ||
List(JT.VarChar, JT.Date, JT.Time, JT.Timestamp), | ||
classOf[OffsetDateTime] | ||
) | ||
|
||
implicit val JavaTimeInstantMeta: Meta[Instant] = | ||
JavaTimeOffsetDateTimeMeta.timap(_.toInstant)(OffsetDateTime.ofInstant(_, ZoneOffset.UTC)) | ||
|
||
implicit val JavaTimeLocalDateTimeMeta: Meta[LocalDateTime] = | ||
Basic.oneObject( | ||
JT.Timestamp, | ||
List(JT.VarChar, JT.Date, JT.Time, JT.TimestampWithTimezone), | ||
classOf[LocalDateTime] | ||
) | ||
|
||
implicit val JavaTimeLocalDateMeta: Meta[LocalDate] = | ||
Basic.oneObject( | ||
JT.Date, | ||
List(JT.VarChar, JT.Time, JT.Timestamp, JT.TimestampWithTimezone), | ||
classOf[LocalDate] | ||
) | ||
|
||
implicit val JavaTimeLocalTimeMeta: Meta[LocalTime] = | ||
Basic.oneObject( | ||
JT.Time, | ||
List(JT.VarChar, JT.Date, JT.Timestamp, JT.TimestampWithTimezone), | ||
classOf[LocalTime] | ||
) | ||
|
||
implicit val JavaTimeOffsetTimeMeta: Meta[OffsetTime] = | ||
Basic.oneObject( | ||
JT.TimestampWithTimezone, | ||
List(JT.VarChar, JT.Date, JT.Time, JT.Timestamp), | ||
classOf[OffsetTime] | ||
) | ||
} |
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,11 @@ | ||
// Copyright (c) 2013-2020 Rob Norris and Contributors | ||
// This software is licensed under the MIT License (MIT). | ||
// For more information see LICENSE or https://opensource.org/licenses/MIT | ||
|
||
package doobie | ||
|
||
package object mysql { | ||
|
||
object implicits | ||
extends JavaTimeInstances | ||
} |
Oops, something went wrong.
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.
MySQL/MariaDB has only
TIMESTAMP
which behaves as ifWITH TIME ZONE
, right? It is the equivalent of Java'sInstant
.So that would mean that
LocalDateTime
is not suitable forTIMESTAMP
, right?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.
For MySQL a
DATETIME
column = JDBCTimestamp
, and aTIMESTAMP
column = JDBCTimestamp
. I've added a hack like for Postgres to treat aTIMESTAMP
column asTimestampWithTimezone
and adjusted the instances.