Skip to content

How to write Write[Tuple] #1898

Answered by jatcwang
nineclue asked this question in Q&A
Jul 22, 2023 · 1 comments · 2 replies
Discussion options

You must be logged in to vote

Maybe this example scala-cli script will help. Note that you need to know the actual Tuple type e.g. (Int, String) because we need to know how to set each column at compile time.

//> using scala "3.3.0"
//> using dep "org.tpolecat::doobie-core:1.0.0-RC4"

import doobie.*
import doobie.implicits.*
import doobie.implicits.given

def tupleQuery[T <: Tuple](values: T)(using write: Write[T], pos: doobie.util.pos.Pos) = {
  fr"insert into study values (${write.toFragment(values)})"
}

@main
def main(): Unit = {
  Read[(Int, String)]
  Write[(Int, String)]
  tupleQuery[(Int, String, Double)]((1,"s",2.0))
}

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@nineclue
Comment options

@jatcwang
Comment options

Answer selected by nineclue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants