assume have database contains 2 tables: coffee , suppliers , have corresponding case classes , tables, in documentation:
import scala.slick.driver.mysqldriver.simple._ import scala.slick.lifted.{provenshape, foreignkeyquery} // suppliers table 6 columns: id, name, street, city, state, zip class suppliers(tag: tag) extends table[(int, string, string, string, string, string)](tag, "suppliers") { def id: column[int] = column[int]("sup_id", o.primarykey) // primary key column def name: column[string] = column[string]("sup_name") def street: column[string] = column[string]("street") def city: column[string] = column[string]("city") def state: column[string] = column[string]("state") def zip: column[string] = column[string]("zip") // every table needs * projection same type table's type parameter def * : provenshape[(int, string, string, string, string, string)] = (id, name, street, city, state, zip) } // coffees table 5 columns: name, supplier id, price, sales, total class coffees(tag: tag) extends table[(string, int, double, int, int)](tag, "coffees") { def name: column[string] = column[string]("cof_name", o.primarykey) def supid: column[int] = column[int]("sup_id") def price: column[double] = column[double]("price") def sales: column[int] = column[int]("sales") def total: column[int] = column[int]("total") def * : provenshape[(string, int, double, int, int)] = (name, supid, price, sales, total) // reified foreign key relation can navigated create join def supplier: foreignkeyquery[suppliers, (int, string, string, string, string, string)] = foreignkey("sup_fk", supid, tablequery[suppliers])(_.id) } now assume want join:
val result = { c <- coffees s <- suppliers if c.supid === s.id } yield (c.name, s.name) and here dealing result complicated ( , it's more complicated if have lot of joins) because need remember order of names, know _._1 or _._2 refer ... etc.
question 1 there way change type of result table of new class contains desired columns ?
question 2 here way can't finish it, construct case class example:
case class joined(names: string,namec: string) and after construct corresponding table don't know how
class joineds extends table[joinedclass] { //todo } and when write join can write ( can transform result type joined) :
val result = { c <- coffees s <- suppliers if c.supid === s.id } yield (c.name, s.name).as(joinds) thank you.
can define like:
val result = { c <- coffees s <- suppliers if c.supid === s.id } yield joined(c.name, s.name) and tuck away in convenient place?
Comments
Post a Comment