Skip to content

Instantly share code, notes, and snippets.

@akiraak
Last active March 15, 2016 12:40
Show Gist options
  • Select an option

  • Save akiraak/bfe7387ed6cfdd972535 to your computer and use it in GitHub Desktop.

Select an option

Save akiraak/bfe7387ed6cfdd972535 to your computer and use it in GitHub Desktop.
高速化のため Slick2 で SQL に書きなおしたときの手法 ref: http://qiita.com/akiraak/items/b9b66ae66799720b16fd
import play.api.db.slick._
import scala.slick.jdbc.{StaticQuery => Q}
// 全てのユーザーの id を返す
def users(): List[Int] = {
DB withSession {
Q.queryNA[Int]("""
select id from users
""").list
}
}
// 全てのユーザーの id と 名前を返す
def select(): List[(Int, String)] = {
DB withSession {
Q.queryNA[(Int, String)]("""
select id, name from users
""").list
}
}
+--------------------+
| Field |
+--------------------+
| id |
| name |
| email |
+--------------------+
case class User (
id: Int,
email: String,
name: String
)
email <-> name の値が入れ替わってしまう
// 例
case class User (
id: Int,
name: String,
email: String,
createdAt: DateTime
)
// ユーザーを取得
def users(): List[User] = {
DB withSession {
implicit val getUser = GetResult(r =>
User(r.<<, r.<<, r.<<, new DateTime(r.nextTimestamp))
)
Q.queryNA[User]("""
select * from users
""").list
}
}
// ユーザーとユーザーのログ情報を取得
def users(): List[(User, UserLog)] = {
DB withSession {
implicit val getUser = GetResult(r => User(r.<<, r.<<, r.<<))
implicit val getUserLog = GetResult(r => UserLog(r.<<, r.<<, r.<<))
Q.queryNA[(User, UserLog)]("""
select users.*, userLogs.* from users
inner join userLogs on userLogs.userId = users.id
""").list
}
}
// 修正前
slickUsers.filter(
_.id === id
).firstOption
// 修正後
val query = Q.query[Int, Int]("""
select id from users
where id = ?
""")
query(id).firstOption
// 名前が一致するユーザーを返す
def user(name: String): Option[Int] = {
DB withSession {
val query = Q.query[String, Int]("""
select id from users
where name = ?
""")
query(name).firstOption
}
}
// city, age が一致するユーザーを返す
def uesrs(city: String, age: Int): Option[Int] = {
DB withSession {
val query = Q.query[(Int, Int), Int]("""
select id from users
where age = ? and city = ?
""")
query((age, city))..firstOption
}
}
import org.joda.time.DateTime
// 指定された時間より後に作成されたユーザーを返す
def users(dateTime: DateTime): List[Int] = {
DB withSession {
val query = Q.query[String, Long]("""
select id from users
where createdAt > ?
""")
query(
dateTime.toString("yyyy-MM-dd HH:mm:ss")
).list
}
}
// 指定されたユーザー id が存在すれば返す
def users(ids: List[Int]): List[Int] = {
DB withSession {
val in = ids.mkString(",")
Q.queryNA[Int)](s"""
select id from users
where id in (${in})
""").list
}
}
// ユーザーを取得
def users(ids: List[Int]): List[Int] = {
DB withSession {
val in = ids.isEmpty match {
case false => "false"
case true => "id in (" ids.mkString(",") + ")"
}
Q.queryNA[Int](s"""
select id, screenName from users
where ${in}
""").list
}
}
select id from users
where false or age >= 20
// ユーザーを取得
def users(offsetLimitOpt: Option[(Int, Int)]): List[Int] = {
DB withSession {
val queryOffsetLimit = (offsetOpt, limitOpt) match {
case (Some(offset), Some(limit)) => s"limit ${offset}, ${limit}"
case _ => ""
}
Q.queryNA[Long](s"""
select id, screenName from users
${queryOffsetLimit}
""").list
}
}
case class User (
id: Int,
name: String,
email: String
)
// ユーザーを取得
def users(): List[User] = {
DB withSession {
implicit val getUser = GetResult(r =>
User(r.<<, r.<<, r.<<)
)
Q.queryNA[User]("""
select * from users
""").list
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment