Permalink
Browse files

[jdbc-scala] Companion function for update result with generated keys.

```scala
import acolyte.{ Acolyte, RowLists }

val res = Acolyte.updateResult(3/*count*/, RowLists.stringList("generatedKey"))
```
  • Loading branch information...
cchantep
cchantep committed May 1, 2014
1 parent 99ac2f1 commit ac3415cabc01f85de1d766c0de53f0ab7e8c6c38
Showing with 46 additions and 2 deletions.
  1. +14 −0 jdbc-scala/src/main/scala/Acolyte.scala
  2. +17 −0 jdbc-scala/src/test/scala/acolyte/AcolyteSpec.scala
  3. +15 −2 readme.md
@@ -103,6 +103,20 @@ object Acolyte {
def withQueryResult[A](res: QueryResult)(f: SqlConnection A): A =
f(connection(handleQuery(_ res)))
/**
* Returns an update result with row |count| and generated |keys|.
* @param count Updated (row) count
* @param keys Generated keys
*
* {{{
* import acolyte.Acolyte.updateResult
* import acolyte.RowLists
*
* updateResult(2/* updated rows */, RowLists.stringList("a", "b"))
* }}}
*/
def updateResult(count: Int, keys: RowList[_]): UpdateResult =
new UpdateResult(count) withGeneratedKeys keys
}
/**
@@ -217,4 +217,21 @@ object AcolyteSpec extends org.specs2.mutable.Specification {
}
}
"Update result" should {
"have generated keys" in {
Acolyte.updateResult(2, RowLists.intList(3, 4)).
aka("update result") must beLike {
case res (res.getUpdateCount aka "count" must_== 2).
and(res.getGeneratedKeys.resultSet aka "keys" must beLike {
case genKeys
(genKeys.next aka "has first generated key" must beTrue).
and(genKeys.getInt(1) aka "first key" must_== 3).
and(genKeys.next aka "has second generated key" must beTrue).
and(genKeys.getInt(1) aka "second key" must_== 4).
and(genKeys.next aka "has third generated key" must beFalse)
})
}
}
}
}
View
@@ -141,7 +141,7 @@ If you just need/want to directly get connection from `acolyte.Driver`, without
Connection con = acolyte.Driver.connection(handler);
```
### Connection properties
#### Connection properties
JDBC allows to pass properties to driver to customize connection creation:
@@ -305,7 +305,7 @@ QueryResult resWithWarning = aRowList.asResult().
withWarning("Row list result with warning");
```
### Generated keys
#### Generated keys
Update case not only returning update count but also generated keys can be represented with `UpdateResult`:
@@ -454,6 +454,19 @@ import acolyte.Acolyte
val str: String = Acolyte.withQueryResult(res) { connection … }
```
#### Generated keys
Update case not only returning update count but also generated keys can be represented with `UpdateResult`:
```java
import acolyte.{ Acolyte, RowLists }
// Result with update count == 1 and a generated key 2L
Acolyte.updateResult(1, RowLists.longList.append(2L))
```
Keys specified on result will be given to JDBC statement `.getGeneratedKeys`.
### 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).

0 comments on commit ac3415c

Please sign in to comment.