本文介绍了Anorm 2.3多值参数:必需的anorm.NamedParameter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在play framework 2.3上使用scala 2.11.1.

因为在以前的版本中Anorm不支持​​多值参数,所以我使用了大卫的解决方法.现在Anorm 支持 多值参数,我开始删除变通方法并使用Anorm多值参数. /p>

示例 [原文]:

 // 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')
 

我的代码是

 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] * )
 

给我这个错误:

type mismatch;
 found   : (Symbol, List[String])
 required: anorm.NamedParameter

type mismatch;
 found   : (Symbol, scala.collection.immutable.Seq[String])
 required: anorm.NamedParameter

取决于我是否尝试列表或序列(或建议进行映射).

到目前为止,我还不是专家,我认为它缺少一些隐式转换?无能为力地找出方式/方式/地点.欢迎提供提示/建议/解决方案!

解决方案

查看Anorm文档,您将看到没有使用List的多值示例.实际上,这是故意的,因为NamedParameter的实例仅针对Seq[T]实施.

只需将代码更新为val names = Seq("Able", "Baker", "Charlie")即可使其有效(并编译).

我建议使用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].*)
 

查看Anorm中的更改 https://github.com/playframework/playframework /pull/3257 (支持列表)

Using scala 2.11.1 on play framework 2.3.

Because Anorm didn't support multi-value parameters in previous versions I used David's workaround. Anorm now supports multi-value parameters and I started removing the workaround and using Anorm multi-value parameters.

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. Indeed that's on purpose as instance of NamedParameter is implemented only for Seq[T].

Just updating your code to val names = Seq("Able", "Baker", "Charlie") make it valid (and compiles).

I would suggest to edit your code using Anorm interpolation:

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 2.3多值参数:必需的anorm.NamedParameter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 22:08