Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic table name override in Slick table

Tags:

scala

slick

I have defined a slick table as below on DB view. Every day we create a dated view matching the same structure with name appended with date as T_SUMMARY_ i.e.T_SUMMARY_20131213.

Is there a way in slick where table name can be dynamically overridden to generate required query for a correct dated view.

object TSummary extends Table[(String)]("T_SUMMARY")
{
    def id = column[String]("ROWID", O.PrimaryKey)
    def tNum = column[Int]("T_NUM")
    def tName = column[Int]("T_NAME")
    def * = id
}
like image 348
Mkt281001 Avatar asked Dec 13 '13 06:12

Mkt281001


1 Answers

In Slick 1.0.1:

def mkSummaryTable(name: String) = new Table[(String)](name){
  def id = column[String]("ROWID", O.PrimaryKey)

  def tNum = column[Int]("T_NUM")

  def tName = column[Int]("T_NAME")

  def * = id
}
val current = mkSummaryTable("T_SUMMARY_20131213")
for( r <- Query(current) ) yield r.tName

For Slick 2.0 see https://groups.google.com/d/msg/scalaquery/95Z7AfxKP_4/omGnAtuN8FcJ:

class MyTable(tag: Tag, tableName: String) extends Table[(String)](tag, tableName){
          def id = column[String]("ROWID", O.PrimaryKey)
          def tNum = column[Int]("T_NUM")
          def tName = column[Int]("T_NAME")
          def * = id
 }

val tQ = TableQuery[MyTable]((tag:Tag) => new MyTable(tag, "SomeTableName")) filter ...
like image 175
cvogt Avatar answered Oct 14 '22 04:10

cvogt