Created
November 20, 2011 01:41
-
-
Save tototoshi/1379676 to your computer and use it in GitHub Desktop.
ScalaQueryでCase文を実行する
This file contains hidden or 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
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