Skunk - Scala - Error with Multiple parameter query

130 Views Asked by At

I am getting an error with the code below

import br.com.gbtech.model.PaymentRequestItem
import cats.effect.{IO, Resource}
import skunk.codec.all.*
import skunk.implicits.*
import skunk.{Query, Session, ~}

  val query: Query[(String *: Int), ValueObject] =
    sql"""
      SELECT field1, field2
      FROM   sample_table
      WHERE  field1 = $text
      AND field2 = $int4
    """.query(ValueObject.decoder)

  database.use(s => s.option(query)((param1, param2)))

And the decoder is here:

case class ValueObject(field1: Int, field2: String)

object ValueObject {
  val decoder: Decoder[ValueObject] =
    (int4 ~ varchar).map { case (f1,  f2) => ValueObject(f1, f2) }
}

And the error is:

type mismatch;
 found   : skunk.Query[String *: Int *: org.typelevel.twiddles.EmptyTuple,br.com.ValueObject]
    (which expands to)  skunk.Query[String :: Int :: shapeless.HNil,br.com.ValueObject]
 required: skunk.Query[String *: Int,br.com.ValueObject]
    (which expands to)  skunk.Query[String :: Int,br.com.ValueObject]
        """.query(ValueObject.decoder)

Where is actually the problem?

1

There are 1 best solutions below

0
André Luís Oliveira On

SOLUTION:

  • Fixing imports
import skunk.codec.all.*
import skunk.implicits.*
import skunk.*
  • Fixing Query type as explained here
Query[String *: Int *: EmptyTuple, ValueObject]
  • Fixing decoder
val decoder: Decoder[ValueObject] =
  (int4 ~ varchar).map { case (f1, f2) =>
    ValueObject(f1, f2) 
  }