मैं कॉमन्स सीआरयूडी और अन्य प्रकार के संचालन के लिए सामान्य विशेषता को कार्यान्वित करने के तरीके के बारे में देख रहा हूं, मैंने this और this पर देखा और निर्दिष्ट विधि अच्छी तरह से काम कर रही है।Slick 2.0 जेनेरिक सीआरयूडी ऑपरेशंस
object CampaignModel {
val campaigns = TableQuery[Campaign]
def insert(campaign: CampaignRow)(implicit s: Session) = {
campaigns.insert(campaign)
}
}
क्या मैं अब तक की कोशिश की, लिंक पर पहले, था:
मैं करना चाहते हैं क्या प्रविष्टि के लिए एक सामान्य तरीका है, मेरी कक्षा पल (गैर सामान्य कार्यान्वयन) में इस तरह दिखता है इस (सामान्य कार्यान्वयन):
trait PostgresGeneric[T <: Table[A], A] {
val tableReference = TableQuery[T]
def insertGeneric(row: ? What type goes here ?)(implicit s: Session) = tableReference.insert(row)
}
जब मैं insert
विधि का निरीक्षण यह सही प्रकार की तरह लग रहा T#TableElementType
होना चाहिए लेकिन मेरी जानकारी के बहुत बुनियादी है और मैं प्रकार के आसपास मेरे सिर लपेटो नहीं कर सकते, मैं T
की कोशिश की और A
और कंपाइलर का कहना है कि क्लैस्स्टइप किसी के गुण के अनुरूप नहीं है।
import scala.slick.driver.PostgresDriver
import scala.slick.driver.PostgresDriver.simple._
import path.to.RichTable
trait PostgresGeneric[T <: RichTable[A], A] {
val tableReference: TableQuery[T]
def insert(row: T#TableElementType)(implicit s: Session) =
tableReference.insert(row)
def insertAndGetId(row: T#TableElementType)(implicit s: Session) =
(tableReference returning tableReference.map(_.id)) += row
def deleteById(id: Long)(implicit s: Session): Boolean =
tableReference.filter(_.id === id).delete == 1
def updateById(id: Long, row: T#TableElementType)(implicit s: Session): Boolean =
tableReference.filter(_.id === id).update(row) == 1
def selectById(id: Long)(implicit s: Session): Option[T#TableElementType] =
tableReference.filter(_.id === id).firstOption
def existsById(id: Long)(implicit s: Session): Boolean = {
(for {
row <- tableReference
if row.id === id
} yield row).firstOption.isDefined
}
}
कहाँ RichTable
एक अमूर्त वर्ग है:
अन्य infos, टेबल चालाक तालिका पीढ़ी उपकरण
case class CampaignRow(id: Long, name: Option[String])
/** Table description of table campaign. Objects of this class serve as prototypes for rows in queries. */
class Campaign(tag: Tag) extends Table[CampaignRow](tag, "campaign") {
def * = (id, name) <>(CampaignRow.tupled, CampaignRow.unapply)
/** Maps whole row to an option. Useful for outer joins. */
def ? = (id.?, name).shaped.<>({
r => import r._; _1.map(_ => CampaignRow.tupled((_1.get, _2)))
}, (_: Any) => throw new Exception("Inserting into ? projection not supported."))
/** Database column id AutoInc, PrimaryKey */
val id: Column[Long] = column[Long]("id", O.AutoInc, O.PrimaryKey)
/** Database column name */
val name: Column[Option[String]] = column[Option[String]]("name")
}