2014-04-28 7 views
6

मैं कॉमन्स सीआरयूडी और अन्य प्रकार के संचालन के लिए सामान्य विशेषता को कार्यान्वित करने के तरीके के बारे में देख रहा हूं, मैंने 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") 
} 

उत्तर

11

मैं यह काम कर करने में कामयाब साथ उत्पन्न कर रहे हैं, यह मेरा सामान्य विशेषता है एक आईडी फ़ील्ड के साथ, यह ऊपरी बाध्य बाधा के साथ T#TableElementType के आईडी फ़ील्ड को प्राप्त करने के लिए उपयोगी है (अधिक जानकारी के लिए this देखें):

import scala.slick.driver.PostgresDriver.simple._ 
import scala.slick.jdbc.{GetResult => GR} 

abstract class RichTable[T](tag: Tag, name: String) extends Table[T](tag, name) { 
    val id: Column[Long] = column[Long]("id", O.PrimaryKey, O.AutoInc) 
} 

और मेरे अभियान तालिका अब इस तरह दिखता है:

import scala.slick.driver.PostgresDriver.simple._ 
import scala.slick.jdbc.{GetResult => GR} 
import scala.slick.lifted.TableQuery 

case class CampaignRow(id: Long, name: Option[String]) 

class Campaign(tag: Tag) extends RichTable[CampaignRow](tag, "campaign") { 
    def * = (id, name) <>(CampaignRow.tupled, CampaignRow.unapply) 

    def ? = (id.?, name).shaped.<>({ 
    r => import r._; _1.map(_ => CampaignRow.tupled((_1.get, _2))) 
    }, (_: Any) => throw new Exception("Inserting into ? projection not supported.")) 

    override val id: Column[Long] = column[Long]("id", O.AutoInc, O.PrimaryKey) 
    val name: Column[Option[String]] = column[Option[String]]("name") 
} 

मॉडल सामान्य विशेषता को लागू इस तरह दिखता है:

object CampaignModel extends PostgresGeneric[Campaign, CampaignRow] { 

    override val tableReference: PostgresDriver.simple.TableQuery[Tables.Campaign] = 
    TableQuery[Campaign] 

    def insertCampaign(row: CampaignRow) = { 
    insert(CampaignRow(0, "test")) 
    } 
} 
संबंधित मुद्दे