101
An Introduction to JOOQ (or, Long Live SQL) Steve Pember @svpember Gr8Conf US, 2016 THIRDCHANNEL @svpember

An Introduction to jOOQ

Embed Size (px)

Citation preview

An Introduction to JOOQ (or, Long Live SQL)

Steve Pember @svpember

Gr8Conf US, 2016

THIRDCHANNEL @svpember

You may find this to be terribly boring.

THIRDCHANNEL @svpember

Agenda• Growing beyond Object Relational Mappers

• An Introduction to jOOQ

• What We’ve Learned (A.K.A. jOOQ and Groovy)

• Demo / Code Samples

The Case for SQL

ORMs are Unnecessary

This debate is not new.

ORMs Can Save You Time…

JPA, anyone?

THIRDCHANNEL @svpember

… but they have flaws.

THIRDCHANNEL @svpember

ORM Difficulties• Object / Relational Impedance Mismatch

Objects: identity, state, behavior

RDBMS: attribute storage, retrieved by predicate logic

THIRDCHANNEL @svpember

THIRDCHANNEL @svpember

ORM Difficulties• Object / Relational Impedance Mismatch

• ORM Oriented Schema Design

THIRDCHANNEL @svpember

THIRDCHANNEL @svpember

THIRDCHANNEL @svpember

Wonderful.

Single Table vs Multi Table Inheritance

THIRDCHANNEL @svpember

ORM Difficulties• Object / Relational Impedance Mismatch

• ORM Oriented Schema Design

• Trouble with Complicated Queries

E.G. Criteria, HQL , @Query, etc.

THIRDCHANNEL @svpember

THIRDCHANNEL @svpember

ORM Difficulties• Object / Relational Impedance Mismatch

• ORM Oriented Schema Design

• Trouble with Complicated Queries

• Resource Usage

Maintain That Entity Graph

THIRDCHANNEL @svpember

ORM Difficulties• Object / Relational Impedance Mismatch

• ORM Oriented Schema Design

• Trouble with Complicated Queries

• Resource Usage

• Performance

To Re-phrase My Original Point:

ORMs Make You Weak

THIRDCHANNEL @svpember

SQL makes you Strong

THIRDCHANNEL @svpember

So What To Do?

How about some raw JDBC?!

Problems?

THIRDCHANNEL @svpember

What Was Wrong?

Enter: jOOQ!

THIRDCHANNEL @svpember

Agenda• Growing beyond Object Relational Mappers

• An Introduction to jOOQ

THIRDCHANNEL @svpember

jOOQ (Intro)• Library for SQL interaction

THIRDCHANNEL @svpember

jOOQ (Intro)• Library for SQL interaction

• For people not afraid of SQL

THIRDCHANNEL @svpember

jOOQ (Intro)• Library for SQL interaction

• For people not afraid of SQL

• The jOOQ authors LOVE SQL

And they are eager to help

THIRDCHANNEL @svpember

jOOQ (Intro)• Library for SQL interaction

• For people not afraid of SQL

• The jOOQ authors LOVE SQL

• Database should drive internal models

jOOQ Features

THIRDCHANNEL @svpember

jOOQ (Features)• Type-Safe SQL

THIRDCHANNEL @svpember

THIRDCHANNEL @svpember

jOOQ (Features)• Type-Safe SQL

• Fluent API

THIRDCHANNEL @svpember

jOOQ (Features)• Type-Safe SQL

• Fluent API

• Code Generation

THIRDCHANNEL @svpember

–Johnny Appleseed

“Type a quote here.”

THIRDCHANNEL @svpember

jOOQ (Features)• Type-Safe SQL

• Fluent API

• Code Generation

• Active Record

THIRDCHANNEL @svpember

THIRDCHANNEL @svpember

jOOQ (Features)• Type-Safe SQL

• Fluent API

• Code Generation

• Active Record

• Full SQL Support

Even Vendor Specific Operations?

THIRDCHANNEL @svpember

jOOQ (Features)• Type-Safe SQL

• Fluent API

• Code Generation

• Active Record

• Full SQL Support

• Native Support of Stored Procedures

THIRDCHANNEL @svpember

jOOQ (Features)• Type-Safe SQL

• Fluent API

• Code Generation

• Active Record

• Full SQL Support

• Native Support of Stored Procedures

• Open Source and Free

Unless You’re Using Oracle…

THIRDCHANNEL @svpember

Code Overview

Don’t want to take up too much of your time…

But let’s talk about Fetching

