Skip to content

Instantly share code, notes, and snippets.

@kryton
Created March 31, 2017 16:21
Show Gist options
  • Save kryton/423d5d680eb7cb7dafec6c1a2d8d890b to your computer and use it in GitHub Desktop.
Save kryton/423d5d680eb7cb7dafec6c1a2d8d890b to your computer and use it in GitHub Desktop.
bill of parts/heirarchial queries
// Slick 2.x / play 2.4
final def managementTree()(implicit session:Session):Set[String] = {
managerTreeIter(Set(this.login.toLowerCase))
}
@tailrec
private def managerTreeIter(acc:Set[String])(implicit session:Session):Set[String] = {
this.manager match {
case None => acc
case Some(mgr) => mgr.managerTreeIter( acc ++Set( mgr.login.toLowerCase))
}
}
// ** Slick 3.2.X / play 2.6 **
def managementTreeUp(login:String):Future[Set[EmprelationsRow]] = {
def managementTreeUpI(login: String, acc: Future[Set[EmprelationsRow]]): Future[Set[EmprelationsRow]] = {
manager(login.toLowerCase).map {
case Some(m) => managementTreeUpI(m.login, acc.map { setMgr => setMgr ++ Set(m) })
case None => acc
}.flatMap(identity)
}
managementTreeUpI(login, Future(Set.empty))
}
@kryton
Copy link
Author

kryton commented Mar 31, 2017

manager function is
def manager(login:String): Future[Option[EmprelationsRow]] = {
val qry = (for {
emp <- Emprelations if emp.login === login.toLowerCase
mgr <- Emprelations if mgr.login === emp.managerid
} yield mgr).result.headOption
db.run(qry)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment