Last active
December 19, 2015 15:39
-
-
Save jg/5978117 to your computer and use it in GitHub Desktop.
Scala Slick Type Problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.slick.driver.PostgresDriver.simple._ | |
import scala.slick.session.{Database => SlickDatabase} | |
import Database.threadLocalSession | |
import scala.slick.jdbc.{GetResult, StaticQuery => Q} | |
import Q.interpolation | |
import scala.slick.lifted.CanBeQueryCondition | |
object SourcesTable extends Table[Source]("sources") { | |
def id = column[Long]("id", O.PrimaryKey, O.AutoInc) | |
def name = column[String]("name") | |
def sourceType = column[String]("type", O.NotNull) | |
def * = id ~ name ~ sourceType <> (Source.apply _, Source.unapply _) | |
} | |
trait Database { | |
val database = Database.forURL("jdbc:postgresql:comet", driver = "org.postgresql.Driver", user = "comet", password = "comet") | |
} | |
trait Finders[R] { | |
this: DAO[R] with Database => | |
def query = Query(table) | |
def find[T](f: table.type => T)(implicit wt: CanBeQueryCondition[T]): Option[R] = database withSession { | |
query.filter(f).firstOption | |
} | |
} | |
case class DAO[T](val table: Table[T]) | |
object SourcesDAO extends DAO(SourcesTable) with Finders[Source] with Database { | |
def findByType(sourceType: String): Option[Source] = database withSession { | |
find(_.sourceType === sourceType) | |
} | |
def all: List[Source] = database withSession {for {source <- query.list} yield source} | |
def count: Int = database withSession {query.list.length} | |
} | |
// SOURCE.scala | |
trait Source { | |
/** Source requires an AggregationMethod */ | |
this: AggregationMethod => | |
var id: Long = -1 | |
var name: String = "" | |
/** Use to identify the piece of code responsible for source aggregation */ | |
def sourceType: String | |
def process(data: AggregationData): Seq[JpgFile] = ??? | |
} | |
object Source { | |
val sourceList = List(new ZwanzigMinutenZuerich()) | |
def fromDBTuple(id: Long, name: String, sourceType: String): Option[Source] = { | |
getSourceInstanceByType(sourceType) match { | |
case Some(source) => { | |
source.id = id | |
source.name = name | |
Some(source) | |
} | |
case None => None | |
} | |
} | |
def getSourceInstanceByType(t: String): Option[Source] = sourceList.find(_.sourceType == t) | |
def apply(id: Long, name: String, sourceType: String): Source = { | |
// This assumes we have a class with corresponding sourceType in the Database | |
// Better fail at this point anyway | |
val source = getSourceInstanceByType(sourceType).get | |
source.id = id | |
source.name = name | |
source | |
} | |
def unapply(s: Source) = Some(s.id, s.name, s.sourceType) | |
} | |
[info] Compiling 1 Scala source to /home/jg/workplace/comet-scala/target/scala-2.10/classes... | |
[error] /home/jg/workplace/comet-scala/src/models/SourcesTable.scala:25: type mismatch; | |
[error] found : Finders.this.table.type => T | |
[error] required: slick.driver.PostgresDriver.simple.Table[R] => ? | |
[error] query.filter(f).firstOption | |
[error] ^ | |
[error] /home/jg/workplace/comet-scala/src/models/SourcesTable.scala:39: value sourceType is not a member of slick.driver.PostgresDriver.simple.Table[Source] | |
[error] find(_.sourceType === sourceType) | |
[error] ^ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment