Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Rep[T] to T in slick 3.0?

I used a code, generated from slick code generator.

My table has more than 22 columns, hence it uses HList

It generates 1 type and 1 function:

type AccountRow def AccountRow(uuid: java.util.UUID, providerid: String, email: Option[String], ...):AccountRow 

How do I write compiled insert code from generated code?

I tried this:

val insertAccountQueryCompiled = {  def q(uuid:Rep[UUID], providerId:Rep[String], email:Rep[Option[String]], ...) = Account += AccountRow(uuid, providerId, email, ...)  Compiled(q _) } 

I need to convert Rep[T] to T for AccountRow function to work. How do I do that?

Thank you

like image 541
kingcw Avatar asked May 13 '15 10:05

kingcw


1 Answers

;TLDR; Not possible

Explanation

There are two levels of abstraction in Slick: Querys and DBIOActions.

When you're dealing with Querys, you have to access your schema definitions, and rows, Reps and, basically, it's very constrained as it's the closest level of abstraction to the actual DB you're using. A Rep refers to an hypothetical value in the database, not in your program.

Then you have DBIOActions, which are the next level... not just some definition of a query, but the execution of it. You usually get DBIOActions when getting information out of a query, like with the result method or (TADAN!) when inserting rows.

Inserts and Updates are not queries and so what you're trying to do is not possible. You're dealing with DBIOAction (the += method), and Query stuff (the Rep types). The only way to get a Rep inside a DBIOAction is by executing a Query and obtaining a DBIOAction and then composing both Actions using flatMap or for comprehensions (which is the same).

like image 155
Alejandro Navas Avatar answered Sep 27 '22 21:09

Alejandro Navas