What to return an updated column using return in sqlu statement, slickexception

I am using Postgresql with slick and I am trying to do the following:

sqlu"update table1 set column1 = column1 + 1 where id = $id returning column1".first

I get the following error. Basically I want to perform an update, and then return the updated value.

Error:

[[SlickException: Update statements should not return a ResultSet]]

Is there any other way to do this? It seems you are not allowed to return anything using the update instruction? (the return seems to be like a result set, but this is just one integer value).

+4
source share
1 answer

From slick source , it seems that if this is an update, slick throws an error in the method applyon line 52 during the extraction of the value.

staticQuery , 35, , slick GetResult.GetUpdateValue rconv, apply, , .

ResultSet, , :


private val updateQuery = "update test set name = 'mohitttttt' where id = ? returning name"
db.withSession { implicit session =>
      session.withPreparedStatement(updateQuery) { ps =>
      ps.setInt(1, 1)
      val rsetIterator = new ResultSetIterator[String](ps.executeQuery(), rset =>   rset.getString(1))
      rsetIterator.toList.head
}

ResultSetIterator -


class ResultSetIterator[T](rset: ResultSet, mapper: ResultSet => T) extends Iterator[T] {
  def hasNext = rset.next
  def next: T = {
    mapper(rset)
  }
}

A mapper , ResultSet . , ResultSet String. .

+5

All Articles