Last active
March 15, 2016 12:40
-
-
Save akiraak/bfe7387ed6cfdd972535 to your computer and use it in GitHub Desktop.
高速化のため Slick2 で SQL に書きなおしたときの手法 ref: http://qiita.com/akiraak/items/b9b66ae66799720b16fd
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 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 | |
| } | |
| } |
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
| // 全てのユーザーの id と 名前を返す | |
| def select(): List[(Int, String)] = { | |
| DB withSession { | |
| Q.queryNA[(Int, String)](""" | |
| select id, name from users | |
| """).list | |
| } | |
| } |
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
| +--------------------+ | |
| | Field | | |
| +--------------------+ | |
| | id | | |
| | name | | |
| | email | | |
| +--------------------+ | |
| case class User ( | |
| id: Int, | |
| email: String, | |
| name: String | |
| ) | |
| email <-> name の値が入れ替わってしまう |
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
| // 例 | |
| 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 | |
| } | |
| } |
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
| // ユーザーとユーザーのログ情報を取得 | |
| 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 | |
| } | |
| } |
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
| // 修正前 | |
| slickUsers.filter( | |
| _.id === id | |
| ).firstOption |
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
| // 修正後 | |
| val query = Q.query[Int, Int](""" | |
| select id from users | |
| where id = ? | |
| """) | |
| query(id).firstOption |
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
| // 名前が一致するユーザーを返す | |
| def user(name: String): Option[Int] = { | |
| DB withSession { | |
| val query = Q.query[String, Int](""" | |
| select id from users | |
| where name = ? | |
| """) | |
| query(name).firstOption | |
| } | |
| } |
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
| // 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 | |
| } | |
| } |
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.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 | |
| } | |
| } |
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
| // 指定されたユーザー 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 | |
| } | |
| } |
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
| // ユーザーを取得 | |
| 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 | |
| } | |
| } |
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
| select id from users | |
| where false or age >= 20 |
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
| // ユーザーを取得 | |
| 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 | |
| } | |
| } |
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
| 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