use of org.jooq.impl.Tools.DataExtendedKey.DATA_WINDOW_FUNCTION in project jOOQ by jOOQ.
the class WindowSpecificationImpl method accept.
@Override
public final void accept(Context<?> ctx) {
SortFieldList o = orderBy;
// [#8414] [#8593] [#11021] [#11851] Some RDBMS require ORDER BY in some window functions
AbstractWindowFunction<?> w = (AbstractWindowFunction<?>) ctx.data(DATA_WINDOW_FUNCTION);
if (o.isEmpty()) {
boolean ordered = w instanceof Ntile && REQUIRES_ORDER_BY_IN_NTILE.contains(ctx.dialect()) || w instanceof Lead && REQUIRES_ORDER_BY_IN_LEAD_LAG.contains(ctx.dialect()) || w instanceof Lag && REQUIRES_ORDER_BY_IN_LEAD_LAG.contains(ctx.dialect()) || w instanceof Rank && REQUIRES_ORDER_BY_IN_RANK_DENSE_RANK.contains(ctx.dialect()) || w instanceof DenseRank && REQUIRES_ORDER_BY_IN_RANK_DENSE_RANK.contains(ctx.dialect()) || w instanceof PercentRank && REQUIRES_ORDER_BY_IN_PERCENT_RANK_CUME_DIST.contains(ctx.dialect()) || w instanceof CumeDist && REQUIRES_ORDER_BY_IN_PERCENT_RANK_CUME_DIST.contains(ctx.dialect());
if (ordered) {
Field<Integer> constant;
switch(ctx.family()) {
default:
constant = field(select(one()));
break;
}
o = new SortFieldList();
o.add(constant.sortDefault());
}
}
boolean hasWindowDefinitions = windowDefinition != null;
boolean hasPartitionBy = !partitionBy.isEmpty();
boolean hasOrderBy = !o.isEmpty();
boolean hasFrame = frameStart != null;
int clauses = 0;
if (hasWindowDefinitions)
clauses++;
if (hasPartitionBy)
clauses++;
if (hasOrderBy)
clauses++;
if (hasFrame)
clauses++;
boolean indent = clauses > 1;
if (indent)
ctx.formatIndentStart().formatNewLine();
if (windowDefinition != null)
ctx.declareWindows(false, c -> c.visit(windowDefinition));
if (hasPartitionBy) {
// constant expressions in the PARTITION BY clause (HANA)
if (partitionByOne && OMIT_PARTITION_BY_ONE.contains(ctx.dialect())) {
} else {
if (hasWindowDefinitions)
ctx.formatSeparator();
ctx.visit(K_PARTITION_BY).separatorRequired(true).visit(partitionBy);
}
}
if (hasOrderBy) {
if (hasWindowDefinitions || hasPartitionBy)
ctx.formatSeparator();
ctx.visit(K_ORDER_BY).separatorRequired(true).visit(o);
}
if (hasFrame) {
if (hasWindowDefinitions || hasPartitionBy || hasOrderBy)
ctx.formatSeparator();
FrameUnits u = frameUnits;
Integer s = frameStart;
Integer e = frameEnd;
ctx.visit(u.keyword).sql(' ');
if (e != null) {
ctx.visit(K_BETWEEN).sql(' ');
toSQLRows(ctx, s);
ctx.sql(' ').visit(K_AND).sql(' ');
toSQLRows(ctx, e);
} else {
toSQLRows(ctx, s);
}
if (exclude != null)
ctx.sql(' ').visit(K_EXCLUDE).sql(' ').visit(exclude.keyword);
}
if (indent)
ctx.formatIndentEnd().formatNewLine();
}
Aggregations