scala - "Hello World" example for Slick 2.0 with MySQL -
is there example of how use slick 2.0.1 library scala connect mysql database? not find up-to-date examples googling...
recently, have been thinking porting code web application have been developing php scala. have been looking programming language has (i) web application framework number of users, (ii) offers compile time error-checking, (iii) nice testing tools, , on... after reading case studies (e.g., huffpost , cousera) of developing web applications using scala , play framework, started looking appealing me , decided give shot.
as data previous project reside in mysql database, looking @ scala offers program connect mysql. came across slick. followed hello-slick example in typesafe activator, guided me how use in-memory database (i.e., h2) , liked syntax , level of abstraction. now, want use slick mysql. tried changing import statements
import scala.slick.driver.h2driver.simple._
to
import scala.slick.driver.mysqldriver.simple._
in both helloslick.scala , tables.scala in aforementioned example (please see full copy of code below). however, getting following error message when compile it:
/users/kotaro/documents/scala/hello-slick/src/test/scala/tablessuite.scala
value ddl not member of scala.slick.lifted.tablequery[suppliers]
def createschema() = (suppliers.ddl ++ coffees.ddl).create
^
does know causing this? according post (slick exception when trying connect mysql), might missing add dependencies though error message i'm getting different. if there example can follow grasp of how use slick mysql, lot too.
i using scala 2.10.3, mysql in xampp 1.7.3 os x 10.8 , jre 1.6.
thank in advance!
following full copy of code.
helloslick.scala
import scala.slick.driver.mysqldriver.simple._ //import scala.slick.driver.h2driver.simple._ // main application object helloslick extends app { // query interface suppliers table val suppliers: tablequery[suppliers] = tablequery[suppliers] // query interface coffees table val coffees: tablequery[coffees] = tablequery[coffees] // create connection (called "session") in-memory h2 database database.forurl("jdbc:mysql://localhost:3306/test", driver="com.mysql.jdbc.driver", user="root", password="").withsession { implicit session => // create schema combining ddls suppliers , coffees tables using query interfaces (suppliers.ddl ++ coffees.ddl).create /* create / insert */ // insert suppliers 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") // insert coffees (using jdbc's batch insert feature) val coffeesinsertresult: option[int] = 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) ) val allsuppliers: list[(int, string, string, string, string, string)] = suppliers.list // print number of rows inserted coffeesinsertresult foreach (numrows => println(s"inserted $numrows rows coffees table")) /* read / query / select */ // print sql coffees query println("generated sql base coffees query:\n" + coffees.selectstatement) // query coffees table using foreach , print each row coffees foreach { case (name, supid, price, sales, total) => println(" " + name + "\t" + supid + "\t" + price + "\t" + sales + "\t" + total) } /* filtering / */ // construct query price of coffees > 9.0 val filterquery: query[coffees, (string, int, double, int, int)] = coffees.filter(_.price > 9.0) println("generated sql filter query:\n" + filterquery.selectstatement) // execute query println(filterquery.list) /* update */ // construct update query sales column being 1 update val updatequery: query[column[int], int] = coffees.map(_.sales) // print sql coffees update query println("generated sql coffees update:\n" + updatequery.updatestatement) // perform update val numupdatedrows = updatequery.update(1) println(s"updated $numupdatedrows rows") /* delete */ // construct delete query deletes coffees price less 8.0 val deletequery: query[coffees,(string, int, double, int, int)] = coffees.filter(_.price < 8.0) // print sql coffees delete query println("generated sql coffees delete:\n" + deletequery.deletestatement) // perform delete val numdeletedrows = deletequery.delete println(s"deleted $numdeletedrows rows") /* selecting specific columns */ // construct new coffees query selects name val justnamequery: query[column[string], string] = coffees.map(_.name) println("generated sql query returning name:\n" + justnamequery.selectstatement) // execute query println(justnamequery.list) /* sorting / order */ val sortbypricequery: query[coffees, (string, int, double, int, int)] = coffees.sortby(_.price) println("generated sql query sorted price:\n" + sortbypricequery.selectstatement) // execute query println(sortbypricequery.list) /* query composition */ val composedquery: query[column[string], string] = coffees.sortby(_.name).take(3).filter(_.price > 9.0).map(_.name) println("generated sql composed query:\n" + composedquery.selectstatement) // execute composed query println(composedquery.list) /* joins */ // join tables using relationship defined in coffees table val joinquery: query[(column[string], column[string]), (string, string)] = { c <- coffees if c.price > 9.0 s <- c.supplier } yield (c.name, s.name) println("generated sql join query:\n" + joinquery.selectstatement) // print rows contain coffee name , supplier name println(joinquery.list) /* computed values */ // create new computed column calculates max price val maxpricecolumn: column[option[double]] = coffees.map(_.price).max println("generated sql max price column:\n" + maxpricecolumn.selectstatement) // execute computed value query println(maxpricecolumn.run) /* manual sql / string interpolation */ // required import sql interpolator import scala.slick.jdbc.staticquery.interpolation // value insert statement val state = "ca" // construct sql statement manually interpolated value val plainquery = sql"select sup_name suppliers state = $state".as[string] println("generated sql plain query:\n" + plainquery.getstatement) // execute query println(plainquery.list) } } tables.scala
import scala.slick.driver.mysqldriver.simple._ import scala.slick.lifted.{provenshape, foreignkeyquery} // suppliers table 6 columns: id, name, street, city, state, zip class suppliers(tag: tag) extends table[(int, string, string, string, string, string)](tag, "suppliers") { def id: column[int] = column[int]("sup_id", o.primarykey) // primary key column def name: column[string] = column[string]("sup_name") def street: column[string] = column[string]("street") def city: column[string] = column[string]("city") def state: column[string] = column[string]("state") def zip: column[string] = column[string]("zip") // every table needs * projection same type table's type parameter def * : provenshape[(int, string, string, string, string, string)] = (id, name, street, city, state, zip) } // coffees table 5 columns: name, supplier id, price, sales, total class coffees(tag: tag) extends table[(string, int, double, int, int)](tag, "coffees") { def name: column[string] = column[string]("cof_name", o.primarykey) def supid: column[int] = column[int]("sup_id") def price: column[double] = column[double]("price") def sales: column[int] = column[int]("sales") def total: column[int] = column[int]("total") def * : provenshape[(string, int, double, int, int)] = (name, supid, price, sales, total) // reified foreign key relation can navigated create join def supplier: foreignkeyquery[suppliers, (int, string, string, string, string, string)] = foreignkey("sup_fk", supid, tablequery[suppliers])(_.id) }
you overlooked error message refers `tablesuite.scala``
/users/kotaro/documents/scala/hello-slick/src/test/scala/tablessuite.scala value ddl not member of scala.slick.lifted.tablequery[suppliers] def createschema() = (suppliers.ddl ++ coffees.ddl).create you need change import in there well.
Comments
Post a Comment