Permalink
Browse files

Added support for ordering

  • Loading branch information...
1 parent d8500fb commit 10feab856dcec851169abdfd97b9e566d5fe43ab @teozkr committed May 4, 2012
Showing with 30 additions and 12 deletions.
  1. +12 −5 nina-play/src/main/scala/MySQL.scala
  2. +2 −2 nina/src/main/scala/Executor.scala
  3. +16 −5 nina/src/main/scala/Query.scala
@@ -42,14 +42,21 @@ package mysql {
.executeUpdate() > 0
)
- def count(table: String, filters: Seq[Filter[_, _]])(implicit conn: Connection): Long = getOne(table, filters, Seq("count(*)")).get("count(*)").asInstanceOf[Option[Long]].get
- def getOne(table: String, filters: Seq[Filter[_, _]], columns: Seq[String])(implicit conn: Connection): Option[Map[String, Option[Any]]] = {
- getMultiple(table, filters, columns, 1).headOption
+ def count(table: String, filters: Seq[Filter[_, _]])(implicit conn: Connection): Long = getOne(table, filters, Seq("count(*)"), None).get("count(*)").asInstanceOf[Option[Long]].get
+ def getOne(table: String, filters: Seq[Filter[_, _]], columns: Seq[String], ordering: Option[(String, OrderDirection)])(implicit conn: Connection): Option[Map[String, Option[Any]]] = {
+ getMultiple(table, filters, columns, ordering, 1).headOption
}
- def getMultiple(table: String, filters: Seq[Filter[_, _]], columns: Seq[String], amount: Long)(implicit conn: Connection): Seq[Map[String, Option[Any]]] = {
+ def getMultiple(table: String, filters: Seq[Filter[_, _]], columns: Seq[String], ordering: Option[(String, OrderDirection)], amount: Long)(implicit conn: Connection): Seq[Map[String, Option[Any]]] = {
val limitStr = if (amount == -1) ""
else " LIMIT "+amount
- val sql = "SELECT "+columns.mkString(",")+" FROM "+table+serializeFilter(filters)+limitStr
+ val orderStr = ordering match {
+ case None => ""
+ case Some((col, dir)) => " ORDER BY "+col+" "+(dir match {
+ case Ascending => "ASC"
+ case Descending => "DESC"
+ })
+ }
+ val sql = "SELECT "+columns.mkString(",")+" FROM "+table+serializeFilter(filters)+orderStr+limitStr
val rows = SQL(sql).on(filters.map(_ match {
case filter: Filter[_, _] => filter.kind.toString+"_"+filter.column.name -> ParameterValue(filter.other, ninaSetterToStatement)
}): _*)()
@@ -7,6 +7,6 @@ trait NinaExecutor {
def delete(table: String, filters: Seq[Filter[_, _]])(implicit conn: Connection): Int
def insert(table: String, data: Map[String, Any])(implicit conn: Connection): Boolean
def count(table: String, filters: Seq[Filter[_, _]])(implicit conn: Connection): Long
- def getOne(table: String, filters: Seq[Filter[_, _]], columns: Seq[String])(implicit conn: Connection): Option[Map[String, Option[Any]]]
- def getMultiple(table: String, filters: Seq[Filter[_, _]], columns: Seq[String], amount: Long = -1)(implicit conn: Connection): Seq[Map[String, Option[Any]]]
+ def getOne(table: String, filters: Seq[Filter[_, _]], columns: Seq[String], ordering: Option[(String, OrderDirection)])(implicit conn: Connection): Option[Map[String, Option[Any]]]
+ def getMultiple(table: String, filters: Seq[Filter[_, _]], columns: Seq[String], ordering: Option[(String, OrderDirection)], amount: Long = -1)(implicit conn: Connection): Seq[Map[String, Option[Any]]]
}
@@ -23,7 +23,7 @@ case class Query[T <: Table](table: T, filters: Seq[Filter[_, T]]) {
Query(table, filters :+ Filter(col, kind, other))
}
- def get[A](cols: table.Columns[A]) = GetQuery(this, cols)
+ def get[A](cols: table.Columns[A]) = GetQuery(this, cols, None)
/**
* @returns The amount of rows affected
*/
@@ -38,8 +38,19 @@ case class Query[T <: Table](table: T, filters: Seq[Filter[_, T]]) {
}
}
-case class GetQuery[A, T <: Table](query: Query[T], cols: T#Columns[A]) {
- def single()(implicit conn: Connection): Option[A] = query.table.executor.getOne(query.table.tableName, query.filters, cols.columnNames).map(cols.bindFromMap(_).value)
- def take(amount: Long)(implicit conn: Connection): Seq[A] = query.table.executor.getMultiple(query.table.tableName, query.filters, cols.columnNames, amount).map(cols.bindFromMap(_).value)
- def all()(implicit conn: Connection): Seq[A] = query.table.executor.getMultiple(query.table.tableName, query.filters, cols.columnNames).map(cols.bindFromMap(_).value)
+sealed trait OrderDirection
+case object Ascending extends OrderDirection
+case object Descending extends OrderDirection
+
+case class GetQuery[A, T <: Table](query: Query[T], cols: T#Columns[A], ordering: Option[(T#Column[_], OrderDirection)]) {
+ private val stringifiedOrdering = ordering match {
+ case None => None
+ case Some((col, dir)) => Some((col.name, dir))
+ }
+
+ def order(column: query.table.Column[_], direction: OrderDirection) = GetQuery(query, cols, Some((column, direction)))
+
+ def single()(implicit conn: Connection): Option[A] = query.table.executor.getOne(query.table.tableName, query.filters, cols.columnNames, stringifiedOrdering).map(cols.bindFromMap(_).value)
+ def take(amount: Long)(implicit conn: Connection): Seq[A] = query.table.executor.getMultiple(query.table.tableName, query.filters, cols.columnNames, stringifiedOrdering, amount).map(cols.bindFromMap(_).value)
+ def all()(implicit conn: Connection): Seq[A] = query.table.executor.getMultiple(query.table.tableName, query.filters, cols.columnNames, stringifiedOrdering).map(cols.bindFromMap(_).value)
}

0 comments on commit 10feab8

Please sign in to comment.