1

我正在尝试将 Doobie 与 oracle 一起使用,但遇到了一些类型问题:

我有一张桌子:

    create table TEST
(
  CREATED_ON  TIMESTAMP(6) not null,
  A           VARCHAR2(50) not null,
  B           VARCHAR2(50) not null,
  C           VARCHAR2(50) not null,
)

我有一个案例课

import java.sql.Timestamp
case class Test(created_on: Timestamp, a:String, b:String, c:String)

当我尝试编译 Doobie 抱怨 Timestamp 类型时,如果我将类型更改为 String 它会编译但插入失败。根据文档,应支持时间戳:https ://tpolecat.github.io/doobie/docs/12-Custom-Mappings.html

它可能是 Oracle 特有的吗?

Error:(36, 13) Cannot find or construct a Read instance for type:
  com.juliusbaer.quant.analytics.persistency.Test
This can happen for a few reasons, but the most common case is that a data
member somewhere within this type doesn't have a Get instance in scope. Here are
some debugging hints:
- For Option types, ensure that a Read instance is in scope for the non-Option
  version.
- For types you expect to map to a single column ensure that a Get instance is
  in scope.
- For case classes, HLists, and shapeless records ensure that each element
  has a Read instance in scope.
- Lather, rinse, repeat, recursively until you find the problematic bit.
You can check that an instance exists for Read in the REPL or in your code:
  scala> Read[Foo]
and similarly with Get:
  scala> Get[Foo]
And find the missing instance and construct it as needed. Refer to Chapter 12
of the book of doobie for more information.
      .query[Test]
4

1 回答 1

0

自定义映射不是特定于 Oracle,它们用于为我们的自定义类型定义映射,如下所示是我们的案例类:

final case class PersonId(id: Int) extends AnyVal
final case class PersonDetail(personId: PersonId, name: String)

根据上面的案例类,当我们通过 doobie 查询表并想用案例类映射详细信息时,例如:

sql"""select id, name from person_detail".query[PersonDetail].to[List]

在这种情况下,我们需要为PersonId案例类定义自定义映射并在范围内提供该映射。有不同的方法来定义映射,如GetPutMeta。就我个人而言,我认为Meta这是定义如下映射的最简单reader方法writer

implicit val personIdMapping: Meta[PersonId] = Meta[Int].imap(PersonId.apply)(_.id)

我知道答案很晚,但我希望这会给我一些想法,对我来说只是知识的一部分。

于 2020-07-24T20:33:29.180 回答