Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions modules/doobie-core/src/main/scala/DoobieMapping.scala
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ trait DoobieMappingLike[F[_]] extends Mapping[F] with SqlMappingLike[F] {
}
}

def fetch(fragment: Fragment, codecs: List[(Boolean, Codec)]): F[Vector[Array[Any]]] = {
def fetch(fragment: Fragment, codecs: List[(Boolean, Codec)]): F[Result[Vector[Array[Any]]]] = {
import cats.syntax.all._

val reads: Array[Read[Any]] = codecs.toArray.map {
Expand All @@ -161,6 +161,6 @@ trait DoobieMappingLike[F[_]] extends Mapping[F] with SqlMappingLike[F] {
}

implicit val read: Read[Array[Any]] = new Read.CompositeOfInstances[Any](reads)
fragment.query[Array[Any]].to[Vector].transact(transactor)
fragment.query[Array[Any]].to[Vector].transact(transactor).map(Result.success)
}
}
84 changes: 42 additions & 42 deletions modules/skunk/shared/src/main/scala/SkunkMapping.scala
Original file line number Diff line number Diff line change
Expand Up @@ -117,55 +117,55 @@ trait SkunkMappingLike[F[_]] extends Mapping[F] with SqlPgMappingLike[F] { outer
Some(codec._1.types.head.name)
}

// And we need to be able to fetch `Rows` given a `Fragment` and a list of decoders.
def fetch(fragment: Fragment, codecs: List[(Boolean, Codec)]): F[Vector[Array[Any]]] = {
val rowDecoder: Decoder[Array[Any]] =
new Decoder[Array[Any]] {
val types = codecs.flatMap { case (_, (d, _)) => d.types }

def arrToList(arr: Arr[_]): List[Any] =
(arr.foldLeft(List.empty[Any]) { case (acc, elem) => elem :: acc }).reverse

def decode(start: Int, ss: List[Option[String]]): Either[Decoder.Error, Array[Any]] = {
val ncols = ss.length-start
val arr = scala.Array.ofDim[Any](ncols)

var i = 0
var ss0 = ss.drop(start)
var codecs0 = codecs
while(i < ncols) {
val (isJoin, (decoder, isNullable)) = codecs0.head
val len = decoder.length
val (seg, tl) = ss0.splitAt(len)
val elem: Either[Decoder.Error, Any] =
if(isJoin && !isNullable)
decoder.opt.decode(0, seg).map(_.getOrElse(FailedJoin))
else
decoder.decode(0, seg)

elem match {
case Left(err) => return Left(err)
case Right(v) =>
v match {
case a: Arr[a] => arr(i) = arrToList(a)
case Some(a: Arr[a]) => arr(i) = Some(arrToList(a))
case other => arr(i) = other
}
}

i = i + 1
ss0 = tl
codecs0 = codecs0.tail
protected def rowDecoder(codecs: List[(Boolean, Codec)]): Decoder[Array[Any]] =
new Decoder[Array[Any]] {
val types = codecs.flatMap { case (_, (d, _)) => d.types }

def arrToList(arr: Arr[_]): List[Any] =
(arr.foldLeft(List.empty[Any]) { case (acc, elem) => elem :: acc }).reverse

def decode(start: Int, ss: List[Option[String]]): Either[Decoder.Error, Array[Any]] = {
val ncols = ss.length-start
val arr = scala.Array.ofDim[Any](ncols)

var i = 0
var ss0 = ss.drop(start)
var codecs0 = codecs
while(i < ncols) {
val (isJoin, (decoder, isNullable)) = codecs0.head
val len = decoder.length
val (seg, tl) = ss0.splitAt(len)
val elem: Either[Decoder.Error, Any] =
if(isJoin && !isNullable)
decoder.opt.decode(0, seg).map(_.getOrElse(FailedJoin))
else
decoder.decode(0, seg)

elem match {
case Left(err) => return Left(err)
case Right(v) =>
v match {
case a: Arr[a] => arr(i) = arrToList(a)
case Some(a: Arr[a]) => arr(i) = Some(arrToList(a))
case other => arr(i) = other
}
}

Right(arr)
i = i + 1
ss0 = tl
codecs0 = codecs0.tail
}

Right(arr)
}
}

// And we need to be able to fetch `Rows` given a `Fragment` and a list of decoders.
def fetch(fragment: Fragment, codecs: List[(Boolean, Codec)]): F[Result[Vector[Array[Any]]]] = {
pool.use { s =>
Resource.eval(s.prepare(fragment.fragment.query(rowDecoder))).use { ps =>
Resource.eval(s.prepare(fragment.fragment.query(rowDecoder(codecs)))).use { ps =>
ps.stream(fragment.argument, 1024).compile.toVector
}
}.map(Result.success)
}.onError {
case NonFatal(e) => Sync[F].delay(e.printStackTrace())
}
Expand Down
2 changes: 1 addition & 1 deletion modules/sql-core/src/main/scala/SqlMapping.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3308,7 +3308,7 @@ trait SqlMappingLike[F[_]] extends CirceMappingLike[F] with SqlModule[F] { self
def fetch: F[Result[Table]] = {
(for {
frag <- ResultT(fragment.pure[F])
rows <- ResultT(self.fetch(frag, query.codecs).map(_.success))
rows <- ResultT(self.fetch(frag, query.codecs))
} yield Table(rows)).value
}

Expand Down
2 changes: 1 addition & 1 deletion modules/sql-core/src/main/scala/SqlModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,5 @@ trait SqlModule[F[_]] {

def intCodec: Codec

def fetch(fragment: Fragment, codecs: List[(Boolean, Codec)]): F[Vector[Array[Any]]]
def fetch(fragment: Fragment, codecs: List[(Boolean, Codec)]): F[Result[Vector[Array[Any]]]]
}