[#6388] Relax type constraints on various set(Map<Field<?>, ?>) to set(Map<?, ?>)

This commit is contained in:
lukaseder 2017-07-06 17:32:16 +02:00
parent 50db69abbd
commit a2dbec2a2f
17 changed files with 87 additions and 47 deletions

View File

@ -90,12 +90,15 @@ public interface InsertOnDuplicateSetStep<R extends Record> {
* Set multiple values for <code>UPDATE</code> in the <code>INSERT</code>
* statement's <code>ON DUPLICATE KEY UPDATE</code> clause.
* <p>
* Keys can either be of type {@link String}, {@link Name}, or
* {@link Field}.
* <p>
* Values can either be of type <code>&lt;T&gt;</code> or
* <code>Field&lt;T&gt;</code>. jOOQ will attempt to convert values to their
* corresponding field's type.
*/
@Support({ CUBRID, HSQLDB, MARIADB, MYSQL, POSTGRES_9_5 })
InsertOnDuplicateSetMoreStep<R> set(Map<? extends Field<?>, ?> map);
InsertOnDuplicateSetMoreStep<R> set(Map<?, ?> map);
/**
* Set multiple values for <code>UPDATE</code> in the <code>INSERT</code>

View File

@ -214,7 +214,7 @@ public interface InsertQuery<R extends Record> extends StoreQuery<R>, Insert<R>
* @see InsertOnDuplicateStep#onDuplicateKeyUpdate()
*/
@Support({ CUBRID, HSQLDB, MARIADB, MYSQL, POSTGRES_9_5 })
void addValuesForUpdate(Map<? extends Field<?>, ?> map);
void addValuesForUpdate(Map<?, ?> map);
/**
* Adds new conditions to the query, connecting them to existing conditions

View File

@ -79,12 +79,15 @@ public interface InsertSetMoreStep<R extends Record> extends InsertOnDuplicateSt
/**
* Set values in the <code>INSERT</code> statement.
* <p>
* Keys can either be of type {@link String}, {@link Name}, or
* {@link Field}.
* <p>
* Values can either be of type <code>&lt;T&gt;</code> or
* <code>Field&lt;T&gt;</code>. jOOQ will attempt to convert values to their
* corresponding field's type.
*/
@Support
InsertSetMoreStep<R> set(Map<? extends Field<?>, ?> map);
InsertSetMoreStep<R> set(Map<?, ?> map);
/**
* Set values in the <code>INSERT</code> statement.

View File

@ -268,12 +268,15 @@ public interface InsertSetStep<R extends Record> {
/**
* Set values in the <code>INSERT</code> statement.
* <p>
* Keys can either be of type {@link String}, {@link Name}, or
* {@link Field}.
* <p>
* Values can either be of type <code>&lt;T&gt;</code> or
* <code>Field&lt;T&gt;</code>. jOOQ will attempt to convert values to their
* corresponding field's type.
*/
@Support
InsertSetMoreStep<R> set(Map<? extends Field<?>, ?> map);
InsertSetMoreStep<R> set(Map<?, ?> map);
/**
* Set values in the <code>INSERT</code> statement.

View File

@ -90,12 +90,15 @@ public interface MergeMatchedSetStep<R extends Record> {
* Set multiple values for <code>UPDATE</code> in the <code>MERGE</code>
* statement's <code>WHEN MATCHED</code> clause.
* <p>
* Keys can either be of type {@link String}, {@link Name}, or
* {@link Field}.
* <p>
* Values can either be of type <code>&lt;T&gt;</code> or
* <code>Field&lt;T&gt;</code>. jOOQ will attempt to convert values to their
* corresponding field's type.
*/
@Support({ CUBRID, FIREBIRD_3_0, HSQLDB })
MergeMatchedSetMoreStep<R> set(Map<? extends Field<?>, ?> map);
MergeMatchedSetMoreStep<R> set(Map<?, ?> map);
/**
* Set multiple values for <code>UPDATE</code> in the <code>MERGE</code>

View File

@ -90,12 +90,15 @@ public interface MergeNotMatchedSetStep<R extends Record> {
* Set multiple values for <code>INSERT</code> in the <code>MERGE</code>
* statement's <code>WHEN NOT MATCHED</code> clause.
* <p>
* Keys can either be of type {@link String}, {@link Name}, or
* {@link Field}.
* <p>
* Values can either be of type <code>&lt;T&gt;</code> or
* <code>Field&lt;T&gt;</code>. jOOQ will attempt to convert values to their
* corresponding field's type.
*/
@Support({ CUBRID, FIREBIRD_3_0, HSQLDB })
MergeNotMatchedSetMoreStep<R> set(Map<? extends Field<?>, ?> map);
MergeNotMatchedSetMoreStep<R> set(Map<?, ?> map);
/**
* Set multiple values for <code>INSERT</code> in the <code>MERGE</code>

View File

@ -76,12 +76,15 @@ public interface StoreQuery<R extends Record> extends Query {
/**
* Add multiple values to the store statement.
* <p>
* Keys can either be of type {@link String}, {@link Name}, or
* {@link Field}.
* <p>
* Values can either be of type <code>&lt;T&gt;</code> or
* <code>Field&lt;T&gt;</code>. jOOQ will attempt to convert values to their
* corresponding field's type.
*/
@Support
void addValues(Map<? extends Field<?>, ?> map);
void addValues(Map<?, ?> map);
/**
* Configure the <code>INSERT</code> or <code>UPDATE</code> statement to return all fields in

View File

@ -74,12 +74,15 @@ public interface UpdateSetStep<R extends Record> {
/**
* Set a value for a field in the <code>UPDATE</code> statement.
* <p>
* Keys can either be of type {@link String}, {@link Name}, or
* {@link Field}.
* <p>
* Values can either be of type <code>&lt;T&gt;</code> or
* <code>Field&lt;T&gt;</code>. jOOQ will attempt to convert values to their
* corresponding field's type.
*/
@Support
UpdateSetMoreStep<R> set(Map<? extends Field<?>, ?> map);
UpdateSetMoreStep<R> set(Map<?, ?> map);
/**
* Set a value for a field in the <code>UPDATE</code> statement.

View File

@ -164,7 +164,7 @@ class DefaultRenderContext extends AbstractContext<RenderContext> implements Ren
@Override
public final String render() {
String prepend = (String) data(DataKey.DATA_PREPEND_SQL);
String prepend = null;
String result = sql.toString();
return prepend == null ? result : prepend + result;
}

View File

@ -45,6 +45,7 @@ import java.util.Map;
import org.jooq.Clause;
import org.jooq.Context;
import org.jooq.Field;
import org.jooq.Table;
/**
* @author Lukas Eder
@ -56,9 +57,11 @@ final class FieldMapForUpdate extends AbstractQueryPartMap<Field<?>, Field<?>> {
*/
private static final long serialVersionUID = -6139709404698673799L;
private final Table<?> table;
private final Clause assignmentClause;
FieldMapForUpdate(Clause assignmentClause) {
FieldMapForUpdate(Table<?> table, Clause assignmentClause) {
this.table = table;
this.assignmentClause = assignmentClause;
}
@ -104,12 +107,12 @@ final class FieldMapForUpdate extends AbstractQueryPartMap<Field<?>, Field<?>> {
return null;
}
final void set(Map<? extends Field<?>, ?> map) {
for (Entry<? extends Field<?>, ?> entry : map.entrySet()) {
Field<?> field = entry.getKey();
final void set(Map<?, ?> map) {
for (Entry<?, ?> entry : map.entrySet()) {
Field<?> field = Tools.tableField(table, entry.getKey());
Object value = entry.getValue();
put(entry.getKey(), Tools.field(value, field));
put(field, Tools.field(value, field));
}
}
}

View File

@ -60,6 +60,7 @@ import org.jooq.Context;
import org.jooq.Field;
import org.jooq.Record;
import org.jooq.Select;
import org.jooq.Table;
import org.jooq.impl.AbstractStoreQuery.UnknownField;
/**
@ -72,13 +73,15 @@ final class FieldMapsForInsert extends AbstractQueryPart {
*/
private static final long serialVersionUID = -6227074228534414225L;
final Table<?> table;
final List<Field<?>> empty;
final Map<Field<?>, List<Field<?>>> values;
int rows;
FieldMapsForInsert() {
values = new LinkedHashMap<Field<?>, List<Field<?>>>();
empty = new ArrayList<Field<?>>();
FieldMapsForInsert(Table<?> table) {
this.table = table;
this.values = new LinkedHashMap<Field<?>, List<Field<?>>>();
this.empty = new ArrayList<Field<?>>();
}
// -------------------------------------------------------------------------
@ -219,16 +222,17 @@ final class FieldMapsForInsert extends AbstractQueryPart {
// The FieldMapsForInsert API
// -------------------------------------------------------------------------
final void addFields(Collection<? extends Field<?>> fields) {
final void addFields(Collection<?> fields) {
if (rows == 0)
newRecord();
for (Field<?> field : fields) {
Field<?> e = DSL.val(null, field);
for (Object field : fields) {
Field<?> f = Tools.tableField(table, field);
Field<?> e = DSL.val(null, f);
empty.add(e);
if (!values.containsKey(field)) {
values.put(field, rows > 0
if (!values.containsKey(f)) {
values.put(f, rows > 0
? new ArrayList<Field<?>>(Collections.nCopies(rows, e))
: new ArrayList<Field<?>>()
);
@ -253,11 +257,13 @@ final class FieldMapsForInsert extends AbstractQueryPart {
return (Field<T>) values.get(field).set(rows - 1, value);
}
final void set(Map<? extends Field<?>, ?> map) {
final void set(Map<?, ?> map) {
addFields(map.keySet());
for (Entry<? extends Field<?>, ?> entry : map.entrySet())
values.get(entry.getKey())
.set(rows - 1, Tools.field(entry.getValue(), entry.getKey()));
for (Entry<?, ?> entry : map.entrySet()) {
Field<?> field = Tools.tableField(table, entry.getKey());
values.get(field)
.set(rows - 1, Tools.field(entry.getValue(), field));
}
}
final void newRecord() {

View File

@ -59,14 +59,6 @@ import org.jooq.InsertResultStep;
import org.jooq.InsertSetMoreStep;
import org.jooq.InsertSetStep;
import org.jooq.InsertValuesStep1;
import org.jooq.InsertValuesStep2;
import org.jooq.InsertValuesStep3;
import org.jooq.InsertValuesStep4;
import org.jooq.InsertValuesStep5;
import org.jooq.InsertValuesStep6;
import org.jooq.InsertValuesStep7;
import org.jooq.InsertValuesStep8;
import org.jooq.InsertValuesStep9;
import org.jooq.InsertValuesStep10;
import org.jooq.InsertValuesStep11;
import org.jooq.InsertValuesStep12;
@ -77,9 +69,17 @@ import org.jooq.InsertValuesStep16;
import org.jooq.InsertValuesStep17;
import org.jooq.InsertValuesStep18;
import org.jooq.InsertValuesStep19;
import org.jooq.InsertValuesStep2;
import org.jooq.InsertValuesStep20;
import org.jooq.InsertValuesStep21;
import org.jooq.InsertValuesStep22;
import org.jooq.InsertValuesStep3;
import org.jooq.InsertValuesStep4;
import org.jooq.InsertValuesStep5;
import org.jooq.InsertValuesStep6;
import org.jooq.InsertValuesStep7;
import org.jooq.InsertValuesStep8;
import org.jooq.InsertValuesStep9;
import org.jooq.InsertValuesStepN;
import org.jooq.Operator;
import org.jooq.QueryPart;
@ -660,7 +660,7 @@ class InsertImpl<R extends Record, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,
}
@Override
public final InsertImpl set(Map<? extends Field<?>, ?> map) {
public final InsertImpl set(Map<?, ?> map) {
if (onDuplicateKeyUpdate) {
getDelegate().addValuesForUpdate(map);
}

View File

@ -112,8 +112,8 @@ final class InsertQueryImpl<R extends Record> extends AbstractStoreQuery<R> impl
InsertQueryImpl(Configuration configuration, WithImpl with, Table<R> into) {
super(configuration, with, into);
this.updateMap = new FieldMapForUpdate(INSERT_ON_DUPLICATE_KEY_UPDATE_ASSIGNMENT);
this.insertMaps = new FieldMapsForInsert();
this.updateMap = new FieldMapForUpdate(into, INSERT_ON_DUPLICATE_KEY_UPDATE_ASSIGNMENT);
this.insertMaps = new FieldMapsForInsert(into);
this.condition = new ConditionProviderImpl();
}
@ -166,7 +166,7 @@ final class InsertQueryImpl<R extends Record> extends AbstractStoreQuery<R> impl
}
@Override
public final void addValuesForUpdate(Map<? extends Field<?>, ?> map) {
public final void addValuesForUpdate(Map<?, ?> map) {
updateMap.set(map);
}
@ -202,7 +202,7 @@ final class InsertQueryImpl<R extends Record> extends AbstractStoreQuery<R> impl
}
@Override
public final void addValues(Map<? extends Field<?>, ?> map) {
public final void addValues(Map<?, ?> map) {
insertMaps.set(map);
}
@ -339,7 +339,7 @@ final class InsertQueryImpl<R extends Record> extends AbstractStoreQuery<R> impl
// CUBRID can emulate this using ON DUPLICATE KEY UPDATE
case CUBRID: {
FieldMapForUpdate update = new FieldMapForUpdate(INSERT_ON_DUPLICATE_KEY_UPDATE_ASSIGNMENT);
FieldMapForUpdate update = new FieldMapForUpdate(table, INSERT_ON_DUPLICATE_KEY_UPDATE_ASSIGNMENT);
Field<?> field = table.field(0);
update.put(field, field);

View File

@ -920,7 +920,7 @@ implements
@Override
public final MergeImpl whenMatchedThenUpdate() {
matchedClause = true;
matchedUpdate = new FieldMapForUpdate(MERGE_SET_ASSIGNMENT);
matchedUpdate = new FieldMapForUpdate(table, MERGE_SET_ASSIGNMENT);
notMatchedClause = false;
return this;
@ -955,7 +955,7 @@ implements
}
@Override
public final MergeImpl set(Map<? extends Field<?>, ?> map) {
public final MergeImpl set(Map<?, ?> map) {
if (matchedClause) {
matchedUpdate.set(map);
}
@ -1124,7 +1124,7 @@ implements
@Override
public final MergeImpl whenNotMatchedThenInsert(Collection<? extends Field<?>> fields) {
notMatchedClause = true;
notMatchedInsert = new FieldMapsForInsert();
notMatchedInsert = new FieldMapsForInsert(table);
notMatchedInsert.addFields(fields);
matchedClause = false;

View File

@ -3891,4 +3891,15 @@ final class Tools {
updateCounts = 0;
data.put(key, updateCounts + 1);
}
static Field<?> tableField(Table<?> table, Object field) {
if (field instanceof Field<?>)
return (Field<?>) field;
else if (field instanceof Name)
return table.field((Name) field);
else if (field instanceof String)
return table.field((String) field);
else
throw new IllegalArgumentException("Field type not supported: " + field);
}
}

View File

@ -155,7 +155,7 @@ final class UpdateImpl<R extends Record>
}
@Override
public final UpdateImpl<R> set(Map<? extends Field<?>, ?> map) {
public final UpdateImpl<R> set(Map<?, ?> map) {
getDelegate().addValues(map);
return this;
}

View File

@ -62,7 +62,6 @@ import org.jooq.Clause;
import org.jooq.Condition;
import org.jooq.Configuration;
import org.jooq.Context;
import org.jooq.Field;
import org.jooq.Operator;
import org.jooq.Record;
import org.jooq.Record1;
@ -134,7 +133,7 @@ final class UpdateQueryImpl<R extends Record> extends AbstractStoreQuery<R> impl
UpdateQueryImpl(Configuration configuration, WithImpl with, Table<R> table) {
super(configuration, with, table);
this.updateMap = new FieldMapForUpdate(UPDATE_SET_ASSIGNMENT);
this.updateMap = new FieldMapForUpdate(table, UPDATE_SET_ASSIGNMENT);
this.from = new TableList();
this.condition = new ConditionProviderImpl();
}
@ -435,7 +434,7 @@ final class UpdateQueryImpl<R extends Record> extends AbstractStoreQuery<R> impl
}
@Override
public final void addValues(Map<? extends Field<?>, ?> map) {
public final void addValues(Map<?, ?> map) {
updateMap.set(map);
}