如果在Slick 3.0.0中不存在则插入

时间:2015-06-08 09:59:19

标签: scala slick

我试图插入如果不存在,我发现this post为1.0.1,2.0。

我在the docs of 3.0.0

中使用事务处理找到了代码段
val a = (for {
  ns <- coffees.filter(_.name.startsWith("ESPRESSO")).map(_.name).result
  _ <- DBIO.seq(ns.map(n => coffees.filter(_.name === n).delete): _*)
} yield ()).transactionally

val f: Future[Unit] = db.run(a)

如果不存在这种结构,我很难从插入编写逻辑。我是Slick的新手,对Scala没什么经验。这是我尝试在事务之外进行插入...

val result: Future[Boolean] = db.run(products.filter(_.name==="foo").exists.result)
result.map { exists =>  
  if (!exists) {
    products += Product(
      None,
      productName,
      productPrice
    ) 
  }  
}

但是如何将它放在事务块中呢?这是我能走得最远的地方:

val a = (for {
  exists <- products.filter(_.name==="foo").exists.result
  //???  
//    _ <- DBIO.seq(ns.map(n => coffees.filter(_.name === n).delete): _*)
} yield ()).transactionally

提前致谢

4 个答案:

答案 0 :(得分:24)

可以使用单个insert ... if not exists查询。这可以避免多个数据库往返和竞争条件(根据隔离级别,事务可能不够)。

def insertIfNotExists(name: String) = users.forceInsertQuery {
  val exists = (for (u <- users if u.name === name.bind) yield u).exists
  val insert = (name.bind, None) <> (User.apply _ tupled, User.unapply)
  for (u <- Query(insert) if !exists) yield u
}

Await.result(db.run(DBIO.seq(
  // create the schema
  users.schema.create,

  users += User("Bob"),
  users += User("Bob"),
  insertIfNotExists("Bob"),
  insertIfNotExists("Fred"),
  insertIfNotExists("Fred"),

  // print the users (select * from USERS)
  users.result.map(println)
)), Duration.Inf)

输出:

Vector(User(Bob,Some(1)), User(Bob,Some(2)), User(Fred,Some(3)))

生成的SQL:

insert into "USERS" ("NAME","ID") select ?, null where not exists(select x2."NAME", x2."ID" from "USERS" x2 where x2."NAME" = ?)

Here's the full example on github

答案 1 :(得分:19)

这是我提出的版本:

val a = (
    products.filter(_.name==="foo").exists.result.flatMap { exists => 
      if (!exists) {
        products += Product(
          None,
          productName,
          productPrice
        ) 
      } else {
        DBIO.successful(None) // no-op
      }
    }
).transactionally

虽然有点缺乏,例如返回插入的或现有的对象会很有用。

为了完整性,这里有表定义:

case class DBProduct(id: Int, uuid: String, name: String, price: BigDecimal)
class Products(tag: Tag) extends Table[DBProduct](tag, "product") {
  def id = column[Int]("id", O.PrimaryKey, O.AutoInc) // This is the primary key column
  def uuid = column[String]("uuid")
  def name = column[String]("name")
  def price = column[BigDecimal]("price", O.SqlType("decimal(10, 4)"))

  def * = (id, uuid, name, price) <> (DBProduct.tupled, DBProduct.unapply)
}
val products = TableQuery[Products]

我正在使用映射表,该解决方案也适用于元组,只需稍作更改。

另请注意,根据插入操作中忽略的documentation,没有必要将id定义为可选:

  

如果在插入操作中包含AutoInc列,则会以静默方式忽略它,以便数据库可以生成正确的值

这里的方法是:

def insertIfNotExists(productInput: ProductInput): Future[DBProduct] = {

  val productAction = (
    products.filter(_.uuid===productInput.uuid).result.headOption.flatMap { 
    case Some(product) =>
      mylog("product was there: " + product)
      DBIO.successful(product)

    case None =>
      mylog("inserting product")

      val productId =
        (products returning products.map(_.id)) += DBProduct(
            0,
            productInput.uuid,
            productInput.name,
            productInput.price
            )

          val product = productId.map { id => DBProduct(
            id,
            productInput.uuid,
            productInput.name,
            productInput.price
          )
        }
      product
    }
  ).transactionally

  db.run(productAction)
}

(感谢Google group thread的Matthew Pocock,让我选择这个解决方案)。

答案 2 :(得分:2)

我遇到了看起来更完整的解决方案。 Essential Slick 书中的Section 3.1.7 More Control over Inserts就是示例。

最后,您会得到类似的东西:

  val entity = UserEntity(UUID.random, "jay", "jay@localhost")

  val exists =
    users
      .filter(
        u =>
          u.name === entity.name.bind
            && u.email === entity.email.bind
      )
      .exists
  val selectExpression = Query(
    (
      entity.id.bind,
      entity.name.bind,
      entity.email.bind
    )
  ).filterNot(_ => exists)

  val action = usersDecisions
    .map(u => (u.id, u.name, u.email))
    .forceInsertQuery(selectExpression)

  exec(action)
  // res17: Int = 1

  exec(action)
  // res18: Int = 0

答案 3 :(得分:-1)

根据光滑的3.0手动插入查询部分(http://slick.typesafe.com/doc/3.0.0/queries.html),插入的值可以返回id,如下所示:

def insertIfNotExists(productInput: ProductInput): Future[DBProduct] = {

  val productAction = (
    products.filter(_.uuid===productInput.uuid).result.headOption.flatMap { 
    case Some(product) =>
      mylog("product was there: " + product)
      DBIO.successful(product)

    case None =>
      mylog("inserting product")

      (products returning products.map(_.id) 
                into ((prod,id) => prod.copy(id=id))) += DBProduct(
            0,
            productInput.uuid,
            productInput.name,
            productInput.price
            )
    }
  ).transactionally

  db.run(productAction)
}