Search in sources :

Example 1 with K_ON

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

the class MergeImpl method toSQLStandard.

private final void toSQLStandard(Context<?> ctx) {
    ctx.start(MERGE_MERGE_INTO).visit(K_MERGE_INTO).sql(' ').declareTables(true, c -> c.visit(table)).end(MERGE_MERGE_INTO).formatSeparator().start(MERGE_USING).visit(K_USING).sql(' ');
    ctx.declareTables(true, c1 -> c1.data(DATA_WRAP_DERIVED_TABLES_IN_PARENTHESES, true, c2 -> {
        // in its MERGE statement.
        if (usingDual) {
            switch(c2.family()) {
                case DERBY:
                    c2.visit(new Dual());
                    break;
                default:
                    c2.visit(DSL.selectOne());
                    break;
            }
        } else
            c2.visit(using);
    }));
    boolean onParentheses = false;
    ctx.end(MERGE_USING).formatSeparator().start(MERGE_ON).visit(K_ON).sql(onParentheses ? " (" : " ").visit(on).sql(onParentheses ? ")" : "").end(MERGE_ON).start(MERGE_WHEN_MATCHED_THEN_UPDATE).start(MERGE_SET);
    // [#7291] Multi MATCHED emulation
    boolean emulate = false;
    boolean requireMatchedConditions = false;
    // [#10054] TODO: Skip all WHEN MATCHED clauses after a WHEN MATCHED clause with no search condition
    if (NO_SUPPORT_CONDITION_AFTER_NO_CONDITION.contains(ctx.dialect())) {
        boolean withoutMatchedConditionFound = false;
        for (MatchedClause m : matched) {
            if (requireMatchedConditions |= withoutMatchedConditionFound)
                break;
            withoutMatchedConditionFound |= m.condition instanceof NoCondition;
        }
    }
    emulateCheck: if ((NO_SUPPORT_MULTI.contains(ctx.dialect()) && matched.size() > 1)) {
        boolean matchUpdate = false;
        boolean matchDelete = false;
        for (MatchedClause m : matched) {
            if (m.delete) {
                if (emulate |= matchDelete)
                    break emulateCheck;
                matchDelete = true;
            } else {
                if (emulate |= matchUpdate)
                    break emulateCheck;
                matchUpdate = true;
            }
        }
    }
    if (emulate) {
        MatchedClause update = null;
        MatchedClause delete = null;
        Condition negate = noCondition();
        for (MatchedClause m : matched) {
            Condition condition = negate.and(m.condition);
            if (m.delete) {
                if (delete == null)
                    delete = new MatchedClause(noCondition(), true);
                delete.condition = delete.condition.or(condition);
            } else {
                if (update == null)
                    update = new MatchedClause(noCondition());
                for (Entry<Field<?>, Field<?>> e : m.updateMap.entrySet()) {
                    Field<?> exp = update.updateMap.get(e.getKey());
                    if (exp instanceof CaseConditionStepImpl)
                        ((CaseConditionStepImpl) exp).when(negate.and(condition), e.getValue());
                    else
                        update.updateMap.put(e.getKey(), when(negate.and(condition), (Field) e.getValue()).else_(e.getKey()));
                }
                update.condition = update.condition.or(condition);
            }
            if (REQUIRE_NEGATION.contains(ctx.dialect()))
                negate = negate.andNot(m.condition instanceof NoCondition ? trueCondition() : m.condition);
        }
        {
            if (delete != null)
                toSQLMatched(ctx, delete, requireMatchedConditions);
            if (update != null)
                toSQLMatched(ctx, update, requireMatchedConditions);
        }
    } else // [#7291] Workaround for https://github.com/h2database/h2database/issues/2552
    if (REQUIRE_NEGATION.contains(ctx.dialect())) {
        Condition negate = noCondition();
        for (MatchedClause m : matched) {
            toSQLMatched(ctx, new MatchedClause(negate.and(m.condition), m.delete, m.updateMap), requireMatchedConditions);
            negate = negate.andNot(m.condition instanceof NoCondition ? trueCondition() : m.condition);
        }
    } else {
        for (MatchedClause m : matched) toSQLMatched(ctx, m, requireMatchedConditions);
    }
    ctx.end(MERGE_SET).end(MERGE_WHEN_MATCHED_THEN_UPDATE).start(MERGE_WHEN_NOT_MATCHED_THEN_INSERT);
    for (NotMatchedClause m : notMatched) toSQLNotMatched(ctx, m);
    ctx.end(MERGE_WHEN_NOT_MATCHED_THEN_INSERT);
}
Also used : QueryPartListView.wrap(org.jooq.impl.QueryPartListView.wrap) THROW(org.jooq.conf.WriteIfReadonly.THROW) Arrays(java.util.Arrays) DSL.condition(org.jooq.impl.DSL.condition) DSL.when(org.jooq.impl.DSL.when) Condition(org.jooq.Condition) DSL.trueCondition(org.jooq.impl.DSL.trueCondition) K_UPDATE(org.jooq.impl.Keywords.K_UPDATE) Record1(org.jooq.Record1) Map(java.util.Map) MERGE_SET_ASSIGNMENT(org.jooq.Clause.MERGE_SET_ASSIGNMENT) MergeMatchedSetMoreStep(org.jooq.MergeMatchedSetMoreStep) SQLDialect(org.jooq.SQLDialect) Select(org.jooq.Select) DSL.exists(org.jooq.impl.DSL.exists) K_NOT(org.jooq.impl.Keywords.K_NOT) MERGE(org.jooq.Clause.MERGE) Set(java.util.Set) DataExtendedKey(org.jooq.impl.Tools.DataExtendedKey) K_MATCHED(org.jooq.impl.Keywords.K_MATCHED) Serializable(java.io.Serializable) QueryPart(org.jooq.QueryPart) Tools.filter(org.jooq.impl.Tools.filter) K_AS(org.jooq.impl.Keywords.K_AS) DSL.noCondition(org.jooq.impl.DSL.noCondition) MergeMatchedThenStep(org.jooq.MergeMatchedThenStep) K_AND(org.jooq.impl.Keywords.K_AND) MergeKeyStep9(org.jooq.MergeKeyStep9) MergeKeyStep8(org.jooq.MergeKeyStep8) MERGE_WHEN_MATCHED_THEN_UPDATE(org.jooq.Clause.MERGE_WHEN_MATCHED_THEN_UPDATE) MergeKeyStep1(org.jooq.MergeKeyStep1) MergeKeyStep3(org.jooq.MergeKeyStep3) MergeKeyStep2(org.jooq.MergeKeyStep2) MergeKeyStep5(org.jooq.MergeKeyStep5) MergeKeyStep4(org.jooq.MergeKeyStep4) MergeKeyStep7(org.jooq.MergeKeyStep7) ArrayList(java.util.ArrayList) MergeKeyStep6(org.jooq.MergeKeyStep6) LinkedHashMap(java.util.LinkedHashMap) MERGE_MERGE_INTO(org.jooq.Clause.MERGE_MERGE_INTO) MergeNotMatchedValuesStepN(org.jooq.MergeNotMatchedValuesStepN) MergeUsingStep(org.jooq.MergeUsingStep) DSL.notExists(org.jooq.impl.DSL.notExists) MERGE_ON(org.jooq.Clause.MERGE_ON) Record(org.jooq.Record) IGNORE(org.jooq.conf.WriteIfReadonly.IGNORE) FIREBIRD(org.jooq.SQLDialect.FIREBIRD) K_ON(org.jooq.impl.Keywords.K_ON) StringUtils(org.jooq.tools.StringUtils) MERGE_SET(org.jooq.Clause.MERGE_SET) K_WHERE(org.jooq.impl.Keywords.K_WHERE) EMPTY_FIELD(org.jooq.impl.Tools.EMPTY_FIELD) MergeNotMatchedValuesStep10(org.jooq.MergeNotMatchedValuesStep10) K_VALUES(org.jooq.impl.Keywords.K_VALUES) UniqueKey(org.jooq.UniqueKey) MergeNotMatchedValuesStep12(org.jooq.MergeNotMatchedValuesStep12) Tools.map(org.jooq.impl.Tools.map) MergeNotMatchedValuesStep11(org.jooq.MergeNotMatchedValuesStep11) MergeKeyStep19(org.jooq.MergeKeyStep19) MergeNotMatchedValuesStep14(org.jooq.MergeNotMatchedValuesStep14) UNotYetImplemented(org.jooq.impl.QOM.UNotYetImplemented) MergeNotMatchedValuesStep13(org.jooq.MergeNotMatchedValuesStep13) MergeNotMatchedValuesStep16(org.jooq.MergeNotMatchedValuesStep16) MergeNotMatchedValuesStep15(org.jooq.MergeNotMatchedValuesStep15) MergeKeyStep15(org.jooq.MergeKeyStep15) MergeKeyStep16(org.jooq.MergeKeyStep16) Table(org.jooq.Table) MergeKeyStep17(org.jooq.MergeKeyStep17) MergeOnConditionStep(org.jooq.MergeOnConditionStep) MergeKeyStep18(org.jooq.MergeKeyStep18) Operator(org.jooq.Operator) MergeKeyStep11(org.jooq.MergeKeyStep11) DATA_WRAP_DERIVED_TABLES_IN_PARENTHESES(org.jooq.impl.Tools.BooleanDataKey.DATA_WRAP_DERIVED_TABLES_IN_PARENTHESES) MergeKeyStep12(org.jooq.MergeKeyStep12) MergeKeyStep13(org.jooq.MergeKeyStep13) SQL(org.jooq.SQL) MergeKeyStep14(org.jooq.MergeKeyStep14) MergeKeyStep10(org.jooq.MergeKeyStep10) Clause(org.jooq.Clause) K_DELETE(org.jooq.impl.Keywords.K_DELETE) DataTypeException(org.jooq.exception.DataTypeException) MergeNotMatchedValuesStep21(org.jooq.MergeNotMatchedValuesStep21) MergeNotMatchedValuesStep20(org.jooq.MergeNotMatchedValuesStep20) MergeOnStep(org.jooq.MergeOnStep) MergeNotMatchedValuesStep22(org.jooq.MergeNotMatchedValuesStep22) MergeNotMatchedSetMoreStep(org.jooq.MergeNotMatchedSetMoreStep) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) Field(org.jooq.Field) MergeKeyStep22(org.jooq.MergeKeyStep22) MergeNotMatchedValuesStep9(org.jooq.MergeNotMatchedValuesStep9) HSQLDB(org.jooq.SQLDialect.HSQLDB) MergeNotMatchedValuesStep8(org.jooq.MergeNotMatchedValuesStep8) List(java.util.List) MergeKeyStep20(org.jooq.MergeKeyStep20) MergeNotMatchedValuesStep7(org.jooq.MergeNotMatchedValuesStep7) K_INSERT(org.jooq.impl.Keywords.K_INSERT) MergeKeyStep21(org.jooq.MergeKeyStep21) MergeNotMatchedValuesStep6(org.jooq.MergeNotMatchedValuesStep6) Context(org.jooq.Context) MergeNotMatchedValuesStep5(org.jooq.MergeNotMatchedValuesStep5) K_SET(org.jooq.impl.Keywords.K_SET) MergeNotMatchedValuesStep4(org.jooq.MergeNotMatchedValuesStep4) K_KEY(org.jooq.impl.Keywords.K_KEY) MergeNotMatchedValuesStep3(org.jooq.MergeNotMatchedValuesStep3) MergeNotMatchedValuesStep2(org.jooq.MergeNotMatchedValuesStep2) Entry(java.util.Map.Entry) MergeNotMatchedValuesStep1(org.jooq.MergeNotMatchedValuesStep1) MergeNotMatchedValuesStep18(org.jooq.MergeNotMatchedValuesStep18) MergeNotMatchedValuesStep17(org.jooq.MergeNotMatchedValuesStep17) MergeNotMatchedValuesStep19(org.jooq.MergeNotMatchedValuesStep19) MERGE_WHEN_NOT_MATCHED_THEN_INSERT(org.jooq.Clause.MERGE_WHEN_NOT_MATCHED_THEN_INSERT) K_UPSERT(org.jooq.impl.Keywords.K_UPSERT) Tools.nullSafe(org.jooq.impl.Tools.nullSafe) K_USING(org.jooq.impl.Keywords.K_USING) HashSet(java.util.HashSet) MergeMatchedDeleteStep(org.jooq.MergeMatchedDeleteStep) DSL.insertInto(org.jooq.impl.DSL.insertInto) TableLike(org.jooq.TableLike) K_WHEN(org.jooq.impl.Keywords.K_WHEN) FALSE(java.lang.Boolean.FALSE) MERGE_VALUES(org.jooq.Clause.MERGE_VALUES) MERGE_USING(org.jooq.Clause.MERGE_USING) Tools.collect(org.jooq.impl.Tools.collect) K_WITH_PRIMARY_KEY(org.jooq.impl.Keywords.K_WITH_PRIMARY_KEY) Configuration(org.jooq.Configuration) H2(org.jooq.SQLDialect.H2) K_MERGE_INTO(org.jooq.impl.Keywords.K_MERGE_INTO) K_THEN(org.jooq.impl.Keywords.K_THEN) Condition(org.jooq.Condition) DSL.trueCondition(org.jooq.impl.DSL.trueCondition) DSL.noCondition(org.jooq.impl.DSL.noCondition) Field(org.jooq.Field)

Aggregations

Serializable (java.io.Serializable)1 FALSE (java.lang.Boolean.FALSE)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections.emptyList (java.util.Collections.emptyList)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Set (java.util.Set)1 Clause (org.jooq.Clause)1 MERGE (org.jooq.Clause.MERGE)1 MERGE_MERGE_INTO (org.jooq.Clause.MERGE_MERGE_INTO)1 MERGE_ON (org.jooq.Clause.MERGE_ON)1 MERGE_SET (org.jooq.Clause.MERGE_SET)1 MERGE_SET_ASSIGNMENT (org.jooq.Clause.MERGE_SET_ASSIGNMENT)1 MERGE_USING (org.jooq.Clause.MERGE_USING)1 MERGE_VALUES (org.jooq.Clause.MERGE_VALUES)1