【问题标题】:Dynamic SQL Parameters with Anorm and Scala Play Framework使用 Anorm 和 Scala Play 框架的动态 SQL 参数
【发布时间】:2013-03-13 13:22:31
【问题描述】:

是否可以为 anorm 的“on”方法动态创建一个列表?

我有一个带有可选输入的表单,目前我检查每个选项并创建一个包含已定义选项的列表,并试图将其传递给 anorm。目前我得到这个编译错误

type mismatch; found : List[java.io.Serializable] required: (Any, anorm.ParameterValue[_])

我不确定如何创建此列表。 当前代码:

val onList = List(
        'school_id = input.school,
        if(input.rooms isDefined)       ('rooms -> input.rooms) else "None" ,
        if(input.bathrooms isDefined)   ('bathrooms -> input.bathrooms) else "None" ,
        if(input.houseType isDefined)   ('houseType -> input.houseType) else "None" ,
        if(input.priceLow isDefined)    ('priceLow -> input.priceLow) else "None" ,
        if(input.priceHigh isDefined)   ('priceHigh -> input.priceHigh) else "None" ,
        if(input.utilities isDefined)   ('utilities -> input.utilities) else "None" 
).filter(_!="None")
SQL("SELECT * FROM Houses WHERE " + whereString).on(onList).as(sqlToHouse *)

我尝试过这样做,因为最初我认为它会和

.on('rooms -> input.rooms, 'bathroom -> input.bathrooms... etc)

编辑:

代码现在是:

val onList = Seq(
        ('school_id -> input.school),
        if(input.rooms isDefined)       ('rooms -> input.rooms.get)         else None ,
        if(input.bathrooms isDefined)   ('bathrooms -> input.bathrooms.get) else None ,
        if(input.houseType isDefined)   ('houseType -> input.houseType.get) else None ,
        if(input.priceLow isDefined)    ('priceLow -> input.priceLow.get)   else None ,
        if(input.priceHigh isDefined)   ('priceHigh -> input.priceHigh.get) else None ,
        if(input.utilities isDefined)   ('utilities -> input.utilities.get) else None 
).filter(_!=None).asInstanceOf[Seq[(Any,anorm.ParameterValue[_])]]

使用 SQL 命令:

SQL("SELECT * FROM Houses WHERE " + whereString).on(onList:_*).as(sqlToHouse *)

现在得到异常

[ClassCastException: java.lang.Integer cannot be cast to anorm.ParameterValue]

【问题讨论】:

  • 这应该如何工作? whereString 长什么样子?

标签: sql scala playframework playframework-2.0 anorm


【解决方案1】:

重要的是您必须创建ParameterValue 类型的值。 这通常使用toParameterValue() 函数完成。

一种方法是创建一系列您展平的选项:

val onList = Seq(
  Some('school_id -> input.school),
  input.rooms.map('rooms -> _),
  input.bathrooms.map('bathrooms -> _)
).flatten

然后可以将此序列映射到正确的值:

SQL(
  "SELECT * FROM Houses WHERE " + whereString
).on(
  onList.map(v => v._1 -> toParameterValue(v._2)): _*
)

可以这样简化:

val onList = Seq(
  Some('school_id -> input.school),
  input.rooms.map('rooms -> _),
  input.bathrooms.map('bathrooms -> _)
).flatMap(_.map(v => v._1 -> toParameterValue(v._2)))

SQL(
  "SELECT * FROM Houses WHERE " + whereString
).on(
  onList: _*
)

或者也许最简单的解决方案是这样的:

val onList = Seq(
  Some('school_id -> toParameterValue(input.school)),
  input.rooms.map('rooms -> toParameterValue(_)),
  input.bathrooms.map('bathrooms -> toParameterValue(_))
).flatten

SQL(
  "SELECT * FROM Houses WHERE " + whereString
).on(
  onList: _*
)

【讨论】:

  • 太好了!我不知道 toParameterValue 函数。谢谢
【解决方案2】:

所以我最后只是多次打电话。

var query = SQL("SELECT * FROM Houses WHERE " + whereString).on('school_id -> input.school)
if(input.rooms isDefined)       query= query.on('rooms -> input.rooms.get)
if(input.bathrooms isDefined)   query= query.on('bathrooms -> input.bathrooms.get)
if(input.houseType isDefined)   query= query.on('houseType -> input.houseType.get)
if(input.priceLow isDefined)    query= query.on('priceLow -> input.priceLow.get)
if(input.priceHigh isDefined)   query= query.on('priceHigh -> input.priceHigh.get)
if(input.utilities isDefined)   query= query.on('utilities -> input.utilities.get)
query.as(sqlToHouse *)

【讨论】:

    【解决方案3】:

    你可以看看 multivalue parameter is next Anorm (coming Play 2.3/master)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-20
      相关资源
      最近更新 更多