Skip to content

Instantly share code, notes, and snippets.

@Jacoby6000
Last active August 29, 2015 14:17
Show Gist options
  • Save Jacoby6000/2c8c2f92d0fe92346655 to your computer and use it in GitHub Desktop.
Save Jacoby6000/2c8c2f92d0fe92346655 to your computer and use it in GitHub Desktop.
trait Queryable {
def id: Rep[Long]
}
trait BaseDao[T <: Queryable] {
// Import the query language features from the driver
val driver: JdbcProfile
val table: TableQuery[T]
import driver.api._
type RecordType = table.shaped.shape.Unpacked
def create(implicit session: Session) =
table.ddl.create
def insert(record: RecordType)(implicit session: Session) =
table += record
def get(id: Long)(implicit session: Session)/**: Option[RecordType]**/ = {
table.filter(_.id === id)
}
}
class GenericDao[T <: Queryable](val props: TableQuery[T])(implicit val driver: JdbcProfile) extends BaseDao[T]
case class SomeRow(id: Option[Long] = None, name: String, date: DateTime) extends DBObject
class SomeRows(tag: Tag) extends Table[SomeRow](tag, "some_rows") with Queryable {
def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
def name = column[String]("name")
def date = column[DateTime]("date")
def * = (id ?, name, date) <> (SomeRow tupled _, SomeRow unapply _)
}
val dao = GenericDao[SomeRows](slick.driver.PostgresDriver, TableQuery[SomeRows])
dao.insert(SomeRow(None, "test", new DateTime))
/* this fails with:
[error] type mismatch;
[error] found : SomeRow
[error] required: dao.RecordType
[error] (which expands to) SomeRows#TableElementType
And I THOUGHT that table.shaped.shape.Unpacked (SomeRows#TableElementType) _is_ SomeRow.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment