Permalink
Browse files

[scala] Single result case connection using `withQueryResult` in DSL:

```scala
import acolyte.Acolyte

// res: acolyte.QueryResult
val str: String = Acolyte.withQueryResult(res) { connection ⇒ … }
```
  • Loading branch information...
cchantep
cchantep committed Jan 12, 2014
1 parent 38f0b52 commit f33f9c82f4e64bc3b83c9f64ab82319b47af873e
View
@@ -403,6 +403,15 @@ can be used:
handleQuery withQueryHandler { e … }
```
When you only need connection for a single result case, `withQueryResult` is useful:
```scala
import acolyte.Acolyte
// res: acolyte.QueryResult
val str: String = Acolyte.withQueryResult(res) { connection … }
```
### Anorm
Acolyte is useful to write test about persistence in projects using [Anorm](http://www.playframework.com/documentation/2.2.x/ScalaAnorm): read [10 minutes tutorial about Acolyte with Anorm](https://github.com/cchantep/acolyte/tree/10m-anorm-tutorial#acolyteanorm-10-minutes-tutorial).
@@ -619,12 +628,10 @@ object ZooSpec extends org.specs2.mutable.Specification {
classOf[String] -> "color")
"Dog" should {
"be found at location 1, and be red" in {
val conn = connection(handleQuery { _
zooSchema :+ ("dog", 1, "Scooby", null.asInstanceOf[Boolean], "red")
})
atLocation(conn)(1) aka "animal" must beSome(Dog("Scooby", "red"))
"be found at location 1, and be red" in withQueryResult(
zooSchema :+ ("dog", 1, "Scooby", null.asInstanceOf[Boolean], "red")) {
conn
atLocation(conn)(1) aka "animal" must beSome(Dog("Scooby", "red"))
}
}
@@ -854,17 +861,20 @@ mvn gpg:sign-and-deploy-file -Dkeyname=$KEY -DpomFile=scala/target/scala-2.10/ac
At Sonatype:
```
mvn gpg:sign-and-deploy-file -Dkeyname=$KEY -DpomFile=core/target/acolyte-core-$VERSION.pom -Dfile=core/target/acolyte-core-$VERSION.jar -Durl=https://oss.sonatype.org/service/local/staging/deploy/maven2/ -DrepositoryId=sonatype-nexus-staging
export REPO="https://oss.sonatype.org/service/local/staging/deploy/maven2/"
# or https://oss.sonatype.org/content/repositories/snapshots/
mvn gpg:sign-and-deploy-file -Dkeyname=$KEY -DpomFile=core/target/acolyte-core-$VERSION.pom -Dfile=core/target/acolyte-core-$VERSION.jar -Durl=$REPO -DrepositoryId=sonatype-nexus-staging
mvn gpg:sign-and-deploy-file -Dkeyname=$KEY -DpomFile=core/target/acolyte-core-$VERSION.pom -Dfile=core/target/acolyte-core-$VERSION-javadoc.jar -Dclassifier=javadoc -Durl=https://oss.sonatype.org/service/local/staging/deploy/maven2/ -DrepositoryId=sonatype-nexus-staging
mvn gpg:sign-and-deploy-file -Dkeyname=$KEY -DpomFile=core/target/acolyte-core-$VERSION.pom -Dfile=core/target/acolyte-core-$VERSION-javadoc.jar -Dclassifier=javadoc -Durl=$REPO -DrepositoryId=sonatype-nexus-staging
mvn gpg:sign-and-deploy-file -Dkeyname=$KEY -DpomFile=core/target/acolyte-core-$VERSION.pom -Dfile=core/target/acolyte-core-$VERSION-sources.jar -Dclassifier=sources -Durl=https://oss.sonatype.org/service/local/staging/deploy/maven2/ -DrepositoryId=sonatype-nexus-staging
mvn gpg:sign-and-deploy-file -Dkeyname=$KEY -DpomFile=core/target/acolyte-core-$VERSION.pom -Dfile=core/target/acolyte-core-$VERSION-sources.jar -Dclassifier=sources -Durl=$REPO -DrepositoryId=sonatype-nexus-staging
mvn gpg:sign-and-deploy-file -Dkeyname=$KEY -DpomFile=scala/target/scala-2.10/acolyte-scala_2.10-$VERSION.pom -Dfile=scala/target/scala-2.10/acolyte-scala_2.10-$VERSION.jar -Durl=https://oss.sonatype.org/service/local/staging/deploy/maven2/ -DrepositoryId=sonatype-nexus-staging
mvn gpg:sign-and-deploy-file -Dkeyname=$KEY -DpomFile=scala/target/scala-2.10/acolyte-scala_2.10-$VERSION.pom -Dfile=scala/target/scala-2.10/acolyte-scala_2.10-$VERSION.jar -Durl=$REPO -DrepositoryId=sonatype-nexus-staging
mvn gpg:sign-and-deploy-file -Dkeyname=$KEY -DpomFile=scala/target/scala-2.10/acolyte-scala_2.10-$VERSION.pom -Dfile=scala/target/scala-2.10/acolyte-scala_2.10-$VERSION-javadoc.jar -Dclassifier=javadoc -Durl=https://oss.sonatype.org/service/local/staging/deploy/maven2/ -DrepositoryId=sonatype-nexus-staging
mvn gpg:sign-and-deploy-file -Dkeyname=$KEY -DpomFile=scala/target/scala-2.10/acolyte-scala_2.10-$VERSION.pom -Dfile=scala/target/scala-2.10/acolyte-scala_2.10-$VERSION-javadoc.jar -Dclassifier=javadoc -Durl=$REPO -DrepositoryId=sonatype-nexus-staging
mvn gpg:sign-and-deploy-file -Dkeyname=$KEY -DpomFile=scala/target/scala-2.10/acolyte-scala_2.10-$VERSION.pom -Dfile=scala/target/scala-2.10/acolyte-scala_2.10-$VERSION-sources.jar -Dclassifier=sources -Durl=https://oss.sonatype.org/service/local/staging/deploy/maven2/ -DrepositoryId=sonatype-nexus-staging
mvn gpg:sign-and-deploy-file -Dkeyname=$KEY -DpomFile=scala/target/scala-2.10/acolyte-scala_2.10-$VERSION.pom -Dfile=scala/target/scala-2.10/acolyte-scala_2.10-$VERSION-sources.jar -Dclassifier=sources -Durl=$REPO -DrepositoryId=sonatype-nexus-staging
```
Authentication should be configured in `~/.m2/settings.xml`:
@@ -3,7 +3,7 @@ package acolyte
import java.util.{ ArrayList, List JList }
import java.util.regex.Pattern
import java.sql.{ Statement, SQLWarning }
import java.sql.{ Connection, Statement, SQLWarning }
import scala.language.implicitConversions
import scala.collection.JavaConversions
@@ -90,7 +90,19 @@ object Acolyte {
*/
def handleQuery(h: QueryExecution QueryResult): ScalaCompositeHandler =
handleStatement withQueryDetection ".*" withQueryHandler h
// TODO? (h: QueryExec => A)(implicit c: A => QueryResult)
/**
* Executes |f| using connection accepting only queries,
* and answering with |result| to any query.
*
* {{{
* import acolyte.Acolyte.withQueryResult
*
* val str: String = withQueryResult(queryRes) { con => "str" }
* }}}
*/
def withQueryResult[A](res: QueryResult)(f: Connection A): A =
f(connection(handleQuery(_ res)))
}
@@ -171,4 +171,34 @@ object AcolyteSpec extends org.specs2.mutable.Specification {
}
}
"Single-case query context" should {
def query(sql: String, c: java.sql.Connection): java.sql.ResultSet = {
val rs = c.prepareStatement(sql).executeQuery
rs.next(); rs
}
lazy val res1: QueryResult =
RowLists.rowList2(classOf[String], classOf[Float]).
append("test", 3.45f).asResult
"always return provided result" >> {
"for SELECT" in {
val str: String = Acolyte.withQueryResult(res1) { c
val rs = query("SELECT * FROM table", c)
s"${rs.getString(1)} -> ${rs.getFloat(2) + 1f}"
}
str aka "from query result" mustEqual "test -> 4.45"
}
"for EXEC" in Acolyte.withQueryResult(res1) { c
query("EXEC proc", c) aka "proc result" must beLike {
case rs
(rs.getString(1) aka "col #1" mustEqual "test").
and(rs.getFloat(2) aka "col #2" mustEqual 3.45f)
}
}
}
}
}
@@ -115,8 +115,7 @@ object ScalaUseCases {
} // end of useCase3
/**
* Use case #4 - Row list convinience constructor
* and query handler convertion.
* Use case #4 - Connection with only query handler.
*/
def useCase4: SqlConnection = connection { handleQuery { e true } }

0 comments on commit f33f9c8

Please sign in to comment.