Slick – the modern way to access your Data
-
Upload
jochen-huelss -
Category
Software
-
view
441 -
download
0
description
Transcript of Slick – the modern way to access your Data
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?
13. April 2023 2
Bio of Jochen
• 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
Slick – TechTalk #42
13. April 2023 3
Before and after
Prerequisites
• Know about SQL• MVC Pattern• Collections in
programming• Ask directly!
Learning Goals
• Explain, what Slick is used for.• Know about the
advantages of Slick.• Know the basic
commands used for querying in Slick.
Slick – TechTalk #42
13. April 2023 4
MotivationWhat is Slick all about?
13. April 2023 5
Imagine a simple Database schema…
Slick – TechTalk #42
COFFEE
BUYER SALES
SUPPLIER
COF_ID(FK)
SUP_ID(FK)
PRICE AMOUNT
ID NAME
ID NAME COUNTRY
ID NAME COUNTRY FAV_COFFE_ID(FK)
13. April 2023 6
… being used for a Web Application
Slick – TechTalk #42
Web Framework
Database
Browser
HTML5 JavaScript CSS
JSON
SQL
View Controller Model
How can we simplify the Database-Controller
connection?
13. April 2023 8
As a developer, I want to use Slick!
Modern database query & access library for Scala
Slick – TechTalk #42
Scala Runtime
PostgreSQL Database
SQL
Scala-Controller Slick-Model
13. April 2023 9
Features of Slick
• 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
Slick – TechTalk #42
Slick makes DB access scalable!
13. April 2023 10
Under the HoodHow does it work?
13. April 2023 11
Slick‘s Lifted Query Processing
Slick – TechTalk #42
Every DB table gets its own type classes
lifting
Generate syntax tree for SQL query
computation
13. April 2023 12
Example Code for Relation Coffee
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))
Slick – TechTalk #42
13. April 2023 13
Setup
• Scala >2.10
• Scala Build Tool >0.13
• Add dependencies• Slick >2.0• Database specific driver
• Import database driver
Many tutorials available
Slick – TechTalk #42
More Info:http://
slick.typesafe.com/doc/2.0.1/
gettingstarted.html
13. April 2023 14
Getting startedSchema Generation, Connection, Data inserts
13. April 2023 15
Our sample Database schema
Slick – TechTalk #42
COFFEE
BUYER SALES
SUPPLIER
COF_ID(FK)
SUP_ID(FK)
PRICE AMOUNT
ID NAME
ID NAME COUNTY
ID NAME COUNTRY FAV_COFFE_ID(FK)
13. April 2023 16
Getting started
Task
1. Schema Generation
2. Connection
3. Data inserts
Tips and Tricks
• Use Slick‘s SourceCodeGenerator
• Use wrapped connection object
• Single or batch (faster) insert possible• ID return only with single
Slick – TechTalk #42
13. April 2023 17
Basic QueriesProjection, Filtering, Sorting
13. April 2023 18
Basic Queries
Task
1. Projection1. Result list()2. Result foreach()3. Result first()
2. Where3. Where with LIKE4. Where with AND / OR5. Sorting
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()”
Slick – TechTalk #42
13. April 2023 19
Advanced QueriesJoins, Aggregates, nested Queries
13. April 2023 20
Advanced Queries
Task
1. Inner Join2. Inner Join with condition3. Inner Join with foreign
key4. Inner Join with 3 tables5. Aggregation and nested
queries (I)6. Aggregation and nested
queries (II)
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
Slick – TechTalk #42
13. April 2023 21
ConclusionIs Slick the answer to life, the universe and everything?!
13. April 2023 22Slick – TechTalk #42
Thoughts on Slick
- Generated Queries
(readability)
+ Usability in Scala
+ Production ready
+ Level of control
+ Lightweight
13. April 2023 23
References
• http://slick.typesafe.com/doc/2.0.1/
• https://mackler.org/LearningSlick2/
• “Slick - The Structured Way” from SlideShare
• “Brief introduction of Slick” from SlideShare
Slick – TechTalk #42
Thank you!Jogja Digital ValleyJochen Hülß ([email protected])01/10/2014