According to Slick's website (http://slick.typesafe.com/doc/2.1.0/introduction.html#what-is-slick):
Slick is Typesafe's modern database query and access library for Scala. It allows you to work with stored data almost as if you were using Scala collections while at the same time giving you full control over when a database access happens and which data is transferred. You can also use SQL directly.
When using Scala instead of raw SQL for your queries you benefit from compile-time safety and compositionality. Slick can generate queries for different backend databases including your own, using its extensible query compiler.
We can use Slick in our Play application through the play-slick plugin. The plugin provides some additional features for the use of Slick in a Play application. According to https://github.com/playframework/, play-slick consists of three features:
To use it, we need to add the following library dependency in the build file:
"com.typesafe.play" %% "play-slick" % "0.8.1" Let's see how we can define user operations using Slick.
First, we need to define the schema in Scala. This can be done by mapping the required tables to case classes. For our user table, the schema can be defined as:
case class SlickUser(id: Long, loginId: String, name: String) class SlickUserTable(tag: Tag) extends Table[SlickUser](tag, "user") { def id = column[Long]("id", O.PrimaryKey, O.AutoInc) def loginId = column[String]("login_id") def name = column[String]("name") def dob = column[Long]("dob") def password = column[String]("password") def * = (id, loginId, name) <>(SlickUser.tupled, SlickUser.unapply) }
Table
is a Slick trait and its columns are specified through the column
method. The following types are supported for a column:
Byte
, Short
, Int
, Long
, BigDecimal
, Float
, and Double
java.sql.Date
, java.sql.Time
, and java.sql.Timestamp
This includes java.util.UUID
java.sql.Blob
, java.sql.Clob
, and Array[Byte]
Boolean
, String
, and Unit
The column
method accepts column constraints, such as PrimaryKey
, Default
, AutoInc
, NotNull
, and Nullable
.
The *
method is mandatory for every table and is similar to RowParser
.
Now we can define a TableQuery
Slick using this and use it to query a database. There are simple methods available for performing equivalent DB operations. We can define the methods in the Anorm object using the play-slick wrapper along with the Slick API:
object SlickUserHelper { val users = TableQuery[SlickUserTable] def add(loginId: String, password: String, name: String = "anonymous", dateOfBirth: DateTime): Long = { play.api.db.slick.DB.withSession { implicit session => users.map(p => (p.loginId, p.name, p.dob, p.password)) .returning(users.map(_.id)) .insert((loginId, name, dateOfBirth.getMillis, password)) } } def updatePassword(userId: Long, password: String) = { play.api.db.slick.DB.withSession { implicit session => users.filter(_.id === userId) .map(u => u.password) .update(password) } } def getAll: Seq[SlickUser] = { play.api.db.slick.DB.withSession { implicit session => users.run } } }
The run
method is equivalent to calling SELECT *
.
For more details on this, refer to the Slick (http://slick.typesafe.com/doc/2.1.0/) and the play-slick documentation (https://github.com/playframework/play-slick).
18.227.134.133