Skip to content

Commit

Permalink
[scala] Distinct execution case classes for update or query:
Browse files Browse the repository at this point in the history
```scala
Acolyte.
  withQueryHandler({ e: QueryExecution => res }).
  withUpdateHandler({ e: UpdateExecution => res })
```
  • Loading branch information
cchantep committed Nov 25, 2013
1 parent c52cdff commit e3536e8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 24 deletions.
37 changes: 21 additions & 16 deletions scala/src/main/scala/Acolyte.scala
Expand Up @@ -27,28 +27,27 @@ object Acolyte extends ScalaRowLists with ScalaRows {
implicit def PairAsColumn[T](c: (Class[T], String)): Column[T] = implicit def PairAsColumn[T](c: (Class[T], String)): Column[T] =
Column.defineCol(c._1, c._2) Column.defineCol(c._1, c._2)


implicit def IntUpdateHandler(h: Execution Int): UpdateHandler = implicit def IntUpdateHandler(h: UpdateExecution Int): UpdateHandler =
new UpdateHandler { new UpdateHandler {
def apply(sql: String, p: JList[Parameter]): UpdateResult = 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 { implicit def FunctionUpdateHandler(h: UpdateExecution UpdateResult): UpdateHandler = new UpdateHandler {
def apply(sql: String, p: JList[Parameter]): UpdateResult = { def apply(sql: String, p: JList[Parameter]): UpdateResult =
h(Execution(sql, scalaParameters(p))) h(UpdateExecution(sql, scalaParameters(p)))
}
} }


implicit def WarningUpdateHandler(h: Execution SQLWarning): UpdateHandler = implicit def WarningUpdateHandler(h: UpdateExecution SQLWarning): UpdateHandler =
new UpdateHandler { new UpdateHandler {
def apply(sql: String, p: JList[Parameter]): UpdateResult = { def apply(sql: String, p: JList[Parameter]): UpdateResult =
UpdateResult.Nothing.withWarning(h(Execution(sql, scalaParameters(p)))) 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 = 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)) implicit def SingleValueRow[A, B](value: A)(implicit f: A B): Row.Row1[B] = Rows.row1[B](f(value))
Expand All @@ -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, sql: String,
parameters: List[ExecutedParameter]) parameters: List[ExecutedParameter]) extends Execution


trait ExecutedParameter { trait ExecutedParameter {
def value: Any def value: Any
Expand All @@ -88,15 +93,15 @@ object ParameterVal {
final class ScalaCompositeHandler( final class ScalaCompositeHandler(
b: CompositeHandler) extends CompositeHandler { b: CompositeHandler) extends CompositeHandler {


def withQueryHandler(h: Execution QueryResult): CompositeHandler = { def withQueryHandler(h: QueryExecution QueryResult): CompositeHandler = {
b.withQueryHandler(new QueryHandler { b.withQueryHandler(new QueryHandler {
def apply(sql: String, p: JList[Parameter]): QueryResult = { def apply(sql: String, p: JList[Parameter]): QueryResult = {
val ps = JavaConversions.collectionAsScalaIterable(p). val ps = JavaConversions.collectionAsScalaIterable(p).
foldLeft(Nil: List[ExecutedParameter]) { (l, t) foldLeft(Nil: List[ExecutedParameter]) { (l, t)
l :+ DefinedParameter(t.right, t.left) l :+ DefinedParameter(t.right, t.left)
} }


h(Execution(sql, ps)) h(QueryExecution(sql, ps))
} }
}) })
} }
Expand Down
16 changes: 8 additions & 8 deletions scala/src/test/scala/acolyte/ScalaUseCases.scala
Expand Up @@ -3,7 +3,7 @@ package acolyte
import java.sql.{ Connection SqlConnection, Date, DriverManager } import java.sql.{ Connection SqlConnection, Date, DriverManager }


import acolyte.{ Driver AcolyteDriver } import acolyte.{ Driver AcolyteDriver }
import acolyte.RowLists.{ rowList1, rowList3 } import acolyte.RowLists.{ rowList1, rowList3, stringList }
import acolyte.Rows.{ row1, row3 } import acolyte.Rows.{ row1, row3 }
import Acolyte._ // import DSL import Acolyte._ // import DSL


Expand All @@ -27,15 +27,15 @@ object ScalaUseCases {
withQueryDetection( withQueryDetection(
"^SELECT ", // regex test from beginning "^SELECT ", // regex test from beginning
"EXEC that_proc"). // second detection regex "EXEC that_proc"). // second detection regex
withUpdateHandler({ e: Execution withUpdateHandler({ e: UpdateExecution
if (e.sql.startsWith("DELETE ")) { if (e.sql.startsWith("DELETE ")) {
// Process deletion ... // Process deletion ...
/* deleted = */ 2; /* deleted = */ 2;
} else { } else {
// ... Process ... // ... Process ...
/* count = */ 1; /* count = */ 1;
} }
}).withQueryHandler({ e: Execution }).withQueryHandler({ e: QueryExecution
if (e.sql.startsWith("SELECT ")) { if (e.sql.startsWith("SELECT ")) {
RowLists.rowList1(classOf[String]).asResult RowLists.rowList1(classOf[String]).asResult
} else { } else {
Expand Down Expand Up @@ -95,12 +95,12 @@ object ScalaUseCases {


val handler: CompositeHandler = handleStatement. val handler: CompositeHandler = handleStatement.
withQueryDetection("^SELECT "). withQueryDetection("^SELECT ").
withQueryHandler({ e: Execution withQueryHandler({ e: QueryExecution
e match { e match {
case Execution(s, ParameterVal("id") :: Nil) case QueryExecution(s, ParameterVal("id") :: Nil)
(rowList1(classOf[String]) :+ row1("useCase_3a")).asResult (stringList :+ "useCase_3a").asResult


case Execution(s, case QueryExecution(s,
DefinedParameter("id", _) :: DefinedParameter(3, _) :: Nil) DefinedParameter("id", _) :: DefinedParameter(3, _) :: Nil)
(rowList3(classOf[String], classOf[Int], classOf[Long]) :+ row3( (rowList3(classOf[String], classOf[Int], classOf[Long]) :+ row3(
"useCase_3str", 2, 3l)).asResult "useCase_3str", 2, 3l)).asResult
Expand All @@ -123,7 +123,7 @@ object ScalaUseCases {
def useCase4: SqlConnection = connection { def useCase4: SqlConnection = connection {
handleStatement. handleStatement.
withQueryDetection("^SELECT "). withQueryDetection("^SELECT ").
withQueryHandler({ e: Execution RowLists.booleanList :+ true }) withQueryHandler({ e: QueryExecution true })


} // end of useCase4 } // end of useCase4
} // end of class ScalaUseCases } // end of class ScalaUseCases

0 comments on commit e3536e8

Please sign in to comment.