Created
November 1, 2019 20:23
-
-
Save gregopet/9a9c0321352ba5f99859ec4789222edc to your computer and use it in GitHub Desktop.
jOOQ plain SQL examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jooq.fetchOne("select * from film limit 1"); | |
// type safety for fields | |
jooq.fetchOne("select * from film limit 1").get(FILM.TITLE); | |
jooq.fetchOne("select title || ' ' || description from film limit 1").get(0, String.class); | |
// parameters | |
jooq | |
.fetchOne("select title || ' ' || description from film where title like ? limit 1", "DINOSAUR%") | |
.get(0, String.class); | |
jooq | |
.fetchOne("select title || ' ' || description from film where title like :title limit 1", param("title", "DINOSAUR%")) | |
.get(0, String.class); | |
// convenience methods (despite using plain SQL) | |
jooq | |
.fetch("select * from film where title like :title limit 1", param("title", "DINOSAUR%")) | |
.intoGroups(FILM.LANGUAGE_ID); | |
// conditions | |
jooq | |
.select() | |
.from(FILM) | |
.where( | |
condition("film.fulltext @@ to_tsquery(?)", "(dinosaur | crocodile) & girl"), | |
FILM.LENGTH.lt((short)120) | |
) | |
.fetch(); | |
import org.jooq.Condition; | |
import org.jooq.Field; | |
public Condition fullTextSearch(Field field, String terms) { | |
return condition(field.getQualifiedName() + " @@ to_tsquery(?)", terms); | |
} | |
jooq | |
.select() | |
.from(FILM) | |
.where( | |
fullTextSearch(FILM.FULLTEXT, "(dinosaur | crocodile) & girl"), | |
FILM.LENGTH.lt((short)120) | |
) | |
.fetch(); | |
// composed fields | |
jooq | |
.select( | |
field("title || ' - ' || description ").as("description_and_title") | |
) | |
.from(FILM) | |
.where( | |
fullTextSearch(FILM.FULLTEXT, "(dinosaur | crocodile) & girl"), | |
FILM.LENGTH.lt((short)120) | |
) | |
.fetch(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can you please give an example with
IN
orANY
of PostgreSQL with Jooq running plain sql