From 6c5e8caa8b4fed30e43eda30c83f6f10d41b0076 Mon Sep 17 00:00:00 2001 From: junjun Date: Mon, 21 Oct 2024 11:08:00 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=95=B0=E6=8D=AE=E6=BA=90):=20=E4=BF=AE?= =?UTF-8?q?=E5=A4=8DSQL=20Server=E8=BF=87=E6=BB=A4=E4=B8=AD=E6=96=87?= =?UTF-8?q?=E6=97=A0=E7=BB=93=E6=9E=9C=E7=9A=84=E9=97=AE=E9=A2=98=20#12759?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dataease/engine/constant/SQLConstants.java | 4 +++- .../dataease/engine/trans/CustomWhere2Str.java | 18 +++++++++++------- .../io/dataease/engine/trans/ExtWhere2Str.java | 13 ++++++++----- .../dataease/engine/trans/WhereTree2Str.java | 18 +++++++++++------- .../datasource/provider/Provider.java | 14 +++++++++++++- 5 files changed, 46 insertions(+), 21 deletions(-) diff --git a/core/core-backend/src/main/java/io/dataease/engine/constant/SQLConstants.java b/core/core-backend/src/main/java/io/dataease/engine/constant/SQLConstants.java index e0ea7fec57..ba0d2ba6df 100644 --- a/core/core-backend/src/main/java/io/dataease/engine/constant/SQLConstants.java +++ b/core/core-backend/src/main/java/io/dataease/engine/constant/SQLConstants.java @@ -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-"; } diff --git a/core/core-backend/src/main/java/io/dataease/engine/trans/CustomWhere2Str.java b/core/core-backend/src/main/java/io/dataease/engine/trans/CustomWhere2Str.java index be4253e5f4..254501a46d 100644 --- a/core/core-backend/src/main/java/io/dataease/engine/trans/CustomWhere2Str.java +++ b/core/core-backend/src/main/java/io/dataease/engine/trans/CustomWhere2Str.java @@ -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); diff --git a/core/core-backend/src/main/java/io/dataease/engine/trans/ExtWhere2Str.java b/core/core-backend/src/main/java/io/dataease/engine/trans/ExtWhere2Str.java index 007fc64175..a6e1594d62 100644 --- a/core/core-backend/src/main/java/io/dataease/engine/trans/ExtWhere2Str.java +++ b/core/core-backend/src/main/java/io/dataease/engine/trans/ExtWhere2Str.java @@ -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)); diff --git a/core/core-backend/src/main/java/io/dataease/engine/trans/WhereTree2Str.java b/core/core-backend/src/main/java/io/dataease/engine/trans/WhereTree2Str.java index 69565cdec1..25708c3a0a 100644 --- a/core/core-backend/src/main/java/io/dataease/engine/trans/WhereTree2Str.java +++ b/core/core-backend/src/main/java/io/dataease/engine/trans/WhereTree2Str.java @@ -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); diff --git a/sdk/extensions/extensions-datasource/src/main/java/io/dataease/extensions/datasource/provider/Provider.java b/sdk/extensions/extensions-datasource/src/main/java/io/dataease/extensions/datasource/provider/Provider.java index 3984d00bae..029fb32ba5 100644 --- a/sdk/extensions/extensions-datasource/src/main/java/io/dataease/extensions/datasource/provider/Provider.java +++ b/sdk/extensions/extensions-datasource/src/main/java/io/dataease/extensions/datasource/provider/Provider.java @@ -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 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 fieldsDialect = new HashMap<>(); if (sqlMeta.getXFieldsDialect() != null && !sqlMeta.getXFieldsDialect().isEmpty()) {