This version of the manual is outdated. For the latest version, follow this link: http://www.jooq.org/doc/3.0/manual.
| The jOOQ User Manual. Multiple Pages : jOOQ classes and their usage : QueryParts and the global architecture | previous : next |
# Everything is a QueryPart
A org.jooq.Query and all its contained objects is a org.jooq.QueryPart. QueryParts essentially provide this functionality:
- they can render SQL using the toSQL(RenderContext) method
- they can bind variables using the bind(BindContext) method
Both of these methods are contained in jOOQ's internal API's org.jooq.QueryPartInternal, which is internally implemented by every QueryPart. QueryPart internals are best illustrated with an example.
# Example: CompareCondition
A simple example can be provided by checking out jOOQ's internal representation of a org.jooq.impl.CompareCondition. It is used for any condition comparing two fields as for example the T_AUTHOR.ID = T_BOOK.AUTHOR_ID condition here:
-- [...] FROM T_AUTHOR JOIN T_BOOK ON T_AUTHOR.ID = T_BOOK.AUTHOR_ID -- [...]
This is how jOOQ implements such a condition:
@Override
public final void bind(BindContext context) throws DataAccessException {
// The CompareCondition itself does not bind any variables.
// But the two fields involved in the condition might do so...
context.bind(field1).bind(field2);
}
@Override
public final void toSQL(RenderContext context) {
// The CompareCondition delegates rendering of the Fields to the Fields
// themselves and connects them using the Condition's comparator operator:
context.sql(field1)
.sql(" ");
// If the second field is null, some convenience behaviour can be
// implemented here
if (field2.isNullLiteral()) {
switch (comparator) {
case EQUALS:
context.sql("is null");
break;
case NOT_EQUALS:
context.sql("is not null");
break;
default:
throw new IllegalStateException("Cannot compare null with " + comparator);
}
}
// By default, also delegate the right hand side's SQL rendering to the
// underlying field
else {
context.sql(comparator.toSQL())
.sql(" ")
.sql(field2);
}
}
For more complex examples, please refer to the codebase, directly
| The jOOQ User Manual. Multiple Pages : jOOQ classes and their usage : QueryParts and the global architecture | previous : next |
