[jOOQ/jOOQ#3497] Add Context::derivedTableSubquery

This commit is contained in:
Lukas Eder 2022-02-23 12:08:01 +01:00
parent 8346ee4fa3
commit 3c2fb3721e
23 changed files with 73 additions and 29 deletions

View File

@ -247,6 +247,17 @@ public interface Context<C extends Context<C>> extends Scope {
@NotNull
C subquery(boolean subquery);
/**
* Whether the current context is rendering a derived table subquery.
*/
boolean derivedTableSubquery();
/**
* Set the new context value for {@link #derivedTableSubquery()}.
*/
@NotNull
C derivedTableSubquery(boolean derivedTableSubquery);
/**
* Whether the current context is rendering a predicand subquery, i.e. a
* subquery that is an operand of a predicate.

View File

@ -123,6 +123,7 @@ abstract class AbstractContext<C extends Context<C>> extends AbstractScope imple
int subquery;
BitSet subqueryScopedNestedSetOperations;
boolean predicandSubquery;
boolean derivedTableSubquery;
int stringLiteral;
String stringLiteralEscapedApos = "'";
int index;
@ -313,7 +314,7 @@ abstract class AbstractContext<C extends Context<C>> extends AbstractScope imple
@Override
public final C visitSubquery(QueryPart part) {
Tools.visitSubquery(this, part, false);
Tools.visitSubquery(this, part, false, false);
return (C) this;
}
@ -658,6 +659,17 @@ abstract class AbstractContext<C extends Context<C>> extends AbstractScope imple
return (C) this;
}
@Override
public final boolean derivedTableSubquery() {
return derivedTableSubquery;
}
@Override
public final C derivedTableSubquery(boolean s) {
derivedTableSubquery = s;
return (C) this;
}
@Override
public final boolean subquery() {
return subquery > 0;

View File

@ -214,7 +214,7 @@ final class Alias<Q extends QueryPart> extends AbstractQueryPart implements UEmp
&& (SUPPORT_DERIVED_COLUMN_NAMES_SPECIAL1.contains(dialect))
&& (wrapped instanceof TableImpl || wrapped instanceof CommonTableExpressionImpl)) {
visitSubquery(context, select(asterisk()).from(((Table<?>) wrapped).as(alias)), false);
visitSubquery(context, select(asterisk()).from(((Table<?>) wrapped).as(alias)), true, false);
}
// [#1801] Some databases do not support "derived column names".
@ -287,15 +287,14 @@ final class Alias<Q extends QueryPart> extends AbstractQueryPart implements UEmp
}
}
visitSubquery(context, select(fields).where(falseCondition()).unionAll(wrappedAsSelect), false);
visitSubquery(context, select(fields).where(falseCondition()).unionAll(wrappedAsSelect), true, false);
}
}
}
// The default behaviour
else {
else
toSQLWrapped(context);
}
// [#291] some aliases cause trouble, if they are not explicitly marked using "as"
toSQLAs(context);

View File

@ -120,9 +120,9 @@ final class AliasedSelect<R extends Record> extends AbstractTable<R> implements
// they cannot be referenced. In that case, revert to
// actual derived table usage.
if (ctx.family() == DERBY && q != null && q.hasUnions())
visitSubquery(ctx, selectFrom(query.asTable(DSL.name("t"), aliases)), false, false);
visitSubquery(ctx, selectFrom(query.asTable(DSL.name("t"), aliases)), true, false, false);
else
ctx.data(DATA_SELECT_ALIASES, aliases, subquery ? c -> visitSubquery(c, query, false, false) : c -> c.visit(query));
ctx.data(DATA_SELECT_ALIASES, aliases, subquery ? c -> visitSubquery(c, query, true, false, false) : c -> c.visit(query));
}
@Override // Avoid AbstractTable implementation

View File

@ -82,7 +82,7 @@ final class ArrayQuery<T> extends AbstractField<T[]> implements QOM.ArrayQuery<T
// [#11053] TODO: Move ORDER BY clause from subquery to ARRAY_AGG
// See https://github.com/jOOQ/jOOQ/issues/11053#issuecomment-735773248
visitSubquery(ctx, DSL.select(arrayAgg(c)).from(t), false);
visitSubquery(ctx, DSL.select(arrayAgg(c)).from(t), false, false);
break;
}

View File

@ -138,7 +138,7 @@ final class CommonTableExpressionImpl<R extends Record> extends AbstractTable<R>
}
visitSubquery(ctx, s, false);
visitSubquery(ctx, s, true, false);

View File

@ -94,7 +94,7 @@ class DerivedTable<R extends Record> extends AbstractTable<R> implements QOM.Der
visitSubquery(ctx, query, false, false);
visitSubquery(ctx, query, true, false, false);
}
@Override // Avoid AbstractTable implementation

View File

@ -105,7 +105,7 @@ implements
default:
ctx.visit(K_EXISTS).sql(' ');
visitSubquery(ctx, query, true);
visitSubquery(ctx, query, false, true);
break;
}
}

View File

@ -157,6 +157,7 @@ implements
withRecursive(name, name)
.as(select(from).unionAll(select(iadd(f, step == null ? inline(1) : step)).from(name).where(f.lt(to))))
.select(f).from(name),
true,
false
);
}
@ -208,6 +209,8 @@ implements

View File

@ -229,6 +229,7 @@ implements
json.getType() == JSONB.class ? json : json.cast(JSONB),
path
),
true,
false
);
}

View File

@ -167,6 +167,7 @@ final class Multiset<R extends Record> extends AbstractField<Result<R>> implemen
default: {
JSONArrayAggOrderByStep<JSON> order;
JSONArrayAggReturningStep<JSON> returning;
@ -188,7 +189,7 @@ final class Multiset<R extends Record> extends AbstractField<Result<R>> implemen
if (multisetCondition && NO_SUPPORT_JSON_COMPARE.contains(ctx.dialect()))
ctx.visit(DSL.field(s).cast(VARCHAR));
else
visitSubquery(ctx, s, false);
visitSubquery(ctx, s, false, false);
break;
}
@ -219,6 +220,7 @@ final class Multiset<R extends Record> extends AbstractField<Result<R>> implemen
default: {
JSONArrayAggOrderByStep<JSONB> order;
JSONArrayAggReturningStep<JSONB> returning;
@ -240,7 +242,7 @@ final class Multiset<R extends Record> extends AbstractField<Result<R>> implemen
if (multisetCondition && NO_SUPPORT_JSONB_COMPARE.contains(ctx.dialect()))
ctx.visit(DSL.field(s).cast(VARCHAR));
else
visitSubquery(ctx, s, false);
visitSubquery(ctx, s, false, false);
break;
}
@ -287,7 +289,7 @@ final class Multiset<R extends Record> extends AbstractField<Result<R>> implemen
if (multisetCondition && NO_SUPPORT_XML_COMPARE.contains(ctx.dialect()))
ctx.visit(xmlserializeContent(DSL.field(s), VARCHAR));
else
visitSubquery(ctx, s, false);
visitSubquery(ctx, s, false, false);
break;
}
@ -297,7 +299,7 @@ final class Multiset<R extends Record> extends AbstractField<Result<R>> implemen
}
case NATIVE:
visitSubquery(ctx.visit(K_MULTISET), select, false);
visitSubquery(ctx.visit(K_MULTISET), select, false, false);
break;
}
}

View File

@ -111,7 +111,7 @@ final class QuantifiedSelectImpl<R extends Record> extends AbstractQueryPart imp
default:
ctx.visit(quantifier.toKeyword());
ctx.sql(extraParentheses ? " ((" : " (");
visitSubquery(ctx, delegate(ctx.configuration()), true, false);
visitSubquery(ctx, delegate(ctx.configuration()), false, true, false);
ctx.sql(extraParentheses ? "))" : ")");
break;
}

View File

@ -138,7 +138,7 @@ final class RowIsDistinctFrom extends AbstractCondition implements UNotYetImplem
if (rhsRow != null)
ctx.visit(rhsRow);
else
visitSubquery(ctx, rhsSelect, true);
visitSubquery(ctx, rhsSelect, false, true);
if (!not)
ctx.sql(')');
@ -154,7 +154,7 @@ final class RowIsDistinctFrom extends AbstractCondition implements UNotYetImplem
if (rhsRow != null)
ctx.visit(rhsRow);
else
visitSubquery(ctx, rhsSelect, true);
visitSubquery(ctx, rhsSelect, false, true);
}

View File

@ -251,7 +251,7 @@ final class RowSubqueryCondition extends AbstractCondition implements UNotYetImp
boolean extraParentheses = false ;
ctx.sql(extraParentheses ? "((" : "(")
.data(BooleanDataKey.DATA_ROW_VALUE_EXPRESSION_PREDICATE_SUBQUERY, true, c -> visitSubquery(c, right, true, false))
.data(BooleanDataKey.DATA_ROW_VALUE_EXPRESSION_PREDICATE_SUBQUERY, true, c -> visitSubquery(c, right, false, true, false))
.sql(extraParentheses ? "))" : ")");
}

View File

@ -86,9 +86,9 @@ final class ScalarSubquery<T> extends AbstractField<T> implements QOM.ScalarSubq
// HSQLDB allows for using WITH inside of IN, see: https://sourceforge.net/p/hsqldb/bugs/1617/
// We'll still emulate CTE in scalar subqueries with a derived tables in all cases.
if (q != null && q.with != null && NO_SUPPORT_WITH_IN_SCALAR_SUBQUERY.contains(ctx.dialect()))
visitSubquery(ctx, select(asterisk()).from(query.asTable("t")), predicandSubquery);
visitSubquery(ctx, select(asterisk()).from(query.asTable("t")), false, predicandSubquery);
else
visitSubquery(ctx, query, predicandSubquery);
visitSubquery(ctx, query, false, predicandSubquery);
}
// -------------------------------------------------------------------------

View File

@ -90,7 +90,7 @@ final class SelectIsNotNull extends AbstractCondition implements QOM.SelectIsNot
}
private final void acceptStandard(Context<?> ctx) {
visitSubquery(ctx, select, true);
visitSubquery(ctx, select, false, true);
switch (ctx.family()) {

View File

@ -124,7 +124,7 @@ final class SelectIsNull extends AbstractCondition implements QOM.SelectIsNull {
}
private final void acceptStandard(Context<?> ctx) {
visitSubquery(ctx, select, true);
visitSubquery(ctx, select, false, true);
switch (ctx.family()) {

View File

@ -176,6 +176,7 @@ implements
visitSubquery(
ctx,
withRecursive(s1, s2).select(DSL.coalesce(DSL.max(DSL.field(name("x"))), inline(""))).from(s2).where(s2.field("n").eq((Field) n)),
true,
false
);
break;

View File

@ -2498,11 +2498,22 @@ final class Tools {
return e;
}
static final void visitSubquery(Context<?> ctx, QueryPart query, boolean predicandSubquery) {
visitSubquery(ctx, query, predicandSubquery, true);
static final void visitSubquery(
Context<?> ctx,
QueryPart query,
boolean derivedTableSubquery,
boolean predicandSubquery
) {
visitSubquery(ctx, query, derivedTableSubquery, predicandSubquery, true);
}
static final void visitSubquery(Context<?> ctx, QueryPart query, boolean predicandSubquery, boolean parentheses) {
static final void visitSubquery(
Context<?> ctx,
QueryPart query,
boolean derivedTableSubquery,
boolean predicandSubquery,
boolean parentheses
) {
@ -2512,14 +2523,17 @@ final class Tools {
ctx.sql('(');
boolean previousPredicandSubquery = ctx.predicandSubquery();
boolean previousDerivedTableSubquery = ctx.derivedTableSubquery();
ctx.subquery(true)
.predicandSubquery(predicandSubquery)
.derivedTableSubquery(derivedTableSubquery)
.formatIndentStart()
.formatNewLine()
.visit(query)
.formatIndentEnd()
.formatNewLine()
.derivedTableSubquery(previousDerivedTableSubquery)
.predicandSubquery(previousPredicandSubquery)
.subquery(false);

View File

@ -103,7 +103,7 @@ implements
case H2:
ctx.visit(K_UNIQUE).sql(' ');
visitSubquery(ctx, query, true);
visitSubquery(ctx, query, false, true);
break;
default:

View File

@ -650,7 +650,7 @@ final class UpdateQueryImpl<R extends Record> extends AbstractStoreQuery<R> impl
else
select = multiSelect;
visitSubquery(ctx, select, false);
visitSubquery(ctx, select, false, false);
}
ctx.formatIndentEnd().end(UPDATE_SET_ASSIGNMENT);

View File

@ -183,7 +183,7 @@ final class Values<R extends Record> extends AbstractTable<R> implements QOM.Val
selects = selects.unionAll(select);
}
visitSubquery(ctx, selects, false, false);
visitSubquery(ctx, selects, true, false, false);
}

View File

@ -262,6 +262,7 @@ implements
private final void acceptStandard(Context<?> ctx) {