How do you modify existing records using ScalaQuery?

957 Views Asked by At

By modify I mean counterparts of SQL UPDATE and DELETE.

In both cases I have an object-record and I would like to delete it in the database. The table has always primary key, and it is set in my object-record.

Please note that I don't have query or other source which "created" that object-record, all I have is it and the table. So in general it looks like this:

fetch the Record from Table
...
// forget how I get the Record
...
Record.person_name = "joe"
? update Record ?

How to do it?


I define records and tables as below:

case class Topic(var id : Long,
                 var sectionId : Int,
                 ...

object TopicTable  extends Table[Topic]("Topic") {
       def id = column[Long]("top_Id", O.PrimaryKey) 
       def sectionId = column[Int]("sect_Id")
       ...
1

There are 1 best solutions below

3
On BEST ANSWER

It seems there are no direct methods, so you have to create explicitly a recordset in order to modify (for comparison -- I know SQ is not ORM -- in EF you fetch records, modify them and at this point your data context "knows" they were modify, so all you have to do is submit changes).

So first you create RS as you like:

val rs = for (rec <- MyTable if rec.id===10) yield rec;

and the delete records:

rs.mutate(rec => rec.delete())

for update:

rs.update(new MyRecord(...))

or (gossip is, it is faster ;-) )

rs.mutate(rec => rec.row = new MyRecord(...))

Please note I am complete newbie with SQ so I might just misinformed you. I works for me though.

Now, the only missing part is adding some nice wrappers, so delete and update could be done directly per record.