SlideShare a Scribd company logo
Arpit Suthar
Software Consultant
Knoldus Software LLP
AgendaAgenda
1. What is Quill
2. Why Quill
3. How to use Quill
4. Slick v/s Quill
5. Demo
1. What is Quill
2. Why Quill
3. How to use Quill
4. Slick v/s Quill
5. Demo
What is QuillWhat is Quill
Quill is a library that provides a Quoted Domain Specific Language (QDSL) for Scala
which simplifies working with relational databases. It is an alternative for Slick. The
advantage of Quill is support for Compile-Time Language Integrated Queries which
allows for database access similar to Scala collections and enables compile-time
generated queries. This feature minimizes the runtime overhead of queries. Moreover, it
allows query validation during compile-time.
Why QuillWhy Quill
- Boilerplate-free
- Quoted DSL
- Compile-time query generation
- Compile-time query validation
Cont.Cont.
select s4.s56, s5.s62, s4.s60, s4.s54 from (select s6.s41 as s51, s6.s42 as s52, s6.s43 as
s53, s6.s44 as s54, s6.s45 as s55, s6.s46 as s56, s6.s47 as s57, s7.s48 as s58, s7.s49 as
s59, s7.s50 as s60 from (select s8.s34 as s41, s8.s35 as s42, s8.s36 as s43, s8.s37 as s44,
s9.s38 as s45, s9.s39 as s46, s9.s40 as s47 from (select s81.`id` as s34,
s81.`purchaser_id` as s35, s81.`product_id` as s36, s81.`total` as s37 from `sale` s81) s8
inner join (select s83.`id` as s38, s83.`name` as s39, s83.`address` as s40 from
`purchaser` s83) s9 on 1=1) s6 inner join (select s85.`id` as s48, s85.`supplier_id` as
s49, s85.`name` as s50 from `product` s85) s7 on 1=1) s4 inner join (select s87.`id` as
s61, s87.`name` as s62, s87.`address` as s63 from `supplier` s87) s5 on ((s4.s53 = s4.s58)
and (s4.s52 = s4.s55)) and (s4.s59 = s5.s61) where s4.s54 >= ?
What’s this?What’s this?
That’s actually a join query of 4 tables created by Slick of
SELECT purchaser.name, supplier.name, product.name, sale.total
FROM sale join purchaser join product join supplier
ON (sale.purchaser_id = purchaser.id AND
sale.product_id = product.id AND
product.supplier_id = supplier.id)
WHERE sale.total >= 500
Comparison:-
Lifted Embedding: 87.09 s
Plain SQL: 0.10 s
Cont.Cont.
Working with QuillWorking with Quill
1. Build.sbt =
libraryDependencies += "io.getquill" %% "quill-async" % "0.8.0"
2. Application.conf =
db.default {
host = "localhost"
port = 3306
user = "root"
database = "quill"
}
3. Database
4. case classes
And you good to go...
Cont.Cont.
case class User(id: Long, name: String, isActive: Boolean)
val Users = quote(query[User].schema(_.generated(_.id))) or
query[Users].schema(_.entity("user_table").columns(_.id -> "id_column", _.namr →
“name_col”))
lazy val ctx = new MysqlAsyncContext[SnakeCase]("db.default")
Or you can use built in Dialects
lazy val ctx = new JdbcContext[MySQLDialect, SnakeCase]("db.default")
Cont.Cont.
Naming strategy
io.getquill.naming.Literal = some_ident -> some_ident
io.getquill.naming.Escape = some_ident -> “some_ident”
io.getquill.naming.UpperCase = some_ident -> SOME_IDENT
io.getquill.naming.LowerCase = SOME_IDENT -> some_ident
io.getquill.naming.SnakeCase = someIdent -> some_ident
io.getquill.naming.CamelCase = some_ident -> someIdent
io.getquill.naming.MysqlEscape = some_ident -> `some_ident`
io.getquill.naming.PostgresEscape= $some_ident -> $some_ident
Cont.Cont.
Operations:-
1. Filter
2. Map
3. FlatMap
4. SortBy
5. Drop/take
6. GroupBy
7. Union
8. Joins
And many more...
Cont.Cont.
Insert:
val insQuery = quote(query[Contact].insert)
ctx.run(insQuery)(List(Contact(999, "987654321")))
// INSERT INTO Person (id,age) VALUES (?, ?)
Update:
val upQuery = quote {query[Person].filter(_.id == 999).update}
ctx.run(upQuery)(List(Person(999, "John", 22)))
// UPDATE Person SET id = ?, name = ?, age = ? WHERE id = 999
Delete:
val delQuery = quote {query[Person].filter(p => p.name == "").delete}
ctx.run(delQuery)
// DELETE FROM Person WHERE name = ''
Cont.Cont.
- Supports transactions
- Not supported features can be implemented by INFIX
- Supports H2, MySql, Postgres, cassandra.
Slick v/s Quill
SLICK Quill
Popularity 9.2 7.7
Activity 6.7 9.2
Repository Stars 1602
Watchers 156
Forks 376
Last Commit 9 days ago
Stars 631
Watchers 40
Forks 75
Last Commit 3 days ago
References
http://getquill.io/
http://blog.scalac.io/2016/07/21/compile-time-queries-with-quill.html
http://blog.scalac.io/2015/01/27/rough-experience-with-slick.html
Questions & Ans.fold(“you’ll get it tomorrow”)(identity)
Thanks

