Friday, March 17, 2023
HomeJavaA Temporary Overview over the Most Widespread jOOQ Sorts – Java, SQL...

A Temporary Overview over the Most Widespread jOOQ Sorts – Java, SQL and jOOQ.


For brand new customers working with jOOQ for the primary time, the variety of sorts within the jOOQ API might be overwhelming. The SQL language doesn’t have many such “seen” sorts, though if you consider SQL the way in which jOOQ does, then they’re there simply the identical, however hidden from customers by way of an English model syntax.

This overview will listing an important jOOQ sorts in a cheat sheet type.

Configuration sorts

The Configuration is the one most necessary configuration kind, which comprises references to all different kinds of configuration, Settings, customized SPI implementations, JDBC or R2DBC Connection, and many others. SPIs embrace:

And lots of extra, which you’ll see from the Configuration Javadoc

It’s made accessible from each Scope kind within the API, see under for particulars

Scopes

The Scope sorts are varied sorts which might be created “within the scope” of a Configuration, and as such can present entry to all the Configuration‘s contained objects and SPIs. This design permits for very versatile, programmatic dependency injection all through the internals of jOOQ. A number of the most necessary Scope sorts embrace:

For different sorts, seek advice from the Scope Javadoc.

Settings

Settings are largely scalar flags that specify detailed behaviour in jOOQ. Some choose examples embrace:

As of jOOQ 3.17, there are over 160 such settings, so we are able to’t listing all of them right here. For extra particulars, seek advice from the Settings Javadoc.

DSL sorts

The DSL API is an important API to work with jOOQ. It is available in 2 flavours.

The static DSL

The static DSL comprises entry factors to each kind of QueryPart building DSLs, together with:

… and much more. All of those sorts are constructed statically, and as such, they don’t have any Configuration hooked up.

The “context” DSL

The “context” DSL, represented by the DSLContext kind, solely affords establishing QueryPart sorts that revenue from being created “within the context” of a Configuration. That is primarily simply together with:

A Question that has been constructed from the DSLContext kind might be executed straight by utilizing Question.execute() or ResultQuery.fetch(), or many different execution strategies, together with asynchronous or reactive ones.

Step sorts

All through the DSL API, you will note so-called “Step” sorts, i.e. sorts with a “Step” suffix, resembling e.g. SelectFromStep, which is the “Step” that provides entry to the Choose DSL’s FROM clause.

You need to by no means reference these sorts straight, nor see them in your personal code. They’re intermediate DSL artifacts

QueryPart sorts

QueryPart is the widespread base kind of the whole jOOQ expression tree, or mannequin API. Each kind that you simply assemble with the DSL API will prolong QueryPart, for instance:

QueryPart p1 = TABLE;
QueryPart p2 = TABLE.COLUMN;
QueryPart p3 = TABLE.COLUMN.eq(1);

The above expressions produce a extra particular kind than QueryPart, which we’ll clarify after, however all of them prolong QueryPart.

A QueryPart is a sort that may be rendered within the context of a Configuration utilizing DSLContext::render

String sql = ctx.render(TABLE.COLUMN.eq(1));

A very powerful QueryPart subtypes embrace:

Desk

A Desk can be utilized in a FROM clause of a SELECT assertion, or as a goal of a DML assertion, and extra. There are numerous completely different desk sorts, together with:

There are lots of extra attainable desk expressions in jOOQ, all implementing the Desk kind. An instance of utilizing Desk expressions is:

Desk<?> joined = CUSTOMER
    .be a part of(ADDRESS)
    .on(ADDRESS.CUSTOMER_ID.eq(CUSTOMER.CUSTOMER_ID));

Whereas most jOOQ statements gained’t work with such native variables, it’s all the time fascinating to do not forget that with jOOQ, each question is a dynamic SQL question, and each SQL fragment is a totally self contained expression tree in Java, which might be assigned to any native variable or returned from a technique, and many others.

Subject

A Subject is a column expression, which can be utilized in plenty of locations all through the jOOQ API, in every single place the place column expressions can be utilized, together with:

And rather more.

Situation

A Situation is only a Subject<Boolean> with some extra API particular to Situation constructing, together with the opportunity of calling Situation::and, Situation::or, and others. Varied clauses settle for Situation explicitly, together with:

And extra.

Row

A Row or row worth expression is used to mannequin a tuple of values each for:

Such tuples are helpful to create a structural kind that teams Subject expressions into teams of re-usable objects. Some dialects additionally assist nominal variants of this, referred to as UDT (Consumer Outlined Sort), and jOOQ can emulate UDTs by way of embeddable sorts.

Choose

A Choose is a particular kind of ResultQuery, which can be utilized as:

ResultQuery

A ResultQuery is a Question that may produce Document values in varied assortment kinds (e.g. Stream, End result, Writer, CompletionStage, and many others.). It may be created from varied Question sorts by including the RETURNING clause

Question

A Question is a Assertion that may be executed, that means:

  • A SQL string is generated
  • A PreparedStatement is ready
  • Bind values are sure
  • The PreparedStatement is executed
  • Probably, a ResultSet is fetched.

In an effort to execute a Question, it should be hooked up to a Configuration, which is finished most simply by creating the Question from a DSLContext.

Assertion

A Assertion (not the JDBC Assertion!) is a QueryPart that represents a procedural assertion in:

All Question implementations can be utilized as Assertion in such a procedural context.

QOM Sorts

The QOM (Question Object Mannequin) sorts are an experimental set of sorts publicly declaring the interior mannequin API, which may be very helpful for tree traversal and SQL transformation

End result sorts

When executing a ResultQuery, there are various kinds of End result supported by jOOQ, End result being the default:

End result

The End result kind is a Listing<Document> with plenty of mapping comfort API. It fashions an eagerly fetched JDBC ResultSet, which comprises all the outcomes from the database and no extra reference to the ResultSet itself. That is helpful when the outcome set is reasonably sized.

Cursor

The Cursor kind is an Iterable<Document> with comparable mapping comfort API because the End result kind, but it surely comprises an open JDBC ResultSet, permitting for fetching knowledge from the server in a lazy means. That is helpful when the outcome set is large.

Document

A Document is a base kind for a database file. It permits area based mostly entry of particular person attributes in addition to mapping comfort into customized knowledge sorts. It specialises as:

Bookmark this

Discovered this listing helpful? Bookmark it, as we’ll add extra sorts sooner or later in case new necessary ideas come up.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments