0

I'm currently sorting rows in Squeryl (0.9.9) on various field using this quite terrible pattern matching:

sortBy match {
  case Some("checkId") => orderBy.getOrElse("DESC") match {
    case "ASC" => where(whereClause(chk)) select(chk) orderBy (chk.checkId asc)
    case _ => where(whereClause(chk)) select(chk) orderBy (chk.checkId desc)
  }
  case Some("checkName") => orderBy.getOrElse("DESC") match {
    case "ASC" => where(whereClause(chk)) select(chk) orderBy (chk.checkName asc)
    case _ => where(whereClause(chk)) select(chk) orderBy (chk.checkName desc)
  }
}

I need to reuse the orderBypart in another query but I can't find a way to isolate it.

If I attempt to use something like:

def ord = orderBy(chk.checkId desc)

I get a syntax error.

UPDATE: to be more clear on what I'm trying todo:

I could define this 3 functions:

 def desc(node: ExpressionNode):ExpressionNode = new OrderByArg(node) {desc}
  def asc(node: ExpressionNode):ExpressionNode = new OrderByArg(node) {desc}

  def selectSorted2(chk: CheckResultsItem,  sortBy: Option[String], orderBy: Option[String]):ExpressionNode =
    sortBy match {
      case Some("checkId") => orderBy.getOrElse("DESC") match {
        case "ASC" => asc(chk.checkId )
        case _ => desc (chk.checkId )
      }
      case Some("checkName") =>     orderBy.getOrElse("DESC") match {
        case "ASC" =>asc (chk.checkName )
        case _ =>desc (chk.checkName )
      }

but when I try to use them :

  val res = from(checkResultsTable)(chk => {
        where(chk.checkId === checkId) select(chk)  model.CheckResultsItem.selectSorted2(chk, sortBy, orderBy )
      }).page(offset_and_limit._1, offset_and_limit._2)

I always get cannot resolve symbol "selectSorted2" even if is defined, imported and visible.

maborg
  • 435
  • 5
  • 24

1 Answers1

1

The orderBy operator isn't a thing that can be returned. It is a method defined in the trait org.squeryl.dsl.boilerplate.OrderBySignatures. You'll need to define your own method to accept an object of type OrderBySignatures (or a type that extends it, in this case I believe it's SelectState) and call the .orderBy method on that object. If you want your method to function like a Squeryl operator, you'll need an implicit conversion from SelectState -> YouModifiedSelectStateThatContainsYourMethod

Dave Whittaker
  • 3,102
  • 13
  • 14