This document is a presentation about Slick, a modern database query and access library for Scala. It introduces Slick and its main features like easy database access similar to Scala collections and compile time safety. It covers topics like schema generation, basic queries with filtering, sorting and joins, and advanced queries with aggregations and nested queries. Code examples are provided to demonstrate how to get started with Slick and perform common tasks. The presentation concludes that while Slick provides benefits like usability in Scala and being production ready, its generated queries can lack readability.
1. Slick – The modern Way
to query your Data!
on
Jogja Digital Valley
Jochen Hülß
01/10/2014
The answer to life,
the universe and
everything?
2. • M.Sc. in Business Information Systems
(Mannheim University)
• Data Mining & Web Usage Mining
• Cloud Technologies
• 5 years with SAP
(Business Software Vendor)
• Business Intelligence
• Android Development
• Heinz-Nixdorf Fellowship-Program
• Venture Capital Investment
• Ideabox / Mountain SEA Ventures
Bio of Jochen
05/10/14 Slick – TechTalk #42 2
3. Prerequisites
• Know about SQL
• MVC Pattern
• Collections in
programming
• Ask directly!
Before and after
Learning Goals
• Explain, what Slick is
used for.
• Know about the
advantages of Slick.
• Know the basic
commands used for
querying in Slick.
05/10/14 Slick – TechTalk #42 3
5. COFFEE
ID NAME
BUYER SALES
SUP_ID
(FK)
PRICE AMOUNT
SUPPLIER
COF_ID
(FK)
ID NAME COUNTRY
ID NAME COUNTRY FAV_COFFE_ID
(FK)
Imagine a simple Database schema…
05/10/14 Slick – TechTalk #42 5
6. Browser
HTML5 JavaScript CSS
JSON
Web Framework
View Controller Model
Database
SQL
How can we simplify
the Database-Controller
connection?
… being used for a Web Application
05/10/14 Slick – TechTalk #42 6
7. Modern database query & access library for Scala
Scala Runtime
Scala-Controller Slick-Model
SQL
PostgreSQL Database
As a developer, I want to use Slick!
05/10/14 Slick – TechTalk #42 8
8. • Easy
• Database access just like Scala collections
• Manages your Database sessions via JDBC
• Supports plain SQL
• Simple setup, many supported databases
• Precise
• Compile time safety
• Query re-use with ease Scala functions
• Immutable collections
• Stateless
Features of Slick
Slick makes DB
access scalable!
05/10/14 Slick – TechTalk #42 9
10. Every DB table gets
its own type classes
Generate syntax
tree for SQL query
computation
Slick‘s Lifted Query Processing
lifting
05/10/14 Slick – TechTalk #42 11
11. 1) /** Entity class storing rows of table Coffee */
2) case class CoffeeRow(id: Long, name: Option[String])
3) /** Table description of table coffee. Objects of this class serve as
prototypes for rows in queries. */
4) class Coffee(tag: Tag) extends Table[CoffeeRow](tag, "coffee") {
5) def * = (id, name) <> (CoffeeRow.tupled, CoffeeRow.unapply)
6) val id: Column[Long] = column[Long]("id", O.AutoInc, O.PrimaryKey)
7) val name: Column[Option[String]] = column[Option[String]]("name")
8) }
9) /** Collection-like TableQuery object for table Coffee */
10) lazy val Coffee = new TableQuery(tag => new Coffee(tag))
Example Code for Relation Coffee
05/10/14 Slick – TechTalk #42 12
12. • Scala >2.10
• Scala Build Tool >0.13
• Add dependencies
• Slick >2.0
• Database specific driver
• Import database driver
Many tutorials available
Setup
More Info:
http://slick.typesafe.
com/doc/2.0.1/getti
ngstarted.html
05/10/14 Slick – TechTalk #42 13
14. COFFEE
ID NAME
BUYER SALES
SUP_ID
(FK)
PRICE AMOUNT
SUPPLIER
COF_ID
(FK)
ID NAME COUNTY
ID NAME COUNTRY FAV_COFFE_ID
(FK)
Our sample Database schema
05/10/14 Slick – TechTalk #42 15
15. Task
1. Schema Generation
2. Connection
3. Data inserts
Getting started
Tips and Tricks
• Use Slick‘s
SourceCodeGenerator
• Use wrapped connection
object
• Single or batch (faster)
insert possible
• ID return only with single
05/10/14 Slick – TechTalk #42 16
17. Task
1. Projection
1. Result list()
2. Result foreach()
3. Result first()
2. Where
3. Where with LIKE
4. Where with AND / OR
5. Sorting
Basic Queries
Tips and Tricks
• “map()” iterates over collection
• Result with “list()” in memory,
with “foreach()” only temporary
• If attribute is not “NOT NULL” on
database use “Option()”
• Filter with “===“
• Use “toLowerCase()” with “like()”
05/10/14 Slick – TechTalk #42 18
19. Task
1. Inner Join
2. Inner Join with
condition
3. Inner Join with foreign
key
4. Inner Join with 3 tables
5. Aggregation and
nested queries (I)
6. Aggregation and
nested queries (II)
Advanced Queries
Tips and Tricks
• For-Loop connects two
collections
• Where conditions start with “if“
• Slick model provides helper for
foreign key relations
• “groupBy” new collection with
tuples
• Aggregation max, min, sum, …
• Use queries within other queries
05/10/14 Slick – TechTalk #42 20
20. Conclusion
Is Slick the answer to life, the universe and everything?!
05/10/14 21
21. + Production
+ Level of
control
+ Lightweight
Thoughts on Slick
+ Usability in
Scala
- Generated
Queries
(readability)
ready
05/10/14 Slick – TechTalk #42 22
22. • http://slick.typesafe.com/doc/2.0.1/
• https://mackler.org/LearningSlick2/
• “Slick - The Structured Way” from SlideShare
• “Brief introduction of Slick” from SlideShare
References
05/10/14 Slick – TechTalk #42 23
23. Thank you!
Jogja Digital Valley
Jochen Hülß (jochen@huelss.de)
01/10/2014
Hinweis der Redaktion
+ Lightweight
+ Level of control
+ Production ready
Documentation
Generated Queries (readability)