Reputation: 45476
I'm having some trouble composing different query components into a single Query. My goal is to create a set of traits (e.g. SoftDeletable, HasName, SortedByName, WithTimestamps) that I can simply mix-in to Table objects to add that behavior.
The ideal would look like:
abstract class BaseModel[Tuple <: Product,CaseClass](tableName: String)
extends Table[Tuple](tableName) {
def id = column[Int]("id", O.AutoInc, O.PrimaryKey)
def mapped: MappedProjection[CaseClass, TupleClass]
def allQuery = this.map(_.mapped)
final def all = database.withSession { implicit session: Session =>
allQuery.list()
}
...
}
trait SoftDeletable[Tuple <: Product, CaseClass]
extends BaseModel[Tuple,CaseClass] {
def isActive = column[String]("is_active")
def * = super.* ~ isActive
def allQuery = /* here, I'd like to compose super.allQuery
with a filter that returns rows where isActive is true */
}
trait HasName[Tuple <: Product] extends Table[Tuple] {
def name = column[String]("name")
def * = super.* ~ name
}
trait SortedByName[Tuple <: Product] extends HasName[Tuple {
override def allQuery = super.allQuery /* compose somehow
with (_ <- Query orderBy name */
}
Can I do these kinds of things with ScalaQuery? The main sticking points are:
How do I cleanly compose the filters in SoftDeletable.allQuery
and the sort in SortedByName.allQuery
with BaseModel.allQuery
?
By adding columns in subclass implementations of the *
method, the tuple type parameter to Table
no latter matches - is there a way for these traits to incrementally add new types to the columns tuple in the ultimate concrete class? (I don't expect there to be, but it would be nice if there was something I'm missing).
I need to repeat the long tuple declaration in every trait, which becomes very unwieldy if a table has five or six columns. Is there something I can do with type members to avoid having to do things like:
case class Foo
class Foos[(Int,Int,Boolean,String), Foo] extends
Table[(Int,Int,Boolean,String)] with
SoftDeletable[(Int,Int,Boolean,String), Foo] with
SortedByName[(Int,Int,Boolean,String), Foo] with
HasName[(Int,Int,Boolean,String)] {
}
Can I avoid all this repetition? Based on a suggestion from jesnor on IRC, I was able to avoid some of this like so:
abstract class SoftDeletableBaseModel[TupleClass <: Product, CaseClass](tableName: String)
extends BaseModel[TupleClass, CaseClass](tableName)
with SoftDeletable[TupleClass,CaseClass]
In other words, by combining specific traits together, I don't need to repeat the entire tuple declaration; of course, the disadvantage is that easy mixing-in of various traits is no longer possible - I need to create lots of specific subclasses to avoid this repetition. Is there another way?
Update: So I realized that I don't need to use separate CaseClass and TupleClass type parameters. Since case classes implement Product*
, you can just pass the case class name into Table, which solves the problem in 3:
trait SoftDeletable[CaseClass] extends BaseModel[CaseClass] { ... }
class Models extends BaseModel[Model]("models") with SoftDeletable[Model] { ... }
Upvotes: 13
Views: 472
Reputation: 2534
If your issue is only adding the sort, isn't it just a matter of flatMap?
def sortBy[T,U,C](q: Query[T,U], col: NamedColumn[C]) = q.flatMap(_ => Query orderBy col)
Upvotes: 1