Scala slick 2.0 updateAll equivalent to insertALL?

纵饮孤独 提交于 2019-12-01 23:34:50
Ende Neu

It's not clear to me what you are trying to achieve, insert and update are two different operation, for insert makes sense to have a bulk function, for update it doesn't in my opinion, in fact in SQL you can just write something like this

UPDATE
  SomeTable
SET SomeColumn = SomeValue
WHERE AnotherColumn = AnotherValue

Which translates to update SomeColumn with the value SomeValue for all the rows which have AnotherColumn equal to AnotherValue.

In Slick this is a simple filter combined with map and update

table
  .filter(_.someCulomn === someValue)
  .map(_.FieldToUpdate)
  .update(NewValue)

If instead you want to update the whole row just drop the map and pass a Row object to the update function.

Edit:

If you want to update different case classes I'm lead to think that these case classes are rows defined in your schema and if that's the case you can pass them directly to the update function since it's so defined:

def update(value: T)(implicit session: Backend#Session): Int

For the second problem I can't suggest you a solution, looking at the JdbcInvokerComponent trait it looks like the update function invokes the execute method immediately

def update(value: T)(implicit session: Backend#Session): Int = session.withPreparedStatement(updateStatement) { st =>
  st.clearParameters
  val pp = new PositionedParameters(st)
  converter.set(value, pp, true)
  sres.setter(pp, param)
  st.executeUpdate
}

Probably because you can actually run one update query at the time per table and not multiple update on multiple tables as stated also on this SO question, but you could of course update multiple rows on the same table.

Sorry to answer my own question, but what i ended up doing is just dropping down to JDBC and doing batchUpdate.

  private def batchUpdateQuery = "update table set value = ? where id = ?"

  /**
   * Dropping to jdbc b/c slick doesnt support this batched update
   */
  def batchUpate(batch:List[MyCaseClass])(implicit subject:Subject, session:Session) = {
    val pstmt = session.conn.prepareStatement(batchUpdateQuery)

    batch map { myCaseClass =>
      pstmt.setString(1, myCaseClass.value)
      pstmt.setString(2, myCaseClass.id)
      pstmt.addBatch()
    }

    session.withTransaction {
      pstmt.executeBatch()
    }
  }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!