Skip to content

Instantly share code, notes, and snippets.

@seratch
Created April 27, 2012 03:09
Show Gist options
  • Save seratch/2505361 to your computer and use it in GitHub Desktop.
Save seratch/2505361 to your computer and use it in GitHub Desktop.
ScalikeJDBC Play20 plugin prototype
package controllers
import play.api._
import play.api.mvc._
import scalikejdbc._
object Application extends Controller {
def index = Action {
val userIdList: List[Int] = DB readOnly { implicit session =>
SQL("select id from user").map(rs => rs.int("id")).toList.apply()
}
// TODO NamedDB('ds1) readOnly { ... }
Ok(views.html.index("Your new application is ready."))
}
}
777:scalikejdbc.play20.ScalikeJDBCPlugin
package scalikejdbc.play20
import play.api._
import play.api.db.BoneCPPlugin
import java.sql.Connection
import scalikejdbc._
class ScalikeJDBCPlugin(app: Application) extends BoneCPPlugin(app) {
override def onStart() {}
private lazy val config = app.configuration.getConfig("db").getOrElse(Configuration.empty)
private def getConfigValue(config: Configuration, name: String, key: String): String = config.getString(name + "." + key) getOrElse {
throw config.reportError(name, "Missing configuration [db." + name + "." + key + "]")
}
config.subKeys map { name =>
val url = getConfigValue(config, name, "url")
val user = getConfigValue(config, name, "user")
val password = getConfigValue(config, name, "password")
name match {
case "default" => ConnectionPool.singleton(url, user, password)
case _ => ConnectionPool.add(Symbol(name), url, user, password)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment