Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added the ability to remove rows, setting/deleting now returns the am…

…ount of rows affected
  • Loading branch information...
commit 2cdea63815f6ca4ea0eb84eea41191152495dc1b 1 parent 97d642a
@teozkr authored
View
9 nina-play/src/main/scala/MySQL.scala
@@ -23,13 +23,18 @@ package mysql {
case Filter(col, filter.LTE, _) => col.name+"<={LTE_"+col.name+"}"
}).mkString(" AND ")
- def update(table: String, filters: Seq[Filter[_, _]], data: Map[String, Any])(implicit conn: Connection) {
+ def update(table: String, filters: Seq[Filter[_, _]], data: Map[String, Any])(implicit conn: Connection) = {
val dataStr = if (data.isEmpty) ""
else " SET "+(data.keys.map { name => name+"={val_"+name+"}" } mkString(","))
SQL("UPDATE "+table+dataStr+serializeFilter(filters)).on((
data.toSeq.map { kv => val (key, value) = kv; "val_"+key -> toParameterValue(value) } ++
filters.toSeq.map(_ match { case filter: Filter[_, _] => filter.kind.toString+"_"+filter.column.name -> ParameterValue(filter.other, filter.setter) })
- ): _*).execute()
+ ): _*).executeUpdate()
+ }
+ def delete(table: String, filters: Seq[Filter[_, _]])(implicit conn: Connection): Int = {
+ SQL("DELETE FROM "+table+serializeFilter(filters)).on(filters.map(_ match {
+ case filter: Filter[_, _] => filter.kind.toString+"_"+filter.column.name -> ParameterValue(filter.other, filter.setter)
+ }): _*).executeUpdate()
}
def insert(table: String, data: Map[String, Any])(implicit conn: Connection): Boolean = (
SQL("INSERT INTO "+table+"("+data.keys.mkString(",")+") VALUES("+data.keys.map("{"+_.toString+"}").mkString(",")+")")
View
14 nina-play/src/test/scala/TableSpec.scala
@@ -108,5 +108,19 @@ class TableSpec extends Specification {
}
}
}
+
+ "be able to remove items" in {
+ running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
+ import Play.current
+ DB.withConnection { implicit c =>
+ anorm.SQL(TestingTable.creationSQL).execute()
+ val Some(haskellId) = TestingTable where (TestingTable.name === "Haskell") get (TestingTable.id) single()
+ val Some(id) = TestingTable where (TestingTable.name === "Java") get (TestingTable.id) single()
+ TestingTable where (TestingTable.id === id) delete()
+ TestingTable where (TestingTable.id === id) get (TestingTable.name) single() must equalTo(None)
+ TestingTable where (TestingTable.id === haskellId) get (TestingTable.name) single() must equalTo(Some("Haskell"))
+ }
+ }
+ }
}
}
View
3  nina/src/main/scala/Executor.scala
@@ -3,7 +3,8 @@ package nina
import java.sql.Connection
trait NinaExecutor {
- def update(table: String, filters: Seq[Filter[_, _]], data: Map[String, Any])(implicit conn: Connection)
+ def update(table: String, filters: Seq[Filter[_, _]], data: Map[String, Any])(implicit conn: Connection): Int
+ 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]]]
View
5 nina/src/main/scala/Query.scala
@@ -28,9 +28,12 @@ case class Query[T <: Table](table: T, filters: Seq[Filter[_, T]]) {
}
def get[A](cols: table.Columns[A]) = GetQuery(this, cols)
- def set(values: table.SingleBoundColumn[_]*)(implicit conn: Connection) {
+ def set(values: table.SingleBoundColumn[_]*)(implicit conn: Connection) = {
table.executor.update(table.tableName, filters, values.map {col => col.column.name -> col.value} toMap)
}
+ def delete()(implicit conn: Connection) = {
+ table.executor.delete(table.tableName, filters)
+ }
}
case class GetQuery[A, T <: Table](query: Query[T], cols: T#Columns[A]) {
Please sign in to comment.
Something went wrong with that request. Please try again.