Search in sources :

Example 1 with K_EXCLUDE

use of org.jooq.impl.Keywords.K_EXCLUDE 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();
}
Also used : ROWS(org.jooq.impl.QOM.FrameUnits.ROWS) Arrays(java.util.Arrays) BiFunction(java.util.function.BiFunction) MAX_VALUE(java.lang.Integer.MAX_VALUE) DSL.field(org.jooq.impl.DSL.field) UnmodifiableList(org.jooq.impl.QOM.UnmodifiableList) CURRENT_ROW(org.jooq.impl.QOM.FrameExclude.CURRENT_ROW) GROUP(org.jooq.impl.QOM.FrameExclude.GROUP) RANGE(org.jooq.impl.QOM.FrameUnits.RANGE) SQLDialect(org.jooq.SQLDialect) MIN_VALUE(java.lang.Integer.MIN_VALUE) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) GROUPS(org.jooq.impl.QOM.FrameUnits.GROUPS) Field(org.jooq.Field) DSL.select(org.jooq.impl.DSL.select) CUBRID(org.jooq.SQLDialect.CUBRID) K_CURRENT_ROW(org.jooq.impl.Keywords.K_CURRENT_ROW) K_BETWEEN(org.jooq.impl.Keywords.K_BETWEEN) QueryPart(org.jooq.QueryPart) WindowSpecificationPartitionByStep(org.jooq.WindowSpecificationPartitionByStep) Context(org.jooq.Context) K_UNBOUNDED_PRECEDING(org.jooq.impl.Keywords.K_UNBOUNDED_PRECEDING) StringUtils.defaultIfNull(org.jooq.tools.StringUtils.defaultIfNull) K_AND(org.jooq.impl.Keywords.K_AND) WindowSpecificationFinalStep(org.jooq.WindowSpecificationFinalStep) K_FOLLOWING(org.jooq.impl.Keywords.K_FOLLOWING) RenderImplicitWindowRange(org.jooq.conf.RenderImplicitWindowRange) EMPTY_SORTFIELD(org.jooq.impl.Tools.EMPTY_SORTFIELD) FrameExclude(org.jooq.impl.QOM.FrameExclude) WindowSpecificationRowsAndStep(org.jooq.WindowSpecificationRowsAndStep) K_EXCLUDE(org.jooq.impl.Keywords.K_EXCLUDE) DATA_WINDOW_FUNCTION(org.jooq.impl.Tools.DataExtendedKey.DATA_WINDOW_FUNCTION) WindowSpecificationOrderByStep(org.jooq.WindowSpecificationOrderByStep) K_ORDER_BY(org.jooq.impl.Keywords.K_ORDER_BY) SQLITE(org.jooq.SQLDialect.SQLITE) K_PARTITION_BY(org.jooq.impl.Keywords.K_PARTITION_BY) Function1(org.jooq.Function1) K_PRECEDING(org.jooq.impl.Keywords.K_PRECEDING) MYSQL(org.jooq.SQLDialect.MYSQL) WindowSpecificationExcludeStep(org.jooq.WindowSpecificationExcludeStep) Tools.isEmpty(org.jooq.impl.Tools.isEmpty) K_UNBOUNDED_FOLLOWING(org.jooq.impl.Keywords.K_UNBOUNDED_FOLLOWING) TIES(org.jooq.impl.QOM.FrameExclude.TIES) NO_OTHERS(org.jooq.impl.QOM.FrameExclude.NO_OTHERS) DSL.one(org.jooq.impl.DSL.one) FrameUnits(org.jooq.impl.QOM.FrameUnits) SortField(org.jooq.SortField) H2(org.jooq.SQLDialect.H2) OrderField(org.jooq.OrderField) MARIADB(org.jooq.SQLDialect.MARIADB) EMPTY_FIELD(org.jooq.impl.Tools.EMPTY_FIELD) FrameUnits(org.jooq.impl.QOM.FrameUnits)

Aggregations

MAX_VALUE (java.lang.Integer.MAX_VALUE)1 MIN_VALUE (java.lang.Integer.MIN_VALUE)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Set (java.util.Set)1 BiFunction (java.util.function.BiFunction)1 Predicate (java.util.function.Predicate)1 Context (org.jooq.Context)1 Field (org.jooq.Field)1 Function1 (org.jooq.Function1)1 OrderField (org.jooq.OrderField)1 QueryPart (org.jooq.QueryPart)1 SQLDialect (org.jooq.SQLDialect)1 CUBRID (org.jooq.SQLDialect.CUBRID)1 H2 (org.jooq.SQLDialect.H2)1 MARIADB (org.jooq.SQLDialect.MARIADB)1 MYSQL (org.jooq.SQLDialect.MYSQL)1 SQLITE (org.jooq.SQLDialect.SQLITE)1 SortField (org.jooq.SortField)1 WindowSpecificationExcludeStep (org.jooq.WindowSpecificationExcludeStep)1