Skip to content

Commit

Permalink
Merge pull request #86 from lucidsoftware/add-support-for-LocalDate
Browse files Browse the repository at this point in the history
Add support for LocalDate
  • Loading branch information
tmccombs authored Sep 28, 2023
2 parents fae9766 + 6d145f3 commit 1efb986
Show file tree
Hide file tree
Showing 2 changed files with 14 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
@@ -1,6 +1,6 @@
package com.lucidchart.relate

import java.time.Instant
import java.time.{Instant, LocalDate}
import java.util.{Date, UUID}

class NullColumnException(col: String) extends Exception(s"Unexpected null value in column: $col")
Expand Down Expand Up @@ -42,6 +42,7 @@ object ColReader {
implicit val byteArrayReader: ColReader[Array[Byte]] = ColReader { (col, row) => row.byteArrayOption(col)}
implicit val byteReader: ColReader[Byte] = ColReader { (col, row) => row.byteOption(col)}
implicit val dateReader: ColReader[Date] = ColReader { (col, row) => row.dateOption(col)}
implicit val localDateReader: ColReader[LocalDate] = ColReader { (col, row) => row.localDateOption(col)}
implicit val instantReader: ColReader[Instant] = ColReader { (col, row) => row.instantOption(col)}
implicit val doubleReader: ColReader[Double] = ColReader { (col, row) => row.doubleOption(col)}
implicit val intReader: ColReader[Int] = ColReader { (col, row) => row.intOption(col)}
Expand Down
21 changes: 12 additions & 9 deletions relate/src/main/scala/com/lucidchart/relate/SqlRow.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import java.io.{InputStream, Reader}
import java.net.URL
import java.nio.ByteBuffer
import java.sql.{Blob, Clob, NClob, Ref, RowId, SQLXML, Time, Timestamp}
import java.time.Instant
import java.util.{Calendar, Date, UUID}
import java.time.{Instant, LocalDate}
import java.util.{Calendar, UUID}
import scala.collection.JavaConverters._
import scala.language.higherKinds
import scala.util.Try

object SqlRow {
Expand Down Expand Up @@ -41,10 +40,10 @@ class SqlRow(val resultSet: java.sql.ResultSet) extends ResultSetWrapper {
def strictCharacterStreamOption(column: String): Option[Reader] = getResultSetOption(resultSet.getCharacterStream(column))
def strictClob(column: String): Clob = resultSet.getClob(column)
def strictClobOption(column: String): Option[Clob] = getResultSetOption(resultSet.getClob(column))
def strictDate(column: String): Date = resultSet.getDate(column)
def strictDateOption(column: String): Option[Date] = getResultSetOption(resultSet.getDate(column))
def strictDate(column: String, cal: Calendar): Date = resultSet.getDate(column, cal)
def strictDateOption(column: String, cal: Calendar): Option[Date] = getResultSetOption(resultSet.getDate(column, cal))
def strictDate(column: String): java.sql.Date = resultSet.getDate(column)
def strictDateOption(column: String): Option[java.sql.Date] = getResultSetOption(resultSet.getDate(column))
def strictDate(column: String, cal: Calendar): java.sql.Date = resultSet.getDate(column, cal)
def strictDateOption(column: String, cal: Calendar): Option[java.sql.Date] = getResultSetOption(resultSet.getDate(column, cal))
def strictDouble(column: String): Double = resultSet.getDouble(column)
def strictDoubleOption(column: String): Option[Double] = getResultSetOption(resultSet.getDouble(column))
def strictFloat(column: String): Float = resultSet.getFloat(column)
Expand Down Expand Up @@ -138,12 +137,16 @@ class SqlRow(val resultSet: java.sql.ResultSet) extends ResultSetWrapper {
def javaBigDecimal(column: String): java.math.BigDecimal = javaBigDecimalOption(column).get
def javaBigDecimalOption(column: String): Option[java.math.BigDecimal] = getResultSetOption(resultSet.getBigDecimal(column))

def date(column: String): Date = dateOption(column).get
def date(column: String): java.util.Date = dateOption(column).get
// Timestamp documentation says that "it is recommended that code not view Timestamp values generically as an instance
// of java.util.Date." Since this is typed as a Date we should probably return an instance of a Date.
// `date == timestamp` can be true but `timestamp == date` is always false "the Timestamp.equals(Object) method is not
// symmetric with respect to the java.util.Date.equals(Object)"
def dateOption(column: String): Option[Date] = getResultSetOption(resultSet.getTimestamp(column)).map(ts => new Date(ts.getTime))
def dateOption(column: String): Option[java.util.Date] =
getResultSetOption(resultSet.getTimestamp(column)).map(ts => new java.util.Date(ts.getTime))

def localDate(column: String): LocalDate = localDateOption(column).get
def localDateOption(column: String): Option[LocalDate] = strictDateOption(column).map(_.toLocalDate)

def instant(column: String): Instant = instantOption(column).get
def instantOption(column: String): Option[Instant] = getResultSetOption(resultSet.getTimestamp(column)).map(_.toInstant)
Expand Down

0 comments on commit 1efb986

Please sign in to comment.