feat: 添加 With 构建支持深度克隆。

This commit is contained in:
Suomm 2023-06-22 14:28:31 +08:00
parent e33ee947b5
commit 1a179780e3
7 changed files with 81 additions and 10 deletions

View File

@ -267,6 +267,7 @@ public class BaseQueryWrapper<T extends BaseQueryWrapper<T>> implements CloneSup
try { try {
T clone = (T) super.clone(); T clone = (T) super.clone();
// deep clone ... // deep clone ...
clone.with = ObjectUtil.clone(this.with);
clone.queryTables = CollectionUtil.cloneArrayList(this.queryTables); clone.queryTables = CollectionUtil.cloneArrayList(this.queryTables);
clone.selectColumns = CollectionUtil.cloneArrayList(this.selectColumns); clone.selectColumns = CollectionUtil.cloneArrayList(this.selectColumns);
clone.joins = CollectionUtil.cloneArrayList(this.joins); clone.joins = CollectionUtil.cloneArrayList(this.joins);

View File

@ -17,13 +17,14 @@ package com.mybatisflex.core.query;
import com.mybatisflex.core.FlexConsts; import com.mybatisflex.core.FlexConsts;
import com.mybatisflex.core.dialect.IDialect; import com.mybatisflex.core.dialect.IDialect;
import com.mybatisflex.core.exception.FlexExceptions;
import com.mybatisflex.core.util.ArrayUtil; import com.mybatisflex.core.util.ArrayUtil;
import com.mybatisflex.core.util.CollectionUtil;
import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class With implements Serializable { public class With implements CloneSupport<With> {
private boolean recursive; private boolean recursive;
private List<WithItem> withItems; private List<WithItem> withItems;
@ -79,4 +80,17 @@ public class With implements Serializable {
} }
return paramValues; return paramValues;
} }
@Override
public With clone() {
try {
With clone = (With) super.clone();
// deep clone ...
clone.withItems = CollectionUtil.cloneArrayList(this.withItems);
return clone;
} catch (CloneNotSupportedException e) {
throw FlexExceptions.wrap(e);
}
}
} }

View File

