2013-03-23 150 views
13

是否可以为anorm的“on”方法动态创建一个列表?带有Anorm和Scala Play Framework的动态SQL参数

我有一个可选输入的表单,目前我检查每个选项,并用定义的选项创建一个列表,并试图将其传递给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] 
+1

这应该如何工作? 'whereString'看起来像什么? – 2013-08-14 16:42:32

回答

12

重要的是您必须创建类型为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: _* 
) 
+0

太棒了!我不知道toParameterValue函数。谢谢 – kingdamian42 2013-03-26 05:21:20

1

所以我最终只是多次呼吁。

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

你可以看看多值参数旁边ANORM(即将播放2.3 /硕士)。

相关问题