Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
WIP addressing pr feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
brbrown25 committed Jan 25, 2021
1 parent f49c12e commit 17b9640
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
6 changes: 3 additions & 3 deletions core/jvm/src/main/scala/zio/sql/typetag.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import zio.Chunk

trait TypeTagModule {

type TypeTagExtension[+A] <: Tag[A] with Decodeable[A]
type TypeTagExtension[+A] <: Tag[A] with Decodable[A]

trait Decodeable[+A] {
def decode[A, DecodingError](column: Either[Int, String], resultSet: ResultSet): Either[DecodingError, A] = ???
trait Decodable[+A] {
def decode[DecodingError](column: Either[Int, String], resultSet: ResultSet): Either[DecodingError, A]
}

trait Tag[+A] {
Expand Down
39 changes: 27 additions & 12 deletions postgres/src/main/scala/zio/sql/postgresql/PostgresModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,38 @@ trait PostgresModule extends Jdbc { self =>

object PostgresSpecific {
import self.ReadExecutor.DecodingError
trait PostgresTypeTag[+A] extends Tag[A] with Decodeable[A]
trait PostgresTypeTag[+A] extends Tag[A] with Decodable[A]
object PostgresTypeTag {
implicit case object TInterval extends PostgresTypeTag[Interval] {
override def decode[Interval, DecodingError](
override def decode[DecodingError](
column: Either[Int, String],
resultSet: ResultSet
): Either[DecodingError, Interval] =
scala.util
.Try(Interval.fromPgInterval(new PGInterval(column.fold(resultSet.getString(_), resultSet.getString(_)))))
.fold(
_ => Left(DecodingError.UnexpectedNull(column).asInstanceOf[DecodingError]),
r => Right(r.asInstanceOf[Interval])
r => Right(r)
)
}
implicit case object TTimestampz extends PostgresTypeTag[Timestampz] {
override def decode[ZonedDateTime, DecodingError](
override def decode[DecodingError](
column: Either[Int, String],
resultSet: ResultSet
): Either[DecodingError, ZonedDateTime] =
): Either[DecodingError, Timestampz] =
scala.util
.Try(
ZonedDateTime
.ofInstant(
column.fold(resultSet.getTimestamp(_), resultSet.getTimestamp(_)).toInstant,
ZoneId.of(ZoneOffset.UTC.getId)
)
Timestampz.fromZonedDateTime(
ZonedDateTime
.ofInstant(
column.fold(resultSet.getTimestamp(_), resultSet.getTimestamp(_)).toInstant,
ZoneId.of(ZoneOffset.UTC.getId)
)
)
)
.fold(
_ => Left(DecodingError.UnexpectedNull(column).asInstanceOf[DecodingError]),
r => Right(r.asInstanceOf[ZonedDateTime])
r => Right(r)
)
}
}
Expand Down Expand Up @@ -155,6 +157,19 @@ trait PostgresModule extends Jdbc { self =>
interval.seconds.toDouble
)
}

object Timestampz {
def fromZonedDateTime(zdt: ZonedDateTime): Timestampz =
Timestampz(
zdt.getYear,
zdt.getMonthValue,
zdt.getDayOfMonth,
zdt.getHour,
zdt.getMinute,
zdt.getSecond,
zdt.getZone.getId
)
}
}

object PostgresFunctionDef {
Expand Down Expand Up @@ -208,7 +223,7 @@ trait PostgresModule extends Jdbc { self =>
FunctionName("make_timestamp")
)
val MakeTimestampz =
FunctionDef[Timestampz, ZonedDateTime](FunctionName("make_timestamptz"))
FunctionDef[Timestampz, Timestampz](FunctionName("make_timestamptz"))
}

override def renderRead(read: self.Read[_]): String = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1128,15 +1128,16 @@ object FunctionDefSpec extends PostgresRunnableSpec with ShopSchema {
def runTest(tz: Timestampz) = {
val query = select(MakeTimestampz(tz)) from customers
for {
r <- execute(query).to[ZonedDateTime, ZonedDateTime](identity).runCollect
r <- execute(query).to[Timestampz, Timestampz](identity).runCollect
} yield r.head
}

val expectedRoundTripTimestamp = ZonedDateTime.of(2020, 11, 21, 19, 10, 25, 0, ZoneId.of(ZoneOffset.UTC.getId))
val expectedRoundTripTimestamp =
Timestampz.fromZonedDateTime(ZonedDateTime.of(2020, 11, 21, 19, 10, 25, 0, ZoneId.of(ZoneOffset.UTC.getId)))

(for {
t1 <- assertM(runTest(Timestampz(2013, 7, 15, 8, 15, 23.5)))(
equalTo(ZonedDateTime.parse("2013-07-15T08:15:23.5+00:00"))
equalTo(Timestampz.fromZonedDateTime(ZonedDateTime.parse("2013-07-15T08:15:23.5+00:00")))
)
t2 <- assertM(runTest(Timestampz(2020, 11, 21, 19, 10, 25, "+00:00")))(
equalTo(expectedRoundTripTimestamp)
Expand Down

0 comments on commit 17b9640

Please sign in to comment.