Skip to content

Instantly share code, notes, and snippets.

@yareally
Last active December 10, 2015 02:30
Show Gist options
  • Save yareally/2aea191222c49d2722e2 to your computer and use it in GitHub Desktop.
Save yareally/2aea191222c49d2722e2 to your computer and use it in GitHub Desktop.
import slick.backend.DatabaseConfig
import slick.dbio.{NoStream, Effect}
import slick.driver.JdbcProfile
import slick.jdbc.JdbcBackend
import slick.profile.{SqlStreamingAction, SqlAction}
import scala.concurrent.Await
import scala.concurrent.duration.Duration
import scala.io.Source
import scala.language.postfixOps
object driver {
val conn = DatabaseConfig.forConfig[JdbcProfile]("pgsql_test")
import Extensions._
import conn.driver.api._
implicit val db: JdbcBackend#DatabaseDef = conn.db
def main(args: Array[String]) {
val result: Vector[Drink] = sql"""SELECT * FROM store LIMIT 50""".as[Store].run
println(result)
}
// or this instead of extension methods:
// used like so: val result = run(sql"""SELECT * FROM store LIMIT 50""".as[Store])
def run[T](sql: SqlAction[T, NoStream, Effect]): T = {
Await.result(db.run(sql), Duration.Inf)
}
}
object Extensions {
implicit class SqlExtensions[T](sql: SqlStreamingAction[T, Drink, Effect]) {
def run[A](implicit db: JdbcBackend#DatabaseDef): T = {
Await.result(db.run(sql), Duration.Inf)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment