1
votes

I have two tables that are identical, each in a different database. Also, the tables have different names.

I have hardcoded the name of the table in the class BankDB:

class BankDB(tag: Tag) extends Table[Bank](tag, "banks1") {

  def sk = column[Int]("sk", O.PrimaryKey)
  def name = column[String]("name")
 // other columns

What I need is, depending on the database name, to set the name of the table, like so:

 val tableName = if (dbName == "DB1") "banks1" else "banks2"

And then use tableName in TableQuery to have Slick point to the correct table:

    val db = Database.forConfig(dbName)
    try {
      val banks = TableQuery[BankDB](tableName)  // <== this doesn't work
      val future = db.run(banks.filter(_.sk === sk).result)
      val result = Await.result(future, Duration.Inf)
      result
    } finally db.close

Is this possible? or I need to define to classes, one for each database?

1
Have a look here. Also, for table names, you could parametrize DAO class with tableName: String for example...insan-e

1 Answers

0
votes
class BankDB(tag: Tag,tableName: String) extends Table[Bank](tag, tableName) 
def getTableQuery(tableName: String) = 
   TableQuery[BankDB]((t: slick.lifted.Tag) => new BankDB(t, tableName))