Slick insert based on previous readings

32 views
Skip to first unread message

Robert Koziol

unread,
Jun 30, 2016, 12:30:46 PM6/30/16
to scala-user
Dear all,

I have a standalone application of scala Play Framework. I am trying to perform a DB insert based on the values I have previously read. I read a great number of tutorials and documentation and I analized all these simple examples of slick operations. However I am still stuck with my problem.

I know that if we want to run the DB code sequentially we need to use 'for comprehension'.

My code is something like:

val bookiesOdds = for {
    firstBoxer <- boxerDao.findByName(event.firstBoxer.firstName, event.firstBoxer.middleName, event.firstBoxer.lastName) map {_.getOrElse(throw new NoBoxerException("No boxer " + event.firstBoxer.fullName + " in database!"))}
    secondBoxer <- boxerDao.findByName(event.secondBoxer.firstName, event.secondBoxer.middleName, event.secondBoxer.lastName) map {_.getOrElse(throw new NoBoxerException("No boxer " + event.secondBoxer.fullName + " in database!"))}
    fight <- fightDao.findOrCreateFight(firstBoxer, secondBoxer, new java.sql.Date(event.date.getTime))
    oddsId <- bookiesDao.createOdds(fight.id, event.periods.head.firstOdds.getOrElse(0), event.periods.head.drawOdds, event.periods.head.secondOdds.getOrElse(0), "pinnaclesports")
} yield {
    fight
}


Each dao operation is one db.run call. FirstBoxer and secondBoxer are read from DB without any problem. Then I would like to check if the fight between these boxers already exists. In this case findOrCreateFight should return that fight. Otherwise findOrCreateFight should create a new fight and return this newly created fight. Each case returned fight's id is a parameter for the last call of createOdds function.

So the findOrCreateFight function should look like this:

def findOrCreateFight(firstBoxer: BoxersRow, secondBoxer: BoxersRow, eventDate: java.sql.Date): Future[FightsRow] = {
    for {
        fight <- findByBoxersAndDate(firstBoxer, secondBoxer, eventDate)
        if (fight == None)      
            f <- createAndFindFight(firstBoxer, secondBoxer, eventDate)
    } yield fight.getOrElse(throw new NoFightException("Can not find a fight in DB"))
}

But of course this 'if' condition inside is not possible and it seems that invoking the code invokes createAndFindFight each time which leads for DB's Duplicate entry errors.. Could anyone guide me how to fix the findOrCreateFight function?

Best Regards
 

Egon Nijns

unread,
Jul 14, 2016, 1:48:47 AM7/14/16
to Robert Koziol, scala-user

You don't need a for comprehension here:
findByBoxersAndDate(firstBoxer, secondBoxer, eventDate).getOrElse(createAndFindFight(firstBoxer, secondBoxer, eventDate)) should do

Op 30-jun.-2016 18:30 schreef "Robert Koziol" <rkoz...@gmail.com>:
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages