Slick 2.0 define a generic field search method

import scala.slick.driver.MySQLDriver.simple._ class RichTable[T](tag: Tag, name: String) extends Table[T](tag, name) { case class QueryExt[B](q: Query[RichTable.this.type, B]) { def whereEq[C](col: RichTable.this.type => Column[C], c: C) = { q.filter { fields => col(fields) === c } } } } 

Then he complains

 [error] /home/jilen/workspace/play-slick/src/main/scala/play/slick/SlickQueryExtension.scala:10: value === is not a member of slick.driver.MySQLDriver.simple.Column[C] [error] col(fields) === c [error] ^ [error] /home/jilen/workspace/play-slick/src/main/scala/play/slick/SlickQueryExtension.scala:9: ambiguous implicit values: [error] both value BooleanColumnCanBeQueryCondition in object CanBeQueryCondition of type => scala.slick.lifted.CanBeQueryCondition[scala.slick.lifted.Column[Boolean]] [error] and value BooleanOptionColumnCanBeQueryCondition in object CanBeQueryCondition of type => scala.slick.lifted.CanBeQueryCondition[scala.slick.lifted.Column[Option[Boolean]]] [error] match expected type scala.slick.lifted.CanBeQueryCondition[Nothing] [error] q.filter { fields => [error] ^ [error] two errors found [error] (compile:compile) Compilation failed [error] Total time: 0 s, completed Mar 6, 2014 1:21:48 AM 

There were questions about this, but the answers did not work for 2.0

How to parameterize Scala Slice queries with WHERE clause conditions?

+6
source share
1 answer

Slick has no information about C , so it does not know if it can and how it should match it with the value of the database, and if it can use === on it. Thus, you get a type error. You will need to use the Scala type system to restrict the type to one for which Slick knows how to map it. You can do this by providing a so-called Context Bound, in this case :BaseColumnType .

 def whereEq[C:BaseColumnType](col: RichTable.this.type => Column[C], c: C) = { q.filter { fields => col(fields) === c } } 

BaseColumnType provided by Slick and, using it this way, basically tells the Scala compiler to look for an implicit value of type BaseColumnType[C] in the area where you call whereEq , because then it is usually known that C will actually be. Slick comes with BaseColumnType[Int] , BaseColumnType[String] , etc. Therefore, the Scala compiler can find it on the call site when your C really Int or String in that particular call and thus the information goes further to Slick.

Same for the LiuTiger question. abstract class Crud[..., PK:BaseColumnType] should do the trick, the dash doesn't work with context boundaries. When implementing an abstract DAO, you have to face a lot of problems and overcome your system skills like Scala and learn a little about type inference order, implicit parameters, etc.

+7
source

All Articles