Search in sources :

Example 1 with K_ORDER_BY

use of org.jooq.impl.Keywords.K_ORDER_BY in project jOOQ by jOOQ.

the class UpdateQueryImpl method accept0.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
final void accept0(Context<?> ctx) {
    boolean declareTables = ctx.declareTables();
    ctx.start(UPDATE_UPDATE).visit(K_UPDATE).sql(' ').declareTables(true).visit(table(ctx)).declareTables(declareTables).end(UPDATE_UPDATE);
    ctx.formatSeparator().start(UPDATE_SET).visit(K_SET).separatorRequired(true);
    // A multi-row update was specified
    if (multiRow != null) {
        // [#6884] This syntax can be emulated trivially, if the RHS is not a SELECT subquery
        if (multiValue != null && !SUPPORT_RVE_SET.contains(ctx.dialect())) {
            FieldMapForUpdate map = new FieldMapForUpdate(table(), UPDATE_SET_ASSIGNMENT);
            for (int i = 0; i < multiRow.size(); i++) {
                Field<?> k = multiRow.field(i);
                Field<?> v = multiValue.field(i);
                map.put(k, Tools.field(v, k));
            }
            ctx.formatIndentStart().formatSeparator().visit(map).formatIndentEnd();
        } else {
            Row row = removeReadonly(ctx, multiRow);
            ctx.start(UPDATE_SET_ASSIGNMENT).formatIndentStart().formatSeparator().qualify(false, c -> c.visit(row)).sql(" = ");
            // right hand side of a SET clause
            if (multiValue != null) {
                // single-degree rows. Let's just always render it, here.
                if (REQUIRE_RVE_ROW_CLAUSE.contains(ctx.dialect()))
                    ctx.visit(K_ROW).sql(" ");
                ctx.visit(removeReadonly(ctx, multiRow, multiValue));
            } else // Subselects or subselect emulations of row value expressions
            {
                Select<?> select;
                if (multiValue != null)
                    select = select(removeReadonly(ctx, multiRow, multiValue).fields());
                else
                    select = multiSelect;
                visitSubquery(ctx, select, false, false, false);
            }
            ctx.formatIndentEnd().end(UPDATE_SET_ASSIGNMENT);
        }
    } else // A regular (non-multi-row) update was specified
    {
        ctx.formatIndentStart().formatSeparator().visit(updateMap).formatIndentEnd();
    }
    ctx.end(UPDATE_SET);
    switch(ctx.family()) {
        default:
            acceptFrom(ctx);
            break;
    }
    if (limit != null && NO_SUPPORT_LIMIT.contains(ctx.dialect()) || !orderBy.isEmpty() && NO_SUPPORT_ORDER_BY_LIMIT.contains(ctx.dialect())) {
        Field<?>[] keyFields = table().getKeys().isEmpty() ? new Field[] { table().rowid() } : (table().getPrimaryKey() != null ? table().getPrimaryKey() : table().getKeys().get(0)).getFieldsArray();
        ctx.start(UPDATE_WHERE).formatSeparator().visit(K_WHERE).sql(' ');
        if (keyFields.length == 1)
            ctx.visit(keyFields[0].in(select((Field) keyFields[0]).from(table()).where(getWhere()).orderBy(orderBy).limit(limit)));
        else
            ctx.visit(row(keyFields).in(select(keyFields).from(table()).where(getWhere()).orderBy(orderBy).limit(limit)));
        ctx.end(UPDATE_WHERE);
    } else {
        ctx.start(UPDATE_WHERE);
        if (hasWhere())
            ctx.formatSeparator().visit(K_WHERE).sql(' ').visit(getWhere());
        ctx.end(UPDATE_WHERE);
        if (!orderBy.isEmpty())
            ctx.formatSeparator().visit(K_ORDER_BY).sql(' ').visit(orderBy);
        if (limit != null)
            ctx.formatSeparator().visit(K_LIMIT).sql(' ').visit(limit);
    }
    ctx.start(UPDATE_RETURNING);
    toSQLReturning(ctx);
    ctx.end(UPDATE_RETURNING);
}
Also used : THROW(org.jooq.conf.WriteIfReadonly.THROW) Arrays(java.util.Arrays) POSTGRES(org.jooq.SQLDialect.POSTGRES) Condition(org.jooq.Condition) DSL.trueCondition(org.jooq.impl.DSL.trueCondition) Record20(org.jooq.Record20) Record4(org.jooq.Record4) Record21(org.jooq.Record21) Record5(org.jooq.Record5) Record2(org.jooq.Record2) Record22(org.jooq.Record22) K_UPDATE(org.jooq.impl.Keywords.K_UPDATE) Record3(org.jooq.Record3) Record1(org.jooq.Record1) Tools.unqualified(org.jooq.impl.Tools.unqualified) Map(java.util.Map) Tools.visitSubquery(org.jooq.impl.Tools.visitSubquery) Record8(org.jooq.Record8) SQLDialect(org.jooq.SQLDialect) Scope(org.jooq.Scope) Record9(org.jooq.Record9) Record6(org.jooq.Record6) Select(org.jooq.Select) Record7(org.jooq.Record7) SettingsTools.getExecuteUpdateWithoutWhere(org.jooq.conf.SettingsTools.getExecuteUpdateWithoutWhere) Set(java.util.Set) K_ROW(org.jooq.impl.Keywords.K_ROW) Row17(org.jooq.Row17) DERBY(org.jooq.SQLDialect.DERBY) Row16(org.jooq.Row16) UPDATE_UPDATE(org.jooq.Clause.UPDATE_UPDATE) Row15(org.jooq.Row15) Row14(org.jooq.Row14) Row19(org.jooq.Row19) Row18(org.jooq.Row18) DSL.row(org.jooq.impl.DSL.row) K_ORDER_BY(org.jooq.impl.Keywords.K_ORDER_BY) SQLITE(org.jooq.SQLDialect.SQLITE) Row20(org.jooq.Row20) UPDATE(org.jooq.Clause.UPDATE) Row22(org.jooq.Row22) Row21(org.jooq.Row21) Record(org.jooq.Record) K_FROM(org.jooq.impl.Keywords.K_FROM) FIREBIRD(org.jooq.SQLDialect.FIREBIRD) K_WHERE(org.jooq.impl.Keywords.K_WHERE) IGNITE(org.jooq.SQLDialect.IGNITE) YUGABYTEDB(org.jooq.SQLDialect.YUGABYTEDB) Tools.fieldName(org.jooq.impl.Tools.fieldName) Row13(org.jooq.Row13) Row12(org.jooq.Row12) Row11(org.jooq.Row11) Row10(org.jooq.Row10) RowN(org.jooq.RowN) Row(org.jooq.Row) Tools.map(org.jooq.impl.Tools.map) UNotYetImplemented(org.jooq.impl.QOM.UNotYetImplemented) K_LIMIT(org.jooq.impl.Keywords.K_LIMIT) Table(org.jooq.Table) Operator(org.jooq.Operator) UpdateQuery(org.jooq.UpdateQuery) Clause(org.jooq.Clause) DataTypeException(org.jooq.exception.DataTypeException) DSL.name(org.jooq.impl.DSL.name) Row1(org.jooq.Row1) Row2(org.jooq.Row2) Row3(org.jooq.Row3) Collection(java.util.Collection) Row4(org.jooq.Row4) Row5(org.jooq.Row5) Row6(org.jooq.Row6) Field(org.jooq.Field) Row7(org.jooq.Row7) DSL.select(org.jooq.impl.DSL.select) Row8(org.jooq.Row8) CUBRID(org.jooq.SQLDialect.CUBRID) Row9(org.jooq.Row9) HSQLDB(org.jooq.SQLDialect.HSQLDB) List(java.util.List) Context(org.jooq.Context) K_SET(org.jooq.impl.Keywords.K_SET) UPDATE_WHERE(org.jooq.Clause.UPDATE_WHERE) UPDATE_RETURNING(org.jooq.Clause.UPDATE_RETURNING) UPDATE_SET_ASSIGNMENT(org.jooq.Clause.UPDATE_SET_ASSIGNMENT) UPDATE_FROM(org.jooq.Clause.UPDATE_FROM) TableLike(org.jooq.TableLike) FieldMapForUpdate.removeReadonly(org.jooq.impl.FieldMapForUpdate.removeReadonly) Record17(org.jooq.Record17) Record18(org.jooq.Record18) Record19(org.jooq.Record19) Record13(org.jooq.Record13) Record14(org.jooq.Record14) Record15(org.jooq.Record15) Record16(org.jooq.Record16) Configuration(org.jooq.Configuration) Record10(org.jooq.Record10) Record11(org.jooq.Record11) Record12(org.jooq.Record12) H2(org.jooq.SQLDialect.H2) OrderField(org.jooq.OrderField) UPDATE_SET(org.jooq.Clause.UPDATE_SET) Field(org.jooq.Field) OrderField(org.jooq.OrderField) Row(org.jooq.Row)

Example 2 with K_ORDER_BY

use of org.jooq.impl.Keywords.K_ORDER_BY 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

Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 Set (java.util.Set)2 Context (org.jooq.Context)2 Field (org.jooq.Field)2 OrderField (org.jooq.OrderField)2 SQLDialect (org.jooq.SQLDialect)2 CUBRID (org.jooq.SQLDialect.CUBRID)2 H2 (org.jooq.SQLDialect.H2)2 SQLITE (org.jooq.SQLDialect.SQLITE)2 DSL.select (org.jooq.impl.DSL.select)2 K_ORDER_BY (org.jooq.impl.Keywords.K_ORDER_BY)2 MAX_VALUE (java.lang.Integer.MAX_VALUE)1 MIN_VALUE (java.lang.Integer.MIN_VALUE)1 List (java.util.List)1 Map (java.util.Map)1 BiFunction (java.util.function.BiFunction)1 Predicate (java.util.function.Predicate)1 Clause (org.jooq.Clause)1 UPDATE (org.jooq.Clause.UPDATE)1