0

我正在尝试创建一个接受案例类并将元素作为 List[C] 返回的方法。为了测试东西,我有以下工作

object LocalRun extends App {

  implicit private val cs = IO.contextShift(ExecutionContexts.synchronous)

  private lazy val xa = Transactor.fromDriverManager[IO](
    "org.h2.Driver",
    "jdbc:h2:./dbdir/mydb:mydb",
    "",
    "",
    Blocker
      .liftExecutionContext(ExecutionContexts.synchronous)
  )

  val stmt: String =
    "select table_name, count(1) as col_cnt from information_schema.columns where table_name = 'FUNCTION_COLUMNS'"

  private val proc: fs2.Stream[doobie.hi.ConnectionIO, (String, Int)] =
    HC.stream[(String, Int)](stmt, HPS.set(()), 512)

  proc.compile.toList
    .transact(xa).unsafeRunSync.take(5).foreach(e =>
      println(s"table name: ${e._1}, number of columns: ${e._2}")
    )

  new Directory(new File("./dbdir/mydb")).deleteRecursively()

}

输出

table name: FUNCTION_COLUMNS, number of columns: 17

我想创建类似的东西

def runSelect[C](stmt: String) = HC.stream[C](stmt, HPS.set(()), 512)

但编译器向我抛出以下错误:

在此处输入图像描述

有没有办法克服这个错误并创建多态函数,该函数将接受任何具有匹配选择的任意案例类并返回一个列表?

4

0 回答 0