Search in sources :

Example 1 with DATA_ON_DUPLICATE_KEY_WHERE

use of org.jooq.impl.Tools.DataKey.DATA_ON_DUPLICATE_KEY_WHERE in project jOOQ by jOOQ.

the class FieldMapForUpdate method accept.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public final void accept(Context<?> ctx) {
    if (size() > 0) {
        String separator = "";
        // [#989] Some dialects do not support qualified column references
        // in the UPDATE statement's SET clause
        // [#2055] Other dialects require qualified column references to
        // disambiguated columns in queries like
        // UPDATE t1 JOIN t2 .. SET t1.val = ..., t2.val = ...
        boolean supportsQualify = !NO_SUPPORT_QUALIFY.contains(ctx.dialect()) && ctx.qualify();
        // [#2823] [#10034] Few dialects need bind value casts for UPDATE .. SET
        // Some regressions have been observed e.g. in PostgreSQL with JSON types, so let's be careful.
        CastMode previous = ctx.castMode();
        if (!CASTS_NEEDED.contains(ctx.dialect()))
            ctx.castMode(CastMode.NEVER);
        for (Entry<Field<?>, Field<?>> entry : removeReadonly(ctx, flattenEntrySet(entrySet(), true))) {
            if (!"".equals(separator))
                ctx.sql(separator).formatSeparator();
            ctx.start(assignmentClause).qualify(supportsQualify, c -> c.visit(entry.getKey())).sql(" = ");
            // [#8479] Emulate WHERE clause using CASE
            Condition condition = (Condition) ctx.data(DATA_ON_DUPLICATE_KEY_WHERE);
            if (condition != null)
                ctx.visit(when(condition, (Field) entry.getValue()).else_(entry.getKey()));
            else
                ctx.visit(entry.getValue());
            ctx.end(assignmentClause);
            separator = ",";
        }
        if (!CASTS_NEEDED.contains(ctx.dialect()))
            ctx.castMode(previous);
    } else
        ctx.sql("[ no fields are updated ]");
}
Also used : THROW(org.jooq.conf.WriteIfReadonly.THROW) Row(org.jooq.Row) UNotYetImplemented(org.jooq.impl.QOM.UNotYetImplemented) IGNORE(org.jooq.conf.WriteIfReadonly.IGNORE) Tools.collect(org.jooq.impl.Tools.collect) Set(java.util.Set) Table(org.jooq.Table) Tools.row0(org.jooq.impl.Tools.row0) Field(org.jooq.Field) POSTGRES(org.jooq.SQLDialect.POSTGRES) DSL.when(org.jooq.impl.DSL.when) Condition(org.jooq.Condition) Tools.filter(org.jooq.impl.Tools.filter) YUGABYTEDB(org.jooq.SQLDialect.YUGABYTEDB) DATA_ON_DUPLICATE_KEY_WHERE(org.jooq.impl.Tools.DataKey.DATA_ON_DUPLICATE_KEY_WHERE) Clause(org.jooq.Clause) Context(org.jooq.Context) CastMode(org.jooq.RenderContext.CastMode) SQLITE(org.jooq.SQLDialect.SQLITE) Map(java.util.Map) Tools.flattenEntrySet(org.jooq.impl.Tools.flattenEntrySet) DataTypeException(org.jooq.exception.DataTypeException) SQLDialect(org.jooq.SQLDialect) Tools.anyMatch(org.jooq.impl.Tools.anyMatch) Condition(org.jooq.Condition) Field(org.jooq.Field) CastMode(org.jooq.RenderContext.CastMode)

Aggregations

Map (java.util.Map)1 Set (java.util.Set)1 Clause (org.jooq.Clause)1 Condition (org.jooq.Condition)1 Context (org.jooq.Context)1 Field (org.jooq.Field)1 CastMode (org.jooq.RenderContext.CastMode)1 Row (org.jooq.Row)1 SQLDialect (org.jooq.SQLDialect)1 POSTGRES (org.jooq.SQLDialect.POSTGRES)1 SQLITE (org.jooq.SQLDialect.SQLITE)1 YUGABYTEDB (org.jooq.SQLDialect.YUGABYTEDB)1 Table (org.jooq.Table)1 IGNORE (org.jooq.conf.WriteIfReadonly.IGNORE)1 THROW (org.jooq.conf.WriteIfReadonly.THROW)1 DataTypeException (org.jooq.exception.DataTypeException)1 DSL.when (org.jooq.impl.DSL.when)1 UNotYetImplemented (org.jooq.impl.QOM.UNotYetImplemented)1 DATA_ON_DUPLICATE_KEY_WHERE (org.jooq.impl.Tools.DataKey.DATA_ON_DUPLICATE_KEY_WHERE)1 Tools.anyMatch (org.jooq.impl.Tools.anyMatch)1