Skip to content

Instantly share code, notes, and snippets.

@tototoshi
Created November 20, 2011 01:41
Show Gist options
  • Save tototoshi/1379676 to your computer and use it in GitHub Desktop.
Save tototoshi/1379676 to your computer and use it in GitHub Desktop.
ScalaQueryでCase文を実行する
import org.scalaquery._
import ql._
import basic.{BasicTable => Table, _}
import BasicDriver.Implicit._
import session.{ Session, Database }
/*
scala_query_example=> select * from Coffees;
id | name | supid | price
----+-----------------+-------+-------
1 | Colombian | 101 | 100
2 | Espresso | 150 | 200
3 | Colombian_Decat | 42 | 300
(3 rows)
*/
object Coffees extends Table[(Int, String, Int, Int)]("coffees") {
def id = column[Int]("id")
def name = column[String]("name")
def supid = column[Int]("supid")
def price = column[Int]("price")
def * = id ~ name ~ supid ~ price
}
val db = Database.forURL("jdbc:postgresql:scala_query_example", driver="org.postgresql.Driver", user="user", password="password")
db withSession { s: Session =>
{
for {
c <- Coffees
} yield {
c.id ~ c.name ~ c.supid ~
(Case when c.price <= 100 then "cheap"
when c.price <= 200 then "medium"
otherwise "expensive")
}
}.list()(s).foreach(println)
}
/*
result:
(1,Colombian,101,cheap)
(2,Espresso,150,medium)
(3,Colombian_Decat,42,expensive)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment