mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 09:08:24 +08:00
optimize "case" builder, close #I7CIF9
This commit is contained in:
parent
5fd278a7a0
commit
b77faf4bf1
@ -83,10 +83,6 @@ public class CaseQueryColumn extends QueryColumn implements HasParamsColumn {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> QueryColumn as(LambdaGetter<T> fn) {
|
||||
return as(LambdaUtil.getFieldName(fn));
|
||||
}
|
||||
|
||||
private String buildValue(Object value) {
|
||||
if (value instanceof Number || value instanceof Boolean) {
|
||||
@ -114,19 +110,15 @@ public class CaseQueryColumn extends QueryColumn implements HasParamsColumn {
|
||||
|
||||
|
||||
public static class When implements CloneSupport<When> {
|
||||
private Builder builder;
|
||||
private QueryCondition whenCondition;
|
||||
private Object thenValue;
|
||||
|
||||
public When(Builder builder, QueryCondition whenCondition) {
|
||||
this.builder = builder;
|
||||
public When(QueryCondition whenCondition) {
|
||||
this.whenCondition = whenCondition;
|
||||
}
|
||||
|
||||
public Builder then(Object thenValue) {
|
||||
public void then(Object thenValue) {
|
||||
this.thenValue = thenValue;
|
||||
this.builder.caseQueryColumn.addWhen(this);
|
||||
return builder;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -143,12 +135,14 @@ public class CaseQueryColumn extends QueryColumn implements HasParamsColumn {
|
||||
}
|
||||
}
|
||||
|
||||
public static class Builder implements CloneSupport<Builder> {
|
||||
public static class Builder {
|
||||
|
||||
private CaseQueryColumn caseQueryColumn = new CaseQueryColumn();
|
||||
private When lastWhen;
|
||||
|
||||
public When when(QueryCondition condition) {
|
||||
return new When(this, condition);
|
||||
public Then when(QueryCondition condition) {
|
||||
lastWhen = new When(condition);
|
||||
return new Then(this);
|
||||
}
|
||||
|
||||
public Builder else_(Object elseValue) {
|
||||
@ -160,15 +154,18 @@ public class CaseQueryColumn extends QueryColumn implements HasParamsColumn {
|
||||
return caseQueryColumn;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder clone() {
|
||||
try {
|
||||
Builder clone = (Builder) super.clone();
|
||||
// deep clone ...
|
||||
clone.caseQueryColumn = this.caseQueryColumn.clone();
|
||||
return clone;
|
||||
} catch (CloneNotSupportedException e) {
|
||||
throw FlexExceptions.wrap(e);
|
||||
public static class Then {
|
||||
|
||||
private Builder builder;
|
||||
|
||||
public Then(Builder builder) {
|
||||
this.builder = builder;
|
||||
}
|
||||
|
||||
public Builder then(Object thenValue) {
|
||||
this.builder.lastWhen.then(thenValue);
|
||||
this.builder.caseQueryColumn.addWhen(builder.lastWhen);
|
||||
return builder;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -98,11 +98,6 @@ public class CaseSearchQueryColumn extends QueryColumn implements HasParamsColum
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> QueryColumn as(LambdaGetter<T> fn) {
|
||||
return as(LambdaUtil.getFieldName(fn));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Object[] getParamValues() {
|
||||
@ -115,19 +110,15 @@ public class CaseSearchQueryColumn extends QueryColumn implements HasParamsColum
|
||||
|
||||
|
||||
public static class When implements CloneSupport<When> {
|
||||
private Builder builder;
|
||||
private Object searchValue;
|
||||
private Object thenValue;
|
||||
|
||||
public When(Builder builder, Object searchValue) {
|
||||
this.builder = builder;
|
||||
public When(Object searchValue) {
|
||||
this.searchValue = searchValue;
|
||||
}
|
||||
|
||||
public Builder then(Object thenValue) {
|
||||
public void setThenValue(Object thenValue) {
|
||||
this.thenValue = thenValue;
|
||||
this.builder.caseQueryColumn.addWhen(this);
|
||||
return builder;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -144,19 +135,19 @@ public class CaseSearchQueryColumn extends QueryColumn implements HasParamsColum
|
||||
}
|
||||
}
|
||||
|
||||
public static class Builder implements CloneSupport<Builder> {
|
||||
|
||||
public static class Builder {
|
||||
|
||||
private CaseSearchQueryColumn caseQueryColumn = new CaseSearchQueryColumn();
|
||||
|
||||
private Builder() {
|
||||
}
|
||||
private When lastWhen;
|
||||
|
||||
public Builder(QueryColumn queryColumn) {
|
||||
this.caseQueryColumn.queryColumn = queryColumn;
|
||||
}
|
||||
|
||||
public When when(Object searchValue) {
|
||||
return new When(this, searchValue);
|
||||
public Then when(Object searchValue) {
|
||||
lastWhen = new When(searchValue);
|
||||
return new Then(this);
|
||||
}
|
||||
|
||||
public Builder else_(Object elseValue) {
|
||||
@ -168,15 +159,18 @@ public class CaseSearchQueryColumn extends QueryColumn implements HasParamsColum
|
||||
return caseQueryColumn;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder clone() {
|
||||
try {
|
||||
Builder clone = (Builder) super.clone();
|
||||
// deep clone ...
|
||||
clone.caseQueryColumn = this.caseQueryColumn.clone();
|
||||
return clone;
|
||||
} catch (CloneNotSupportedException e) {
|
||||
throw FlexExceptions.wrap(e);
|
||||
public static class Then {
|
||||
|
||||
private Builder builder;
|
||||
|
||||
public Then(Builder builder) {
|
||||
this.builder = builder;
|
||||
}
|
||||
|
||||
public Builder then(Object thenValue) {
|
||||
this.builder.lastWhen.setThenValue(thenValue);
|
||||
this.builder.caseQueryColumn.addWhen(builder.lastWhen);
|
||||
return builder;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -374,8 +374,6 @@ public class AccountSqlTester {
|
||||
|
||||
@Test
|
||||
public void testCase1() {
|
||||
IDialect dialect = new CommonsDialectImpl();
|
||||
|
||||
QueryWrapper queryWrapper = QueryWrapper.create()
|
||||
.select(ACCOUNT.ALL_COLUMNS,
|
||||
case_()
|
||||
@ -386,8 +384,7 @@ public class AccountSqlTester {
|
||||
.from(ACCOUNT)
|
||||
.and(ACCOUNT.USER_NAME.like("michael"));
|
||||
|
||||
String sql = dialect.forSelectByQuery(queryWrapper);
|
||||
System.out.println(sql);
|
||||
System.out.println(queryWrapper.toSQL());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user