database - Slick lifted update on an Object -


i updates on lifted entities using slick. code updates firstname of contact object:

def updatecontact(id: int, firstname: option[string]): unit = {   val q1 = {     c <- contacts     if c.id id   } yield c.firstname   // update value same or new value   q1.update(firstname.getorelse(q1.list().head)) } 

the option here useful updating value in case (although nicer if update happened if there new value).

what looking way query object id, updates in memory using getorelse , update on whole object.

else have run above each field of object works know, feels dirty hack.

instead of q1.update(firstname.getorelse(q1.list().head)) can write firstname.foreach{ fn => q1.update(fn) } shorter, simpler, 1 instead of 2 queries :). using foreach on option stops looking weird when think of collection 1 or 0 elements.

regarding idea fetch whole object, modify , save back, can this:

def updatecontact(id: int, firstname: option[string], lastname:option[string], ...): unit = {   val q1 = query(contacts).filter(_.id === id)   val c = q1.first   val modifiedc = c.copy(     firstname = firstname.getorelse(c.firstname),     lastname = lastname.getorelse(c.lastname),     ...   )   q1.update(modifiedc) } 

here example: http://sysgears.com/notes/how-to-update-entire-database-record-using-slick/

this clean , simple , best way if performance not mission critical transfers columns of contacts. can save traffic transferring selected columns.


Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -