I would like to perform a database insert with some ids taken from previous DB reading. All DB objects generated with slick.codegen.SourceCodeGenerator. I have the following reading function:
def findByName(firstName: String, lastName: String): Future[Option[PersonsRow]] = {
db.run(Persons.filter(person => (person.firstName === firstName && person.lastName === lastName)).result.headOption)
}
I have to perform such a reading twice - because in my second table I have a person1 and person2. So in the end I have got 2 objects of the class Future[Option[PersonsRow]]. Then I would like to perform some insert:
def createMatch(firstPerson: PersonsRow, secondPerson: PersonsRow, eventDate: java.sql.Date): Future[Long] = {
val match = MatchesRow(id = 0, firstPersonId = firstPerson.id, secondPersonId = secondPerson.id, date = eventDate)
db.run(Matches returning Matches.map(_.id) += match)
}
I have heard that the best way to deal with Future objects is just to pass this object further without any explicit waiting for the Future complete. However I guess there is no possibility to pass Future here and I have to use some blocking code to get this Future's values here.
I even prepared a function:
def getFutureValue(person: Future[Option[PersonsRow]]): Option[PersonsRow] = {
val emptyPersonsRow = PersonsRow(0, ...) //empty person
person onComplete {
case Success(bsucc) => {
return bsucc
}
case Failure(bfail) => {
Logger.warn("There are problems with reading person from database: " + bfail.toString)
}
}
return Some(emptyPersonsRow)
}
But it seems not to be working properly (returning person is always an emptyPersonsRow). Besides I believe that I use it wrong - since this Future mechanism is my great enemy and not a friend here :)
Any suggestion to put me in the right direction? Best Regards