[英]Anorm 2.3 multi-value parameter: required anorm.NamedParameter
Using scala 2.11.1 on play framework 2.3.在 play 框架 2.3 上使用 Scala 2.11.1。
Because Anorm didn't support multi-value parameters in previous versions I used David's workaround.因为 Anorm 在以前的版本中不支持多值参数,所以我使用了David 的解决方法。 Anorm now supports multi-value parameters and I started removing the workaround and using Anorm multi-value parameters.
Anorm 现在支持多值参数,我开始删除解决方法并使用 Anorm 多值参数。
The example [sic] mentioned:提到的例子[原文如此]:
// With default formatting (", " as separator)
SQL("SELECT * FROM Test WHERE cat IN ({categories})").
on('categories -> Seq("a", "b", "c")
// -> SELECT * FROM Test WHERE cat IN ('a', 'b', 'c')
Yet my code:然而我的代码:
val names = List("Able", "Baker", "Charlie") // Passed as a parameter to my method!
val result =
SQL( """
SELECT city
FROM addresses
WHERE name IN ({names});
""" ).on( 'names -> names ).as( scalar[String] * )
gives me this error:给我这个错误:
type mismatch;
found : (Symbol, List[String])
required: anorm.NamedParameter
or或者
type mismatch;
found : (Symbol, scala.collection.immutable.Seq[String])
required: anorm.NamedParameter
depending if i try a list or sequence (or one of the suggestions to map it).取决于我是否尝试列表或序列(或映射它的建议之一)。
I'm no expert, by far, and I think it's missing some implicit conversion?到目前为止,我不是专家,我认为它缺少一些隐式转换? Clueless to find out how/what/where.
不知道如何/什么/在哪里。 Tips/suggestions/solutions welcome!
欢迎提示/建议/解决方案!
Looking at Anorm document, you will see there is no multi-value example using List.查看 Anorm 文档,您将看到没有使用 List 的多值示例。 Indeed that's on purpose as instance of
NamedParameter
is implemented only for Seq[T]
.实际上,这是
NamedParameter
因为NamedParameter
实例仅针对Seq[T]
。
Just updating your code to val names = Seq("Able", "Baker", "Charlie")
make it valid (and compiles).只需将您的代码更新为
val names = Seq("Able", "Baker", "Charlie")
使其有效(并编译)。
I would suggest to edit your code using Anorm interpolation:我建议使用 Anorm 插值编辑您的代码:
val result =
SQL"SELECT city FROM addresses WHERE name IN ($names)" as scalar[String].*
// OR (if names won't change):
val result = SQL"""SELECT city FROM addresses
WHERE name IN (${Seq("Able", "Baker", "Charlie")})""".
as(scalar[String].*)
EDIT: See changes in Anorm https://github.com/playframework/playframework/pull/3257 (supports List)编辑:查看 Anorm https://github.com/playframework/playframework/pull/3257 中的更改(支持列表)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.