brief introduction of slick

19
SLICK SLICK 2.0.1 2.0.1

Upload: knoldus-software-llp

Post on 30-Oct-2014

1.051 views

Category:

Software


1 download

DESCRIPTION

Here I am going to briefly introduce Slick

TRANSCRIPT

Page 1: Brief introduction of Slick

SLICK SLICK 2.0.12.0.1

Page 2: Brief introduction of Slick

INTRODUCTION

● 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.

sql"select COF_NAME from COFFEES where PRICE < $limit".as[String].list

Page 3: Brief introduction of Slick

FEATURESFEATURES

● Easy :-

Access stored data just like Scala collections.

Unified session management based on JDBC Connections.

Supports SQL if you need it.

Simple setup.● Concise:-

Scala syntax

Fetch results without pain

Page 4: Brief introduction of Slick

● Scales naturally :-

Stateless.

Explicit control of execution time and transferred data.● Safe :-

No SQL-injections.

Compile-time safety (types, names, no typos, etc.).

Type-safe support of stored procedures.● Composable :-

It‘s Scala code: abstract and re-use with ease

Slick requires scala 2.10.

Scala 2.9 use ScalaQuery, the predecessor of Slick

Page 5: Brief introduction of Slick

Supported Database

1.DB2 (via slick-extensions)

2.Derby/JavaDB

3.H2

4.HSQLDB/HyperSQL

5.Microsoft SQL Server

6.MySQL

7.PostgreSQL etc.

Page 6: Brief introduction of Slick

Set Up Set Up

● First of all, you need to add Slick and the embedded databases or drivers for external databases to your project.

● If you are using sbt, you do this in your main build.sbt file.

libraryDependencies ++= List( // use the right Slick version here:

"com.typesafe.slick" %% "slick" % "2.0.1", "org.slf4j" % "slf4j-nop" % "1.6.4",

"postgresql" % "postgresql" % "9.1-901.jdbc4")

Add Dependency in Build.sbt

Page 7: Brief introduction of Slick

SLF4JSLF4J

● Slick uses SLF4J for its own debug logging so you also need to add an SLF4J implementation.

● Here we are using slf4j-nop to disable logging.

● You have to replace this with a real logging framework like Logback if you want to see log output.

Page 8: Brief introduction of Slick

ImportsImports

● Since we are using Postgresql as our database system, we need to import features from Slick’s PostgresDriver.

● A driver’s simple object contains all commonly needed imports from the driver and other parts of Slick such as session handling.

import scala.slick.driver.PostgresDriver.simple._

Page 9: Brief introduction of Slick

Lifted Embedding

● The name Lifted Embedding refers to the fact that you are not working with standard Scala types (as in the direct embedding) but with types that are lifted into a Rep type constructor.

● This becomes clear when you compare the types of a simple Scala collections example with the types of similar code using the lifted embedding

Page 10: Brief introduction of Slick

● Direct Embedding

● Lifted Embedding

case class Coffee(name: String, price: Double)val coffees: List[Coffee] = //...

val l = coffees.filter(_.price > 8.0).map(_.name)

class Coffees(tag: Tag) extends Table[(String, Double)](tag, "COFFEES") { def name = column[String]("COF_NAME")

def price = column[Double]("PRICE") def * = (name, price)

}val coffees = TableQuery[Coffees]

val q = coffees.filter(_.price > 8.0).map(_.name)

Page 11: Brief introduction of Slick

FUNCTIONAL PROGRAMMING

Page 12: Brief introduction of Slick

Reason Behind Lifting

● This lifting is necessary because the lifted types allow us to generate a syntax tree that captures the query computations.

● Getting plain Scala functions and values would not give us enough information for translating those computations to SQL.

Page 13: Brief introduction of Slick

QUERY PROCESSING

Page 14: Brief introduction of Slick

SLICK QUERY PROCESSING

Page 15: Brief introduction of Slick

QUERIES IN SLICK

● Filtering Query:-

// compiles to SQL (simplified):

// select "COF_NAME", "SUP_ID", "PRICE", "SALES", "TOTAL"

// from "COFFEES"

// where "SUP_ID" = 101

val q1 = coffees.filter(_.supID === 101)

Page 16: Brief introduction of Slick

● Drop Query :-

// compiles to SQL (simplified):

// select "COF_NAME", "SUP_ID", "PRICE", "SALES", "TOTAL"

// from "COFFEES"

// limit 5 offset 10

val q2 = coffees.drop(10).take(5)

Coffee.ddl.dropStatements

Page 17: Brief introduction of Slick

● Deleting:-

● Create:-

val affectedRowsCount = q.delete

users2.ddl.create

Page 18: Brief introduction of Slick

References

● References :-● http://slick.typesafe.com/talks/scalax2012/Sli

ck_ScalaExchange_2012.pdf● http://slick.typesafe.com/doc/2.0.1/migration.htm

l● Database System Concepts(Abraham

Silberschatz ,Henry f. Korth, S. Sudarshan)

Page 19: Brief introduction of Slick

THANK YOU :)