From 127a3043f830e66c8d7eb9bb76fbea93a25b8432 Mon Sep 17 00:00:00 2001 From: Richard Shurtz Date: Sun, 17 Sep 2023 22:54:21 -0700 Subject: [PATCH] Add in a new ColReader for java.util.Date that parses a full timestamp rather than just the day-date (java.sql.Date). --- .../com/lucidchart/relate/ColReader.scala | 3 +- relate/src/test/scala/ColReaderTest.scala | 28 +++++++++++++------ 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/relate/src/main/scala/com/lucidchart/relate/ColReader.scala b/relate/src/main/scala/com/lucidchart/relate/ColReader.scala index 4276c4c..94e81cd 100644 --- a/relate/src/main/scala/com/lucidchart/relate/ColReader.scala +++ b/relate/src/main/scala/com/lucidchart/relate/ColReader.scala @@ -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)) diff --git a/relate/src/test/scala/ColReaderTest.scala b/relate/src/test/scala/ColReaderTest.scala index 4a39dab..a725ac1 100644 --- a/relate/src/test/scala/ColReaderTest.scala +++ b/relate/src/test/scala/ColReaderTest.scala @@ -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._ @@ -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, @@ -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"), @@ -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 @@ -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], @@ -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"), @@ -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) @@ -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, @@ -167,6 +175,7 @@ class ColReaderTest extends Specification with Mockito { None, None, None, + None, None ) } @@ -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),