diff --git a/server/app/com/xsn/explorer/data/anorm/interpreters/ColumnNameResolver.scala b/server/app/com/xsn/explorer/data/anorm/interpreters/ColumnNameResolver.scala index 7723d09..169099c 100644 --- a/server/app/com/xsn/explorer/data/anorm/interpreters/ColumnNameResolver.scala +++ b/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 } diff --git a/server/app/com/xsn/explorer/data/anorm/interpreters/FieldOrderingSQLInterpreter.scala b/server/app/com/xsn/explorer/data/anorm/interpreters/FieldOrderingSQLInterpreter.scala index c25b15e..162d7ca 100644 --- a/server/app/com/xsn/explorer/data/anorm/interpreters/FieldOrderingSQLInterpreter.scala +++ b/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 { diff --git a/server/app/com/xsn/explorer/models/fields/BalanceField.scala b/server/app/com/xsn/explorer/models/fields/BalanceField.scala index 97345c7..939eac4 100644 --- a/server/app/com/xsn/explorer/models/fields/BalanceField.scala +++ b/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 + } } }