Last active
August 2, 2018 07:35
-
-
Save petitviolet/a8052b3f276314db224c6793462772e0 to your computer and use it in GitHub Desktop.
helper class for execution "upsert" (update or insert) query using Skinny-ORM.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scalikejdbc._ | |
import Implicits._ | |
val column = daos.User.column | |
def upsertUser(entity: User) = { | |
daos.User.upsert( | |
sqls.eq(column.id, entity.id) | |
)(update = | |
column.name -> entity.name, | |
column.updatedAt -> java.time.LocalDateTime.now() | |
)(insert = | |
column.id -> entity.id, | |
column.name -> entity.name, | |
column.createdAt -> entity.createdAt, | |
column.updatedAt -> java.time.LocalDateTime.now() | |
) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import skinny.orm.feature.NoIdCUDFeature | |
object Implicits { | |
implicit class Upserter[T](val orm: NoIdCUDFeature[T]) extends AnyVal { | |
def upsert(condition: SQLSyntax)(update: (SQLSyntax, Any)*)(insert: (SQLSyntax, Any)*)(implicit s: DBSession): Unit = { | |
orm.updateBy(condition).withNamedValues(update: _*) match { | |
case 1 => () | |
case 0 => | |
orm.createWithNamedValues(insert: _*) | |
() | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment