Skip to content

Instantly share code, notes, and snippets.

@tototoshi
Created December 22, 2011 12:06
Show Gist options
  • Save tototoshi/1510084 to your computer and use it in GitHub Desktop.
Save tototoshi/1510084 to your computer and use it in GitHub Desktop.
ScalaQueryでcase classへのマッピング
import org.scalaquery._
import ql._
import session.{ Session, Database }
import basic.{ BasicTable => Table }
import org.scalaquery.ql.basic.BasicDriver.Implicit._
object Main extends App {
val db = Database.forURL("jdbc:postgresql:example",
driver="org.postgresql.Driver",
user="username",
password="password")
case class Fruits(id: Long, name: String, price: Int)
/*
Data:
id | name | price
----+--------+-------
1 | apple | 100
2 | orange | 50
3 | grape | 200
(3 rows)
*/
object FruitsTable extends Table[Fruits]("fruits") {
def id = column[Long]("id")
def name = column[String]("name")
def price = column[Int]("price")
def * = id ~ name ~ price <> (Fruits, Fruits.unapply _)
}
val q = for (f <- FruitsTable if f.price > 80) yield f
db withSession { implicit s: Session =>
q.list() foreach println
}
/*
result:
Fruits(1,apple,100)
Fruits(3,grape,200)
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment