如何将 java.util.UUID 转换为 doobie.syntax.SqlInterpolator.SingleFragment?
How do I convert a java.util.UUID to doobie.syntax.SqlInterpolator.SingleFragment?
我正在尝试使用 doobie、http4s 和 circe 设置一个带有数据库的简单 Scala 应用程序。
如何将 java.util.UUID 转换为 doobie.syntax.SqlInterpolator.SingleFragment?
final case class User(id: UUID, details: UserDetails)
implicit val userDecoder: Decoder[User] = deriveDecoder[User]
implicit def userEntityDecoder[F[_]: Sync]: EntityDecoder[F, User] = jsonOf
implicit val userEncoder: Encoder[User] = deriveEncoder[User]
implicit def userEntityEncoder[F[_]: Applicative]: EntityEncoder[F, User] = jsonEncoderOf
implicit val put: Put[User] =
Put[Json].contramap(_.asJson)
implicit val get: Get[User] =
Get[Json].temap(_.as[User].left.map(_.show))
[info] welcome to sbt 1.3.12 (N/A Java 14.0.1)
[info] loading global plugins from /Users/ryan/.sbt/1.0/plugins
[info] loading settings for project bobbymoore-build from plugins.sbt ...
[info] loading project definition from /Users/ryan/fullStackRyan/bobbymoore/project
[info] loading settings for project root from build.sbt ...
[info] set current project to bobbymoore (in build file:/Users/ryan/fullStackRyan/bobbymoore/)
[info] sbt server started at local:///Users/ryan/.sbt/1.0/server/90f560b1e0964865fc4c/sock
sbt:bobbymoore> compile
[info] Compiling 1 Scala source to /Users/ryan/fullStackRyan/bobbymoore/target/scala-2.13/classes ...
[error] /Users/ryan/fullStackRyan/bobbymoore/src/main/scala/com/c/bobbymoore/database/UserQueries.scala:24:20: type mismatch;
[error] found : java.util.UUID
[error] required: doobie.syntax.SqlInterpolator.SingleFragment
[error] | ${user.id},
[error] ^
[error] /Users/ryan/fullStackRyan/bobbymoore/src/main/scala/com/c/bobbymoore/database/UserQueries.scala:34:21: type mismatch;
[error] found : java.util.UUID
[error] required: doobie.syntax.SqlInterpolator.SingleFragment
[error] |WHERE id=$id
[error] ^
[error] two errors found
[error] (Compile / compileIncremental) Compilation failed
[error] Total time: 2 s, completed 13 Oct 2020, 16:31:46
如果您正在使用 PostgreSQL 并且 Java 中的 UUID 转换为 Postgres table 中的 UUID 列,那么您必须使用 Postgres extensions for Doobie
// other doobie imports
import doobie.postgres._
import doobie.postgres.implicits._
sql"""SELECT id, name FROM users WHERE id = ${user.id}""".query[(UUID, String)]
如果您不使用 PSQL 并且此 UUID 映射到 TEXT 字段,您必须在查询中自行映射:
sql"""SELECT id, name FROM users WHERE id = ${user.id.toString}"""
.query[(String, String)]
.map { case (uuid, name) => (UUID.fromString(uuid), name) }
或定义您自己的 Meta[UUID]
实例,例如
implicit val uuidMeta: Meta[UUID] =
Meta[String].imap[UUID](UUID.fromString)(_.toString)
如果您想派生一个实例,例如case class 包含 UUID。 Postgres 扩展库提供了一个与 PSQL 实现相匹配的库。
如果您使用例如,您肯定必须定义您自己的 Meta 实例Array[Byte]
或数据库端的其他一些数字表示形式来表示 UUID。
我正在尝试使用 doobie、http4s 和 circe 设置一个带有数据库的简单 Scala 应用程序。
如何将 java.util.UUID 转换为 doobie.syntax.SqlInterpolator.SingleFragment?
final case class User(id: UUID, details: UserDetails)
implicit val userDecoder: Decoder[User] = deriveDecoder[User]
implicit def userEntityDecoder[F[_]: Sync]: EntityDecoder[F, User] = jsonOf
implicit val userEncoder: Encoder[User] = deriveEncoder[User]
implicit def userEntityEncoder[F[_]: Applicative]: EntityEncoder[F, User] = jsonEncoderOf
implicit val put: Put[User] =
Put[Json].contramap(_.asJson)
implicit val get: Get[User] =
Get[Json].temap(_.as[User].left.map(_.show))
[info] welcome to sbt 1.3.12 (N/A Java 14.0.1)
[info] loading global plugins from /Users/ryan/.sbt/1.0/plugins
[info] loading settings for project bobbymoore-build from plugins.sbt ...
[info] loading project definition from /Users/ryan/fullStackRyan/bobbymoore/project
[info] loading settings for project root from build.sbt ...
[info] set current project to bobbymoore (in build file:/Users/ryan/fullStackRyan/bobbymoore/)
[info] sbt server started at local:///Users/ryan/.sbt/1.0/server/90f560b1e0964865fc4c/sock
sbt:bobbymoore> compile
[info] Compiling 1 Scala source to /Users/ryan/fullStackRyan/bobbymoore/target/scala-2.13/classes ...
[error] /Users/ryan/fullStackRyan/bobbymoore/src/main/scala/com/c/bobbymoore/database/UserQueries.scala:24:20: type mismatch;
[error] found : java.util.UUID
[error] required: doobie.syntax.SqlInterpolator.SingleFragment
[error] | ${user.id},
[error] ^
[error] /Users/ryan/fullStackRyan/bobbymoore/src/main/scala/com/c/bobbymoore/database/UserQueries.scala:34:21: type mismatch;
[error] found : java.util.UUID
[error] required: doobie.syntax.SqlInterpolator.SingleFragment
[error] |WHERE id=$id
[error] ^
[error] two errors found
[error] (Compile / compileIncremental) Compilation failed
[error] Total time: 2 s, completed 13 Oct 2020, 16:31:46
如果您正在使用 PostgreSQL 并且 Java 中的 UUID 转换为 Postgres table 中的 UUID 列,那么您必须使用 Postgres extensions for Doobie
// other doobie imports
import doobie.postgres._
import doobie.postgres.implicits._
sql"""SELECT id, name FROM users WHERE id = ${user.id}""".query[(UUID, String)]
如果您不使用 PSQL 并且此 UUID 映射到 TEXT 字段,您必须在查询中自行映射:
sql"""SELECT id, name FROM users WHERE id = ${user.id.toString}"""
.query[(String, String)]
.map { case (uuid, name) => (UUID.fromString(uuid), name) }
或定义您自己的 Meta[UUID]
实例,例如
implicit val uuidMeta: Meta[UUID] =
Meta[String].imap[UUID](UUID.fromString)(_.toString)
如果您想派生一个实例,例如case class 包含 UUID。 Postgres 扩展库提供了一个与 PSQL 实现相匹配的库。
如果您使用例如,您肯定必须定义您自己的 Meta 实例Array[Byte]
或数据库端的其他一些数字表示形式来表示 UUID。