-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #761 from jeffgbutler/case-expression
Add Support for CASE Expressions
- Loading branch information
Showing
36 changed files
with
3,612 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/main/java/org/mybatis/dynamic/sql/select/caseexpression/BasicWhenCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright 2016-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.mybatis.dynamic.sql.select.caseexpression; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
import org.mybatis.dynamic.sql.BasicColumn; | ||
|
||
public class BasicWhenCondition<T> extends SimpleCaseWhenCondition<T> { | ||
private final List<T> conditions = new ArrayList<>(); | ||
|
||
public BasicWhenCondition(List<T> conditions, BasicColumn thenValue) { | ||
super(thenValue); | ||
this.conditions.addAll(conditions); | ||
} | ||
|
||
public Stream<T> conditions() { | ||
return conditions.stream(); | ||
} | ||
|
||
@Override | ||
public <R> R accept(SimpleCaseWhenConditionVisitor<T, R> visitor) { | ||
return visitor.visit(this); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/org/mybatis/dynamic/sql/select/caseexpression/ConditionBasedWhenCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2016-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.mybatis.dynamic.sql.select.caseexpression; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
import org.mybatis.dynamic.sql.BasicColumn; | ||
import org.mybatis.dynamic.sql.VisitableCondition; | ||
|
||
public class ConditionBasedWhenCondition<T> extends SimpleCaseWhenCondition<T> { | ||
private final List<VisitableCondition<T>> conditions = new ArrayList<>(); | ||
|
||
public ConditionBasedWhenCondition(List<VisitableCondition<T>> conditions, BasicColumn thenValue) { | ||
super(thenValue); | ||
this.conditions.addAll(conditions); | ||
} | ||
|
||
public Stream<VisitableCondition<T>> conditions() { | ||
return conditions.stream(); | ||
} | ||
|
||
@Override | ||
public <R> R accept(SimpleCaseWhenConditionVisitor<T, R> visitor) { | ||
return visitor.visit(this); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/org/mybatis/dynamic/sql/select/caseexpression/ElseDSL.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2016-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.mybatis.dynamic.sql.select.caseexpression; | ||
|
||
import org.mybatis.dynamic.sql.BasicColumn; | ||
import org.mybatis.dynamic.sql.Constant; | ||
import org.mybatis.dynamic.sql.StringConstant; | ||
|
||
public interface ElseDSL<T> { | ||
|
||
@SuppressWarnings("java:S100") | ||
default T else_(String value) { | ||
return else_(StringConstant.of(value)); | ||
} | ||
|
||
@SuppressWarnings("java:S100") | ||
default T else_(Boolean value) { | ||
return else_(Constant.of(value.toString())); | ||
} | ||
|
||
@SuppressWarnings("java:S100") | ||
default T else_(Integer value) { | ||
return else_(Constant.of(value.toString())); | ||
} | ||
|
||
@SuppressWarnings("java:S100") | ||
default T else_(Long value) { | ||
return else_(Constant.of(value.toString())); | ||
} | ||
|
||
@SuppressWarnings("java:S100") | ||
default T else_(Double value) { | ||
return else_(Constant.of(value.toString())); | ||
} | ||
|
||
@SuppressWarnings("java:S100") | ||
T else_(BasicColumn column); | ||
} |
108 changes: 108 additions & 0 deletions
108
src/main/java/org/mybatis/dynamic/sql/select/caseexpression/SearchedCaseDSL.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Copyright 2016-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.mybatis.dynamic.sql.select.caseexpression; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.mybatis.dynamic.sql.AndOrCriteriaGroup; | ||
import org.mybatis.dynamic.sql.BasicColumn; | ||
import org.mybatis.dynamic.sql.BindableColumn; | ||
import org.mybatis.dynamic.sql.ColumnAndConditionCriterion; | ||
import org.mybatis.dynamic.sql.CriteriaGroup; | ||
import org.mybatis.dynamic.sql.SqlCriterion; | ||
import org.mybatis.dynamic.sql.VisitableCondition; | ||
import org.mybatis.dynamic.sql.common.AbstractBooleanExpressionDSL; | ||
|
||
public class SearchedCaseDSL implements ElseDSL<SearchedCaseDSL.SearchedCaseEnder> { | ||
private final List<SearchedCaseModel.SearchedWhenCondition> whenConditions = new ArrayList<>(); | ||
private BasicColumn elseValue; | ||
|
||
public <T> WhenDSL when(BindableColumn<T> column, VisitableCondition<T> condition, | ||
AndOrCriteriaGroup... subCriteria) { | ||
return when(column, condition, Arrays.asList(subCriteria)); | ||
} | ||
|
||
public <T> WhenDSL when(BindableColumn<T> column, VisitableCondition<T> condition, | ||
List<AndOrCriteriaGroup> subCriteria) { | ||
SqlCriterion sqlCriterion = ColumnAndConditionCriterion.withColumn(column) | ||
.withCondition(condition) | ||
.withSubCriteria(subCriteria) | ||
.build(); | ||
|
||
return initialize(sqlCriterion); | ||
} | ||
|
||
public WhenDSL when(SqlCriterion initialCriterion, AndOrCriteriaGroup... subCriteria) { | ||
return when(initialCriterion, Arrays.asList(subCriteria)); | ||
} | ||
|
||
public WhenDSL when(SqlCriterion initialCriterion, List<AndOrCriteriaGroup> subCriteria) { | ||
SqlCriterion sqlCriterion = new CriteriaGroup.Builder() | ||
.withInitialCriterion(initialCriterion) | ||
.withSubCriteria(subCriteria) | ||
.build(); | ||
|
||
return initialize(sqlCriterion); | ||
} | ||
|
||
private WhenDSL initialize(SqlCriterion sqlCriterion) { | ||
return new WhenDSL(sqlCriterion); | ||
} | ||
|
||
@SuppressWarnings("java:S100") | ||
@Override | ||
public SearchedCaseEnder else_(BasicColumn column) { | ||
elseValue = column; | ||
return new SearchedCaseEnder(); | ||
} | ||
|
||
public BasicColumn end() { | ||
return new SearchedCaseModel.Builder() | ||
.withElseValue(elseValue) | ||
.withWhenConditions(whenConditions) | ||
.build(); | ||
} | ||
|
||
public class WhenDSL extends AbstractBooleanExpressionDSL<WhenDSL> implements ThenDSL<SearchedCaseDSL> { | ||
private WhenDSL(SqlCriterion sqlCriterion) { | ||
setInitialCriterion(sqlCriterion); | ||
} | ||
|
||
@Override | ||
public SearchedCaseDSL then(BasicColumn column) { | ||
whenConditions.add(new SearchedCaseModel.SearchedWhenCondition(getInitialCriterion(), subCriteria, | ||
column)); | ||
return SearchedCaseDSL.this; | ||
} | ||
|
||
@Override | ||
protected WhenDSL getThis() { | ||
return this; | ||
} | ||
} | ||
|
||
public class SearchedCaseEnder { | ||
public BasicColumn end() { | ||
return SearchedCaseDSL.this.end(); | ||
} | ||
} | ||
|
||
public static SearchedCaseDSL searchedCase() { | ||
return new SearchedCaseDSL(); | ||
} | ||
} |
Oops, something went wrong.