1
votes

I'm having an issue with Slick 3 getting a proper reference to the Table in an implicit query enhancement. This code works fine in Slick 2 but the new Table only has a Seq[Columns] on which I still can't call the column method on.

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

1 Answers

0
votes

We use this trait with slick 3 and it works like a charm

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
      }
    }
  }
}