0
votes

I did in slick abstract table:

abstract class SubResourceTabledef[E, A, I, U](tag: Tag, name: String) extends Table[E](tag, name) {
  val id = column[Long]("id", O.PrimaryKey, O.AutoInc)
  val version = column[Long]("version", O.Default(0L))
  val resource_id = column[Long]("resource_id")

  def * : ProvenShape[E]
  def allMapped: MappedProjection[E, A]

  def insertMapped: MappedProjection[E, I]
  def updateMapped: MappedProjection[E, U]
}

so later based on this I can create:

  type EntityType = SomeThingResource
  type AllProjectionTuple = (Option[Long], Option[Long], DateTime, Option[Long], Option[Long], Option[Long], Long)
  type InsertProjectionTuple = (Option[Long], Option[Long], Option[Long], Long)
  type UpdateProjectionTuple = (Long, Option[Long], Option[Long], Option[Long])

  class SomeThingResourceTabledef(tag: Tag) extends SubResourceTabledef[SomeThingResource, AllProjectionTuple, InsertProjectionTuple, UpdateProjectionTuple](tag, tablenameConverter("Table_name")) {
    val a = column[Option[Long]]("a")
    val b = column[Option[Long]]("b")
    val c = column[Option[Long]]("c")

    ...

  }

I can create TableQuery by this:

 object TQDomain extends TableQuery(new ArticleGoodsMovementTabledef(_))

but then I dont know how to pass it to my repository where I do all queries:

  class SubResourceCommonRepository[Entity <: SubResource, A, I, U, TableDef <: SubResourceTabledef[Entity, A, I, U]](subResourceTabledef: TableDef)

Any ideas how to deal with it ? thanks!

1

1 Answers

1
votes

Normally TableQuery is created like TableQuery[SomeThingResourceTabledef], TableQuery[TableDef] ... so that you can write

TableQuery[SomeThingResourceTabledef].filter(...).map(...)

for SELECT ... FROM Table_name WHERE ....

http://slick.lightbend.com/doc/3.3.1/gettingstarted.html#querying

But

class SubResourceCommonRepository[Entity <: SubResource, A, I, U, TableDef <: SubResourceTabledef[Entity, A, I, U] : ClassTag](subResourceTabledef: TableDef) {
  val query = TableQuery[TableDef]
}

will not work. It doesn't compile with Error: class type required but TableDef found. The thing is that TableQuery#apply is implemented as a macro using constructor of TableDef, which doesn't exist upon macro expansion.

Try to use overloaded TableQuery#apply(Tag => E)

class SubResourceCommonRepository[Entity <: SubResource, A, I, U, TableDef <: SubResourceTabledef[Entity, A, I, U]](subResourceTabledef: Tag /* Added!!! */ => TableDef) {
  val query = TableQuery[TableDef](subResourceTabledef(_))
}

new SubResourceCommonRepository[SomeThingResource, AllProjectionTuple, InsertProjectionTuple, UpdateProjectionTuple, SomeThingResourceTabledef](new SomeThingResourceTabledef(_))