Czy możliwe jest dynamiczne tworzenie listy dla metody anorm? On?Dynamiczne parametry SQL z Anorm i Scala Play Framework
Mam formularz z opcjonalnymi wejściami i obecnie sprawdzam każdą opcję i tworzę listę z określonymi opcjami i próbuję przekazać to do anorm. Obecnie dostaję ten błąd kompilacji
Nie jestem pewien, w jaki sposób chciałbym utworzyć tę listę. Aktualny kod:
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 *)
Próbowałem to zrobić, ponieważ początkowo myślałem, że to będzie taka sama jak
.on('rooms -> input.rooms, 'bathroom -> input.bathrooms... etc)
EDIT:
Code jest teraz:
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[_])]]
przy użyciu polecenia SQL:
SQL("SELECT * FROM Houses WHERE " + whereString).on(onList:_*).as(sqlToHouse *)
Teraz coraz wyjątek
[ClassCastException: java.lang.Integer cannot be cast to anorm.ParameterValue]
Jak to ma działać? Jak wygląda 'whereString'? –