Skip to content

Commit

Permalink
Add in LocalDate parsing options
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-shurtz committed Sep 27, 2023
1 parent cc6f028 commit 6d145f3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 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
6 changes: 4 additions & 2 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.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 @@ -146,6 +145,9 @@ class SqlRow(val resultSet: java.sql.ResultSet) extends ResultSetWrapper {
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 6d145f3

Please sign in to comment.