More Related Content

An Introduction to Quill

  • 2. AgendaAgenda 1. What is Quill 2. Why Quill 3. How to use Quill 4. Slick v/s Quill 5. Demo 1. What is Quill 2. Why Quill 3. How to use Quill 4. Slick v/s Quill 5. Demo
  • 3. What is QuillWhat is Quill Quill is a library that provides a Quoted Domain Specific Language (QDSL) for Scala which simplifies working with relational databases. It is an alternative for Slick. The advantage of Quill is support for Compile-Time Language Integrated Queries which allows for database access similar to Scala collections and enables compile-time generated queries. This feature minimizes the runtime overhead of queries. Moreover, it allows query validation during compile-time.
  • 4. Why QuillWhy Quill - Boilerplate-free - Quoted DSL - Compile-time query generation - Compile-time query validation
  • 5. Cont.Cont. select s4.s56, s5.s62, s4.s60, s4.s54 from (select s6.s41 as s51, s6.s42 as s52, s6.s43 as s53, s6.s44 as s54, s6.s45 as s55, s6.s46 as s56, s6.s47 as s57, s7.s48 as s58, s7.s49 as s59, s7.s50 as s60 from (select s8.s34 as s41, s8.s35 as s42, s8.s36 as s43, s8.s37 as s44, s9.s38 as s45, s9.s39 as s46, s9.s40 as s47 from (select s81.`id` as s34, s81.`purchaser_id` as s35, s81.`product_id` as s36, s81.`total` as s37 from `sale` s81) s8 inner join (select s83.`id` as s38, s83.`name` as s39, s83.`address` as s40 from `purchaser` s83) s9 on 1=1) s6 inner join (select s85.`id` as s48, s85.`supplier_id` as s49, s85.`name` as s50 from `product` s85) s7 on 1=1) s4 inner join (select s87.`id` as s61, s87.`name` as s62, s87.`address` as s63 from `supplier` s87) s5 on ((s4.s53 = s4.s58) and (s4.s52 = s4.s55)) and (s4.s59 = s5.s61) where s4.s54 >= ? What’s this?What’s this?
  • 6. That’s actually a join query of 4 tables created by Slick of SELECT purchaser.name, supplier.name, product.name, sale.total FROM sale join purchaser join product join supplier ON (sale.purchaser_id = purchaser.id AND sale.product_id = product.id AND product.supplier_id = supplier.id) WHERE sale.total >= 500 Comparison:- Lifted Embedding: 87.09 s Plain SQL: 0.10 s Cont.Cont.
  • 7. Working with QuillWorking with Quill 1. Build.sbt = libraryDependencies += "io.getquill" %% "quill-async" % "0.8.0" 2. Application.conf = db.default { host = "localhost" port = 3306 user = "root" database = "quill" } 3. Database 4. case classes And you good to go...
  • 8. Cont.Cont. case class User(id: Long, name: String, isActive: Boolean) val Users = quote(query[User].schema(_.generated(_.id))) or query[Users].schema(_.entity("user_table").columns(_.id -> "id_column", _.namr → “name_col”)) lazy val ctx = new MysqlAsyncContext[SnakeCase]("db.default") Or you can use built in Dialects lazy val ctx = new JdbcContext[MySQLDialect, SnakeCase]("db.default")
  • 9. Cont.Cont. Naming strategy io.getquill.naming.Literal = some_ident -> some_ident io.getquill.naming.Escape = some_ident -> “some_ident” io.getquill.naming.UpperCase = some_ident -> SOME_IDENT io.getquill.naming.LowerCase = SOME_IDENT -> some_ident io.getquill.naming.SnakeCase = someIdent -> some_ident io.getquill.naming.CamelCase = some_ident -> someIdent io.getquill.naming.MysqlEscape = some_ident -> `some_ident` io.getquill.naming.PostgresEscape= $some_ident -> $some_ident
  • 10. Cont.Cont. Operations:- 1. Filter 2. Map 3. FlatMap 4. SortBy 5. Drop/take 6. GroupBy 7. Union 8. Joins And many more...
  • 11. Cont.Cont. Insert: val insQuery = quote(query[Contact].insert) ctx.run(insQuery)(List(Contact(999, "987654321"))) // INSERT INTO Person (id,age) VALUES (?, ?) Update: val upQuery = quote {query[Person].filter(_.id == 999).update} ctx.run(upQuery)(List(Person(999, "John", 22))) // UPDATE Person SET id = ?, name = ?, age = ? WHERE id = 999 Delete: val delQuery = quote {query[Person].filter(p => p.name == "").delete} ctx.run(delQuery) // DELETE FROM Person WHERE name = ''
  • 12. Cont.Cont. - Supports transactions - Not supported features can be implemented by INFIX - Supports H2, MySql, Postgres, cassandra.
  • 13. Slick v/s Quill SLICK Quill Popularity 9.2 7.7 Activity 6.7 9.2 Repository Stars 1602 Watchers 156 Forks 376 Last Commit 9 days ago Stars 631 Watchers 40 Forks 75 Last Commit 3 days ago
  • 15. Questions & Ans.fold(“you’ll get it tomorrow”)(identity)

