如何在 Doobie (Postgres) 中 read/write 时间戳
How to read/write Timestamp in Doobie (Postgres)
如何在 Doobie 中 read/write 时间戳?
我有一条包含时间戳字段的记录 class。当我尝试将它写入数据库或使用 doobie 读取它时,出现错误 Cannot find or construct a Read instance for type
.
case class ExampleRecord(data: String, created_at: Timestamp)
val create = sql"create table if not exists example_ts (data TEXT NOT NULL, created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP)".update.run
val insert = Update[ExampleRecord]("insert into example_ts (data, created_at) values (?, ?)")
.updateMany(List(
ExampleRecord("one", Timestamp.valueOf(LocalDateTime.now())),
ExampleRecord("two", Timestamp.valueOf(LocalDateTime.now()))
))
val select = sql"select data, created_at from example_ts".query[ExampleRecord].stream
val app = for {
_ <- create.transact(xa).compile.drain
_ <- insert.transact(xa).compile.drain
_ <- select.transact(xa).compile.drain
} yield ()
app.unsafeRunSync()
您需要导入 doobie.implicits.javasql._
和 doobie.implicits.javatime._
release notes。这是一个完整的应用示例 reading/writing 带有 doobie 的时间戳。
// sbt
// "org.tpolecat" %% "doobie-core" % "0.8.8",
// "org.tpolecat" %% "doobie-postgres" % "0.8.8"
import java.sql.Timestamp
import java.time.LocalDateTime
import doobie._
import doobie.implicits._
import doobie.implicits.javasql._
import doobie.postgres._
import doobie.postgres.implicits._
import doobie.postgres.pgisimplicits._
import cats._
import cats.implicits._
import cats.effect._
import cats.effect.implicits._
case class ExampleRecord(data: String, created_at: Timestamp)
object Example extends IOApp {
override def run(args: List[String]): IO[ExitCode] = {
val xa = Transactor.fromDriverManager[IO](
"org.postgresql.Driver", // driver classname
"jdbc:postgresql:example_db", // connect URL (driver-specific)
"postgres", // user
"" // password
)
val drop = sql"drop table if exists example_ts".update.run
val create =
sql"create table if not exists example_ts (data TEXT NOT NULL, created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP)".update.run
val insert = Update[ExampleRecord]("insert into example_ts (data, created_at) values (?, ?)")
.updateMany(List(
ExampleRecord("one", Timestamp.valueOf(LocalDateTime.now())),
ExampleRecord("two", Timestamp.valueOf(LocalDateTime.now()))
))
val setup = for {
_ <- drop.transact(xa)
_ <- create.transact(xa)
_ <- insert.transact(xa)
} yield ()
val select =
sql"select data, created_at from example_ts".query[ExampleRecord].stream.transact(xa)
val output = select.evalTap { record =>
IO(println(record))
}.compile.drain
for {
_ <- setup
_ <- output
} yield ExitCode.Success
}
}
如何在 Doobie 中 read/write 时间戳?
我有一条包含时间戳字段的记录 class。当我尝试将它写入数据库或使用 doobie 读取它时,出现错误 Cannot find or construct a Read instance for type
.
case class ExampleRecord(data: String, created_at: Timestamp)
val create = sql"create table if not exists example_ts (data TEXT NOT NULL, created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP)".update.run
val insert = Update[ExampleRecord]("insert into example_ts (data, created_at) values (?, ?)")
.updateMany(List(
ExampleRecord("one", Timestamp.valueOf(LocalDateTime.now())),
ExampleRecord("two", Timestamp.valueOf(LocalDateTime.now()))
))
val select = sql"select data, created_at from example_ts".query[ExampleRecord].stream
val app = for {
_ <- create.transact(xa).compile.drain
_ <- insert.transact(xa).compile.drain
_ <- select.transact(xa).compile.drain
} yield ()
app.unsafeRunSync()
您需要导入 doobie.implicits.javasql._
和 doobie.implicits.javatime._
release notes。这是一个完整的应用示例 reading/writing 带有 doobie 的时间戳。
// sbt
// "org.tpolecat" %% "doobie-core" % "0.8.8",
// "org.tpolecat" %% "doobie-postgres" % "0.8.8"
import java.sql.Timestamp
import java.time.LocalDateTime
import doobie._
import doobie.implicits._
import doobie.implicits.javasql._
import doobie.postgres._
import doobie.postgres.implicits._
import doobie.postgres.pgisimplicits._
import cats._
import cats.implicits._
import cats.effect._
import cats.effect.implicits._
case class ExampleRecord(data: String, created_at: Timestamp)
object Example extends IOApp {
override def run(args: List[String]): IO[ExitCode] = {
val xa = Transactor.fromDriverManager[IO](
"org.postgresql.Driver", // driver classname
"jdbc:postgresql:example_db", // connect URL (driver-specific)
"postgres", // user
"" // password
)
val drop = sql"drop table if exists example_ts".update.run
val create =
sql"create table if not exists example_ts (data TEXT NOT NULL, created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP)".update.run
val insert = Update[ExampleRecord]("insert into example_ts (data, created_at) values (?, ?)")
.updateMany(List(
ExampleRecord("one", Timestamp.valueOf(LocalDateTime.now())),
ExampleRecord("two", Timestamp.valueOf(LocalDateTime.now()))
))
val setup = for {
_ <- drop.transact(xa)
_ <- create.transact(xa)
_ <- insert.transact(xa)
} yield ()
val select =
sql"select data, created_at from example_ts".query[ExampleRecord].stream.transact(xa)
val output = select.evalTap { record =>
IO(println(record))
}.compile.drain
for {
_ <- setup
_ <- output
} yield ExitCode.Success
}
}