Skip to content

Commit

Permalink
fix: 修复当not in条件数量较大时,条件失效问题.
Browse files Browse the repository at this point in the history
部分数据库单个in条件支持的数量有限制,默认做了拆分,not in应当使用and条件进行拼接.

可通过jvm启动参数-Deasyorm.term.in.split-large-parameter来全局关闭拆分.
可通过jvm启动参数-Deasyorm.term.in.split-large-size来指定拆分数量.
  • Loading branch information
zhou-hao committed Sep 26, 2023
1 parent c5a4aa0 commit af35b55
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,30 @@
import org.hswebframework.ezorm.rdb.operator.builder.fragments.PrepareSqlFragments;
import org.hswebframework.ezorm.rdb.operator.builder.fragments.SqlFragments;

import java.util.Arrays;
import java.util.List;

public class InTermFragmentBuilder extends AbstractTermFragmentBuilder {

private static final boolean SPLIT_LARGE = Boolean.parseBoolean(System.getProperty("easyorm.term.in.split-large-parameter", "true"));

private static final int SPLIT_LARGE_SIZE = Integer.getInteger("easyorm.term.in.split-large-size", 500);

private final String symbol;

private final boolean not;

private final boolean splitLargeParameter;

public InTermFragmentBuilder(String termType, String name, boolean isNot) {
this(termType, name, isNot, SPLIT_LARGE);
}

public InTermFragmentBuilder(String termType, String name, boolean isNot, boolean splitLargeParameter) {
super(termType, name);
not = isNot;
symbol = isNot ? "not in(" : "in(";
this.splitLargeParameter = splitLargeParameter;
}

@Override
Expand All @@ -27,8 +42,19 @@ public SqlFragments createFragments(String columnFullName, RDBColumnMetadata col
int len = value.size();

PrepareSqlFragments fragments = PrepareSqlFragments.of();
if (!splitLargeParameter) {
String[] arr = new String[len];
Arrays.fill(arr, "?");
fragments.addSql(columnFullName)
.addSql(symbol)
.addSql("(")
.addSql(arr)
.addSql(")")
.addParameter(value);
return fragments;
}
//参数数量大于 500时,使用(column in (?,?,?) or column in(?,?,?))
if (len > 500) {
if (len > SPLIT_LARGE_SIZE) {
fragments.addSql("(");
}
fragments.addSql(columnFullName)
Expand All @@ -40,14 +66,18 @@ public SqlFragments createFragments(String columnFullName, RDBColumnMetadata col
fragments.addSql(",");
}
fragments.addSql("?");
if (flag > 500 && i != len - 1) {
if (flag > SPLIT_LARGE_SIZE && i != len - 1) {
flag = 0;
fragments.addSql(") or")
.addSql(columnFullName)
if (not) {
fragments.addSql(") and");
} else {
fragments.addSql(") or");
}
fragments.addSql(columnFullName)
.addSql(symbol);
}
}
if (len > 500) {
if (len > SPLIT_LARGE_SIZE) {
fragments.addSql(")");
}
return fragments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,27 @@ public void test() {

}

@Test
public void testNoSplit() {
InTermFragmentBuilder builder = new InTermFragmentBuilder("in", "在...之中",false,false);

Term term = new Term();
List<Integer> values = Flux.range(0, 510).collectList().block();
assertNotNull(values);

term.setValue(values);

SqlFragments fragments = builder.createFragments("id", null, term);

assertNotNull(fragments);
SqlRequest request = fragments.toRequest();

assertArrayEquals(values.toArray(),request.getParameters());

System.out.println(request.getSql());
Assert.assertFalse(request.getSql().contains("and"));
}

@Test
public void testLarge() {

Expand Down

0 comments on commit af35b55

Please sign in to comment.