2016-05-17 89 views
2

我試圖動態地包括一個sortBy到我的查詢,它根據查詢參數的字符串名稱進行排序。在Slick 3中,這已被證明非常棘手。目前,我的設置是:動態sortwith與油滑3

trait Model { 
    type ATable <: AbstractTable[_] 

    def tableQuery: TableQuery[ATable] 

    def sortMap: Map[String, Rep[_]] 

    private def sortKey[T](e: ATable, sort: (String, SortOrder)): ColumnOrdered[_] = sort match { 
     case (field, SortOrder.Asc) => ColumnOrdered(sortMap.getOrElse(field, throw new ClientException(s"Can't sort by $field")), Ordering(Ordering.Asc)) 
     case (field, SortOrder.Desc) => ColumnOrdered(sortMap.getOrElse(field, throw new ClientException(s"Can't sort by $field")), Ordering(Ordering.Desc)) 
    } 
    def all(sort: (String, SortOrder)) = tableQuery.sortBy(sortKey(_, sort)).result 
} 

object User extends Model { 
    type ATable = Tables.User 
    val tableQuery = Tables.User 

    val sortMap = Map("id" -> tableQuery.id) 
} 

但運行db.run(User.all(("id", SortOrder.Asc))引發以下錯誤:

slick.SlickException: No type for symbol name found in Vector[t2<@t3<UnassignedType>>] 

有誰知道一個更好的解決方案,或者我要去哪裏錯了嗎?

回答

0

我解決這個問題的方法是改變我sortMap包含與列類型隱式的(從Map[String, Rep[_]]Map[String, ATable => Rep[_]])表類型:

trait Model { 
    type ATable <: AbstractTable[_] 

    def tableQuery: TableQuery[ATable] 

    def sortMap: Map[String, ATable => Rep[_]] 

    private def sortKey(baseQ: Query[ATable, ATable#TableElementType, Seq], sort: (String, SortOrder)) = { 
     val rep = sortMap.getOrElse(sort._1, throw new ClientException(s"Can't sort by ${sort._1}")) 
     val orderedRep = sort._2 match { 
      case SortOrder.Asc => (t: ATable) => ColumnOrdered(rep(t), slick.ast.Ordering(slick.ast.Ordering.Asc)) 
      case SortOrder.Desc => (t: ATable) => ColumnOrdered(rep(t), slick.ast.Ordering(slick.ast.Ordering.Desc)) 
     } 
     q.sortBy(orderedRep) 
    } 

    def all(sort: (String, SortOrder)) = sortKey(tableQuery, sort).result 
} 

object User extends Model { 
    type ATable = Tables.User 
    val tableQuery = Tables.User 

    val sortMap = Map("id" -> { (t: ATable) => t.id }) 
} 

這種解決方案的唯一不理想的方面是,如果將新列被添加到數據庫,它必須手動添加到sortKey。我正在研究將這些映射包含在表代碼生成腳本中。

0

你只需要在代類似

val columns = Map("id" -> { (t: User) => t.id }) 

包括和使用它像這樣:

class UserDAO{ 
//... 
baseQuery = filterContext.sort.foldLeft[Query[User, UserRow, Seq]](User) ((_, s) => UserColumnFilter.getSort(s,User.baseTableRow.columns)); 
//... 
} 

case class Sort(columnName:String,value: String) {} 

case class FilterContext(filters:List[Filter],page:Int,pageSize:Int,sort:List[Sort]) 
    object UserColumnSort extends ColumnSort[User,UserRow](User) 
    class ColumnSort[T<:Table[C],C](query:TableQuery[T]) 
    {  

     def getSort(s:Sort,columns:Map[String, T => Rep[_]]):Query[T, C, Seq] = { 
     val aux = columns.getOrElse(s.columnName, throw new Exception(s"Can't sort by ${s.columnName}")); 
      val orderedRep = s.value match { 
       case "asc" => (t: T) => ColumnOrdered(aux(t), slick.ast.Ordering(slick.ast.Ordering.Asc)) 
       case _ => (t: T) => ColumnOrdered(aux(t), slick.ast.Ordering(slick.ast.Ordering.Desc)) 
      } 
      return query.sortBy(orderedRep); 

     } 
    }