Skip to content

Instantly share code, notes, and snippets.

@petitviolet
Last active August 2, 2018 07:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petitviolet/a8052b3f276314db224c6793462772e0 to your computer and use it in GitHub Desktop.
Save petitviolet/a8052b3f276314db224c6793462772e0 to your computer and use it in GitHub Desktop.
helper class for execution "upsert" (update or insert) query using Skinny-ORM.
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()
)
}
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