[#3627] Add simplified API for CASE WHEN [ condition ] THEN [ select ] expressions

This commit is contained in:
Lukas Eder 2014-09-08 15:28:52 +02:00
parent ba87ad6e60
commit 983ca51cfb
6 changed files with 97 additions and 4 deletions

View File

@ -124,4 +124,20 @@ public interface Case {
*/
@Support
<T> CaseConditionStep<T> when(Condition condition, Field<T> result);
/**
* This construct 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> Instances of Case are created through the
*
* @param <T> The generic field type parameter
* @param condition A condition to check in the case statement
* @param result The result if the condition holds true
* @return An intermediary step for case statement construction
*/
@Support
<T> CaseConditionStep<T> when(Condition condition, Select<? extends Record1<T>> result);
}

View File

@ -76,6 +76,17 @@ public interface CaseConditionStep<T> extends Field<T> {
@Support
CaseConditionStep<T> when(Condition condition, Field<T> result);
/**
* Compare a condition to the already constructed case statement, return
* result if the condition holds true
*
* @param condition The condition to add to the case statement
* @param result The result value if the condition holds true
* @return An intermediary step for case statement construction
*/
@Support
CaseConditionStep<T> when(Condition condition, Select<? extends Record1<T>> result);
/**
* Add an else clause to the already constructed case statement
*
@ -93,4 +104,13 @@ public interface CaseConditionStep<T> extends Field<T> {
*/
@Support
Field<T> otherwise(Field<T> result);
/**
* Add an else clause to the already constructed case statement
*
* @param result The result value if no other value matches the case
* @return The resulting field from case statement construction
*/
@Support
Field<T> otherwise(Select<? extends Record1<T>> result);
}

View File

@ -80,6 +80,19 @@ public interface CaseValueStep<V> {
@Support
<T> CaseWhenStep<V, T> when(V compareValue, Field<T> result);
/**
* Compare a value to the already constructed case statement, return result
* if values are equal.
*
* @param <T> The generic result field type parameter
* @param compareValue The value to compare with the already constructed
* case statement
* @param result The result value if values are equal
* @return An intermediary step for case statement construction
*/
@Support
<T> CaseWhenStep<V, T> when(V compareValue, Select<? extends Record1<T>> result);
/**
* Compare a value to the already constructed case statement, return result
* if values are equal.
@ -105,4 +118,17 @@ public interface CaseValueStep<V> {
*/
@Support
<T> CaseWhenStep<V, T> when(Field<V> compareValue, Field<T> result);
/**
* Compare a value to the already constructed case statement, return result
* if values are equal.
*
* @param <T> The generic result field type parameter
* @param compareValue The value to compare with the already constructed
* case statement
* @param result The result value if values are equal
* @return An intermediary step for case statement construction
*/
@Support
<T> CaseWhenStep<V, T> when(Field<V> compareValue, Select<? extends Record1<T>> result);
}

View File

@ -50,6 +50,8 @@ import org.jooq.Configuration;
import org.jooq.Context;
import org.jooq.Field;
import org.jooq.QueryPart;
import org.jooq.Record1;
import org.jooq.Select;
class CaseConditionStepImpl<T> extends AbstractFunction<T> implements CaseConditionStep<T> {
@ -84,6 +86,11 @@ class CaseConditionStepImpl<T> extends AbstractFunction<T> implements CaseCondit
return this;
}
@Override
public final CaseConditionStep<T> when(Condition condition, Select<? extends Record1<T>> result) {
return when(condition, DSL.field(result));
}
@Override
public final Field<T> otherwise(T result) {
return otherwise(Utils.field(result));
@ -96,6 +103,11 @@ class CaseConditionStepImpl<T> extends AbstractFunction<T> implements CaseCondit
return this;
}
@Override
public final Field<T> otherwise(Select<? extends Record1<T>> result) {
return otherwise(DSL.field(result));
}
@Override
final QueryPart getFunction0(Configuration configuration) {
switch (configuration.dialect().family()) {

View File

@ -45,6 +45,8 @@ import org.jooq.CaseConditionStep;
import org.jooq.CaseValueStep;
import org.jooq.Condition;
import org.jooq.Field;
import org.jooq.Record1;
import org.jooq.Select;
/**
* @author Lukas Eder
@ -73,4 +75,9 @@ class CaseImpl implements Case {
public final <T> CaseConditionStep<T> when(Condition condition, Field<T> result) {
return new CaseConditionStepImpl<T>(condition, result);
}
@Override
public final <T> CaseConditionStep<T> when(Condition condition, Select<? extends Record1<T>> result) {
return when(condition, DSL.field(result));
}
}

View File

@ -43,6 +43,8 @@ package org.jooq.impl;
import org.jooq.CaseValueStep;
import org.jooq.CaseWhenStep;
import org.jooq.Field;
import org.jooq.Record1;
import org.jooq.Select;
/**
* @author Lukas Eder
@ -56,22 +58,32 @@ class CaseValueStepImpl<V> implements CaseValueStep<V> {
}
@Override
public <T> CaseWhenStep<V, T> when(V compareValue, T result) {
public final <T> CaseWhenStep<V, T> when(V compareValue, T result) {
return when(Utils.field(compareValue), Utils.field(result));
}
@Override
public <T> CaseWhenStep<V, T> when(V compareValue, Field<T> result) {
public final <T> CaseWhenStep<V, T> when(V compareValue, Field<T> result) {
return when(Utils.field(compareValue), result);
}
@Override
public <T> CaseWhenStep<V, T> when(Field<V> compareValue, T result) {
public final <T> CaseWhenStep<V, T> when(V compareValue, Select<? extends Record1<T>> result) {
return when(Utils.field(compareValue), DSL.field(result));
}
@Override
public final <T> CaseWhenStep<V, T> when(Field<V> compareValue, T result) {
return when(compareValue, Utils.field(result));
}
@Override
public <T> CaseWhenStep<V, T> when(Field<V> compareValue, Field<T> result) {
public final <T> CaseWhenStep<V, T> when(Field<V> compareValue, Field<T> result) {
return new CaseWhenStepImpl<V, T>(value, compareValue, result);
}
@Override
public final <T> CaseWhenStep<V, T> when(Field<V> compareValue, Select<? extends Record1<T>> result) {
return when(compareValue, DSL.field(result));
}
}