I have two Option
s:
val name: Option[String] = ...
val shared: Option[Boolean] = ...
I would like to build an UPDATE
query that SET
s these fields if the above values are Some
, but leaves them unchanged if they are None
.
I have managed to achieve this like so, but I am not so keen on the check for the comma, which is not going to scale if I need to add extra columns. I'm also not so keen on having to use a var
.
var query = Q.u + "UPDATE wishlist SET"
if(name.isDefined) query = query + " name = " +? name + (if (share.isDefined) ", " else "")
if(shared.isDefined) query = query + " shared = " +? share
If I wasn't concerned with SQL injection, I could do something along the lines of:
val fields = List(
name.map(n => s"name = '$n'"),
shared.map(e => s"shared = $e")
).flatten
s"UPDATE wishlist SET ${fields.mkString(", ")}"
Question: Is there a nicer way to do this with Slick's plain SQL/String interpolation or is my only option to move to lifted embedded?