Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Slick 3, how does one SQL-Compile an insert using a mapped case class?

Tags:

scala

slick

To SQL-compile a query, you need to compile a function which takes, for each query parameter arg: type, a lifted parameter of type Rep[type] .

I have a case class JobRecord and a TableQuery jobRecords.

So to insert a JobRecord case-class instance, I need to be able to say something like:

val qMapToId = (jobRecords returning jobRecords.map(_.id))
def ucCreate(jobRecord: Rep[JobRecord]) = qMapToId += jobRecord
val cCreate = Compiled(ucCreate _)

But of course this doesn't compile, because += doesn't take a Rep, and I'm not sure Rep[JobRecord] is valid either.

I've tried many things, not worth showing, including mixing in the Monomorphic Case Classes guidance. I probably got a step away from a solution a few times. A pointer to a working example would be great!

like image 563
Ed Staub Avatar asked Oct 19 '22 14:10

Ed Staub


1 Answers

You don't have to do anything, val qMapToId = (jobRecords returning jobRecords.map(_.id)) generates the statement once, at compile time (i.e. on container startup).

Compiled replaces Parameters in the new API, and comes into play for selects, updates, and (I believe) deletes where you are binding placeholders for generating a prepared statement. For insert statements there's nothing to bind, you already have an instance for +=.

like image 86
virtualeyes Avatar answered Oct 23 '22 00:10

virtualeyes