refactor(数据源): 优化SQL Server过滤

This commit is contained in:
junjun 2024-09-24 15:22:53 +08:00
parent 70e32116e0
commit 501876dd25
4 changed files with 61 additions and 15 deletions

View File

@ -71,6 +71,8 @@ public class SQLConstants {
public static final String WHERE_VALUE_VALUE = "'%s'"; public static final String WHERE_VALUE_VALUE = "'%s'";
public static final String WHERE_VALUE_VALUE_CH = "N'%s'";
public static final String WHERE_NUMBER_VALUE = "%s"; public static final String WHERE_NUMBER_VALUE = "%s";
public static final String AGG_COUNT = "COUNT(*)"; public static final String AGG_COUNT = "COUNT(*)";

View File

@ -17,6 +17,7 @@ import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
/** /**
* @Author Junjun * @Author Junjun
@ -135,7 +136,11 @@ public class CustomWhere2Str {
if (StringUtils.equalsIgnoreCase(item.getFilterType(), "enum")) { if (StringUtils.equalsIgnoreCase(item.getFilterType(), "enum")) {
if (ObjectUtils.isNotEmpty(item.getEnumValue())) { if (ObjectUtils.isNotEmpty(item.getEnumValue())) {
res = "(" + whereName + " IN ('" + String.join("','", item.getEnumValue()) + "'))"; if (StringUtils.equalsIgnoreCase(field.getType(), "NVARCHAR")) {
res = "(" + whereName + " IN (" + item.getEnumValue().stream().map(str -> "N" + "'" + str + "'").collect(Collectors.joining(",")) + "))";
} else {
res = "(" + whereName + " IN ('" + String.join("','", item.getEnumValue()) + "'))";
}
} }
} else { } else {
if (field.getDeType() == 1 && isCross) { if (field.getDeType() == 1 && isCross) {
@ -156,9 +161,17 @@ public class CustomWhere2Str {
} else if (StringUtils.equalsIgnoreCase(item.getTerm(), "not_empty")) { } else if (StringUtils.equalsIgnoreCase(item.getTerm(), "not_empty")) {
whereValue = "''"; whereValue = "''";
} else if (StringUtils.containsIgnoreCase(item.getTerm(), "in") || StringUtils.containsIgnoreCase(item.getTerm(), "not in")) { } else if (StringUtils.containsIgnoreCase(item.getTerm(), "in") || StringUtils.containsIgnoreCase(item.getTerm(), "not in")) {
whereValue = "('" + String.join("','", value.split(",")) + "')"; if (StringUtils.equalsIgnoreCase(field.getType(), "NVARCHAR")) {
whereValue = "(" + Arrays.stream(value.split(",")).map(str -> "N" + "'" + str + "'").collect(Collectors.joining(",")) + ")";
} else {
whereValue = "('" + String.join("','", value.split(",")) + "')";
}
} else if (StringUtils.containsIgnoreCase(item.getTerm(), "like")) { } else if (StringUtils.containsIgnoreCase(item.getTerm(), "like")) {
whereValue = "'%" + value + "%'"; if (StringUtils.equalsIgnoreCase(field.getType(), "NVARCHAR")) {
whereValue = "N'%" + value + "%'";
} else {
whereValue = "'%" + value + "%'";
}
} else { } else {
// 如果是时间字段过滤当条件是等于和不等于的时候转换成between和not between // 如果是时间字段过滤当条件是等于和不等于的时候转换成between和not between
if (field.getDeType() == 1) { if (field.getDeType() == 1) {
@ -199,7 +212,11 @@ public class CustomWhere2Str {
whereValue = String.format(SQLConstants.WHERE_VALUE_VALUE, value); whereValue = String.format(SQLConstants.WHERE_VALUE_VALUE, value);
} }
} else { } else {
whereValue = String.format(SQLConstants.WHERE_VALUE_VALUE, value); if (StringUtils.equalsIgnoreCase(field.getType(), "NVARCHAR")) {
whereValue = String.format(SQLConstants.WHERE_VALUE_VALUE_CH, value);
} else {
whereValue = String.format(SQLConstants.WHERE_VALUE_VALUE, value);
}
} }
} }
SQLObj build = SQLObj.builder() SQLObj build = SQLObj.builder()

View File

@ -13,10 +13,8 @@ import io.dataease.extensions.view.dto.ChartExtFilterDTO;
import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList; import java.util.*;
import java.util.HashMap; import java.util.stream.Collectors;
import java.util.List;
import java.util.Map;
/** /**
* @Author Junjun * @Author Junjun
@ -129,10 +127,18 @@ public class ExtWhere2Str {
if (value.contains(SQLConstants.EMPTY_SIGN)) { if (value.contains(SQLConstants.EMPTY_SIGN)) {
whereValue = "('" + StringUtils.join(value, "','") + "', '')" + " or " + whereName + " is null "; whereValue = "('" + StringUtils.join(value, "','") + "', '')" + " or " + whereName + " is null ";
} else { } else {
whereValue = "('" + StringUtils.join(value, "','") + "')"; if (StringUtils.equalsIgnoreCase(request.getDatasetTableField().getType(), "NVARCHAR")) {
whereValue = "(" + value.stream().map(str -> "N" + "'" + str + "'").collect(Collectors.joining(",")) + ")";
} else {
whereValue = "('" + StringUtils.join(value, "','") + "')";
}
} }
} else if (StringUtils.containsIgnoreCase(request.getOperator(), "like")) { } else if (StringUtils.containsIgnoreCase(request.getOperator(), "like")) {
whereValue = "'%" + value.get(0) + "%'"; if (StringUtils.equalsIgnoreCase(request.getDatasetTableField().getType(), "NVARCHAR")) {
whereValue = "N'%" + value.get(0) + "%'";
} else {
whereValue = "'%" + value.get(0) + "%'";
}
} else if (StringUtils.containsIgnoreCase(request.getOperator(), "between")) { } else if (StringUtils.containsIgnoreCase(request.getOperator(), "between")) {
if (request.getDatasetTableField().getDeType() == 1) { if (request.getDatasetTableField().getDeType() == 1) {
if (request.getDatasetTableField().getDeExtractType() == 2 if (request.getDatasetTableField().getDeExtractType() == 2
@ -159,7 +165,11 @@ public class ExtWhere2Str {
if (StringUtils.equals(value.get(0), SQLConstants.EMPTY_SIGN)) { if (StringUtils.equals(value.get(0), SQLConstants.EMPTY_SIGN)) {
whereValue = String.format(SQLConstants.WHERE_VALUE_VALUE, "") + " or " + whereName + " is null "; whereValue = String.format(SQLConstants.WHERE_VALUE_VALUE, "") + " or " + whereName + " is null ";
} else { } else {
whereValue = String.format(SQLConstants.WHERE_VALUE_VALUE, value.get(0)); if (StringUtils.equalsIgnoreCase(request.getDatasetTableField().getType(), "NVARCHAR")) {
whereValue = String.format(SQLConstants.WHERE_VALUE_VALUE_CH, value.get(0));
} else {
whereValue = String.format(SQLConstants.WHERE_VALUE_VALUE, value.get(0));
}
} }
} }
list.add(SQLObj.builder() list.add(SQLObj.builder()

View File

@ -18,6 +18,7 @@ import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
/** /**
* @Author Junjun * @Author Junjun
@ -148,7 +149,11 @@ public class WhereTree2Str {
if (StringUtils.equalsIgnoreCase(item.getFilterType(), "enum")) { if (StringUtils.equalsIgnoreCase(item.getFilterType(), "enum")) {
if (CollectionUtils.isNotEmpty(item.getEnumValue())) { if (CollectionUtils.isNotEmpty(item.getEnumValue())) {
res = "(" + whereName + " IN ('" + String.join("','", item.getEnumValue()) + "'))"; if (StringUtils.equalsIgnoreCase(field.getType(), "NVARCHAR")) {
res = "(" + whereName + " IN (" + item.getEnumValue().stream().map(str -> "N" + "'" + str + "'").collect(Collectors.joining(",")) + "))";
} else {
res = "(" + whereName + " IN ('" + String.join("','", item.getEnumValue()) + "'))";
}
} }
} else { } else {
String value = item.getValue(); String value = item.getValue();
@ -168,9 +173,17 @@ public class WhereTree2Str {
} else if (StringUtils.equalsIgnoreCase(item.getTerm(), "not_empty")) { } else if (StringUtils.equalsIgnoreCase(item.getTerm(), "not_empty")) {
whereValue = "''"; whereValue = "''";
} else if (StringUtils.containsIgnoreCase(item.getTerm(), "in") || StringUtils.containsIgnoreCase(item.getTerm(), "not in")) { } else if (StringUtils.containsIgnoreCase(item.getTerm(), "in") || StringUtils.containsIgnoreCase(item.getTerm(), "not in")) {
whereValue = "('" + String.join("','", value.split(",")) + "')"; if (StringUtils.equalsIgnoreCase(field.getType(), "NVARCHAR")) {
whereValue = "(" + Arrays.stream(value.split(",")).map(str -> "N" + "'" + str + "'").collect(Collectors.joining(",")) + ")";
} else {
whereValue = "('" + String.join("','", value.split(",")) + "')";
}
} else if (StringUtils.containsIgnoreCase(item.getTerm(), "like")) { } else if (StringUtils.containsIgnoreCase(item.getTerm(), "like")) {
whereValue = "'%" + value + "%'"; if (StringUtils.equalsIgnoreCase(field.getType(), "NVARCHAR")) {
whereValue = "N'%" + value + "%'";
} else {
whereValue = "'%" + value + "%'";
}
} else { } else {
// 如果是时间字段过滤当条件是等于和不等于的时候转换成between和not between // 如果是时间字段过滤当条件是等于和不等于的时候转换成between和not between
if (field.getDeType() == 1) { if (field.getDeType() == 1) {
@ -209,7 +222,11 @@ public class WhereTree2Str {
whereValue = String.format(SQLConstants.WHERE_VALUE_VALUE, value); whereValue = String.format(SQLConstants.WHERE_VALUE_VALUE, value);
} }
} else { } else {
whereValue = String.format(SQLConstants.WHERE_VALUE_VALUE, value); if (StringUtils.equalsIgnoreCase(field.getType(), "NVARCHAR")) {
whereValue = String.format(SQLConstants.WHERE_VALUE_VALUE_CH, value);
} else {
whereValue = String.format(SQLConstants.WHERE_VALUE_VALUE, value);
}
} }
} }
SQLObj build = SQLObj.builder().whereField(whereName).whereTermAndValue(whereTerm + whereValue).build(); SQLObj build = SQLObj.builder().whereField(whereName).whereTermAndValue(whereTerm + whereValue).build();