Browse Source

server: Improve the ColumnNameResolver

In order to get deterministic results while retrieving
paginated results, the getUniqueColumnName method has been
introduced, when sorting by a non-unique column, we'll break
ties using the unique column.
prometheus-integration
Alexis Hernandez 7 years ago
parent
commit
0d5e37b040
  1. 8
      server/app/com/xsn/explorer/data/anorm/interpreters/ColumnNameResolver.scala
  2. 7
      server/app/com/xsn/explorer/data/anorm/interpreters/FieldOrderingSQLInterpreter.scala
  3. 11
      server/app/com/xsn/explorer/models/fields/BalanceField.scala

8
server/app/com/xsn/explorer/data/anorm/interpreters/ColumnNameResolver.scala

@ -7,5 +7,13 @@ import scala.annotation.implicitNotFound
)
trait ColumnNameResolver[A] {
/**
* Maps a field to the column name on the sql schema.
*/
def getColumnName(field: A): String
/**
* This is used to break ties while sorting by non-unique fields,
*/
def getUniqueColumnName: String
}

7
server/app/com/xsn/explorer/data/anorm/interpreters/FieldOrderingSQLInterpreter.scala

@ -7,8 +7,13 @@ class FieldOrderingSQLInterpreter {
def toOrderByClause[A](fieldOrdering: FieldOrdering[A])(implicit columnNameResolver: ColumnNameResolver[A]) = {
val field = columnNameResolver.getColumnName(fieldOrdering.field)
val condition = getCondition(fieldOrdering.orderingCondition)
val uniqueField = columnNameResolver.getUniqueColumnName
s"ORDER BY $field $condition"
if (field == uniqueField) {
s"ORDER BY $field $condition"
} else {
s"ORDER BY $field $condition, $uniqueField"
}
}
private def getCondition(ordering: OrderingCondition) = ordering match {

11
server/app/com/xsn/explorer/models/fields/BalanceField.scala

@ -19,8 +19,13 @@ object BalanceField {
case _ => None
}
implicit val columnNameResolver: ColumnNameResolver[BalanceField] = (field) => field match {
case Available => s"(${Received.string} - ${Spent.string})"
case f => f.string
implicit val columnNameResolver: ColumnNameResolver[BalanceField] = new ColumnNameResolver[BalanceField] {
override def getUniqueColumnName: String = Address.string
override def getColumnName(field: BalanceField): String = field match {
case Available => s"(${Received.string} - ${Spent.string})"
case f => f.string
}
}
}

Loading…
Cancel
Save