Slick

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:

  • A wrapper DB object that uses the datasources defined in the Play config files, and pulls them from a connection pool. It is there so it is possible to use Slick sessions in the same fashion as you would Anorm JDBC connections. There are some smart caching and load balancing that make your connections to your DB perform better.
  • A DDL plugin that reads Slick tables and automatically creates schema updates on reload. This is useful in particular for demos and to get started.
  • A wrapper to use play Enumeratees together with Slick

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:

  • Numeric types: These include Byte, Short, Int, Long, BigDecimal, Float, and Double
  • Date types: These include java.sql.Date, java.sql.Time, and java.sql.Timestamp
  • UUID type: This includes java.util.UUID
  • LOB types: These include java.sql.Blob, java.sql.Clob, and Array[Byte]
  • Other types: These include 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).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.134.133