[#3986] Add DSL.choose() as a synonym for DSL.decode(), and DSL.when(Field), DSL.when(Condition) as shortcuts

This commit is contained in:
lukaseder 2015-01-26 15:30:10 +01:00
parent 7b17735cc0
commit 0549e202a6
14 changed files with 139 additions and 29 deletions

View File

@ -40,6 +40,7 @@
*/
package org.jooq.util.firebird;
import static org.jooq.impl.DSL.choose;
import static org.jooq.impl.DSL.decode;
import static org.jooq.impl.DSL.inline;
import static org.jooq.impl.DSL.select;
@ -343,7 +344,7 @@ public class FirebirdDatabase extends AbstractDatabase {
}
static Field<Short> CHARACTER_LENGTH(Rdb$fields f) {
return decode().value(f.RDB$FIELD_TYPE)
return choose(f.RDB$FIELD_TYPE)
.when((short) 261, (short) 0)
.otherwise(f.RDB$CHARACTER_LENGTH);
}

View File

@ -99,7 +99,7 @@ public interface Case {
* WHEN x &gt;= 2 THEN 'two'
* ELSE 'three'
* END
* </pre></code> Instances of Case are created through the
* </pre></code>
*
* @param <T> The generic field type parameter
* @param condition A condition to check in the case statement
@ -115,7 +115,7 @@ public interface Case {
* WHEN x &gt;= 2 THEN 'two'
* ELSE 'three'
* END
* </pre></code> Instances of Case are created through the
* </pre></code>
*
* @param <T> The generic field type parameter
* @param condition A condition to check in the case statement
@ -131,7 +131,7 @@ public interface Case {
* WHEN x &gt;= 2 THEN 'two'
* ELSE 'three'
* END
* </pre></code> Instances of Case are created through the
* </pre></code>
*
* @param <T> The generic field type parameter
* @param condition A condition to check in the case statement

View File

@ -312,7 +312,7 @@ abstract class AbstractField<T> extends AbstractQueryPart implements Field<T> {
@Override
public final <Z> SortField<Z> sort(Map<T, Z> sortMap) {
CaseValueStep<T> decode = DSL.decode().value(this);
CaseValueStep<T> decode = DSL.choose(this);
CaseWhenStep<T, Z> result = null;
for (Entry<T, Z> entry : sortMap.entrySet()) {

View File

@ -159,10 +159,10 @@ class Cast<T> extends AbstractFunction<T> {
private final Field<Boolean> asDecodeNumberToBoolean() {
// [#859] 0 => false, null => null, all else is true
return DSL.decode().value((Field<Integer>) field)
.when(inline(0), inline(false))
.when(inline((Integer) null), inline((Boolean) null))
.otherwise(inline(true));
return DSL.choose((Field<Integer>) field)
.when(inline(0), inline(false))
.when(inline((Integer) null), inline((Boolean) null))
.otherwise(inline(true));
}
@SuppressWarnings("unchecked")
@ -170,11 +170,11 @@ class Cast<T> extends AbstractFunction<T> {
Field<String> s = (Field<String>) field;
// [#859] '0', 'f', 'false' => false, null => null, all else is true
return DSL.decode().when(s.equal(inline("0")), inline(false))
.when(DSL.lower(s).equal(inline("false")), inline(false))
.when(DSL.lower(s).equal(inline("f")), inline(false))
.when(s.isNull(), inline((Boolean) null))
.otherwise(inline(true));
return DSL.when(s.equal(inline("0")), inline(false))
.when(DSL.lower(s).equal(inline("false")), inline(false))
.when(DSL.lower(s).equal(inline("f")), inline(false))
.when(s.isNull(), inline((Boolean) null))
.otherwise(inline(true));
}
@SuppressWarnings("unchecked")

View File

@ -82,9 +82,9 @@ class ConditionAsField extends AbstractFunction<Boolean> {
case FIREBIRD:
// [#3206] Correct implementation of three-valued logic is important here
return DSL.decode().when(condition, inline(true))
.when(not(condition), inline(false))
.otherwise(inline((Boolean) null));
return DSL.when(condition, inline(true))
.when(not(condition), inline(false))
.otherwise(inline((Boolean) null));
// These databases can inline predicates in column expression contexts
case DERBY:

View File

@ -93,6 +93,8 @@ import org.jooq.AlterTableStep;
import org.jooq.ArrayAggOrderByStep;
// ...
import org.jooq.Case;
import org.jooq.CaseConditionStep;
import org.jooq.CaseValueStep;
import org.jooq.CommonTableExpression;
import org.jooq.Condition;
import org.jooq.Configuration;
@ -6949,6 +6951,113 @@ public class DSL {
return select.<T>asField();
}
/**
* Initialise a {@link Case} statement.
* <p>
* Choose is used as a method name to avoid name clashes with Java's
* reserved literal "case"
*
* @see Case
*/
@Support
public static Case choose() {
return decode();
}
/**
* Initialise a {@link Case} statement.
* <p>
* This API can be used to create expressions of the type <code><pre>
* CASE value WHEN 1 THEN 'one'
* WHEN 2 THEN 'two'
* ELSE 'three'
* END
* </pre></code>
* <p>
* Choose is used as a method name to avoid name clashes with Java's
* reserved literal "case".
*
* @see Case
*/
@Support
public static <V> CaseValueStep<V> choose(V value) {
return decode().value(value);
}
/**
* Initialise a {@link Case} statement.
* <p>
* This API can be used to create expressions of the type <code><pre>
* CASE value WHEN 1 THEN 'one'
* WHEN 2 THEN 'two'
* ELSE 'three'
* END
* </pre></code>
* <p>
* Choose is used as a method name to avoid name clashes with Java's
* reserved literal "case".
*
* @see Case
*/
@Support
public static <V> CaseValueStep<V> choose(Field<V> value) {
return decode().value(value);
}
/**
* Initialise a {@link Case} statement.
* <p>
* This API can be used to create expressions of the type <code><pre>
* CASE WHEN x &lt; 1 THEN 'one'
* WHEN x &gt;= 2 THEN 'two'
* ELSE 'three'
* END
* </pre></code>
* <p>
* Choose is used as a method name to avoid name clashes with Java's
* reserved literal "case".
*/
@Support
public static <T> CaseConditionStep<T> when(Condition condition, T result) {
return decode().when(condition, result);
}
/**
* Initialise a {@link Case} statement.
* <p>
* This API can be used to create expressions of the type <code><pre>
* CASE WHEN x &lt; 1 THEN 'one'
* WHEN x &gt;= 2 THEN 'two'
* ELSE 'three'
* END
* </pre></code>
* <p>
* Choose is used as a method name to avoid name clashes with Java's
* reserved literal "case".
*/
@Support
public static <T> CaseConditionStep<T> when(Condition condition, Field<T> result) {
return decode().when(condition, result);
}
/**
* Initialise a {@link Case} statement.
* <p>
* This API can be used to create expressions of the type <code><pre>
* CASE WHEN x &lt; 1 THEN 'one'
* WHEN x &gt;= 2 THEN 'two'
* ELSE 'three'
* END
* </pre></code>
* <p>
* Choose is used as a method name to avoid name clashes with Java's
* reserved literal "case".
*/
@Support
public static <T> CaseConditionStep<T> when(Condition condition, Select<? extends Record1<T>> result) {
return decode().when(condition, result);
}
/**
* Initialise a {@link Case} statement.
* <p>

View File

@ -78,12 +78,12 @@ class Every extends Function<Boolean> {
final Field<Integer> sum = DSL.field("{0}", Integer.class, new CustomQueryPart() {
@Override
public void accept(Context<?> c) {
c.visit(DSL.sum(DSL.decode().when(condition, zero()).otherwise(one())));
c.visit(DSL.sum(DSL.when(condition, zero()).otherwise(one())));
toSQLOverClause(c);
}
});
ctx.visit(DSL.decode().when(sum.eq(zero()), inline(true)).otherwise(inline(false)));
ctx.visit(DSL.when(sum.eq(zero()), inline(true)).otherwise(inline(false)));
break;
}
}

View File

@ -452,7 +452,7 @@ class Function<T> extends AbstractField<T> implements
QueryPartList<Field<?>> expressions = new QueryPartList<Field<?>>();
for (QueryPart argument : arguments) {
expressions.add(DSL.decode().when(filter, argument == ASTERISK ? one() : argument));
expressions.add(DSL.when(filter, argument == ASTERISK ? one() : argument));
}
ctx.visit(expressions);

View File

@ -89,12 +89,12 @@ class Greatest<T> extends AbstractFunction<T> {
Field<?>[] remaining = new Field[getArguments().length - 2];
System.arraycopy(getArguments(), 2, remaining, 0, remaining.length);
return DSL.decode()
return DSL
.when(first.greaterThan(other), DSL.greatest(first, remaining))
.otherwise(DSL.greatest(other, remaining));
}
else {
return DSL.decode()
return DSL
.when(first.greaterThan(other), first)
.otherwise(other);
}

View File

@ -88,12 +88,12 @@ class Least<T> extends AbstractFunction<T> {
Field<?>[] remaining = new Field<?>[getArguments().length - 2];
System.arraycopy(getArguments(), 2, remaining, 0, remaining.length);
return DSL.decode()
return DSL
.when(first.lessThan(other), DSL.least(first, remaining))
.otherwise(DSL.least(other, remaining));
}
else {
return DSL.decode()
return DSL
.when(first.lessThan(other), first)
.otherwise(other);
}

View File

@ -91,7 +91,7 @@ class Nvl<T> extends AbstractFunction<T> {
return field("{ifnull}({0}, {1})", getDataType(), arg1, arg2);
default:
return DSL.decode().when(arg1.isNotNull(), arg1).otherwise(arg2);
return DSL.when(arg1.isNotNull(), arg1).otherwise(arg2);
}
}
}

View File

@ -80,7 +80,7 @@ class Nvl2<T> extends AbstractFunction<T> {
return field("{nvl2}({0}, {1}, {2})", getDataType(), arg1, arg2, arg3);
default:
return DSL.decode().when(arg1.isNotNull(), arg2).otherwise(arg3);
return DSL.when(arg1.isNotNull(), arg2).otherwise(arg3);
}
}
}

View File

@ -80,7 +80,7 @@ class Round<T extends Number> extends AbstractFunction<T> {
// evaluate "round" if unavailable
case DERBY: {
if (decimals == 0) {
return DSL.decode()
return DSL
.when(argument.sub(DSL.floor(argument))
.lessThan((T) Double.valueOf(0.5)), DSL.floor(argument))
.otherwise(DSL.ceil(argument));
@ -89,7 +89,7 @@ class Round<T extends Number> extends AbstractFunction<T> {
Field<BigDecimal> factor = DSL.val(BigDecimal.ONE.movePointRight(decimals));
Field<T> mul = argument.mul(factor);
return DSL.decode()
return DSL
.when(mul.sub(DSL.floor(mul))
.lessThan((T) Double.valueOf(0.5)), DSL.floor(mul).div(factor))
.otherwise(DSL.ceil(mul).div(factor));

View File

@ -76,7 +76,7 @@ class Sign extends AbstractFunction<Integer> {
xx [/pro] */
case SQLITE:
return DSL.decode()
return DSL
.when(((Field<Integer>) argument).greaterThan(zero()), one())
.when(((Field<Integer>) argument).lessThan(zero()), one().neg())
.otherwise(zero());