【发布时间】:2016-10-19 12:32:52
【问题描述】:
我对 scala 和 slick 还很陌生。我无法从此函数中获取联系人序列。每个联系人对象都有名字、姓氏和一系列电话号码。姓名和电话号码使用外键存储在各自的表中。
def testContacts: Future[Seq[Contact]] = {
names.result.map { namelist =>
var contacts = Seq[Contact]()
for (n <- namelist) {
println("NAME: " + n) // THIS IS PRINTED!
val phoneNumbers = for {
p <- phones.filter(_.nameId === n.id)
} yield p.phoneNumber
phoneNumbers.result.map { plist =>
contacts = contacts :+ Contact(n.firstName, n.lastName, plist)
// Q: HOW DO I RETURN "contacts" to the caller?
println("CONTACTS: " + plist) // THIS IS **NOT** PRINTED!
}
}
}
Future { Seq[Contact]() } // dummy statement to avoid compilation error
}
从控制台:
scala> Await.result(db.run(names.result), 1000 毫秒) res9: Seq[Example.NameTable#TableElementType] = Vector(Name(George,W,1), Name(John,A,2))
scala> Await.result(db.run(phones.result), 1000 毫秒) res10:Seq[Example.PhoneTable#TableElementType] = 矢量(电话(+1 301 531 1121,1,1),电话(+1 301 748 5192,1,2),电话(+1 301 531 4519,2,3 ), 电话(+1 202 667 9612,2,4), 电话(+1 202 667 4044,2,5))
scala> Await.result(testContacts, 1000 毫秒) res5: Seq[Contacts.Contact] = List()
斯卡拉>
感谢任何帮助。这是完整的源代码:
import scala.concurrent.Await
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
import slick.driver.H2Driver.api._
import scala.util.Try
import scala.concurrent.Future
object Contacts extends App {
val db = Database.forConfig("dbconfig")
case class Contact(firstName: String, lastName: String, phones: Seq[String])
def testContacts: Future[Seq[Contact]] = {
names.result.map { namelist =>
var contacts = Seq[Contact]()
for (n <- namelist) {
println("NAME: " + n)
val phoneNumbers = for {
p <- phones.filter(_.nameId === n.id)
} yield p.phoneNumber
phoneNumbers.result.map { plist =>
contacts = contacts :+ Contact(n.firstName, n.lastName, plist)
// Q: HOW DO I RETURN "contacts" to the caller?
println("CONTACTS: " + plist) // THIS IS NOT PRINTED!
}
}
}
Future { Seq[Contact]() } // dummy statement to avoid compilation error
}
case class Name(firstName: String, lastName: String, id: Long = 0L)
class NameTable(tag: Tag) extends Table[Name](tag, "Names") {
def id = column[Long]("ID", O.PrimaryKey, O.AutoInc)
def firstName = column[String]("FIRSTNAME")
def lastName = column[String]("LASTNAME")
override def * = (firstName, lastName, id) <> (Name.tupled, Name.unapply)
}
lazy val names = TableQuery[NameTable]
case class Phone(phoneNumber: String, nameId: Long, id: Long = 0L)
class PhoneTable(tag: Tag) extends Table[Phone](tag, "Phones") {
def id = column[Long]("ID", O.PrimaryKey, O.AutoInc)
def phoneNumber = column[String]("PHONE_NUMBER")
def nameId = column[Long]("NAME")
override def * = (phoneNumber, nameId, id) <> (Phone.tupled, Phone.unapply)
def name_fk = foreignKey("Phones_Names_ID_FK", nameId, names)(_.id, onUpdate = ForeignKeyAction.Cascade, onDelete = ForeignKeyAction.Cascade)
}
lazy val phones = TableQuery[PhoneTable]
def testNames = Seq(
Name("George", "W"),
Name("John", "A"))
def testPhones = Seq(
Phone("+1 301 531 1121", 1L),
Phone("+1 301 748 5192", 1L),
Phone("+1 301 531 4519", 2L),
Phone("+1 202 667 9612", 2L),
Phone("+1 202 667 4044", 2L))
def populate: DBIOAction[Option[Int], NoStream,Effect.All] = {
for {
_ <- names.schema.drop.asTry andThen names.schema.create
_ <- phones.schema.drop.asTry andThen phones.schema.create
nameCount <- names ++= testNames
phoneCount <- phones ++= testPhones
} yield nameCount
}
}
【问题讨论】: