Common type for Query or DBIOAction

760 views Asked by At

I'm trying to pass a query or action as a parameter to a method. The reason is I want to run the same actions on the result of different queries on the same table.

For example:

val query1 = listItems   
val query2 = listItems.filter { x => x.id===1 }

And my method:

private def findListItems(query: Query[???]): Future[Foo] = {  ... }

Turns I can't just pass query1 or query2 because they have different types (this is what my ide auto completes):

val query1: TableQuery[ListItems] = listItems
val query2: Query[ListItems, ListItems.TableElementType, Seq] = listItems.filter(_.listId===1)

Where moreover the ListItems in ListItems.TableElementType causes a compiler error: "not found: value ListItems".

I also tried to pass the action instead of the query to see if I have more luck, but this was not the case. The type is the same but ListItems.TableElementType still shows the error "not found: value ListItems".

val action1: DBIOAction[Seq[ListItems.TableElementType], ListItems.TableElementType, Effect.Read] = query1.result 
val action2: DBIOAction[Seq[ListItems.TableElementType], ListItems.TableElementType, Effect.Read] = query2.result

How can I pass these queries or actions as parameter to a common method?

1

There are 1 answers

1
Aldo Stracquadanio On BEST ANSWER

It's a bit hard not having the table definition, but I believe that you can pass both queries by having as a parameter type something like this:

private def findListItems(query: Query[ListItems, _, Seq]): Future[Foo] = {  ... }

The _ in there should work but you can make it explicit by using the proper type of your table (i.e. the one you pass to the Table constructor when you extend it in your definition).

You can achieve the same with DBIOs, just use DBIO[YourType] as the parameter type (where again YourType is the type that you are mapping in the table definition).