THIRDCHANNEL @svpember

Fetch• fetchOne

THIRDCHANNEL @svpember

THIRDCHANNEL @svpember

THIRDCHANNEL @svpember

Fetch• fetchOne

• fetchAny

THIRDCHANNEL @svpember

THIRDCHANNEL @svpember

Fetch• fetchOne

• fetchAny

• fetchLazy

THIRDCHANNEL @svpember

THIRDCHANNEL @svpember

Fetch• fetchOne

• fetchAny

• fetchLazy

• fetchLater

fetchLater -> returns a Promise… but is gone in 4.0

THIRDCHANNEL @svpember

Fetch• fetchOne

• fetchAny

• fetchLazy

• fetchLater

• fetchMany

fetchMany() -> you have stored procedures with multiple result sets

THIRDCHANNEL @svpember

Fetch• fetchOne

• fetchAny

• fetchLazy

• fetchLater

• fetchMany

• fetchInto

fetchInto() is quite flexible

Fetch into Records! Fetch into POGOs!

Fetching into POGOs keeps layers separate

THIRDCHANNEL @svpember

THIRDCHANNEL @svpember

–Johnny Appleseed

“Type a quote here.”

And with Java 8…

Fetch Into Streams!

THIRDCHANNEL @svpember

Agenda• Growing beyond Object Relational Mappers

• An Introduction to jOOQ

• What We’ve Learned (A.K.A. jOOQ and Groovy)

THIRDCHANNEL @svpember

What We’ve Learned• Code Generation is Not Mandatory

THIRDCHANNEL @svpember

THIRDCHANNEL @svpember

What We’ve Learned• Code Generation is Not Mandatory

• jOOQ Does Not Manage Transactions For You!

THIRDCHANNEL @svpember

What We’ve Learned• Code Generation is Not Mandatory

• jOOQ Does Not Manage Transactions For You!

• Import / Export Direct to CSV

THIRDCHANNEL @svpember

What We’ve Learned• Code Generation is Not Mandatory

• jOOQ Does Not Manage Transactions For You!

• Import / Export Direct to CSV

• Easy & Clear Logging

THIRDCHANNEL @svpember

What We’ve Learned• Code Generation is Not Mandatory

• jOOQ Does Not Manage Transactions For You!

• Import / Export Direct to CSV

• Easy & Clear Logging

• Binders / Casting

–Johnny Appleseed

“Type a quote here.”

THIRDCHANNEL @svpember

THIRDCHANNEL @svpember

THIRDCHANNEL @svpember

What We’ve Learned• Code Generation is Not Mandatory

• jOOQ Does Not Manage Transactions For You!

• Import / Export Direct to CSV

• Easy & Clear Logging

• Binders / Casting

• jOOQ & CompileStatic

THIRDCHANNEL @svpember

THIRDCHANNEL @svpember

Agenda• Growing beyond Object Relational Mappers

• An Introduction to jOOQ

• What We’ve Learned (A.K.A. jOOQ and Groovy)

• Demo / Code Samples

Thank You!

Steve Pember @svpember

THIRDCHANNEL @svpember

THIRDCHANNEL @svpember

More Info• jOOQ Blog: https://blog.jooq.org/

• Vietnam of Computer Science: http://blogs.tedneward.com/post/the-vietnam-of-computer-science/

• Fluent API: http://martinfowler.com/bliki/FluentInterface.html

• jOOQ Gradle Plugin: https://github.com/etiennestuder/gradle-jooq-plugin

• Code Demo: https://github.com/spember/jooq-demo

THIRDCHANNEL @svpember

Images• Brave Puffin: https://memegenerator.net/instance/63187852

• Weak(Gravity Falls): http://giphy.com/gifs/weak-gravity-falls-sTm3FmD7knvO0

• Strong (Spongebob): http://www.reactiongifs.com/strongbob/?utm_source=rss&utm_medium=rss&utm_campaign=strongbob

• Andy Dwyer (No Idea): http://memecaptain.com/gend_image_pages/iepvqA

• Atticus Finch: http://www.latimes.com/entertainment/movies/la-et-mn-atticus-white-savior-20150715-story.html

• Yikes (Adam Scott): http://giphy.com/gifs/awkward-yikes-adam-scott-srFijDgoNj008

• Suspicious Fry: http://imgur.com/gallery/OCcno

• Strong (Spongebob): http://www.reactiongifs.com/strongbob/?utm_source=rss&utm_medium=rss&utm_campaign=strongbob