Skip to content

Instantly share code, notes, and snippets.

@tango238
Last active January 15, 2016 07:10
Show Gist options
  • Select an option

  • Save tango238/ff68380216ae3c3fecc4 to your computer and use it in GitHub Desktop.

Select an option

Save tango238/ff68380216ae3c3fecc4 to your computer and use it in GitHub Desktop.
#!/usr/bin/env scalas
// @see http://eed3si9n.com/ja/scripting-with-scala
// chmod +x database_sample.scala
// export CONSCRIPT_OPTS="-XX:MaxPermSize=512M -Dfile.encoding=UTF-8"
// ./database_sample.scala
/***
scalaVersion := "2.11.7"
libraryDependencies ++= Seq(
"mysql" % "mysql-connector-java" % "5.1.28",
"com.typesafe.slick" %% "slick" % "3.1.1",
"org.slf4j" % "slf4j-nop" % "1.6.4"
)
*/
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.Duration
import scala.concurrent._
import slick.driver.MySQLDriver.api._
class Suppliers(tag: Tag) extends Table[(Int, String, String, String, String, String)](tag, "SUPPLIERS") {
def id = column[Int]("SUP_ID", O.PrimaryKey)
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")
def * = (id, name, street, city, state, zip)
}
val suppliers = TableQuery[Suppliers]
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)
def supplier = foreignKey("SUP_FK", supID, suppliers)(_.id)
}
val coffees = TableQuery[Coffees]
val db = Database.forURL("jdbc:mysql://localhost/test?user=root&password=", driver="com.mysql.jdbc.Driver")
val setup = DBIO.seq(
(suppliers.schema ++ coffees.schema).drop,
(suppliers.schema ++ coffees.schema).create,
suppliers += (101, "Acme, Inc.", "99 Market Street", "Groundsville", "CA", "95199"),
suppliers += ( 49, "Superior Coffee", "1 Party Place", "Mendocino", "CA", "95460"),
suppliers += (150, "The High Ground", "100 Coffee Lane", "Meadows", "CA", "93966"),
coffees ++= Seq(
("Colombian", 101, 7.99, 0, 0),
("French_Roast", 49, 8.99, 0, 0),
("Espresso", 150, 9.99, 0, 0),
("Colombian_Decaf", 101, 8.99, 0, 0),
("French_Roast_Decaf", 49, 9.99, 0, 0)
)
)
try {
/*
println("start")
val setupFuture: Future[Unit] = db.run(setup)
setupFuture.map { result =>
println("end")
}
println("end2")
*/
println("start")
Await.result(db.run(setup), Duration.Inf)
println("end")
} finally db.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment