Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internally, how SQL queries are eventually executed by JOOQ? #1

Open
acherm opened this issue Oct 16, 2016 · 9 comments
Open

Internally, how SQL queries are eventually executed by JOOQ? #1

acherm opened this issue Oct 16, 2016 · 9 comments

Comments

@acherm
Copy link
Owner

acherm commented Oct 16, 2016

No description provided.

@maici
Copy link

maici commented Oct 17, 2016

I'll answer to this issue.

@gigouni
Copy link

gigouni commented Oct 17, 2016

I have the intention to close the issue.

@Florian14
Copy link

I'll answer this issue.

2 similar comments
@quentin29200
Copy link

I'll answer this issue.

@dkaakati
Copy link

I'll answer this issue.

@SandraIsabel
Copy link

I'll answer this issue

1 similar comment
@kaouthare
Copy link

I'll answer this issue

@gigouni
Copy link

gigouni commented Oct 19, 2016

The following content will try to explain how JOOQ execute SQL queries.

Technical explanations

According to the documentation, JOOQ provides a SQL Builder to "integrate SQL itself as an "internal domain specific language" directly into Java". JOOQ is actually wrapping a JDBC. When you're doing a SQL query, the wrapped JDBC will call executable objects "java.sql.Statement". The documentation splits it into three different types.

  • java.sql.Statement, or "static statement": This statement type is used for any arbitrary type of SQL statement. It is particularly useful with inlined parameters
  • java.sql.PreparedStatement: This statement type is used for any arbitrary type of SQL statement. It is particularly useful with indexed parameters (note that JDBC does not support named parameters)
  • java.sql.CallableStatement: This statement type is used for SQL statements that are "called" rather than "executed". In particular, this includes calls to stored procedures. Callable statements can register OUT parameters

Examples

Let's get some examples to understand how does it work.

    1. Simple SELECT statement

Using SQL

SELECT 1 FROM BOOK

Using JOOQ

create.selectOne().from(BOOK);

JOOQ, to simplify the query writing process, uses obvious function names.

    1. Complex SELECT statement

Using SQL

  SELECT AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME, COUNT(*)
    FROM AUTHOR
    JOIN BOOK ON AUTHOR.ID = BOOK.AUTHOR_ID
   WHERE BOOK.LANGUAGE = 'DE'
     AND BOOK.PUBLISHED > '2008-01-01'
GROUP BY AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME
  HAVING COUNT(*) > 5
ORDER BY AUTHOR.LAST_NAME ASC NULLS FIRST
   LIMIT 2
  OFFSET 1
     FOR UPDATE

Using JOOQ

Factory create = new Factory(connection, dialect);

create.select(AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME, count())
      .from(AUTHOR)
      .join(BOOK).on(BOOK.AUTHOR_ID.equal(AUTHOR.ID))
      .where(BOOK.LANGUAGE.equal("DE"))
      .and(BOOK.PUBLISHED.greaterThan("2008-01-01"))
      .groupBy(AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)
      .having(count().greaterThan(5))
      .orderBy(AUTHOR.LAST_NAME.asc().nullsFirst())
      .limit(2)
      .offset(1)
      .forUpdate();

Complementary information

Querying SQL with JOOQ is well documented and you can get a lot of information following this link here.

Sources:

@quentin29200
Copy link

Gigouni has a very good summary. JOOQ is an database-mapping software library in Java. JOOQ uses JDBC to execute SQL queries. JOOQ prevents SQL syntax errors.
JOOQ calls JDBC API and JPA annotations to generate SQL queries.

To see more, read https://blog.jooq.org/tag/jdbc/

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

No branches or pull requests

8 participants