Permalink
Browse files

[scala] Distinct execution case classes for update or query:

```scala
Acolyte.
  withQueryHandler({ e: QueryExecution => res }).
  withUpdateHandler({ e: UpdateExecution => res })
```
  • Loading branch information...
cchantep
cchantep committed Nov 14, 2013
1 parent c52cdff commit e3536e8a6d10d72385cf8b4880e31a583d2a2f7a
Showing with 29 additions and 24 deletions.
  1. +21 −16 scala/src/main/scala/Acolyte.scala
  2. +8 −8 scala/src/test/scala/acolyte/ScalaUseCases.scala
@@ -27,28 +27,27 @@ object Acolyte extends ScalaRowLists with ScalaRows {
implicit def PairAsColumn[T](c: (Class[T], String)): Column[T] =
Column.defineCol(c._1, c._2)
implicit def IntUpdateHandler(h: Execution Int): UpdateHandler =
implicit def IntUpdateHandler(h: UpdateExecution Int): UpdateHandler =
new UpdateHandler {
def apply(sql: String, p: JList[Parameter]): UpdateResult =
new UpdateResult(h(Execution(sql, scalaParameters(p))))
new UpdateResult(h(UpdateExecution(sql, scalaParameters(p))))
}
implicit def FunctionUpdateHandler(h: Execution UpdateResult): UpdateHandler = new UpdateHandler {
def apply(sql: String, p: JList[Parameter]): UpdateResult = {
h(Execution(sql, scalaParameters(p)))
}
implicit def FunctionUpdateHandler(h: UpdateExecution UpdateResult): UpdateHandler = new UpdateHandler {
def apply(sql: String, p: JList[Parameter]): UpdateResult =
h(UpdateExecution(sql, scalaParameters(p)))
}
implicit def WarningUpdateHandler(h: Execution SQLWarning): UpdateHandler =
implicit def WarningUpdateHandler(h: UpdateExecution SQLWarning): UpdateHandler =
new UpdateHandler {
def apply(sql: String, p: JList[Parameter]): UpdateResult = {
UpdateResult.Nothing.withWarning(h(Execution(sql, scalaParameters(p))))
}
def apply(sql: String, p: JList[Parameter]): UpdateResult =
UpdateResult.Nothing.
withWarning(h(UpdateExecution(sql, scalaParameters(p))))
}
implicit def RowListQueryHandler[R <: RowList[_]](h: Execution R): QueryHandler = new QueryHandler {
implicit def RowListQueryHandler[R <: RowList[_]](h: QueryExecution R): QueryHandler = new QueryHandler {
def apply(sql: String, params: JList[Parameter]): QueryResult =
h(Execution(sql, scalaParameters(params))).asResult
h(QueryExecution(sql, scalaParameters(params))).asResult
}
implicit def SingleValueRow[A, B](value: A)(implicit f: A B): Row.Row1[B] = Rows.row1[B](f(value))
@@ -61,9 +60,15 @@ object Acolyte extends ScalaRowLists with ScalaRows {
}
case class Execution(
trait Execution
case class QueryExecution(
sql: String,
parameters: List[ExecutedParameter]) extends Execution
case class UpdateExecution(
sql: String,
parameters: List[ExecutedParameter])
parameters: List[ExecutedParameter]) extends Execution
trait ExecutedParameter {
def value: Any
@@ -88,15 +93,15 @@ object ParameterVal {
final class ScalaCompositeHandler(
b: CompositeHandler) extends CompositeHandler {
def withQueryHandler(h: Execution QueryResult): CompositeHandler = {
def withQueryHandler(h: QueryExecution QueryResult): CompositeHandler = {
b.withQueryHandler(new QueryHandler {
def apply(sql: String, p: JList[Parameter]): QueryResult = {
val ps = JavaConversions.collectionAsScalaIterable(p).
foldLeft(Nil: List[ExecutedParameter]) { (l, t)
l :+ DefinedParameter(t.right, t.left)
}
h(Execution(sql, ps))
h(QueryExecution(sql, ps))
}
})
}
@@ -3,7 +3,7 @@ package acolyte
import java.sql.{ Connection SqlConnection, Date, DriverManager }
import acolyte.{ Driver AcolyteDriver }
import acolyte.RowLists.{ rowList1, rowList3 }
import acolyte.RowLists.{ rowList1, rowList3, stringList }
import acolyte.Rows.{ row1, row3 }
import Acolyte._ // import DSL
@@ -27,15 +27,15 @@ object ScalaUseCases {
withQueryDetection(
"^SELECT ", // regex test from beginning
"EXEC that_proc"). // second detection regex
withUpdateHandler({ e: Execution
withUpdateHandler({ e: UpdateExecution
if (e.sql.startsWith("DELETE ")) {
// Process deletion ...
/* deleted = */ 2;
} else {
// ... Process ...
/* count = */ 1;
}
}).withQueryHandler({ e: Execution
}).withQueryHandler({ e: QueryExecution
if (e.sql.startsWith("SELECT ")) {
RowLists.rowList1(classOf[String]).asResult
} else {
@@ -95,12 +95,12 @@ object ScalaUseCases {
val handler: CompositeHandler = handleStatement.
withQueryDetection("^SELECT ").
withQueryHandler({ e: Execution
withQueryHandler({ e: QueryExecution
e match {
case Execution(s, ParameterVal("id") :: Nil)
(rowList1(classOf[String]) :+ row1("useCase_3a")).asResult
case QueryExecution(s, ParameterVal("id") :: Nil)
(stringList :+ "useCase_3a").asResult
case Execution(s,
case QueryExecution(s,
DefinedParameter("id", _) :: DefinedParameter(3, _) :: Nil)
(rowList3(classOf[String], classOf[Int], classOf[Long]) :+ row3(
"useCase_3str", 2, 3l)).asResult
@@ -123,7 +123,7 @@ object ScalaUseCases {
def useCase4: SqlConnection = connection {
handleStatement.
withQueryDetection("^SELECT ").
withQueryHandler({ e: Execution RowLists.booleanList :+ true })
withQueryHandler({ e: QueryExecution true })
} // end of useCase4
} // end of class ScalaUseCases

0 comments on commit e3536e8

Please sign in to comment.