簡體   English   中英

使用PlaySlick和slick-pg將一系列時間戳范圍存儲到PostgreSQL中

[英]Storing an array of ranges of timestamps into postgresql using PlaySlick and slick-pg

我正在嘗試將時間戳范圍列表存儲到PostgreSQL表的“可用”列中。 我正在使用slick-pg來提供幫助。 對於表數據,我有:

create table users (
    id text NOT NULL PRIMARY KEY,
    action text NOT NULL,
    scheduled timestamptz,
    available tstzrange[]
);

在我的DAO中,我有:

private class UsersTable(tag: Tag) extends Table[User](tag, "users") {

    def id = column[String]("id", O.PrimaryKey)
    def action = column[String]("action")
    def timestamp = column[Option[Timestamp]]("scheduled")
    def available = column[Option[List[com.github.tminglei.slickpg.Range[Timestamp]]]]("available")

    def * = (id, action, timestamp, available) <> (User.tupled, User.unapply _)
}

以及與該表關聯的案例類:

case class User(id: String, action: String, timestamp: Option[Timestamp] = None, available:Option[List[com.github.tminglei.slickpg.Range[Timestamp]]] = None)

我知道我缺少一個隱式的東西,類似於本示例文件中的隱式的東西。 但是我仍然對Scala還是陌生的,並且堅持到底如何定義它。

在類中,您正在執行一些DAO訪問,如下注入DatabaseConfigProvider

class Class @Inject() (protected val dbConfigProvider: DatabaseConfigProvider)

並使其extends HasDatabaseConfigProvider[MyPostgresDriver]

您必須導入:(數據庫是包的名稱,其中是MyPostgresDriver文件)

import database.MyPostgresDriver.api._
import database.MyPostgresDriver
import play.api.db.slick.{DatabaseConfigProvider, HasDatabaseConfigProvider}

這是MyPostgresDriver文件:

package database

import com.github.tminglei.slickpg._

trait MyPostgresDriver extends ExPostgresDriver
    with PgArraySupport
    with PgDate2Support
    with PgPlayJsonSupport
    with PgNetSupport
    with PgLTreeSupport
    with PgRangeSupport
    with PgHStoreSupport
    with PgSearchSupport
    with PgPostGISSupport {

  override val pgjson = "jsonb"
  override lazy val Implicit = new ImplicitsPlus {}
  override val simple = new SimpleQLPlus {}

  trait ImplicitsPlus extends Implicits
  with ArrayImplicits
  with DateTimeImplicits
  with RangeImplicits
  with HStoreImplicits
  with JsonImplicits
  with SearchImplicits
  with PostGISImplicits

  trait SimpleQLPlus extends SimpleQL
  with ImplicitsPlus
  with SearchAssistants
  with PostGISAssistants

  override val api = new API with ArrayImplicits
    with DateTimeImplicits
    with PlayJsonImplicits
    with NetImplicits
    with LTreeImplicits
    with RangeImplicits
    with HStoreImplicits
    with SearchImplicits
    with PostGISImplicits
    with SearchAssistants {}
}

object MyPostgresDriver extends MyPostgresDriver

(僅選擇所需的隱式)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM