Permalink
Browse files

Added support for modifying row content

  • Loading branch information...
teozkr committed Apr 30, 2012
1 parent be73753 commit c69cb386da21109bba72181244ea5ea03c2382e4
@@ -12,9 +12,28 @@ package object mysql extends NinaPlayImplPackage {
package mysql {
object NinaPlayMySQLExecutor extends NinaExecutor {
+ def serializeFilter(filters: Seq[Filter[_, _]]) = if (filters.isEmpty) ""
+ else " WHERE "+filters.map(_ match {
+ case Filter(col, filter.Like, _) => col.name+" LIKE {Like_"+col.name+"}"
+ case Filter(col, filter.EQ, _) => col.name+"={EQ_"+col.name+"}"
+ case Filter(col, filter.NEQ, _) => col.name+"!={NEQ_"+col.name+"}"
+ case Filter(col, filter.GT, _) => col.name+">{GT_"+col.name+"}"
+ case Filter(col, filter.GTE, _) => col.name+">={GTE_"+col.name+"}"
+ case Filter(col, filter.LT, _) => col.name+"<{LT_"+col.name+"}"
+ 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) {
+ 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()
+ }
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(",")+")")
- .on(data.toSeq.map { (kv) => val (key, value) = kv; (key, toParameterValue(value)) }: _*)
+ .on(data.toSeq.map { kv => val (key, value) = kv; key -> toParameterValue(value) }: _*)
.executeUpdate() > 0
)
@@ -23,21 +42,11 @@ package mysql {
getMultiple(table, filters, columns, 1).headOption
}
def getMultiple(table: String, filters: Seq[Filter[_, _]], columns: Seq[String], amount: Long)(implicit conn: Connection): Seq[Map[String, Option[Any]]] = {
- val filtersStr = if (filters.isEmpty) ""
- else " WHERE "+filters.map(_ match {
- case Filter(col, filter.Like, _) => col.name+" LIKE {"+col.name+"}"
- case Filter(col, filter.EQ, _) => col.name+"={"+col.name+"}"
- case Filter(col, filter.NEQ, _) => col.name+"!={"+col.name+"}"
- case Filter(col, filter.GT, _) => col.name+">{"+col.name+"}"
- case Filter(col, filter.GTE, _) => col.name+">={"+col.name+"}"
- case Filter(col, filter.LT, _) => col.name+"<{"+col.name+"}"
- case Filter(col, filter.LTE, _) => col.name+"<={"+col.name+"}"
- }).mkString(" AND ")
val limitStr = if (amount == -1) ""
else " LIMIT "+amount
- val sql = "SELECT "+columns.mkString(",")+" FROM "+table+filtersStr+limitStr
+ val sql = "SELECT "+columns.mkString(",")+" FROM "+table+serializeFilter(filters)+limitStr
val rows = SQL(sql).on(filters.map(_ match {
- case filter: Filter[_, _] => filter.column.name -> ParameterValue(filter.other, filter.setter)
+ case filter: Filter[_, _] => filter.kind.toString+"_"+filter.column.name -> ParameterValue(filter.other, filter.setter)
}): _*)()
var allRows = Seq[Row]() // Hack to make sure that all rows are fetched
@@ -94,5 +94,17 @@ class TableSpec extends Specification {
}
}
}
+
+ "be updatable" in {
+ running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
+ import Play.current
+ DB.withConnection { implicit c =>
+ anorm.SQL(TestingTable.creationSQL).execute()
+ val Some(id) = TestingTable where (TestingTable.name === "Java") get (TestingTable.id) single()
+ TestingTable where (TestingTable.id === id) set (TestingTable.name := "Test")
+ TestingTable where (TestingTable.id === id) get (TestingTable.name) single() must equalTo(Some("Test"))
+ }
+ }
+ }
}
}
@@ -3,6 +3,7 @@ package nina
import java.sql.Connection
trait NinaExecutor {
+ def update(table: String, filters: Seq[Filter[_, _]], data: Map[String, Any])(implicit conn: Connection)
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]]]
@@ -28,6 +28,9 @@ 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) {
+ table.executor.update(table.tableName, filters, values.map {col => col.column.name -> col.value} toMap)
+ }
}
case class GetQuery[A, T <: Table](query: Query[T], cols: T#Columns[A]) {

0 comments on commit c69cb38

Please sign in to comment.