Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Anorm ToStatement for ValueEnum
- Loading branch information
Showing
9 changed files
with
114 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
enumeratum-anorm/src/main/scala/values/AnormToStatement.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package enumeratum.values | ||
|
||
import java.sql.PreparedStatement | ||
|
||
import anorm.ToStatement | ||
|
||
private[values] object AnormToStatement { | ||
def apply[ValueType, EntryType <: ValueEnumEntry[ValueType]]( | ||
enum: ValueEnum[ValueType, EntryType] | ||
)( | ||
implicit baseToStmt: ToStatement[ValueType] | ||
): ToStatement[EntryType] = new ToStatement[EntryType] { | ||
def set(s: PreparedStatement, i: Int, e: EntryType) = | ||
baseToStmt.set(s, i, e.value) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
enumeratum-anorm/src/test/scala/values/AnormToStatementSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package enumeratum.values | ||
|
||
import scala.util.control.NonFatal | ||
|
||
import anorm._ | ||
|
||
import org.scalatest.matchers.must.Matchers | ||
import org.scalatest.wordspec.AnyWordSpec | ||
|
||
import acolyte.jdbc.{DefinedParameter => DParam, ParameterMetaData => ParamMeta, UpdateExecution} | ||
import acolyte.jdbc.AcolyteDSL.{connection, handleStatement} | ||
import acolyte.jdbc.Implicits._ | ||
|
||
final class AnormToStatementSpec extends AnyWordSpec with Matchers { | ||
"Sensitive enum" should { | ||
"successfully passed as parameter" when { | ||
def spec(value: Drink, repr: Short) = | ||
value.toString in withConnection(repr) { implicit c => | ||
SQL"set-short ${value}" match { | ||
case q @ SimpleSql(_, _, _, _) => | ||
// execute = false: update ok but returns no resultset | ||
// see java.sql.PreparedStatement#execute | ||
q.execute() mustEqual false | ||
} | ||
} | ||
|
||
spec(Drink.OrangeJuice, 1) | ||
spec(Drink.AppleJuice, 2) | ||
spec(Drink.Cola, 3) | ||
spec(Drink.Beer, 4) | ||
} | ||
} | ||
|
||
// --- | ||
|
||
private val SqlShort = ParamMeta.Short | ||
|
||
private def withConnection[A](repr: Short)(f: java.sql.Connection => A): A = | ||
f( | ||
connection( | ||
handleStatement withUpdateHandler { | ||
case UpdateExecution("set-short ?", DParam(`repr`, SqlShort) :: Nil) => 1 /* case ok */ | ||
|
||
case _ => | ||
throw new Exception("Unexpected execution") | ||
|
||
} | ||
)) | ||
} |
File renamed without changes.