我有一個DAO輔助特徵,可提供通用功能的DAO。它需要能夠訪問表查詢並運行操作。我無法定義或以其他方式將查詢類型提供給助手特徵。上界爲油滑3.1.1查詢類型
下面是一些代碼,在很短的demo project on GitHub也可在action
分支。
trait DBComponent {
import slick.driver.JdbcProfile
val driver: JdbcProfile
import driver.api._
val db: Database
}
的類被保留延長HasId
:
trait HasId {
def id: Option[Int] = None
}
這裏是一個這樣的類被保留:
case class BankInfo(
owner: String,
branches: Int,
bankId: Int,
override val id: Option[Int] = None
) extends HasId
問題 首先,db
在性狀DBComponent
定義是我不知道如何設置QueryType
在以下DAO幫手特質;我預計大多數後面的錯誤是不當類型的結果,我用:
/** Handles all actions pertaining to HasId or that do not require parameters */
trait DbAction[T <: HasId] { this: DBComponent =>
import driver.api._ // defines DBIOAction
type QueryType <: slick.lifted.TableQuery[Table[T]] // this type is wrong! What should it be?
def tableQuery: QueryType
// Is this defined correctly?
def run[R](action: DBIOAction[R, NoStream, Nothing]): Future[R] = db.run { action }
def deleteById(id: Option[Long]): Unit =
for { i <- id } run { tableQuery.filter(_.id === id).delete } // id is unknown because QueryType is wrong
def findAll: Future[List[T]] = run { tableQuery.to[List].result } // also b0rked
// Remaining methods shown on GitHub
}
僅供參考,這裏是如何在上述將被使用。首先,定義表查詢的特點:
trait BankInfoTable extends BankTable { this: DBComponent =>
import driver.api._
class BankInfoTable(tag: Tag) extends Table[BankInfo](tag, "bankinfo") {
val id = column[Int]("id", O.PrimaryKey, O.AutoInc)
val owner = column[String]("owner")
val bankId = column[Int]("bank_id")
val branches = column[Int]("branches")
def bankFK = foreignKey("bank_product_fk", bankId, bankTableQuery)(_.id)
def * = (owner, branches, bankId, id.?) <> (BankInfo.tupled, BankInfo.unapply)
}
val tableQuery = TableQuery[BankInfoTable]
def autoInc = tableQuery returning tableQuery.map(_.id)
}
這一切都在這裏匯聚:
trait BankInfoRepositoryLike extends BankInfoTable with DbAction[BankInfo]
{ this: DBComponent =>
import driver.api._
@inline def updateAsync(bankInfo: BankInfo): Future[Int] =
run { tableQuery.filter(_.id === bankInfo.id.get).update(bankInfo) }
@inline def getByIdAsync(id: Int): Future[Option[BankInfo]] =
run { tableQuery.filter(_.id === id).result.headOption }
}
建議?
看看[這個例子](https://gist.github.com/lshoo/9785645) – Sky
看起來很有趣。我想嘗試一下,但現在我已經離開了我的電腦。任何機會,你可以分叉我的項目,並顯示你的代碼? –
現在,我看對了一下代碼,我覺得這是油滑2.1.x的代碼,也不會用油滑3.1.x.工作使用Slick 2.1.x更簡單 - 我有類似的東西,然後工作。這是一個Slick 3.1.x問題,不幸的是 –