Update statement with multiple return values for slick 3

1,066 views
Skip to first unread message

gitted

unread,
Dec 9, 2015, 9:49:46 PM12/9/15
to Slick / ScalaQuery
Hello,

Currently in Slick 2.x I am doing the following to update and return multiple columns:

val query = u"update users set c1 = c1 + 1, c2 = c2 + 1 where user_id = $userId returning c1, c2"

session.withPreparedStatement(query) { ps =>
      val rs = ps.executeQuery()
      if(rs.next()) {
        val c2 = rs.getInt(1)
        val c2 = rs.getInt(2)
        (c1 - c2)
      } else {
        0
      }
    }


Is it possible to do this in slick 3.0, if not, how can i re-write the above in slick 3?



If that link is the only type of solution can I just write it like I have pasted above in slick 3 somehow?


Thanks!

gitted

unread,
Dec 17, 2015, 10:19:58 PM12/17/15
to Slick / ScalaQuery
I was trying this, but couldnt' get it to work:

sql"update sales set total_sales = total_sales + 1 where user_id = $userId returning total_sales, total_returns".as[(Int, Int)]

Can this work?  If not, is it possible to make an update statement following by a select statement that returns just those 2 columns total_sales, total_returns?

Can I do this in a single query or do I have to make 2 database round trips?  Please provide some code as I am stuck on this :)

Richard Dallaway

unread,
Dec 21, 2015, 9:05:58 AM12/21/15
to Slick / ScalaQuery
Hello - I've just tried that with Postgres and Slick 3, and it did what I think you want.  Code formatting in email is a pain, so the example I toyed with (based on your Dec 10th post) is here: https://gist.github.com/d6y/8655650b7d2d57389fd0

I've not included the imports in that gist, but happy to turn it into a runnable project if you need that.

Hope that's some help
Richard



--

---
You received this message because you are subscribed to the Google Groups "Slick / ScalaQuery" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalaquery+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scalaquery/123b2ceb-6b30-493e-a153-01a7a3241518%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

gitted

unread,
Dec 29, 2015, 10:31:34 AM12/29/15
to Slick / ScalaQuery
Thanks, that is what I ended up doing and it works.

The only thing I don't like is that it returns a Vector.  I don't need a collection coming back, just the 2 values.

It is updating based on a PK so it will never return more than 1 row.
Reply all
Reply to author
Forward
0 new messages