fix(数据源): 修复SQL Server过滤中文无结果的问题 #12759

This commit is contained in:
junjun 2024-10-21 11:08:00 +08:00
parent f97eb3c1d3
commit 6c5e8caa8b
5 changed files with 46 additions and 21 deletions

View File

@ -71,7 +71,7 @@ public class SQLConstants {
public static final String WHERE_VALUE_VALUE = "'%s'";
public static final String WHERE_VALUE_VALUE_CH = "N'%s'";
public static final String WHERE_VALUE_VALUE_CH = "'-DENS-%s'";
public static final String WHERE_NUMBER_VALUE = "%s";
@ -98,4 +98,6 @@ public class SQLConstants {
public static final String EMPTY_SIGN = "_empty_$";
public static final String CONCAT = "CONCAT(%s, %s)";
public static final String MSSQL_N_PREFIX = "-DENS-";
}

View File

@ -151,8 +151,9 @@ public class CustomWhere2Str {
if (StringUtils.equalsIgnoreCase(item.getFilterType(), "enum")) {
if (ObjectUtils.isNotEmpty(item.getEnumValue())) {
if (StringUtils.equalsIgnoreCase(field.getType(), "NVARCHAR")) {
res = "(" + whereName + " IN (" + item.getEnumValue().stream().map(str -> "N" + "'" + str + "'").collect(Collectors.joining(",")) + "))";
if (StringUtils.containsIgnoreCase(field.getType(), "NVARCHAR")
|| StringUtils.containsIgnoreCase(field.getType(), "NCHAR")) {
res = "(" + whereName + " IN (" + item.getEnumValue().stream().map(str -> "'" + SQLConstants.MSSQL_N_PREFIX + str + "'").collect(Collectors.joining(",")) + "))";
} else {
res = "(" + whereName + " IN ('" + String.join("','", item.getEnumValue()) + "'))";
}
@ -176,14 +177,16 @@ public class CustomWhere2Str {
} else if (StringUtils.equalsIgnoreCase(item.getTerm(), "not_empty")) {
whereValue = "''";
} else if (StringUtils.containsIgnoreCase(item.getTerm(), "in") || StringUtils.containsIgnoreCase(item.getTerm(), "not in")) {
if (StringUtils.equalsIgnoreCase(field.getType(), "NVARCHAR")) {
whereValue = "(" + Arrays.stream(value.split(",")).map(str -> "N" + "'" + str + "'").collect(Collectors.joining(",")) + ")";
if (StringUtils.containsIgnoreCase(field.getType(), "NVARCHAR")
|| StringUtils.containsIgnoreCase(field.getType(), "NCHAR")) {
whereValue = "(" + Arrays.stream(value.split(",")).map(str -> "'" + SQLConstants.MSSQL_N_PREFIX + str + "'").collect(Collectors.joining(",")) + ")";
} else {
whereValue = "('" + String.join("','", value.split(",")) + "')";
}
} else if (StringUtils.containsIgnoreCase(item.getTerm(), "like")) {
if (StringUtils.equalsIgnoreCase(field.getType(), "NVARCHAR")) {
whereValue = "N'%" + value + "%'";
if (StringUtils.containsIgnoreCase(field.getType(), "NVARCHAR")
|| StringUtils.containsIgnoreCase(field.getType(), "NCHAR")) {
whereValue = "'" + SQLConstants.MSSQL_N_PREFIX + "%" + value + "%'";
} else {
whereValue = "'%" + value + "%'";
}
@ -227,7 +230,8 @@ public class CustomWhere2Str {
whereValue = String.format(SQLConstants.WHERE_VALUE_VALUE, value);
}
} else {
if (StringUtils.equalsIgnoreCase(field.getType(), "NVARCHAR")) {
if (StringUtils.containsIgnoreCase(field.getType(), "NVARCHAR")
|| StringUtils.containsIgnoreCase(field.getType(), "NCHAR")) {
whereValue = String.format(SQLConstants.WHERE_VALUE_VALUE_CH, value);
} else {
whereValue = String.format(SQLConstants.WHERE_VALUE_VALUE, value);

View File

@ -147,15 +147,17 @@ public class ExtWhere2Str {
if (value.contains(SQLConstants.EMPTY_SIGN)) {
whereValue = "('" + StringUtils.join(value, "','") + "', '')" + " or " + whereName + " is null ";
} else {
if (StringUtils.equalsIgnoreCase(request.getDatasetTableField().getType(), "NVARCHAR")) {
whereValue = "(" + value.stream().map(str -> "N" + "'" + str + "'").collect(Collectors.joining(",")) + ")";
if (StringUtils.containsIgnoreCase(request.getDatasetTableField().getType(), "NVARCHAR")
|| StringUtils.containsIgnoreCase(request.getDatasetTableField().getType(), "NCHAR")) {
whereValue = "(" + value.stream().map(str -> "'" + SQLConstants.MSSQL_N_PREFIX + str + "'").collect(Collectors.joining(",")) + ")";
} else {
whereValue = "('" + StringUtils.join(value, "','") + "')";
}
}
} else if (StringUtils.containsIgnoreCase(request.getOperator(), "like")) {
if (StringUtils.equalsIgnoreCase(request.getDatasetTableField().getType(), "NVARCHAR")) {
whereValue = "N'%" + value.get(0) + "%'";
if (StringUtils.containsIgnoreCase(request.getDatasetTableField().getType(), "NVARCHAR")
|| StringUtils.containsIgnoreCase(request.getDatasetTableField().getType(), "NCHAR")) {
whereValue = "'" + SQLConstants.MSSQL_N_PREFIX + "%" + value.get(0) + "%'";
} else {
whereValue = "'%" + value.get(0) + "%'";
}
@ -189,7 +191,8 @@ public class ExtWhere2Str {
if (StringUtils.equals(value.get(0), SQLConstants.EMPTY_SIGN)) {
whereValue = String.format(SQLConstants.WHERE_VALUE_VALUE, "") + " or " + whereName + " is null ";
} else {
if (StringUtils.equalsIgnoreCase(request.getDatasetTableField().getType(), "NVARCHAR")) {
if (StringUtils.containsIgnoreCase(request.getDatasetTableField().getType(), "NVARCHAR")
|| StringUtils.containsIgnoreCase(request.getDatasetTableField().getType(), "NCHAR")) {
whereValue = String.format(SQLConstants.WHERE_VALUE_VALUE_CH, value.get(0));
} else {
whereValue = String.format(SQLConstants.WHERE_VALUE_VALUE, value.get(0));

View File

@ -164,8 +164,9 @@ public class WhereTree2Str {
if (StringUtils.equalsIgnoreCase(item.getFilterType(), "enum")) {
if (CollectionUtils.isNotEmpty(item.getEnumValue())) {
if (StringUtils.equalsIgnoreCase(field.getType(), "NVARCHAR")) {
res = "(" + whereName + " IN (" + item.getEnumValue().stream().map(str -> "N" + "'" + str + "'").collect(Collectors.joining(",")) + "))";
if (StringUtils.containsIgnoreCase(field.getType(), "NVARCHAR")
|| StringUtils.containsIgnoreCase(field.getType(), "NCHAR")) {
res = "(" + whereName + " IN (" + item.getEnumValue().stream().map(str -> "'" + SQLConstants.MSSQL_N_PREFIX + str + "'").collect(Collectors.joining(",")) + "))";
} else {
res = "(" + whereName + " IN ('" + String.join("','", item.getEnumValue()) + "'))";
}
@ -188,14 +189,16 @@ public class WhereTree2Str {
} else if (StringUtils.equalsIgnoreCase(item.getTerm(), "not_empty")) {
whereValue = "''";
} else if (StringUtils.containsIgnoreCase(item.getTerm(), "in") || StringUtils.containsIgnoreCase(item.getTerm(), "not in")) {
if (StringUtils.equalsIgnoreCase(field.getType(), "NVARCHAR")) {
whereValue = "(" + Arrays.stream(value.split(",")).map(str -> "N" + "'" + str + "'").collect(Collectors.joining(",")) + ")";
if (StringUtils.containsIgnoreCase(field.getType(), "NVARCHAR")
|| StringUtils.containsIgnoreCase(field.getType(), "NCHAR")) {
whereValue = "(" + Arrays.stream(value.split(",")).map(str -> "'" + SQLConstants.MSSQL_N_PREFIX + str + "'").collect(Collectors.joining(",")) + ")";
} else {
whereValue = "('" + String.join("','", value.split(",")) + "')";
}
} else if (StringUtils.containsIgnoreCase(item.getTerm(), "like")) {
if (StringUtils.equalsIgnoreCase(field.getType(), "NVARCHAR")) {
whereValue = "N'%" + value + "%'";
if (StringUtils.containsIgnoreCase(field.getType(), "NVARCHAR")
|| StringUtils.containsIgnoreCase(field.getType(), "NCHAR")) {
whereValue = "'" + SQLConstants.MSSQL_N_PREFIX + "%" + value + "%'";
} else {
whereValue = "'%" + value + "%'";
}
@ -237,7 +240,8 @@ public class WhereTree2Str {
whereValue = String.format(SQLConstants.WHERE_VALUE_VALUE, value);
}
} else {
if (StringUtils.equalsIgnoreCase(field.getType(), "NVARCHAR")) {
if (StringUtils.containsIgnoreCase(field.getType(), "NVARCHAR")
|| StringUtils.containsIgnoreCase(field.getType(), "NCHAR")) {
whereValue = String.format(SQLConstants.WHERE_VALUE_VALUE_CH, value);
} else {
whereValue = String.format(SQLConstants.WHERE_VALUE_VALUE, value);

View File

@ -25,6 +25,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @Author Junjun
@ -129,7 +130,8 @@ public abstract class Provider {
String s = transSqlDialect(sql, dsMap);
String tableDialect = sqlMeta.getTableDialect();
s = replaceTablePlaceHolder(s, tableDialect);
return replaceCalcFieldPlaceHolder(s, sqlMeta);
s = replaceCalcFieldPlaceHolder(s, sqlMeta);
return replaceMssqlN(s);
}
public String transSqlDialect(String sql, Map<Long, DatasourceSchemaDTO> dsMap) throws DEException {
@ -157,6 +159,16 @@ public abstract class Provider {
return s;
}
public String replaceMssqlN(String s) {
Pattern compile = Pattern.compile("'-DENS-.*?'");
Matcher matcher = compile.matcher(s);
while (matcher.find()) {
String v = matcher.group();
s = s.replaceAll(v, "N" + v.replace("-DENS-", ""));
}
return s;
}
public String replaceCalcFieldPlaceHolder(String s, SQLMeta sqlMeta) {
Map<String, String> fieldsDialect = new HashMap<>();
if (sqlMeta.getXFieldsDialect() != null && !sqlMeta.getXFieldsDialect().isEmpty()) {