On Github oschrenk / slick-presentation
Created by Oliver Schrenk / @oschrenk
scalaVersion := "2.10.0" libraryDependencies ++= List( "com.typesafe.slick" %% "slick" % "1.0.0", "org.slf4j" % "slf4j-nop" % "1.6.4", "mysql" % "mysql-connector-java" % "5.1.25" )
import scala.slick.session.Database._ import scala.slick.driver.MySQLDriver.simple._
Database.forURL( "jdbc:mysql://host:3306/elmar", driver = "com.mysql.jdbc.Driver" ) withSession {...} val ds = new MysqlDataSource ds.setUrl("jdbc:mysql://host:3306/elmar") ds.setUser("...") ds.setPassword( "...") Database.forDataSource(ds)
Data Definition
(Q.updateNA + "insert into companies values(101, 'Acme, Inc.'").execute
Queries
import scala.slick.jdbc.{GetResult, StaticQuery => Q} import Q.interpolation case class AvailabilityRecord( brand: String, city: String, dateOfQuery: Timestamp ) implicit val getAvailabilityRecordResult = GetResult(r => AvailabilityRecord( r.nextString, // r.<< r.nextString, // r.<< r.nextTimestamp()) // r.<< ) val myBrand = "OA" val q = sql"select brand, city, dateOfQuery from availabilitylog where brand = $myBrand limit 10".as[AvailabilityRecord] println(q.list())
Table with default projection
object AvailabilityRecords extends Table[(String, String, Timestamp)] ("availabilitylog") { def brand = column[String]("brand") def city = column[String]("city") def dateOfQuery = column[Timestamp]("dateOfQuery") def * = brand ~ city ~ dateOfQuery }
Table with mapped projection
case class AvailabilityRecord( brand: String, city: String, dateOfQuery: Timestamp ) object AvailabilityRecords extends Table[AvailabilityRecord]("availabilitylog") { def brand = column[String]("brand") def city = column[String]("city") def dateOfQuery = column[Timestamp]("dateOfQuery") def * = brand ~ city ~ dateOfQuery <>(AvailabilityRecord, AvailabilityRecord.unapply _) }
AvailabilityRecords.ddl.create AvailabilityRecords.insert("TUI", "Hanoi", "2013-10-02")
val q = for { c <- AvailabilityRecords if c.name === "TUI" } yield c.city q.update("New York")
Query(AvailabilityRecords). sortBy(_.city.desc.nullsFirst). drop(5)take(10)
Query(AvailabilityRecords). take(30). filter(_.brand === "OA")
object Persons extends Table[Person]("person") { def id = column[String]("id") def firstName = column[String]("firstName") def lastName = column[String]("lastName") def * = id ~ firstName ~ lastName <> (Person, Person.unapply _) } object Bookings extends Table[Booking ]("booking") { def city = column[String]("cityName") def bookingDate = column[Timestamp]("bookingDate") def bookerId = column[String]("booker_id") def * = city ~ bookingDate ~ bookerId <> (Booking, Booking.unapply _) } val implicitInnerJoin = for { b <- Bookings p <- Persons if b.bookerId === p.id } yield (b.city, p.firstName, p.lastName) println(implicitInnerJoin.take(5).list)
object Persons extends Table[Person]("person") { def id = column[String]("id") def firstName = column[String]("firstName") def lastName = column[String]("lastName") def * = id ~ firstName ~ lastName <> (Person, Person.unapply _) } object Bookings extends Table[Booking ]("booking") { def city = column[String]("cityName") def bookingDate = column[Timestamp]("bookingDate") def bookerId = column[String]("booker_id") def * = city ~ bookingDate ~ bookerId <> (Booking, Booking.unapply _) } val explicitInnerJoin = for { (b, p) <- Bookings innerJoin Persons on (_.bookerId === _.id) } yield (b.city, p.firstName, p.lastName) println(explicitInnerJoin.take(5).list)