Skip to content

Commit

Permalink
Add in a new ColReader for java.util.Date that parses a full timestam…
Browse files Browse the repository at this point in the history
…p rather than just the day-date (java.sql.Date).
  • Loading branch information
richard-shurtz committed Sep 18, 2023
1 parent 2964822 commit 127a304
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
3 changes: 2 additions & 1 deletion relate/src/main/scala/com/lucidchart/relate/ColReader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ object ColReader {
implicit val boolReader: ColReader[Boolean] = optReader((col, rs) => rs.getBoolean(col))
implicit val byteArrayReader: ColReader[Array[Byte]] = optReader((col, rs) => rs.getBytes(col))
implicit val byteReader: ColReader[Byte] = optReader((col, rs) => rs.getByte(col))
implicit val dateReader: ColReader[Date] = optReader((col, rs) => rs.getDate(col))
implicit val dateReader: ColReader[Date] = optReader((col, rs) => rs.getTimestamp(col)).map(timestamp => new Date(timestamp.getTime))
implicit val sqlDateReader: ColReader[java.sql.Date] = optReader((col, rs) => rs.getDate(col))
implicit val instantReader: ColReader[Instant] = optReader((col, rs) => rs.getTimestamp(col)).map(_.toInstant)
implicit val doubleReader: ColReader[Double] = optReader((col, rs) => rs.getDouble(col))
implicit val intReader: ColReader[Int] = optReader((col, rs) => rs.getInt(col))
Expand Down
28 changes: 19 additions & 9 deletions relate/src/test/scala/ColReaderTest.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.lucidchart.relate

import java.util.{Date, UUID}
import java.sql.Timestamp
import java.time.Instant
import java.util.{Date, UUID}
import org.specs2.mock.Mockito
import org.specs2.mutable._

Expand All @@ -10,7 +11,8 @@ case class RecordA(
bool: Boolean,
ba: Array[Byte],
byte: Byte,
date: Date,
sqlDate: java.sql.Date,
timestampDate: Date,
instant: Instant,
double: Double,
int: Int,
Expand All @@ -29,7 +31,8 @@ object RecordA extends Mockito {
row[Boolean]("bool"),
row[Array[Byte]]("ba"),
row[Byte]("byte"),
row[Date]("date"),
row[java.sql.Date]("sqlDate"),
row[Date]("timestampDate"),
row[Instant]("instant"),
row[Double]("double"),
row[Int]("int"),
Expand All @@ -50,7 +53,8 @@ object RecordA extends Mockito {
rs.getBoolean("bool") returns true
rs.getBytes("ba") returns Array[Byte](1,2,3)
rs.getByte("byte") returns (1: Byte)
rs.getDate("date") returns (new java.sql.Date(10000))
rs.getDate("sqlDate") returns (new java.sql.Date(10000))
rs.getTimestamp("timestampDate") returns (new Timestamp(timeMillis))
rs.getTimestamp("instant") returns (new java.sql.Timestamp(timeMillis))
rs.getDouble("double") returns 1.1
rs.getInt("int") returns 10
Expand All @@ -69,7 +73,8 @@ case class RecordB(
bool: Option[Boolean],
ba: Option[Array[Byte]],
byte: Option[Byte],
date: Option[Date],
sqlDate: Option[java.sql.Date],
timestampDate: Option[Date],
instant: Option[Instant],
double: Option[Double],
int: Option[Int],
Expand All @@ -88,7 +93,8 @@ object RecordB extends Mockito {
row.opt[Boolean]("bool"),
row.opt[Array[Byte]]("ba"),
row.opt[Byte]("byte"),
row.opt[Date]("date"),
row.opt[java.sql.Date]("sqlDate"),
row.opt[Date]("timestampDate"),
row.opt[Instant]("instant"),
row.opt[Double]("double"),
row.opt[Int]("int"),
Expand All @@ -106,7 +112,8 @@ object RecordB extends Mockito {
rs.wasNull() returns true
rs.getBigDecimal("bd") returns null
rs.getBytes("ba") returns null
rs.getDate("date") returns null
rs.getDate("sqlDate") returns null
rs.getTimestamp("timestampDate") returns null
rs.getString("str") returns null
rs.getBytes("uuid") returns null
SqlRow(rs)
Expand Down Expand Up @@ -138,7 +145,8 @@ class ColReaderTest extends Specification with Mockito {
true,
null,
1,
new Date(10000),
new java.sql.Date(10000),
new Date(mockedInstant.toEpochMilli),
mockedInstant,
1.1,
10,
Expand Down Expand Up @@ -167,6 +175,7 @@ class ColReaderTest extends Specification with Mockito {
None,
None,
None,
None,
None
)
}
Expand All @@ -185,7 +194,8 @@ class ColReaderTest extends Specification with Mockito {
Some(true),
null,
Some(1),
Some(new Date(10000)),
Some(new java.sql.Date(10000)),
Some(new Date(mockedInstant.toEpochMilli)),
Some(mockedInstant),
Some(1.1),
Some(10),
Expand Down

0 comments on commit 127a304

Please sign in to comment.