iamlorand
iamlorand

Reputation: 43

Same query on different tables - Scala Slick

I've tried eliminate duplicated code in scala but with no success.

What I have:

(table.filter(someFilters).map(columns).result map { res =>
 res foreach { case (data1, data2, data3) =>
 //some logic
}}) >>
(differentTable.filter(sameFilters).map(sameColumns).result map { res =>
 res foreach { case (data1, data2, data3) =>
 //other logic
}})

return type is DBIOAction

How is it possible to not duplicate this code and execute logic based on what type of table it is?

*The two tables have no relation between them

Upvotes: 1

Views: 103

Answers (1)

Amit Prasad
Amit Prasad

Reputation: 725

Maybe you can do one thing here.

 val table1 = TableQuery[Table1]
 val table2 = TableQuery[Table2]

Let's say you have table type T

def  findAll[T](table: T)  = {
   (table.filter(someFilters).map(columns).result map { res =>
   res foreach { case (data1, data2, data3) =>
    //some logic
  }}) 
  } 

Since your findAll takes generic table name and you mentioned you have all the columns identical, then call findAll like below :

     findAll[Table1](table1) 
     findAll[Table2](table2) 

Hope this helps

Upvotes: 1

Related Questions