critium
critium

Reputation: 652

Scala slick 2.0 updateAll equivalent to insertALL?

Looking for a way to do a batch update using slick. Is there an equivalent updateAll to insertALL? Goole research has failed me thus far.

I have a list of case classes that have varying status. Each one having a different numeric value so I cannot run the typical update query. At the same time, I want to save the multiple update requests as there could be thousands of records I want to update at the same time.

Upvotes: 2

Views: 1997

Answers (2)

critium
critium

Reputation: 652

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()
  }
}

Upvotes: 2

Ende Neu
Ende Neu

Reputation: 15773

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.

Upvotes: 1

Related Questions