|
|
|
@ -1,9 +1,11 @@ |
|
|
|
package org.springframework.data.relational.core.sql; |
|
|
|
package org.springframework.data.relational.core.sql; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.lang.Nullable; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.List; |
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
|
|
import static java.util.stream.Collectors.joining; |
|
|
|
import static java.util.stream.Collectors.*; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Case with one or more conditions expression. |
|
|
|
* Case with one or more conditions expression. |
|
|
|
@ -22,18 +24,33 @@ import static java.util.stream.Collectors.joining; |
|
|
|
* @since 3.4 |
|
|
|
* @since 3.4 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class CaseExpression extends AbstractSegment implements Expression { |
|
|
|
public class CaseExpression extends AbstractSegment implements Expression { |
|
|
|
|
|
|
|
|
|
|
|
private final List<When> whenList; |
|
|
|
private final List<When> whenList; |
|
|
|
|
|
|
|
@Nullable |
|
|
|
private final Expression elseExpression; |
|
|
|
private final Expression elseExpression; |
|
|
|
|
|
|
|
|
|
|
|
private CaseExpression(List<When> whenList, Expression elseExpression) { |
|
|
|
private static Segment[] children(List<When> whenList, @Nullable Expression elseExpression) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<Segment> segments = new ArrayList<>(whenList); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (elseExpression != null) { |
|
|
|
|
|
|
|
segments.add(elseExpression); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return segments.toArray(new Segment[0]); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private CaseExpression(List<When> whenList, @Nullable Expression elseExpression) { |
|
|
|
|
|
|
|
|
|
|
|
super(children(whenList, elseExpression)); |
|
|
|
super(children(whenList, elseExpression)); |
|
|
|
|
|
|
|
|
|
|
|
this.whenList = whenList; |
|
|
|
this.whenList = whenList; |
|
|
|
this.elseExpression = elseExpression; |
|
|
|
this.elseExpression = elseExpression; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Create CASE {@link Expression} with initial {@link When} condition. |
|
|
|
* Create CASE {@link Expression} with initial {@link When} condition. |
|
|
|
|
|
|
|
* |
|
|
|
* @param condition initial {@link When} condition |
|
|
|
* @param condition initial {@link When} condition |
|
|
|
* @return the {@link CaseExpression} |
|
|
|
* @return the {@link CaseExpression} |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@ -43,6 +60,7 @@ public class CaseExpression extends AbstractSegment implements Expression { |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Add additional {@link When} condition |
|
|
|
* Add additional {@link When} condition |
|
|
|
|
|
|
|
* |
|
|
|
* @param condition the {@link When} condition |
|
|
|
* @param condition the {@link When} condition |
|
|
|
* @return the {@link CaseExpression} |
|
|
|
* @return the {@link CaseExpression} |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@ -54,41 +72,16 @@ public class CaseExpression extends AbstractSegment implements Expression { |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Add ELSE clause |
|
|
|
* Add ELSE clause |
|
|
|
|
|
|
|
* |
|
|
|
* @param elseExpression the {@link Expression} else value |
|
|
|
* @param elseExpression the {@link Expression} else value |
|
|
|
* @return the {@link CaseExpression} |
|
|
|
* @return the {@link CaseExpression} |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public CaseExpression elseExpression(Literal elseExpression) { |
|
|
|
public CaseExpression elseExpression(Expression elseExpression) { |
|
|
|
return new CaseExpression(whenList, elseExpression); |
|
|
|
return new CaseExpression(whenList, elseExpression); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @return the {@link When} conditions |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public List<When> getWhenList() { |
|
|
|
|
|
|
|
return whenList; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @return the ELSE {@link Literal} value |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public Expression getElseExpression() { |
|
|
|
|
|
|
|
return elseExpression; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public String toString() { |
|
|
|
public String toString() { |
|
|
|
return "CASE " + whenList.stream().map(When::toString).collect(joining(" ")) + (elseExpression != null ? " ELSE " + elseExpression : "") + " END"; |
|
|
|
return "CASE " + whenList.stream().map(When::toString).collect(joining(" ")) + (elseExpression != null ? " ELSE " + elseExpression : "") + " END"; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static Segment[] children(List<When> whenList, Expression elseExpression) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<Segment> segments = new ArrayList<>(); |
|
|
|
|
|
|
|
segments.addAll(whenList); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (elseExpression != null) { |
|
|
|
|
|
|
|
segments.add(elseExpression); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return segments.toArray(new Segment[segments.size()]); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|