1

我正在使用 Scala Slick-3.1.0 lib

如何制作一个通用的 Slick 过滤器函数,它将 TableQuery 实例作为输入并对其进行相同的 slick 过滤器?

我有几个案例类(例如两个)代表存储在数据库中的数据。有些字段是相同的,因此类可能会扩展共同的祖先:

case class FirstData(symbol: String,
                     date: Timestamp,
                     firstField: Double) 

case class SecondData(symbol: String,
                      date: Timestamp,
                      secondField: String) 

它们中的每一个在 DB 中都有自己的 SQL 表,并由单独的 SlickTable类表示。他们也有相同的主键:

class FirstDataTable(tag: Tag) extends Table[FirstData](tag, "firstData") {
    def symbol = column[String]("symbol")
    def date = column[Timestamp]("date")
    def firstField= column[Double]("firstField")
    def * = (symbol, date, firstField) <> ((FirstData.apply _).tupled, FirstData.unapply)
    def pk = primaryKey("pk_firstData", (symbol, date))
}

class SecondDataTable(tag: Tag) extends Table[SecondData](tag, "secondData"){
    def symbol = column[String]("symbol")
    def date = column[Timestamp]("date")
    def secondField= column[String]("secondField")
    def * = (symbol, date, secondField) <> ((SecondData.apply _).tupled, SecondData.unapply)
    def pk = primaryKey("pk_secondData", (symbol, date))
}

最后TableQuery的类是:

val firstDataTableQuery = TableQuery[FirstDataTable]
val secondDataTableQuery = TableQuery[SecondDataTable]
etc ...

如何制作一个通用的 Slick 过滤器查询函数,该函数将 firstDataTableQueryorsecondDataTableQuery作为参数并在输入上进行相同的 slick 查询。仅过滤它们的公共字段或以另一种方式对它们的 SQL 表表示公共列进行过滤。例如像这样:

def filter(genericTableQuery: TableQuery) = {
    genericTableQuery.filter { data => dataFilterFunction(data.symbol)
    }.filter(_.date >= someDate).sortBy(data => data.date.asc)
}

val firstTableResult = filter(firstDataTableQuery)
val seconfTableResult = filter(secondDataTableQuery)
etc ...

我查看了这个主题,但仍然无法提出解决方案:

Slick 3 可重用的通用存储库

Scala 反射实例化 scala.slick.lifted.TableQuery

4

2 回答 2

0

invariant, thanks a lot. You pointed me to the right direction. I slightly modified your answer and everything works )

Traits:

trait Data {
            def symbol: String
            def date: Timestamp
            }

trait GenericDataTable[T <: Data] extends Table[T] {
            def symbol: Rep[String]
            def date: Rep[Timestamp]
            def pk: PrimaryKey
            }

FirstData and FirstDataTable classes look like this:

case class FirstData(
            symbol: String,
            date: Timestamp,
            firstField: Double) extends Data

class FirstDataTable(tag: Tag) extends Table[FirstData(tag,"firstData") 
    with GenericDataTable[FirstData] {
            def symbol = column[String]("symbol")
            def date = column[Timestamp]("date")
            def firstField= column[Double]("firstField")
            def * = (symbol, date, firstField) <> ((FirstData.apply _).tupled, FirstData.unapply)
            def pk = primaryKey("pk_firstData", (symbol, date))
    }

Finally function looks like this:

private def filter[M <: Data, T <: GenericDataTable[M]] (genericTableQuery: TableQuery[T]) = {
            genericTableQuery.filter { data => dataFilterFunction(data.symbol)}.
            filter(_.date >= someDate).sortBy(data => data.date.asc)
    }
于 2016-01-13T12:16:57.213 回答
0

如何让数据表类扩展一个公共特征,进而扩展数据类的一个公共特征,例如:

trait Data {
  def symbol: String
  def date: Timestamp
}
// make FirstData and SecondData extend the above trait

trait GenericDataTable extends Data {
  def symbol: Rep[String]
  def date: Rep[Timestamp]
  def pk: PrimaryKey
}


class FirstDataTable(tag: Tag) extends Table[FirstData](tag, "firstData") with GenericDataTable {
// ...

接着:

def filter[T <: GenericDataTable](genericTableQuery: TableQuery[T]) = // ...
于 2016-01-12T23:22:51.250 回答