Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamic SQL Parameters with Anorm and Scala Play Framework
    text
    copied!<p>Is it possible to dynamically create a list for anorm's "on" method? </p> <p>I have a form with optional inputs and currently I check each Option and create a list with the defined Options and am trying to pass this through to anorm. Currently I get this compilation error</p> <pre><code>type mismatch; found : List[java.io.Serializable] required: (Any, anorm.ParameterValue[_]) </code></pre> <p>I'm not sure how I would go about creating this list. Current code : </p> <pre><code>val onList = List( 'school_id = input.school, if(input.rooms isDefined) ('rooms -&gt; input.rooms) else "None" , if(input.bathrooms isDefined) ('bathrooms -&gt; input.bathrooms) else "None" , if(input.houseType isDefined) ('houseType -&gt; input.houseType) else "None" , if(input.priceLow isDefined) ('priceLow -&gt; input.priceLow) else "None" , if(input.priceHigh isDefined) ('priceHigh -&gt; input.priceHigh) else "None" , if(input.utilities isDefined) ('utilities -&gt; input.utilities) else "None" ).filter(_!="None") SQL("SELECT * FROM Houses WHERE " + whereString).on(onList).as(sqlToHouse *) </code></pre> <p>I've tried doing this because initially I thought it would be the same as</p> <pre><code>.on('rooms -&gt; input.rooms, 'bathroom -&gt; input.bathrooms... etc) </code></pre> <p>EDIT: </p> <p>Code is now: </p> <pre><code>val onList = Seq( ('school_id -&gt; input.school), if(input.rooms isDefined) ('rooms -&gt; input.rooms.get) else None , if(input.bathrooms isDefined) ('bathrooms -&gt; input.bathrooms.get) else None , if(input.houseType isDefined) ('houseType -&gt; input.houseType.get) else None , if(input.priceLow isDefined) ('priceLow -&gt; input.priceLow.get) else None , if(input.priceHigh isDefined) ('priceHigh -&gt; input.priceHigh.get) else None , if(input.utilities isDefined) ('utilities -&gt; input.utilities.get) else None ).filter(_!=None).asInstanceOf[Seq[(Any,anorm.ParameterValue[_])]] </code></pre> <p>using SQL command:</p> <pre><code>SQL("SELECT * FROM Houses WHERE " + whereString).on(onList:_*).as(sqlToHouse *) </code></pre> <p>Now getting the exception </p> <pre><code>[ClassCastException: java.lang.Integer cannot be cast to anorm.ParameterValue] </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload