[#3691] Improve API to create CASE expressions with arbitrary numbers of arguments
This commit is contained in:
parent
b6ad5f6d8b
commit
1557cfc73b
@ -40,6 +40,8 @@
|
||||
*/
|
||||
package org.jooq;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* An intermediary step in creating a case statement of the type <code><pre>
|
||||
* CASE x WHEN 1 THEN 'one'
|
||||
@ -131,4 +133,21 @@ public interface CaseValueStep<V> {
|
||||
*/
|
||||
@Support
|
||||
<T> CaseWhenStep<V, T> when(Field<V> compareValue, Select<? extends Record1<T>> result);
|
||||
|
||||
/**
|
||||
* Create <code>WHEN .. THEN</code> expressions from a {@link Map}.
|
||||
* <p>
|
||||
* This will iterate over the map's entries to create individual
|
||||
* <code>WHEN .. THEN</code> expressions for each map entry.
|
||||
*/
|
||||
<T> CaseWhenStep<V, T> mapValues(Map<V, T> values);
|
||||
|
||||
/**
|
||||
* Create <code>WHEN .. THEN</code> expressions from a {@link Map}.
|
||||
* <p>
|
||||
* This will iterate over the map's entries to create individual
|
||||
* <code>WHEN .. THEN</code> expressions for each map entry.
|
||||
*/
|
||||
<T> CaseWhenStep<V, T> mapFields(Map<? extends Field<V>, ? extends Field<T>> fields);
|
||||
|
||||
}
|
||||
|
||||
@ -40,6 +40,8 @@
|
||||
*/
|
||||
package org.jooq;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* The final step in creating a case statement of the type <code><pre>
|
||||
* CASE x WHEN 1 THEN 'one'
|
||||
@ -103,6 +105,22 @@ public interface CaseWhenStep<V, T> extends Field<T> {
|
||||
@Support
|
||||
CaseWhenStep<V, T> when(Field<V> compareValue, Field<T> result);
|
||||
|
||||
/**
|
||||
* Create <code>WHEN .. THEN</code> expressions from a {@link Map}.
|
||||
* <p>
|
||||
* This will iterate over the map's entries to create individual
|
||||
* <code>WHEN .. THEN</code> expressions for each map entry.
|
||||
*/
|
||||
CaseWhenStep<V, T> mapValues(Map<V, T> values);
|
||||
|
||||
/**
|
||||
* Create <code>WHEN .. THEN</code> expressions from a {@link Map}.
|
||||
* <p>
|
||||
* This will iterate over the map's entries to create individual
|
||||
* <code>WHEN .. THEN</code> expressions for each map entry.
|
||||
*/
|
||||
CaseWhenStep<V, T> mapFields(Map<? extends Field<V>, ? extends Field<T>> fields);
|
||||
|
||||
/**
|
||||
* Add an else clause to the already constructed case statement
|
||||
*
|
||||
|
||||
@ -40,6 +40,10 @@
|
||||
*/
|
||||
package org.jooq.impl;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.jooq.CaseValueStep;
|
||||
import org.jooq.CaseWhenStep;
|
||||
import org.jooq.Field;
|
||||
@ -86,4 +90,19 @@ final class CaseValueStepImpl<V> implements CaseValueStep<V> {
|
||||
public final <T> CaseWhenStep<V, T> when(Field<V> compareValue, Select<? extends Record1<T>> result) {
|
||||
return when(compareValue, DSL.field(result));
|
||||
}
|
||||
|
||||
@Override
|
||||
public final <T> CaseWhenStep<V, T> mapValues(Map<V, T> values) {
|
||||
Map<Field<V>, Field<T>> fields = new LinkedHashMap<Field<V>, Field<T>>();
|
||||
|
||||
for (Entry<V, T> entry : values.entrySet())
|
||||
fields.put(Tools.field(entry.getKey()), Tools.field(entry.getValue()));
|
||||
|
||||
return mapFields(fields);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final <T> CaseWhenStep<V, T> mapFields(Map<? extends Field<V>, ? extends Field<T>> fields) {
|
||||
return new CaseWhenStepImpl<V, T>(value, fields);
|
||||
}
|
||||
}
|
||||
|
||||
@ -41,12 +41,16 @@
|
||||
package org.jooq.impl;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.jooq.CaseWhenStep;
|
||||
import org.jooq.Clause;
|
||||
import org.jooq.Configuration;
|
||||
import org.jooq.Context;
|
||||
import org.jooq.DataType;
|
||||
import org.jooq.Field;
|
||||
import org.jooq.QueryPart;
|
||||
|
||||
@ -63,13 +67,33 @@ final class CaseWhenStepImpl<V, T> extends AbstractFunction<T> implements CaseWh
|
||||
private Field<T> otherwise;
|
||||
|
||||
CaseWhenStepImpl(Field<V> value, Field<V> compareValue, Field<T> result) {
|
||||
super("case", result.getDataType());
|
||||
this(value, result.getDataType());
|
||||
|
||||
when(compareValue, result);
|
||||
}
|
||||
|
||||
CaseWhenStepImpl(Field<V> value, Map<? extends Field<V>, ? extends Field<T>> map) {
|
||||
this(value, dataType(map));
|
||||
|
||||
for (Entry<? extends Field<V>, ? extends Field<T>> entry : map.entrySet())
|
||||
when(entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
||||
private CaseWhenStepImpl(Field<V> value, DataType<T> type) {
|
||||
super("case", type);
|
||||
|
||||
this.value = value;
|
||||
this.compareValues = new ArrayList<Field<V>>();
|
||||
this.results = new ArrayList<Field<T>>();
|
||||
}
|
||||
|
||||
when(compareValue, result);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static final <T> DataType<T> dataType(Map<? extends Field<?>, ? extends Field<T>> map) {
|
||||
if (map.isEmpty())
|
||||
return (DataType<T>) SQLDataType.OTHER;
|
||||
else
|
||||
return map.entrySet().iterator().next().getValue().getDataType();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -107,6 +131,21 @@ final class CaseWhenStepImpl<V, T> extends AbstractFunction<T> implements CaseWh
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final CaseWhenStep<V, T> mapValues(Map<V, T> values) {
|
||||
Map<Field<V>, Field<T>> fields = new LinkedHashMap<Field<V>, Field<T>>();
|
||||
|
||||
for (Entry<V, T> entry : values.entrySet())
|
||||
fields.put(Tools.field(entry.getKey()), Tools.field(entry.getValue()));
|
||||
|
||||
return mapFields(fields);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final CaseWhenStep<V, T> mapFields(Map<? extends Field<V>, ? extends Field<T>> fields) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
final QueryPart getFunction0(Configuration configuration) {
|
||||
switch (configuration.dialect().family()) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user