Alexis Hernandez
7 years ago
2 changed files with 29 additions and 0 deletions
@ -0,0 +1,11 @@ |
|||||
|
package com.xsn.explorer.data.anorm.interpreters |
||||
|
|
||||
|
import scala.annotation.implicitNotFound |
||||
|
|
||||
|
@implicitNotFound( |
||||
|
"No column name resolver found for type ${A}. Try to implement an implicit ColumnNameResolver for this type." |
||||
|
) |
||||
|
trait ColumnNameResolver[A] { |
||||
|
|
||||
|
def getColumnName(field: A): String |
||||
|
} |
@ -0,0 +1,18 @@ |
|||||
|
package com.xsn.explorer.data.anorm.interpreters |
||||
|
|
||||
|
import com.xsn.explorer.models.base.{FieldOrdering, OrderingCondition} |
||||
|
|
||||
|
class FieldOrderingSQLInterpreter { |
||||
|
|
||||
|
def toOrderByClause[A](fieldOrdering: FieldOrdering[A])(implicit columnNameResolver: ColumnNameResolver[A]) = { |
||||
|
val field = columnNameResolver.getColumnName(fieldOrdering.field) |
||||
|
val condition = getCondition(fieldOrdering.orderingCondition) |
||||
|
|
||||
|
s"ORDER BY $field $condition" |
||||
|
} |
||||
|
|
||||
|
private def getCondition(ordering: OrderingCondition) = ordering match { |
||||
|
case OrderingCondition.AscendingOrder => "ASC" |
||||
|
case OrderingCondition.DescendingOrder => "DESC" |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue