Last active
November 27, 2015 15:38
-
-
Save vicpara/f79b561307a56f1c05e1 to your computer and use it in GitHub Desktop.
A SLICK scala Play Framework example of DAO class
This file contains 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 models._ | |
import org.joda.time.DateTime | |
import play.api.Logger | |
import slick.dbio._ | |
import slick.driver.H2Driver | |
import slick.driver.H2Driver.api._ | |
import slick.jdbc.meta.MTable | |
import scala.concurrent.{Future, Await} | |
import scala.util.{Failure, Success} | |
import slick.dbio.DBIO | |
import slick.driver.H2Driver | |
import slick.jdbc.meta.MTable | |
import scala.concurrent.duration._ | |
import scala.concurrent.ExecutionContext.Implicits.global | |
case class Game(gID: Option[Long] = None, kind: Int, name: String) | |
class Games(tag: Tag) extends Table[Game](tag, GamesDao.tableName) { | |
def jID = column[Long]("ID", O.PrimaryKey, O.AutoInc) | |
def kind = column[Int]("kind") | |
def name = column[String]("name") | |
def * = (jID.?, kind, name) <>((Game.apply _).tupled, Game.unapply) | |
} | |
object GamesDao extends SurgeonsTableObject { | |
val tableGames = TableQuery[Games] | |
val db = Database.forConfig("h2mem1") | |
override def tableName: String = "UberGames" | |
override def tableSchema: H2Driver.DDL = tableGames.schema | |
def all = { | |
val q = tableGames | |
db.run(q.result) | |
} | |
def insertGame(newGame: Game): Future[Int] = { | |
val q = tableGames += newGame | |
db.run(q) | |
} | |
def insertGames(newGames: List[Game]): Future[Option[Int]] = { | |
val q = tableGames ++= newGames | |
db.run(q) | |
} | |
def filterByName(names: List[String]) = { | |
val q = tableGames.filter(c => c.name inSetBind names) | |
println("FILTERBYNAME: " + q.result.statements.head) | |
db.run(q.result) | |
} | |
} | |
case object ToyTest { | |
// using this in application.conf | |
// h2mem1 = { | |
// driver = org.h2.Driver | |
// url = "jdbc:h2:mem:test1" | |
// connectionPool = disabled | |
// keepAliveConnection = true | |
// username=ss | |
// password="" | |
// } | |
val db = Database.forConfig("h2mem1") | |
def initDB() = { | |
Logger.info(s"INITDB: Creating table [UberGames]") | |
Await.result(db.run(DBIO.seq(GamesDao.tableSchema.create)), 10.seconds) | |
val resInsert = Await.result(GamesDao.insertGames(List( | |
Game(None, kind = 1, "a"), | |
Game(None, kind = 1, "b"), | |
Game(None, kind = 1, "c"), | |
Game(None, kind = 1, "d"), | |
Game(None, kind = 1, "e"), | |
Game(None, kind = 1, "f"), | |
Game(None, kind = 1, "g"), | |
Game(None, kind = 1, "h"), | |
Game(None, kind = 1, "i"), | |
Game(None, kind = 1, "j"), | |
Game(None, kind = 1, "k"), | |
Game(None, kind = 1, "l"), | |
Game(None, kind = 1, "m"), | |
Game(None, kind = 1, "n") | |
)), 10.seconds).get | |
println(s"Inserted $resInsert/14") | |
} | |
def getAllTables: Future[Vector[String]] = db.run(MTable.getTables).map(e => e.map(t => t.name.name)) | |
def countTablesWithName(tableName: String): Future[Int] = db.run(MTable.getTables).map { ts => | |
Logger.info("Displaying all tables found if any:") | |
ts.foreach(t => Logger.info(t.name.name)) | |
val num = ts.count(t => t.name.name == tableName) | |
Logger.info(s"SCHEMA: Found $num table with name=$tableName") | |
num | |
} | |
def main(args: Array[String]): Unit = { | |
println("Calling InitDB") | |
initDB() | |
println("All tables:" + Await.result(getAllTables, 10.seconds).toList) | |
Await.result(countTablesWithName(GamesDao.tableName), 10.seconds) | |
println("All table [UberGames] records:\n" + Await.result(GamesDao.all, 10.seconds).toList.mkString("\n")) | |
val coolNames = List("a", "e", "g", "k", "zzzz", "yyyy", "xxxx") | |
println("Filtering only on: " + coolNames) | |
println("All table records:\n" + Await.result(GamesDao.filterByName(coolNames), 10.seconds).toList.mkString("\n")) | |
} | |
} | |
//Calling InitDB | |
// [info] 2015-11-21 19:28:25 +0000 - [INFO] - from application in main | |
// INITDB: Creating table [UberGames] | |
// | |
//Inserted 14/14 | |
//All tables:List(UberGames) | |
// [info] 2015-11-21 19:28:25 +0000 - [INFO] - from application in ForkJoinPool-1-worker-13 | |
//Displaying all tables found if any: | |
// | |
//[info] 2015-11-21 19:28:25 +0000 - [INFO] - from application in ForkJoinPool-1-worker-13 | |
//UberGames | |
// | |
// [info] 2015-11-21 19:28:25 +0000 - [INFO] - from application in ForkJoinPool-1-worker-13 | |
//SCHEMA: Found 1 table with name=UberGames | |
// | |
// All table[UberGames] records: | |
// Game (Some(1), 1, a) | |
// Game(Some(2), 1, b) | |
// Game(Some(3), 1, c) | |
// Game(Some(4), 1, d) | |
// Game(Some(5), 1, e) | |
// Game(Some(6), 1, f) | |
// Game(Some(7), 1, g) | |
// Game(Some(8), 1, h) | |
// Game(Some(9), 1, i) | |
// Game(Some(10), 1, j) | |
// Game(Some(11), 1, k) | |
// Game(Some(12), 1, l) | |
// Game(Some(13), 1, m) | |
// Game(Some(14), 1, n) | |
// Filtering only on: List (a, e, g, k, zzzz, yyyy, xxxx) | |
// FILTERBYNAME: select "ID", "kind", "name" from "UberGames" where "name" in(?, ?, ?, ?, ?, ?, ?) | |
// All table records: | |
// Game (Some(1), 1, a) | |
// Game(Some(5), 1, e) | |
// Game(Some(7), 1, g) | |
// Game(Some(11), 1, k) | |
//The build.sbt file I'm using | |
organization := "com.github.vicpara" | |
name := "game-toy-dao" | |
version := "0.1.0-SNAPSHOT" | |
lazy val root = (project in file(".")).enablePlugins(PlayScala) | |
scalaVersion := "2.11.7" | |
libraryDependencies ++= Seq( | |
jdbc, | |
cache, | |
ws, | |
specs2 % Test, | |
"org.specs2" %% "specs2-scalacheck" % "2.4.9-scalaz-7.0.6" % "test" withSources() withJavadoc(), | |
"com.typesafe.play" %% "play-slick" % "1.1.1" withSources() withJavadoc(), | |
"com.typesafe.slick" % "slick_2.11" % "3.1.0" withSources() withJavadoc(), | |
"com.h2database" % "h2" % "1.4.190", | |
"org.rogach" %% "scallop" % "0.9.5" withSources() withJavadoc(), | |
"net.databinder.dispatch" %% "dispatch-core" % "0.11.3" withSources() withJavadoc() | |
) | |
resolvers += "scalaz-bintray" at "http://dl.bintray.com/scalaz/releases" | |
// Play provides two styles of routers, one expects its actions to be injected, the | |
// other, legacy style, accesses its actions statically. | |
routesGenerator := InjectedRoutesGenerator | |
fork in run := true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment