Ist es möglich, dynamisch eine Liste für die "on" -Methode von anorm zu erstellen?Dynamische SQL-Parameter mit Anorm und Scala Play Framework
Ich habe ein Formular mit optionalen Eingaben und zur Zeit überprüfe ich jede Option und erstelle eine Liste mit den definierten Optionen und versuche dies an anorm weiterzuleiten. Zur Zeit bekomme ich diese Kompilierung Fehler
Ich bin mir nicht sicher, wie ich über das Erstellen dieser Liste gehen würde. Aktueller Code:
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 *)
Ich habe versucht das zu tun, weil anfangs dachte ich, es wäre das gleiche wie
.on('rooms -> input.rooms, 'bathroom -> input.bathrooms... etc)
EDIT:
-Code ist jetzt:
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[_])]]
mit SQL-Befehl:
SQL("SELECT * FROM Houses WHERE " + whereString).on(onList:_*).as(sqlToHouse *)
nun die Ausnahme
bekommen[ClassCastException: java.lang.Integer cannot be cast to anorm.ParameterValue]
Wie soll das funktionieren? Wie sieht 'whereString' aus? –