Last active
August 29, 2015 14:05
-
-
Save netologist/1a735a17c6df41d269ad to your computer and use it in GitHub Desktop.
Slick Example
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 com.typesafe.config.ConfigFactory | |
import scala.slick.jdbc.JdbcBackend.Database | |
class BaseDAO { | |
lazy val conf = ConfigFactory.load() | |
lazy val uri = conf.getString(s"spray.oauth2.datasource.uri") | |
lazy val user = conf.getString(s"spray.oauth2.datasource.username") | |
lazy val password = conf.getString(s"spray.oauth2.datasource.password") | |
lazy val driver = conf.getString(s"spray.oauth2.datasource.driver") | |
protected val defaultDB = Database.forURL(uri, user, password, driver = driver) | |
} |
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 slick.driver.PostgresDriver.simple._ | |
import slick.driver.PostgresDriver.backend.{ Session , DatabaseDef } | |
import scala.slick.lifted.TableQuery | |
import co.s4n.slick.poc.persistence.slick.Tables.Coffees | |
class CoffeesDAO(coffees: TableQuery[Coffees]) extends BaseDAO { | |
val querySalesByName = for { | |
name <- Parameters[String] | |
c <- coffees if c.name is name | |
} yield c.sales | |
} | |
class SuppliersDAO(suppliers: TableQuery[Suppliers]) extends BaseDAO { | |
def updateStreetByName(name: String, street: String) = defaultDB.withTransaction { implicit session => | |
val q = for( supplier <- suppliers if supplier.name === name) yield supplier.street | |
q.update(street) | |
} | |
} |
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 scala.slick.driver.PostgresDriver.simple._ | |
object Tables { | |
// Definition of the SUPPLIERS table | |
class Suppliers(tag: Tag) extends Table[(Int, String, String, String, String, String)](tag, "SUPPLIERS") { | |
def id = column[Int]("SUP_ID", O.PrimaryKey) // This is the primary key column | |
def name = column[String]("SUP_NAME") | |
def street = column[String]("STREET") | |
def city = column[String]("CITY") | |
def state = column[String]("STATE") | |
def zip = column[String]("ZIP") | |
// Every table needs a * projection with the same type as the table's type parameter | |
def * = (id, name, street, city, state, zip) | |
} | |
val suppliers = TableQuery[Suppliers] | |
// Definition of the COFFEES table | |
class Coffees(tag: Tag) extends Table[(String, Int, Double, Int, Int)](tag, "COFFEES") { | |
def name = column[String]("COF_NAME", O.PrimaryKey) | |
def supID = column[Int]("SUP_ID") | |
def price = column[Double]("PRICE") | |
def sales = column[Int]("SALES") | |
def total = column[Int]("TOTAL") | |
def * = (name, supID, price, sales, total) | |
// A reified foreign key relation that can be navigated to create a join | |
def supplier = foreignKey("SUP_FK", supID, suppliers)(_.id) | |
} | |
val coffees = TableQuery[Coffees] | |
def createSchema = (suppliers.ddl ++ coffees.ddl).create | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment