0

I wonder where I can find the exceptions raised by such a code:

def readFromDB: String = {
    db_sqlite_xml.withSession {
      implicit db: Session =>
        xmlQuery.first.text

    }
  }

I can't find it in the slick scaladoc (http://slick.typesafe.com/doc/2.0.1/api/#package); I searched the method "first" in the javadoc's tableQuery class, but without any success.

thanks.

olivier

ps : here is my answer, it's working :

def readFromDB: String = {
    db_sqlite_xml.withSession {
      implicit db: Session =>
        xmlQuery.firstOption.map(u=>u.text).getOrElse("")
    }
  }
}

thanks for the answer, it helped me.

lolveley
  • 1,659
  • 2
  • 18
  • 34

1 Answers1

1

The method belongs to the UnitInvoker trait, from the scaladoc:

final def first()(implicit session: SessionDef): R

Execute the statement and return the first row of the result set. 
If the result set is empty, a NoSuchElementException is thrown.

If I can give you an advice, instead of trying catching exceptions you should use firstOption:

final def firstOption()(implicit session: SessionDef): Option[R]

Execute the statement and return the first row of the result set wrapped in Some, 
or None if the result set is empty.

In this way you could param-match on the query result like this:

def readFromDB: String = {
  db_sqlite_xml.withSession {
    implicit db: Session =>
      xmlQuery.firstOption match {
        case Some(value) => value.text
        case _ => // handle no result
      }
  }
}
cvogt
  • 11,260
  • 30
  • 46
Ende Neu
  • 15,581
  • 5
  • 57
  • 68