Editor's Notes

  1. - Boilerplate-free mapping Quoted DSL: Queries are defined inside a quote block. Quill parses each quoted block of code (quotation) at compile time and translates them to an internal Abstract Syntax Tree (AST) Compile-time query generation: The ctx.run call reads the quotation’s AST and translates it to the target language at compile time, emitting the query string as a compilation message. As the query string is known at compile time, the runtime overhead is very low and similar to using the database driver directly. Compile-time query validation: If configured, the query is verified against the database at compile time and the compilation fails if it is not valid. The query validation does not alter the database state.
  2. - Boilerplate-free mapping Quoted DSL: Queries are defined inside a quote block. Quill parses each quoted block of code (quotation) at compile time and translates them to an internal Abstract Syntax Tree (AST) Compile-time query generation: The ctx.run call reads the quotation’s AST and translates it to the target language at compile time, emitting the query string as a compilation message. As the query string is known at compile time, the runtime overhead is very low and similar to using the database driver directly. Compile-time query validation: If configured, the query is verified against the database at compile time and the compilation fails if it is not valid. The query validation does not alter the database state.
  3. - Boilerplate-free mapping Quoted DSL: Queries are defined inside a quote block. Quill parses each quoted block of code (quotation) at compile time and translates them to an internal Abstract Syntax Tree (AST) Compile-time query generation: The ctx.run call reads the quotation’s AST and translates it to the target language at compile time, emitting the query string as a compilation message. As the query string is known at compile time, the runtime overhead is very low and similar to using the database driver directly. Compile-time query validation: If configured, the query is verified against the database at compile time and the compilation fails if it is not valid. The query validation does not alter the database state.
  4. The SQL dialect to be used by the context is defined by the first type parameter. Some context types are specific to a database and thus not require it. Quill has four built-in dialects: io.getquill.H2Dialect io.getquill.MySQLDialect io.getquill.PostgresDialect io.getquill.SqliteDialect
  5. The SQL dialect to be used by the context is defined by the first type parameter. Some context types are specific to a database and thus not require it. Quill has four built-in dialects: io.getquill.H2Dialect io.getquill.MySQLDialect io.getquill.PostgresDialect io.getquill.SqliteDialect
  6. The SQL dialect to be used by the context is defined by the first type parameter. Some context types are specific to a database and thus not require it. Quill has four built-in dialects: io.getquill.H2Dialect io.getquill.MySQLDialect io.getquill.PostgresDialect io.getquill.SqliteDialect
  7. The SQL dialect to be used by the context is defined by the first type parameter. Some context types are specific to a database and thus not require it. Quill has four built-in dialects: io.getquill.H2Dialect io.getquill.MySQLDialect io.getquill.PostgresDialect io.getquill.SqliteDialect
  8. The SQL dialect to be used by the context is defined by the first type parameter. Some context types are specific to a database and thus not require it. Quill has four built-in dialects: io.getquill.H2Dialect io.getquill.MySQLDialect io.getquill.PostgresDialect io.getquill.SqliteDialect
  9. Piyush Mishra
  10. Piyush Mishra