Created
November 2, 2012 12:15
-
-
Save wshino/4000976 to your computer and use it in GitHub Desktop.
Scalaquery select firstOption
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package models | |
import play.api.db._ | |
import play.api.Play.current | |
import org.scalaquery.ql._ | |
import extended.{ExtendedTable => Table} | |
import org.scalaquery.ql.TypeMapper._ | |
import org.scalaquery.session._ | |
import org.scalaquery.ql.extended.MySQLDriver.Implicit._ | |
case class Task(id: Long, label: String) | |
object Tasks extends Table[Task]("Task") { | |
lazy val database = Database.forDataSource(DB.getDataSource()) | |
def id = column[Long]("id", O AutoInc, O PrimaryKey, O NotNull) | |
def label = column[String]("label", O NotNull) | |
def * = id ~ label <>(Task, Task.unapply _) | |
// 一件取得 | |
def find(id: Long) = database.withSession { | |
implicit db: Session => | |
val q = Tasks.where(_.id is id) | |
// q.first() // 値がなかった場合は例外が飛ぶ | |
q.firstOption // 値がなかった場合は None が返る | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment