Skip to content

Instantly share code, notes, and snippets.

@gregopet
Created November 1, 2019 20:23
Show Gist options
  • Save gregopet/9a9c0321352ba5f99859ec4789222edc to your computer and use it in GitHub Desktop.
Save gregopet/9a9c0321352ba5f99859ec4789222edc to your computer and use it in GitHub Desktop.
jOOQ plain SQL examples
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();
@hrishikesh
Copy link

can you please give an example with IN or ANY of PostgreSQL with Jooq running plain sql

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment