光滑查询浓缩表列

时间:2016-02-01 21:17:01

标签: scala playframework slick

我遇到了Slick 3在隐式查询增强中正确引用Table的问题。此代码在Slick 2中工作正常,但新表只有一个Seq[Columns],我仍然无法调用column方法。

class SlickUtils {
  implicit class QueryEnrichment[M,U<: Table[U] /*not valid */,C[_]](q: Query[U,M,C]) {

    def sortDynamic(sortString: String): Query[U,M,C] = {
      val sortKeys = sortString.split(',').toList.map(_.split('.').map(_.toUpperCase).toList)
      sortDynamicImpl(sortKeys)
    }

    private def sortDynamicImpl(sortKeys: List[Seq[String]]): Query[U,M,C] = {
      sortKeys match {
        case key :: tail =>
          sortDynamicImpl(tail).sortBy(table =>
            key match {
              case name :: Nil => table.column[String](name).desc // DOES NOT HAVE COLUMN METHOD
            }
          )
      }
    }
  }
}

1 个答案:

答案 0 :(得分:0)

我们将这个特性与光滑的3一起使用,它就像一个魅力

trait Sorting {

  implicit class QueryExtensions3[T <: Table[_], E, C[_]](val query: Query[T, E, C]) {

    def sortDynamic(sortString: String): Query[T, E, C] = {
      // split string into useful pieces
      val sortKeys = sortString.split(',').toList.map(_.split('.').toList)
      sortDynamicImpl(sortKeys)
    }

    private[this] def sortDynamicImpl(sortKeys: List[Seq[String]]): Query[T, E, C] = {
      sortKeys match {
        case key :: tail =>
          sortDynamicImpl(tail).sortBy(table =>
            key match {
              case name :: Nil           => table.column[String](name).asc
              case name :: "asc" :: Nil  => table.column[String](name).asc
              case name :: "desc" :: Nil => table.column[String](name).desc
              case o                     => throw new Exception("invalid sorting key: " + o)
            })
        case Nil => query
      }
    }
  }
}