@ -17,7 +17,7 @@ package com.mybatisflex.core.query;
import com.mybatisflex.core.dialect.IDialect; import com.mybatisflex.core.dialect.IDialect;
public interface WithDetail { public interface WithDetail extends CloneSupport<WithDetail> {
String toSql(IDialect dialect); String toSql(IDialect dialect);

View File

@ -16,13 +16,14 @@
package com.mybatisflex.core.query; package com.mybatisflex.core.query;
import com.mybatisflex.core.dialect.IDialect; import com.mybatisflex.core.dialect.IDialect;
import com.mybatisflex.core.exception.FlexExceptions;
import com.mybatisflex.core.util.CollectionUtil; import com.mybatisflex.core.util.CollectionUtil;
import com.mybatisflex.core.util.ObjectUtil;
import com.mybatisflex.core.util.StringUtil; import com.mybatisflex.core.util.StringUtil;
import java.io.Serializable;
import java.util.List; import java.util.List;
public class WithItem implements Serializable { public class WithItem implements CloneSupport<WithItem> {
private String name; private String name;
private List<String> params; private List<String> params;
@ -64,7 +65,7 @@ public class WithItem implements Serializable {
public String toSql(IDialect dialect) { public String toSql(IDialect dialect) {
StringBuilder sql = new StringBuilder(name); StringBuilder sql = new StringBuilder(name);
if (CollectionUtil.isNotEmpty(params)){ if (CollectionUtil.isNotEmpty(params)){
sql.append("(").append(StringUtil.join(", ",params)).append(")"); sql.append("(").append(StringUtil.join(", ", params)).append(")");
} }
sql.append(" AS ("); sql.append(" AS (");
sql.append(withDetail.toSql(dialect)); sql.append(withDetail.toSql(dialect));
@ -74,4 +75,18 @@ public class WithItem implements Serializable {
public Object[] getParamValues() { public Object[] getParamValues() {
return withDetail.getParamValues(); return withDetail.getParamValues();
} }
@Override
public WithItem clone() {
try {
WithItem clone = (WithItem) super.clone();
// deep clone ...
clone.withDetail = ObjectUtil.clone(this.withDetail);
clone.params = CollectionUtil.newArrayList(this.params);
return clone;
} catch (CloneNotSupportedException e) {
throw FlexExceptions.wrap(e);
}
}
} }

View File

@ -16,8 +16,9 @@
package com.mybatisflex.core.query; package com.mybatisflex.core.query;
import com.mybatisflex.core.dialect.IDialect; import com.mybatisflex.core.dialect.IDialect;
import com.mybatisflex.core.exception.FlexExceptions;
public class WithSelectDetail implements WithDetail{ public class WithSelectDetail implements WithDetail {
private QueryWrapper queryWrapper; private QueryWrapper queryWrapper;
@ -45,4 +46,17 @@ public class WithSelectDetail implements WithDetail{
public Object[] getParamValues() { public Object[] getParamValues() {
return queryWrapper.getValueArray(); return queryWrapper.getValueArray();
} }
@Override
public WithSelectDetail clone() {
try {
WithSelectDetail clone = (WithSelectDetail) super.clone();
// deep clone ...
clone.queryWrapper = this.queryWrapper.clone();
return clone;
} catch (CloneNotSupportedException e) {
throw FlexExceptions.wrap(e);
}
}
} }

View File

@ -16,8 +16,9 @@
package com.mybatisflex.core.query; package com.mybatisflex.core.query;
import com.mybatisflex.core.dialect.IDialect; import com.mybatisflex.core.dialect.IDialect;
import com.mybatisflex.core.exception.FlexExceptions;
public class WithStringDetail implements WithDetail{ public class WithStringDetail implements WithDetail {
private String rawSQL; private String rawSQL;
private Object[] params; private Object[] params;
@ -52,4 +53,14 @@ public class WithStringDetail implements WithDetail{
public Object[] getParamValues() { public Object[] getParamValues() {
return params; return params;
} }
@Override
public WithStringDetail clone() {
try {
return (WithStringDetail) super.clone();
} catch (CloneNotSupportedException e) {
throw FlexExceptions.wrap(e);
}
}
} }

View File

@ -16,12 +16,14 @@
package com.mybatisflex.core.query; package com.mybatisflex.core.query;
import com.mybatisflex.core.dialect.IDialect; import com.mybatisflex.core.dialect.IDialect;
import com.mybatisflex.core.exception.FlexExceptions;
import com.mybatisflex.core.util.CollectionUtil;
import com.mybatisflex.core.util.StringUtil; import com.mybatisflex.core.util.StringUtil;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class WithValuesDetail implements WithDetail{ public class WithValuesDetail implements WithDetail {
private List<Object> values; private List<Object> values;
private QueryWrapper queryWrapper; private QueryWrapper queryWrapper;
@ -49,7 +51,7 @@ public class WithValuesDetail implements WithDetail{
stringValues.add(String.valueOf(value)); stringValues.add(String.valueOf(value));
} }
StringBuilder sql = new StringBuilder("VALUES (") StringBuilder sql = new StringBuilder("VALUES (")
.append(StringUtil.join(", ",stringValues)).append(") "); .append(StringUtil.join(", ", stringValues)).append(") ");
return sql.append(dialect.buildNoSelectSql(queryWrapper)).toString(); return sql.append(dialect.buildNoSelectSql(queryWrapper)).toString();
} }
@ -57,4 +59,18 @@ public class WithValuesDetail implements WithDetail{
public Object[] getParamValues() { public Object[] getParamValues() {
return queryWrapper.getValueArray(); return queryWrapper.getValueArray();
} }
@Override
public WithValuesDetail clone() {
try {
WithValuesDetail clone = (WithValuesDetail) super.clone();
// deep clone ...
clone.values = CollectionUtil.newArrayList(this.values);
clone.queryWrapper = this.queryWrapper.clone();
return clone;
} catch (CloneNotSupportedException e) {
throw FlexExceptions.wrap(e);
}
}
} }